1
0
mirror of https://github.com/Toxblh/MTMR.git synced 2026-01-11 09:28:38 +00:00

Merge branch 'master' into feature/icon-or-label-for-groups

This commit is contained in:
maandagdev 2020-01-24 01:21:47 +01:00 committed by GitHub
commit ef0fd7f16b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 56 deletions

15
.github/workflows/swift.yml vendored Normal file
View File

@ -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

View File

@ -16,11 +16,11 @@ class PomodoroBarItem: CustomButtonTouchBarItem, Widget {
case workTime case workTime
case restTime case restTime
} }
let container = try decoder.container(keyedBy: CodingKeys.self) let container = try decoder.container(keyedBy: CodingKeys.self)
let workTime = try container.decodeIfPresent(Double.self, forKey: .workTime) let workTime = try container.decodeIfPresent(Double.self, forKey: .workTime)
let restTime = try container.decodeIfPresent(Double.self, forKey: .restTime) let restTime = try container.decodeIfPresent(Double.self, forKey: .restTime)
return ( return (
item: .pomodoro(workTime: workTime ?? 1500.00, restTime: restTime ?? 300), item: .pomodoro(workTime: workTime ?? 1500.00, restTime: restTime ?? 300),
action: .none, action: .none,
@ -28,26 +28,24 @@ class PomodoroBarItem: CustomButtonTouchBarItem, Widget {
parameters: [:] parameters: [:]
) )
} }
private enum TimeTypes { private enum TimeTypes {
case work case work
case rest case rest
case none case none
} }
//Vars are used for pausing the timer.
private var started = false private let defaultTitle = "🍅 "
private var timerPaused: Bool = false;
private let defaultTitle = ""
private let workTime: TimeInterval private let workTime: TimeInterval
private let restTime: TimeInterval private let restTime: TimeInterval
private var typeTime: TimeTypes = .none private var typeTime: TimeTypes = .none
private var timer: DispatchSourceTimer? private var timer: DispatchSourceTimer?
private var timeLeft: Int = 0 private var timeLeft: Int = 0
private var timeLeftString: String { 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) { init(identifier: NSTouchBarItem.Identifier, workTime: TimeInterval, restTime: TimeInterval) {
self.workTime = workTime self.workTime = workTime
self.restTime = restTime self.restTime = restTime
@ -55,54 +53,30 @@ class PomodoroBarItem: CustomButtonTouchBarItem, Widget {
tapClosure = { [weak self] in self?.startStopWork() } tapClosure = { [weak self] in self?.startStopWork() }
longTapClosure = { [weak self] in self?.startStopRest() } longTapClosure = { [weak self] in self?.startStopRest() }
} }
required init?(coder _: NSCoder) { required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented") fatalError("init(coder:) has not been implemented")
} }
deinit { deinit {
timer?.cancel() timer?.cancel()
timer = nil timer = nil
} }
@objc func startStopWork() { @objc func startStopWork() {
if !started { typeTime = .work
started = true; startStopTimer()
typeTime = .work
startStopTimer()
} else {
if timerPaused {
timerPaused = false;
resumeTimer();
} else {
timerPaused = true;
pauseTimer();
}
}
print("short")
} }
@objc func startStopRest() { @objc func startStopRest() {
print("looong")
started = false;
typeTime = .rest typeTime = .rest
startStopTimer() startStopTimer()
} }
func startStopTimer() { func startStopTimer() {
timer == nil ? start() : reset() 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() { private func start() {
timeLeft = Int(typeTime == .work ? workTime : restTime) timeLeft = Int(typeTime == .work ? workTime : restTime)
let queue: DispatchQueue = DispatchQueue(label: "Timer") let queue: DispatchQueue = DispatchQueue(label: "Timer")
@ -110,30 +84,25 @@ class PomodoroBarItem: CustomButtonTouchBarItem, Widget {
timer?.schedule(deadline: .now(), repeating: .seconds(1), leeway: .never) timer?.schedule(deadline: .now(), repeating: .seconds(1), leeway: .never)
timer?.setEventHandler(handler: tick) timer?.setEventHandler(handler: tick)
timer?.resume() timer?.resume()
NSSound.beep() NSSound.beep()
} }
private func finish() { private func finish() {
if typeTime != .none { if typeTime != .none {
sendNotification() sendNotification()
} }
reset() reset()
} }
private func reset() { private func reset() {
typeTime = .none typeTime = .none
timer?.cancel() timer?.cancel()
if timerPaused {
resumeTimer()
}
timer = nil timer = nil
title = defaultTitle title = defaultTitle
timerPaused = false
started = false
} }
private func tick() { private func tick() {
timeLeft -= 1 timeLeft -= 1
DispatchQueue.main.async { DispatchQueue.main.async {
@ -144,7 +113,7 @@ class PomodoroBarItem: CustomButtonTouchBarItem, Widget {
} }
} }
} }
private func sendNotification() { private func sendNotification() {
let notification: NSUserNotification = NSUserNotification() let notification: NSUserNotification = NSUserNotification()
notification.title = "Pomodoro" notification.title = "Pomodoro"
@ -152,6 +121,4 @@ class PomodoroBarItem: CustomButtonTouchBarItem, Widget {
notification.soundName = "Submarine" notification.soundName = "Submarine"
NSUserNotificationCenter.default.deliver(notification) NSUserNotificationCenter.default.deliver(notification)
} }
}
}