Skip to content

Commit 8f201b2

Browse files
committed
Only schedule notifications from the phone app
And play the new ding sound!
1 parent ab7b359 commit 8f201b2

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

Core/CooldownPresenter.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ public class CooldownPresenter {
8686

8787
private func bumpCooldown(multipliedBy multiplier: Double) {
8888
State.shared.cooldown += Cooldown(created: Date(), remaining: State.shared.cooldownInterval * multiplier)
89+
#if !os(watchOS)
8990
NotificationService.shared.scheduleNotification(for: State.shared.cooldown)
91+
#endif
9092
refresh()
9193

9294
if supportsWatch { WatchService.shared.stateUpdated(State.shared) }

Core/NotificationService.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ class NotificationService: NSObject {
3838
let content = UNMutableNotificationContent()
3939
content.title = "Cooldown complete"
4040
content.body = "Time elapsed: \(DateComponentsFormatter.notificationFormatter.string(from: cooldown.remaining) ?? "???")"
41-
content.sound = .default
42-
41+
content.sound = UNNotificationSound(named: UNNotificationSoundName("ding.wav"))
42+
4343
let components = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: cooldown.target)
4444
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false)
4545

46-
let request = UNNotificationRequest(identifier: "cooldown-complete", content: content, trigger: trigger)
46+
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
47+
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
4748
UNUserNotificationCenter.current().add(request) { error in
4849
if let error = error { print(error) }
4950
}
@@ -58,7 +59,7 @@ extension NotificationService: UNUserNotificationCenterDelegate {
5859
_ center: UNUserNotificationCenter,
5960
willPresent notification: UNNotification,
6061
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
61-
completionHandler(.sound)
62+
completionHandler([.sound, .alert])
6263
}
6364

6465
}

Core/WatchService.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,16 @@ class WatchService: NSObject {
2626

2727
var stateChanged: (() -> Void)?
2828

29+
private var isConnected: Bool {
30+
#if os(watchOS)
31+
return true
32+
#else
33+
return WCSession.default.isPaired && WCSession.default.isWatchAppInstalled
34+
#endif
35+
}
36+
2937
private var canUpdateContext: Bool {
30-
return WCSession.isSupported() && WCSession.default.activationState == .activated
38+
return WCSession.isSupported() && WCSession.default.activationState == .activated && isConnected
3139
}
3240

3341
private var canSendMessage: Bool {
@@ -90,6 +98,10 @@ extension WatchService: WCSessionDelegate {
9098
State.shared.cooldown = cooldown
9199
State.shared.cooldownInterval = interval
92100
DispatchQueue.main.async(execute: stateChanged ?? {})
101+
102+
#if !os(watchOS)
103+
NotificationService.shared.scheduleNotification(for: State.shared.cooldown)
104+
#endif
93105
}
94106

95107
}

0 commit comments

Comments
 (0)