diff --git a/.github/workflows/swift.yml b/.github/workflows/swift.yml new file mode 100644 index 0000000..2e76632 --- /dev/null +++ b/.github/workflows/swift.yml @@ -0,0 +1,15 @@ +name: Swift + +on: [push] + +jobs: + build: + + runs-on: macOS-latest + + steps: + - uses: actions/checkout@v1 + - name: Build + run: swift build -v + - name: Run tests + run: swift test -v diff --git a/MTMR/Widgets/PomodoroBarItem.swift b/MTMR/Widgets/PomodoroBarItem.swift index dc46c12..502fcc1 100644 --- a/MTMR/Widgets/PomodoroBarItem.swift +++ b/MTMR/Widgets/PomodoroBarItem.swift @@ -16,11 +16,11 @@ class PomodoroBarItem: CustomButtonTouchBarItem, Widget { case workTime case restTime } - + let container = try decoder.container(keyedBy: CodingKeys.self) let workTime = try container.decodeIfPresent(Double.self, forKey: .workTime) let restTime = try container.decodeIfPresent(Double.self, forKey: .restTime) - + return ( item: .pomodoro(workTime: workTime ?? 1500.00, restTime: restTime ?? 300), action: .none, @@ -28,26 +28,24 @@ class PomodoroBarItem: CustomButtonTouchBarItem, Widget { parameters: [:] ) } - + private enum TimeTypes { case work case rest case none } - //Vars are used for pausing the timer. - private var started = false - private var timerPaused: Bool = false; - - private let defaultTitle = "" + + private let defaultTitle = "🍅 " private let workTime: TimeInterval private let restTime: TimeInterval private var typeTime: TimeTypes = .none private var timer: DispatchSourceTimer? + private var timeLeft: Int = 0 private var timeLeftString: String { - return String(format: "%.2i:%.2i ", timeLeft / 60, timeLeft % 60) + return String(format: "%.2i:%.2i", timeLeft / 60, timeLeft % 60) } - + init(identifier: NSTouchBarItem.Identifier, workTime: TimeInterval, restTime: TimeInterval) { self.workTime = workTime self.restTime = restTime @@ -55,54 +53,30 @@ class PomodoroBarItem: CustomButtonTouchBarItem, Widget { tapClosure = { [weak self] in self?.startStopWork() } longTapClosure = { [weak self] in self?.startStopRest() } } - + required init?(coder _: NSCoder) { fatalError("init(coder:) has not been implemented") } - + deinit { timer?.cancel() timer = nil } + @objc func startStopWork() { - if !started { - started = true; - typeTime = .work - startStopTimer() - } else { - if timerPaused { - timerPaused = false; - resumeTimer(); - } else { - timerPaused = true; - pauseTimer(); - - } - } - print("short") + typeTime = .work + startStopTimer() } - + @objc func startStopRest() { - print("looong") - started = false; typeTime = .rest startStopTimer() } - + func startStopTimer() { timer == nil ? start() : reset() } - - func resumeTimer() { - guard let timervalue = timer else { return } - timervalue.resume(); - } - - func pauseTimer() { - guard let timervalue = timer else { return } - timervalue.suspend(); - } - + private func start() { timeLeft = Int(typeTime == .work ? workTime : restTime) let queue: DispatchQueue = DispatchQueue(label: "Timer") @@ -110,30 +84,25 @@ class PomodoroBarItem: CustomButtonTouchBarItem, Widget { timer?.schedule(deadline: .now(), repeating: .seconds(1), leeway: .never) timer?.setEventHandler(handler: tick) timer?.resume() - + NSSound.beep() } - + private func finish() { if typeTime != .none { sendNotification() } - + reset() } - + private func reset() { typeTime = .none timer?.cancel() - if timerPaused { - resumeTimer() - } timer = nil title = defaultTitle - timerPaused = false - started = false } - + private func tick() { timeLeft -= 1 DispatchQueue.main.async { @@ -144,7 +113,7 @@ class PomodoroBarItem: CustomButtonTouchBarItem, Widget { } } } - + private func sendNotification() { let notification: NSUserNotification = NSUserNotification() notification.title = "Pomodoro" @@ -152,6 +121,4 @@ class PomodoroBarItem: CustomButtonTouchBarItem, Widget { notification.soundName = "Submarine" NSUserNotificationCenter.default.deliver(notification) } - - -} +} \ No newline at end of file