From eeedf2dc6a4fc744aa8c50c2a1354585ea3b3f2d Mon Sep 17 00:00:00 2001 From: Zhigang Fang Date: Tue, 28 Nov 2017 01:37:50 +0800 Subject: [PATCH] Fix sleep --- CHANGELOG | 3 ++- FloatingToggl.xcodeproj/project.pbxproj | 2 ++ FloatingToggl/ViewController.swift | 7 +++++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 795f7fe..cb283b1 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -7,4 +7,5 @@ Changelog * In recent entries, sort project by usage. * When showing recent entries, enter key autocomplete instead of start timer. * Add option to reminder you at certain interval. -* Add option to auto apply yes on interval timeout. \ No newline at end of file +* Add option to auto apply yes on interval timeout. +* Disable reminder when computer went to sleep. diff --git a/FloatingToggl.xcodeproj/project.pbxproj b/FloatingToggl.xcodeproj/project.pbxproj index c28ac07..2b1d8a3 100644 --- a/FloatingToggl.xcodeproj/project.pbxproj +++ b/FloatingToggl.xcodeproj/project.pbxproj @@ -24,6 +24,7 @@ 041E2BB41F9EF0370036687C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 041E2BB51F9EF0370036687C /* FloatingToggl.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = FloatingToggl.entitlements; sourceTree = ""; }; 041E2BBB1F9EF3450036687C /* FloatingPannel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FloatingPannel.swift; sourceTree = ""; }; + 04D507151FCC84360038E7E0 /* CHANGELOG */ = {isa = PBXFileReference; lastKnownFileType = text; path = CHANGELOG; sourceTree = ""; }; 60799DCC63FBAD0197655FF4 /* Pods-FloatingToggl.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-FloatingToggl.release.xcconfig"; path = "Pods/Target Support Files/Pods-FloatingToggl/Pods-FloatingToggl.release.xcconfig"; sourceTree = ""; }; 6A61B7517328FB18AF3456FF /* Pods-FloatingToggl.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-FloatingToggl.debug.xcconfig"; path = "Pods/Target Support Files/Pods-FloatingToggl/Pods-FloatingToggl.debug.xcconfig"; sourceTree = ""; }; B3256CA209129AD71D5691BF /* Pods_FloatingToggl.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_FloatingToggl.framework; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -44,6 +45,7 @@ 041E2B9F1F9EF0370036687C = { isa = PBXGroup; children = ( + 04D507151FCC84360038E7E0 /* CHANGELOG */, 041E2BAA1F9EF0370036687C /* FloatingToggl */, 041E2BA91F9EF0370036687C /* Products */, AB897BA953C7D862BCC1F2D0 /* Pods */, diff --git a/FloatingToggl/ViewController.swift b/FloatingToggl/ViewController.swift index a4cfa6a..e0aa212 100644 --- a/FloatingToggl/ViewController.swift +++ b/FloatingToggl/ViewController.swift @@ -125,6 +125,7 @@ class TogglViewModel { let input = Variable("") let active = Variable(NSApplication.shared.isActive) + let awake = Variable(true) private let disposeBag = DisposeBag() @@ -197,6 +198,7 @@ class TogglViewModel { NSWorkspace.shared.notificationCenter .rx.notification(NSWorkspace.screensDidSleepNotification) .subscribe(onNext: {[weak self] _ in + self?.awake.value = false guard let entry = self?.current.value else { return } self?.timeEntryWhenSlept = entry self?.screenSleptAt = Date() @@ -206,6 +208,7 @@ class TogglViewModel { NSWorkspace.shared.notificationCenter .rx.notification(NSWorkspace.screensDidWakeNotification) .subscribe(onNext: {[weak self] _ in + self?.awake.value = true guard let date = self?.screenSleptAt, let timer = self?.timeEntryWhenSlept, @@ -224,11 +227,11 @@ class TogglViewModel { var presentReminder: Observable<()> { return Observable.merge([ - self.active.asObservable().distinctUntilChanged().map({_ in ()}), + self.awake.asObservable().distinctUntilChanged().map({_ in ()}), NotificationCenter.default.rx.notification(.reminderIntervalUpdated).map({_ in ()}), self.input.asObservable().distinctUntilChanged().map({_ in ()}) ]).flatMapLatest({[weak self] _ -> Observable<()> in - if self?.active.value != true { return .empty() } + if self?.awake.value != true { return .empty() } let interval = UserDefaults.standard.reminderInterval if interval == 0 { return .empty() } return Observable.interval(RxTimeInterval(interval * 60), scheduler: MainScheduler.asyncInstance).map({ _ in ()})