Created
August 16, 2023 12:02
-
-
Save saroar/d8a98578f7335a7d3f0b6d11d096d686 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct AppDelegateReducer: Reducer { | |
public typealias State = UserSettings | |
public enum Action: Equatable { | |
case didFinishLaunching | |
case userNotifications(UserNotificationClient.DelegateEvent) | |
case userSettingsLoaded(TaskResult<UserSettings>) | |
} | |
public init() {} | |
@Dependency(\.userNotifications) var userNotifications | |
@Dependency(\.applicationClient.setUserInterfaceStyle) var setUserInterfaceStyle | |
@Dependency(\.remoteNotifications) var remoteNotifications | |
public var body: some Reducer<State, Action> { | |
Reduce(self.core) | |
} | |
func core(state: inout State, action: Action) -> Effect<Action> { | |
switch action { | |
case .didFinishLaunching: | |
return .run { send in | |
await withThrowingTaskGroup(of: Void.self) { group in | |
group.addTask { | |
for await event in self.userNotifications.delegate() { | |
await send(.userNotifications(event)) | |
} | |
} | |
group.addTask { | |
let settings = await self.userNotifications.getNotificationSettings() | |
switch settings.authorizationStatus { | |
case .authorized: | |
guard | |
try await self.userNotifications.requestAuthorization([.alert, .badge, .sound]) | |
else { return } | |
case .notDetermined, .provisional: | |
guard try await self.userNotifications.requestAuthorization(.provisional) | |
else { return } | |
default: | |
return | |
} | |
} | |
} | |
} | |
case let .userNotifications(.willPresentNotification(_, completion)): | |
return .run { _ in | |
completion([.list, .banner, .sound]) | |
} | |
case let .userNotifications(.didReceiveResponse(_, completion)): | |
return .run { @MainActor _ in | |
completion() | |
} | |
case .userNotifications: | |
return .none | |
case let .userSettingsLoaded(result): | |
state = (try? result.value) ?? state | |
return .run { [state] _ in | |
async let setUI: Void = | |
await self.setUserInterfaceStyle(state.colorScheme.userInterfaceStyle) | |
_ = await (setUI) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment