Keep in break duration

This commit is contained in:
Zhigang Fang
2017-11-28 10:21:48 +08:00
parent 31715af50a
commit 95986feec0
4 changed files with 51 additions and 15 deletions

View File

@@ -9,3 +9,4 @@ Changelog
* Add option to reminder you at certain interval.
* Add option to auto apply yes on interval timeout.
* Disable reminder when computer went to sleep.
* Keep break duration in timer if it's less than 10 min

View File

@@ -80,6 +80,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
reminderInterval = UserDefaults.standard.reminderInterval
shouldAutoApply = UserDefaults.standard.shouldAutoApply
}
func applicationWillTerminate(_ aNotification: Notification) {

View File

@@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key>
<string>1.1.0</string>
<key>CFBundleVersion</key>
<string>5</string>
<string>6</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.productivity</string>
<key>LSMinimumSystemVersion</key>

View File

@@ -14,7 +14,7 @@ import KeychainSwift
struct TimeEntry: Decodable {
let id: Int64
let start: String
let start: Date
let description: String?
}
@@ -45,6 +45,16 @@ private extension URL {
}
extension JSONEncoder {
static var toggle: JSONEncoder {
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .iso8601
return encoder
}
}
extension JSONDecoder {
static var toggle: JSONDecoder {
@@ -55,6 +65,16 @@ extension JSONDecoder {
}
extension ISO8601DateFormatter {
static var toggle: ISO8601DateFormatter {
let formatter = ISO8601DateFormatter()
formatter.timeZone = .current
return formatter
}
}
struct Endpoint<T: Decodable> {
let method: String
let url: URL
@@ -109,6 +129,14 @@ struct Endpoint<T: Decodable> {
] as NSDictionary)
}
static func update(timeEntry: Int64, duration: Int) -> Endpoint<TimeEntry> {
return Endpoint<TimeEntry>(method: "POST", url: URL.api("time_entries/\(timeEntry)"), body: [
"time_entry": [
"duration": duration
]
])
}
}
@@ -202,25 +230,27 @@ class TogglViewModel {
guard let entry = self?.current.value else { return }
self?.timeEntryWhenSlept = entry
self?.screenSleptAt = Date()
self?.stopTimer()
}).disposed(by: self.disposeBag)
NSWorkspace.shared.notificationCenter
.rx.notification(NSWorkspace.screensDidWakeNotification)
.subscribe(onNext: {[weak self] _ in
self?.awake.value = true
defer {
self?.screenSleptAt = nil
self?.timeEntryWhenSlept = nil
}
guard
let date = self?.screenSleptAt,
let timer = self?.timeEntryWhenSlept,
Date().timeIntervalSince(date) < 60 * 10
Date().timeIntervalSince(date) > 60 * 10
else {
self?.screenSleptAt = nil
self?.timeEntryWhenSlept = nil
return
}
self?.input.value = timer.description ?? ""
self?.startTimer()
self?.stopTimer(entry: timer, stoppedAt: date)
}).disposed(by: self.disposeBag)
}
@@ -283,10 +313,17 @@ class TogglViewModel {
.disposed(by: self.disposeBag)
}
func stopTimer() {
func stopTimer(entry: TimeEntry? = nil, stoppedAt: Date? = nil) {
guard let token = self.token.value else { return }
guard let entryId = self.current.value?.id else { return }
Endpoint<TimeEntry>.stop(timeEntry: entryId).request(with: token)
guard let entry = entry ?? self.current.value else { return }
Endpoint<TimeEntry>.stop(timeEntry: entry.id).request(with: token)
.flatMap({ entry -> Observable<TimeEntry> in
if let stopped = stoppedAt {
let duration = Int(stopped.timeIntervalSince(entry.start))
return Endpoint<TimeEntry>.update(timeEntry: entry.id, duration: duration).request(with: token)
}
return .just(entry)
})
.map({_ in nil})
.catchErrorJustReturn(nil)
.bind(to: current)
@@ -388,11 +425,8 @@ class ViewController: NSViewController {
self.selectedRow = 0
}).disposed(by: self.disposeBag)
let df = DateFormatter()
df.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
viewModel.current.asDriver().flatMapLatest({ entry -> Driver<String> in
guard let start = entry?.start, let date = df.date(from: start) else { return .empty() }
guard let date = entry?.start else { return .empty() }
return Driver<Int>.interval(1).startWith(1).map({ _ in
let time: Int = Int(Date().timeIntervalSince(date))
let hours = time / 3600