Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions Sources/AgentBar/Services/AppUpdateNotificationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,26 @@ final class AppUpdateNotificationService: NSObject, UNUserNotificationCenterDele
private static let notificationThreadIdentifier = "com.lonewolfyx.AgentBar.app-update"
private static let lastNotifiedBuildVersionKey = "AppUpdateLastNotifiedBuildVersion"

private let notificationCenter: UNUserNotificationCenter
private let notificationCenter: UNUserNotificationCenter?
private let userDefaults: UserDefaults
private let stateLock = NSLock()
private var inFlightBuildVersion: String?

override init() {
let notificationCenter = UNUserNotificationCenter.current()
let notificationCenter = Self.isRunningFromAppBundle
? UNUserNotificationCenter.current()
: nil
self.notificationCenter = notificationCenter
userDefaults = .standard
super.init()
notificationCenter.delegate = self
notificationCenter?.delegate = self
}

func removeNotificationIfInstalled(buildVersion: String) {
guard lastNotifiedBuildVersion() == buildVersion else {
guard
let notificationCenter,
lastNotifiedBuildVersion() == buildVersion
else {
return
}

Expand All @@ -31,7 +36,10 @@ final class AppUpdateNotificationService: NSObject, UNUserNotificationCenterDele
}

func notifyUpdateReady(displayVersion: String, buildVersion: String) {
guard beginNotificationAttempt(buildVersion: buildVersion) else {
guard
let notificationCenter,
beginNotificationAttempt(buildVersion: buildVersion)
else {
return
}

Expand Down Expand Up @@ -60,6 +68,11 @@ final class AppUpdateNotificationService: NSObject, UNUserNotificationCenterDele
}

private func requestAuthorizationAndNotify(displayVersion: String, buildVersion: String) {
guard let notificationCenter else {
finishNotificationAttempt(buildVersion: buildVersion, succeeded: false)
return
}

notificationCenter.requestAuthorization(options: [.alert, .sound]) { [weak self] granted, _ in
guard let self else {
return
Expand All @@ -78,6 +91,11 @@ final class AppUpdateNotificationService: NSObject, UNUserNotificationCenterDele
}

private func deliverUpdateReadyNotification(displayVersion: String, buildVersion: String) {
guard let notificationCenter else {
finishNotificationAttempt(buildVersion: buildVersion, succeeded: false)
return
}

let content = UNMutableNotificationContent()
content.title = I18n.current.appUpdateReadyTitle
content.body = I18n.current.appUpdateReadyMessage(version: displayVersion)
Expand Down Expand Up @@ -138,6 +156,10 @@ final class AppUpdateNotificationService: NSObject, UNUserNotificationCenterDele
return userDefaults.string(forKey: Self.lastNotifiedBuildVersionKey)
}

private static var isRunningFromAppBundle: Bool {
Bundle.main.bundleURL.pathExtension.caseInsensitiveCompare("app") == .orderedSame
}

func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
Expand Down