| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- import Combine
- import Foundation
- import LoopKit
- import SwiftUI
- import Swinject
- // protocol AlertPermissionsCheckerDelegate: AnyObject {
- // func notificationsPermissions(requiresRiskMitigation: Bool, scheduledDeliveryEnabled: Bool)
- // }
- public class AlertPermissionsChecker: ObservableObject, Injectable {
- // @Environment(\.appName) private var appName
- private lazy var cancellables = Set<AnyCancellable>()
- private var listeningToNotificationCenter = false
- // @Injected() private var apsManager: APSManager!
- // @Injected() private var router: Router!
- @Published var notificationsDisabled: Bool = false
- init(resolver: Resolver) {
- injectServices(resolver)
- Foundation.NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)
- .sink { [weak self] _ in
- self?.check()
- }
- .store(in: &cancellables)
- Foundation.NotificationCenter.default.publisher(for: UIApplication.didEnterBackgroundNotification)
- .sink { [weak self] _ in
- self?.check()
- }
- .store(in: &cancellables)
- }
- func checkNow() {
- check {
- // Note: we do this, instead of calling notificationCenterSettingsChanged directly, so that we only
- // get called when it _changes_.
- self.listenToNotificationCenter()
- }
- }
- private func check(then completion: (() -> Void)? = nil) {
- UNUserNotificationCenter.current().getNotificationSettings { settings in
- DispatchQueue.main.async {
- self.notificationsDisabled = settings.alertSetting == .disabled
- completion?()
- }
- }
- }
- }
- extension AlertPermissionsChecker {
- private func listenToNotificationCenter() {
- if !listeningToNotificationCenter {
- $notificationsDisabled
- .receive(on: RunLoop.main)
- .removeDuplicates()
- .sink(receiveValue: notificationCenterSettingsChanged)
- .store(in: &cancellables)
- listeningToNotificationCenter = true
- }
- }
- private func notificationCenterSettingsChanged(_: Bool) {
- // TODO: Add processing for other actions in delegate AlertManager, InAppAlertScheduler, etc., from Loop
- debug(.default, "notificationCenterSettingsChanged")
- }
- }
|