Skip to content

Commit

Permalink
WIP: Add short/long breaks
Browse files Browse the repository at this point in the history
  • Loading branch information
denizt committed Apr 1, 2022
1 parent d934e88 commit 337704f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
26 changes: 23 additions & 3 deletions TomatoBar/TomatoBarTimer.swift
Expand Up @@ -13,10 +13,13 @@ public class TomatoBarTimer: ObservableObject {
@AppStorage("stopAfterBreak") public var stopAfterBreak = false
@AppStorage("displayInMenuBar") public var displayInMenuBar = true
@AppStorage("workIntervalLength") public var workIntervalLength = 25
@AppStorage("restIntervalLength") public var restIntervalLength = 5
@AppStorage("shortRestIntervalLength") public var shortRestInterval = 5
@AppStorage("longRestIntervalLength") public var longRestInterval = 15
@AppStorage("shortToLongBreakCounter") public var shortToLongBreakCounter = 3

@Published var startStopString: String = "Start"

public var shortToLongBreakCounterLocal = 0
private var stateMachine = TomatoBarStateMachine(state: .ready)
private var statusBarItem: NSStatusItem? {
return AppDelegate.shared.statusBarItem
Expand Down Expand Up @@ -47,6 +50,7 @@ public class TomatoBarTimer: ObservableObject {
* timerFired (stopAfterBreak)
*
*/
updateBreakCounter()
stateMachine.addRoute(.ready => .idle)
stateMachine.addRoutes(event: .startStop, transitions: [
.idle => .work, .work => .idle, .rest => .idle,
Expand Down Expand Up @@ -85,6 +89,12 @@ public class TomatoBarTimer: ObservableObject {
}
}

public func updateBreakCounter() {
// keeping logic simple, we might want to change it so it 'updates' count
// but it seems overly complex or I am missing something
shortToLongBreakCounterLocal = shortToLongBreakCounter
}

public func toggleMenuBar() {
if self.displayInMenuBar {
self.updateTimeInMenuBar()
Expand Down Expand Up @@ -141,7 +151,11 @@ public class TomatoBarTimer: ObservableObject {
}

private func onWorkFinish(context _: TomatoBarContext) {
sendNotification(title: "Time's up", body: "It's time for a break!")
if shortToLongBreakCounterLocal == 0 {
sendNotification(title: "Time's up", body: "It's time for a long break!")
} else {
sendNotification(title: "Time's up", body: "It's time for a break!")
}
if isDingEnabled {
player.playDing()
}
Expand All @@ -152,7 +166,13 @@ public class TomatoBarTimer: ObservableObject {
}

private func onRestStart(context _: TomatoBarContext) {
startTimer(seconds: restIntervalLength * 60)
if shortToLongBreakCounterLocal > 0 {
startTimer(seconds: shortRestInterval * 60)
shortToLongBreakCounterLocal -= 1
} else {
startTimer(seconds: longRestInterval * 60)
shortToLongBreakCounterLocal = shortToLongBreakCounter
}
}

private func onRestFinish(context _: TomatoBarContext) {
Expand Down
27 changes: 20 additions & 7 deletions TomatoBar/TomatoBarView.swift
Expand Up @@ -25,13 +25,26 @@ public struct TomatoBarView: View {
.onChange(of: timer.displayInMenuBar) { _ in
timer.toggleMenuBar()
}
Stepper(value: $timer.workIntervalLength, in: 1 ... 60) {
Text("Work interval:").frame(maxWidth: .infinity, alignment: .leading)
Text("\(timer.workIntervalLength) min")
}
Stepper(value: $timer.restIntervalLength, in: 1 ... 60) {
Text("Rest interval:").frame(maxWidth: .infinity, alignment: .leading)
Text("\(timer.restIntervalLength) min")
Group {
Stepper(value: $timer.workIntervalLength, in: 1...60) {
Text("Work interval:").frame(maxWidth: .infinity, alignment: .leading)
Text("\(timer.workIntervalLength) min")
}
Stepper(value: $timer.shortRestInterval, in: 1...60) {
Text("Short rest interval:").frame(maxWidth: .infinity, alignment: .leading)
Text("\(timer.shortRestInterval) min")
}
Stepper(value: $timer.longRestInterval, in: 1...60) {
Text("Long rest interval:").frame(maxWidth: .infinity, alignment: .leading)
Text("\(timer.longRestInterval) min")
}
Stepper(value: $timer.shortToLongBreakCounter, in: 1...3) {
Text("Short rest count:").frame(maxWidth: .infinity, alignment: .leading)
Text("\(timer.shortToLongBreakCounter) times").onChange(of: timer.shortToLongBreakCounter) { _ in
timer.updateBreakCounter()
}
}
// Text("Remaining short breaks: \(timer.shortToLongBreakCounterLocal)")
}
Divider()
Text("Sounds:")
Expand Down

0 comments on commit 337704f

Please sign in to comment.