mirror of
https://github.com/sparrowcode/PermissionsKit.git
synced 2026-11-18 20:15:58 +00:00
The manifest moves to swift-tools-version 6.3 with swiftLanguageModes
[.v6], and the per-product PERMISSIONSKIT_* defines are gone — they only
existed to carve the sources into podspec subspecs, while SPM already
compiles every permission as its own target. Products and targets are now
derived from one list of permission names instead of sixteen identical
blocks.
In the sources that means the conditional import of the core module and
the gates that tested nothing but a define both collapse, Text.bundle
always resolves to .module, and the MIT header leaves all 23 files, as in
the other packages.
For strict concurrency the completion closures become @MainActor rather
than @Sendable: a permission dialog is UI work, and @Sendable would reject
the usual `request { [weak self] in ... }` from a view controller. They are
now called through Task { @MainActor in ... }, copied into a local first
where a delegate would otherwise send a non-Sendable handler; openSettingPage
is main actor isolated and drops its queue hop; the handler singletons are
marked nonisolated(unsafe), and the notification settings lookup keeps only
the Sendable authorization status behind its semaphore.
Also fixes the reqeustUpdate and Ovveride typos and renames the notification
options file that carried "userNotifcation" in its name.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
62 lines
2.1 KiB
Swift
62 lines
2.1 KiB
Swift
import PermissionsKit
|
|
import UserNotifications
|
|
|
|
public extension Permission {
|
|
|
|
static func notification(_ access: Set<NotificationAccess> = [.alert, .badge, .sound]) -> NotificationPermission {
|
|
return NotificationPermission(kind: .notification(access: access))
|
|
}
|
|
}
|
|
|
|
public class NotificationPermission: Permission {
|
|
|
|
private var _kind: Permission.Kind
|
|
open override var kind: Permission.Kind { self._kind }
|
|
|
|
init(kind: Permission.Kind) {
|
|
self._kind = kind
|
|
}
|
|
|
|
public override var status: Permission.Status {
|
|
guard let authorizationStatus = fetchAuthorizationStatus() else { return .notDetermined }
|
|
switch authorizationStatus {
|
|
case .authorized: return .authorized
|
|
case .denied: return .denied
|
|
case .notDetermined: return .notDetermined
|
|
case .provisional: return .authorized
|
|
case .ephemeral: return .authorized
|
|
@unknown default: return .denied
|
|
}
|
|
}
|
|
|
|
/*
|
|
PermissionsKit: Notification center reports its settings asynchronously,
|
|
while the status property is synchronous. The semaphore bridges the two,
|
|
and it also orders the write and the read of the status.
|
|
*/
|
|
private func fetchAuthorizationStatus() -> UNAuthorizationStatus? {
|
|
nonisolated(unsafe) var authorizationStatus: UNAuthorizationStatus?
|
|
let semaphore = DispatchSemaphore(value: 0)
|
|
UNUserNotificationCenter.current().getNotificationSettings { settings in
|
|
authorizationStatus = settings.authorizationStatus
|
|
semaphore.signal()
|
|
}
|
|
semaphore.wait()
|
|
return authorizationStatus
|
|
}
|
|
|
|
public override func request(completion: @escaping @MainActor () -> Void) {
|
|
let center = UNUserNotificationCenter.current()
|
|
switch _kind {
|
|
case .notification(let access):
|
|
center.requestAuthorization(options: UNAuthorizationOptions(access.map { $0.userNotifcationAuthorizationOptions })) { (granted, error) in
|
|
Task { @MainActor in
|
|
completion()
|
|
}
|
|
}
|
|
default:
|
|
fatalError()
|
|
}
|
|
}
|
|
}
|