|
| 1 | +// |
| 2 | +// DeviceActivityMonitorExtension.swift |
| 3 | +// DeviceMonitor |
| 4 | +// |
| 5 | +// Created by 지희의 MAC on 1/4/24. |
| 6 | +// |
| 7 | +// |
| 8 | + |
| 9 | +import DeviceActivity |
| 10 | +import ManagedSettings |
| 11 | +import Foundation |
| 12 | +import UserNotifications |
| 13 | + |
| 14 | +import SwiftUI |
| 15 | +import FamilyControls |
| 16 | + |
| 17 | +// Optionally override any of the functions below. |
| 18 | +// Make sure that your class name matches the NSExtensionPrincipalClass in your Info.plist. |
| 19 | +class DeviceActivityMonitorExtension: DeviceActivityMonitor { |
| 20 | + |
| 21 | + @AppStorage(AppStorageKey.selectionApp.rawValue, store: UserDefaults(suiteName: APP_GROUP_NAME)) |
| 22 | + var selectionApps = FamilyActivitySelection() |
| 23 | + |
| 24 | + @StateObject var screenTimeVM = ScreenTimeViewModel() |
| 25 | + |
| 26 | + let store = ManagedSettingsStore() |
| 27 | + let userNotiCenter = UNUserNotificationCenter.current() |
| 28 | + |
| 29 | + override func intervalDidStart(for activity: DeviceActivityName) { |
| 30 | + super.intervalDidStart(for: activity) |
| 31 | + } |
| 32 | + |
| 33 | + override func intervalDidEnd(for activity: DeviceActivityName) { |
| 34 | + super.intervalDidEnd(for: activity) |
| 35 | + store.shield.applications = nil |
| 36 | + } |
| 37 | + |
| 38 | + //threshold에 도착하면 행동한다 |
| 39 | + override func eventDidReachThreshold(_ event: DeviceActivityEvent.Name, activity: DeviceActivityName) { |
| 40 | + store.shield.applications = selectionApps.applicationTokens // 이러면 다른 앱도 잠김 |
| 41 | + Task { |
| 42 | + await screenTimeVM.handleSetBlockApplication() |
| 43 | + } |
| 44 | + let notiContent = UNMutableNotificationContent() |
| 45 | + notiContent.title = "하면함" |
| 46 | + notiContent.body = "이용 시간이 종료 되었습니다." |
| 47 | + notiContent.userInfo = ["token": ""] // 푸시 받을때 오는 데이터 |
| 48 | + |
| 49 | + // 알림이 trigger되는 시간 설정 |
| 50 | + let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false) |
| 51 | + |
| 52 | + let request = UNNotificationRequest( |
| 53 | + identifier: UUID().uuidString, |
| 54 | + content: notiContent, |
| 55 | + trigger: trigger |
| 56 | + ) |
| 57 | + |
| 58 | + self.userNotiCenter.add(request) { (error) in |
| 59 | + print(#function, error as Any) |
| 60 | + } |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + override func intervalWillStartWarning(for activity: DeviceActivityName) { |
| 65 | + super.intervalWillStartWarning(for: activity) |
| 66 | + |
| 67 | + // Handle the warning before the interval starts. |
| 68 | + } |
| 69 | + |
| 70 | + override func intervalWillEndWarning(for activity: DeviceActivityName) { |
| 71 | + super.intervalWillEndWarning(for: activity) |
| 72 | + //NotificationManager.shared.scheduleNotification() |
| 73 | + |
| 74 | + // Handle the warning before the interval ends. |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + //threshold 시간이 되면 경고를 준다 |
| 79 | + override func eventWillReachThresholdWarning(_ event: DeviceActivityEvent.Name, activity: DeviceActivityName) { |
| 80 | + super.eventWillReachThresholdWarning(event, activity: activity) |
| 81 | + //store.shield.applicationCategories = ShieldSettings.ActivityCategoryPolicy.all() |
| 82 | + let notiContent = UNMutableNotificationContent() |
| 83 | + notiContent.title = "하면함" |
| 84 | + notiContent.body = "이용 시간이 종료되었습니다. 곧 어플이 잠깁니다." |
| 85 | + notiContent.userInfo = ["status": ""] // 푸시 받을때 오는 데이터 |
| 86 | + |
| 87 | + // 알림이 trigger되는 시간 설정 |
| 88 | + let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false) |
| 89 | + |
| 90 | + let request = UNNotificationRequest( |
| 91 | + identifier: UUID().uuidString, |
| 92 | + content: notiContent, |
| 93 | + trigger: trigger |
| 94 | + ) |
| 95 | + |
| 96 | + userNotiCenter.add(request) { (error) in |
| 97 | + print(#function, error as Any) |
| 98 | + } |
| 99 | + |
| 100 | + //NotificationManager.shared.scheduleNotification() |
| 101 | + |
| 102 | + } |
| 103 | +} |
0 commit comments