Skip to content

Commit

Permalink
Add reset button
Browse files Browse the repository at this point in the history
  • Loading branch information
antgel committed Apr 19, 2024
1 parent caf3764 commit f78aa3f
Showing 1 changed file with 46 additions and 31 deletions.
77 changes: 46 additions & 31 deletions pyredfruit.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,7 @@
class Timer:
def __init__(self, _type, minutes, seconds):
self.type = _type
# We need to use datetime.datetime, not datetime.time.
# See:
# https://bugs.python.org/issue1118748
# https://bugs.python.org/issue1487389
# for more information as to why. So we can't do this:
# self.remaining = datetime.time(0,25,0)
#
# NB This may not be a Bad Thing because we may decide to
# implement time tracking in the future, which would require
# dates.
today = datetime.date.today()
time = datetime.time(0, minutes, seconds)
self.remaining = datetime.datetime.combine(today, time)
self.set_remaining(minutes, seconds)

def decrement_second(self):
a_second = datetime.timedelta(seconds=1)
Expand All @@ -42,6 +30,21 @@ def done_text(self):
def has_expired(self):
return bool(self.remaining.time() == datetime.time(0, 0, 0))

def set_remaining(self, minutes, seconds):
# We need to use datetime.datetime, not datetime.time.
# See:
# https://bugs.python.org/issue1118748
# https://bugs.python.org/issue1487389
# for more information as to why. So we can't do this:
# self.remaining = datetime.time(0,25,0)
#
# NB This may not be a Bad Thing because we may decide to
# implement time tracking in the future, which would require
# dates.
today = datetime.date.today()
time = datetime.time(0, minutes, seconds)
self.remaining = datetime.datetime.combine(today, time)

def __str__(self):
return self.remaining.strftime("%M:%S")

Expand All @@ -63,37 +66,49 @@ def __init__(self):
box.pack_start(self.time_label, True, True, 0)

self.status = Gtk.Label()
self.sync_state()
self.update_ui_state()
box.pack_start(self.status, True, True, 0)

button = Gtk.Button.new_with_label("Start")
button.connect("clicked", self.clicked_start)
box.pack_start(button, True, True, 0)
state_button = Gtk.Button.new_with_label("Start")
state_button.connect("clicked", self.clicked_state)
box.pack_start(state_button, True, True, 0)

reset_button = Gtk.Button.new_with_label("Reset")
reset_button.connect("clicked", self.clicked_reset)
box.pack_start(reset_button, True, True, 0)

# There is probably (hopefully) a better way to do this rather than
# calling this function every second when the timer ticks. Ideally
# UI components would listen for state changes. But as I don't have
# time to read all the docs, especially
# https://python-gtk-3-tutorial.readthedocs.io/en/latest/application.html
# which mentions Gio.SimpleAction amongst other things, and there is
# only one label that changes state at the moment, do it like this.
def sync_state(self):
# which mentions Gio.SimpleAction amongst other things, and there
# are only two labels that changes state at the moment, do it like this.
# Or is it
# https://python-gtk-3-tutorial.readthedocs.io/en/latest/objects.html
def update_ui_state(self):
self.status.set_label(f"{self.state} {self.timer.type}")
self.time_label.set_markup(
f'<span size="500%" weight="bold">{self.timer}</span>'
)

def clicked_start(self, button):
def clicked_reset(self, reset_button):
self.reset_pomodoro()

def clicked_state(self, state_button):
match self.state:
case "stopped":
button.set_label("Pause")
state_button.set_label("Pause")
self.start_pomodoro()
case "running":
button.set_label("Restart")
state_button.set_label("Restart")
self.state = "paused"
case "paused":
button.set_label("Pause")
state_button.set_label("Pause")
self.state = "running"

def time_up(self):
playsound('bell.wav')
playsound("bell.wav")
dialog = Gtk.MessageDialog(
buttons=Gtk.ButtonsType.OK, text=self.timer.done_text()
)
Expand All @@ -110,25 +125,25 @@ def start_break(self):
self.start_tick()

def start_pomodoro(self):
self.timer = Timer("pomodoro", 25, 0)
self.reset_pomodoro()
self.start_tick()
self.state = "running"

def reset_pomodoro(self):
self.timer = Timer("pomodoro", 25, 0)

def start_tick(self):
GLib.timeout_add_seconds(1, self.update_state)


def update_state(self):
if self.state == "running":
self.timer.decrement_second()
self.time_label.set_markup(
f'<span size="500%" weight="bold">{self.timer}</span>'
)

if self.timer.has_expired():
self.time_up()
return False

self.sync_state()
self.update_ui_state()
return True # timeout_add_seconds will keep going


Expand Down

0 comments on commit f78aa3f

Please sign in to comment.