From 95986feec00e731fc43c64a8e1676dec08636d6a Mon Sep 17 00:00:00 2001 From: Zhigang Fang Date: Tue, 28 Nov 2017 10:21:48 +0800 Subject: [PATCH] Keep in break duration --- CHANGELOG | 1 + FloatingToggl/AppDelegate.swift | 1 + FloatingToggl/Info.plist | 2 +- FloatingToggl/ViewController.swift | 62 +++++++++++++++++++++++------- 4 files changed, 51 insertions(+), 15 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index cb283b1..ad0a757 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 diff --git a/FloatingToggl/AppDelegate.swift b/FloatingToggl/AppDelegate.swift index b6cbe47..275db9d 100644 --- a/FloatingToggl/AppDelegate.swift +++ b/FloatingToggl/AppDelegate.swift @@ -80,6 +80,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { reminderInterval = UserDefaults.standard.reminderInterval shouldAutoApply = UserDefaults.standard.shouldAutoApply + } func applicationWillTerminate(_ aNotification: Notification) { diff --git a/FloatingToggl/Info.plist b/FloatingToggl/Info.plist index fdea0c4..12f6225 100644 --- a/FloatingToggl/Info.plist +++ b/FloatingToggl/Info.plist @@ -19,7 +19,7 @@ CFBundleShortVersionString 1.1.0 CFBundleVersion - 5 + 6 LSApplicationCategoryType public.app-category.productivity LSMinimumSystemVersion diff --git a/FloatingToggl/ViewController.swift b/FloatingToggl/ViewController.swift index e0aa212..d916a82 100644 --- a/FloatingToggl/ViewController.swift +++ b/FloatingToggl/ViewController.swift @@ -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 { let method: String let url: URL @@ -109,6 +129,14 @@ struct Endpoint { ] as NSDictionary) } + static func update(timeEntry: Int64, duration: Int) -> Endpoint { + return Endpoint(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.stop(timeEntry: entryId).request(with: token) + guard let entry = entry ?? self.current.value else { return } + Endpoint.stop(timeEntry: entry.id).request(with: token) + .flatMap({ entry -> Observable in + if let stopped = stoppedAt { + let duration = Int(stopped.timeIntervalSince(entry.start)) + return Endpoint.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 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.interval(1).startWith(1).map({ _ in let time: Int = Int(Date().timeIntervalSince(date)) let hours = time / 3600