-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpresenter.py
44 lines (35 loc) · 1.3 KB
/
presenter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from state import State
from button import Button
from timer import Timer
# from model import Model
class Presenter:
def __init__(self, view):
self.view = view
# self.model = Model()
self.timer = Timer()
self.state = State.IDLE
self.dispatch_table = {
# Format: (button, current_state) : (action, new_state)
(Button.START, State.IDLE): (self.do_run, State.RUNNING),
(Button.START, State.RUNNING): (self.do_pause, State.PAUSED),
(Button.START, State.PAUSED): (self.do_run, State.RUNNING),
(Button.RESET, State.IDLE): (self.do_reset, State.IDLE),
(Button.RESET, State.RUNNING): (self.do_reset, State.IDLE),
(Button.RESET, State.PAUSED): (self.do_reset, State.IDLE)
}
def update_time(self):
if self.state == State.RUNNING:
self.timer.update()
def update_view(self):
self.view.update(self.timer.get_seconds(), self.state)
def do_run(self):
self.timer.start()
def do_pause(self):
self.update_time()
def do_reset(self):
self.timer.reset()
def handle_event(self, button):
action, new_state = self.dispatch_table[(button, self.state)]
action()
self.state = new_state
self.update_view()