diff --git a/Projects/Domain/PomodoroService/Sources/PomodoroService.swift b/Projects/Domain/PomodoroService/Sources/PomodoroService.swift index c07e059..179b44f 100644 --- a/Projects/Domain/PomodoroService/Sources/PomodoroService.swift +++ b/Projects/Domain/PomodoroService/Sources/PomodoroService.swift @@ -18,8 +18,6 @@ extension PomodoroService: DependencyKey { public static let liveValue: PomodoroService = .live() private static func live() -> PomodoroService { - - return .init( syncCategoryList: { apiClient, databaseClient in @@ -60,20 +58,13 @@ extension PomodoroService: DependencyKey { identifier: "com.pomonyang.mohanyang.update_LiveActivity", queue: nil ) { task in - print("BackgroundTask 호출!!") task.expirationHandler = { - print("BackgroundTask 끝!!") task.setTaskCompleted(success: false) } - let pomodoroActivities = liveActivityClient.protocolAdapter.getActivities(type: PomodoroActivityAttributes.self) Task { - for activity in pomodoroActivities { - await liveActivityClient.protocolAdapter.updateActivity( - type(of: activity.attributes), - id: activity.id, - content: activity.content - ) + if let firstActivity = pomodoroActivities.first { + await firstActivity.update(firstActivity.content) } task.setTaskCompleted(success: true) } diff --git a/Projects/Feature/Feature/Sources/AppCore.swift b/Projects/Feature/Feature/Sources/AppCore.swift index 881661e..a6c204d 100644 --- a/Projects/Feature/Feature/Sources/AppCore.swift +++ b/Projects/Feature/Feature/Sources/AppCore.swift @@ -19,9 +19,11 @@ import UserDefaultsClientInterface import UserNotificationClientInterface import CatServiceInterface import UserServiceInterface +import PomodoroServiceInterface import DatabaseClientInterface import StreamListenerInterface import BackgroundTaskClientInterface +import LiveActivityClientInterface import ComposableArchitecture @@ -37,9 +39,9 @@ public struct AppCore { var onboarding: OnboardingCore.State? @Presents var networkError: NetworkErrorCore.State? @Presents var requestError: RequestErrorCore.State? - + var isLoading: Bool = false - + public init() {} } @@ -62,7 +64,8 @@ public struct AppCore { @Dependency(DatabaseClient.self) var databaseClient @Dependency(StreamListener.self) var streamListener @Dependency(BackgroundTaskClient.self) var backgroundTaskClient - + @Dependency(LiveActivityClient.self) var liveActivityClient + public init() {} public var body: some ReducerOf { @@ -101,56 +104,62 @@ public struct AppCore { await send(.serverState(serverState)) } } - + case .appDelegate: return .none case .didChangeScenePhase(.background): - let request = BGAppRefreshTaskRequest(identifier: "com.pomonyang.mohanyang.update_LiveActivity") - request.earliestBeginDate = Date(timeIntervalSinceNow: 60) - - - do { - try BGTaskScheduler.shared.submit(request) - } catch { - print("BGTaskScheduler not submitted") - } return .run { send in - // 현재 타이머가 실행중이면 submit 해야함 - // + let pomodoroActivity = liveActivityClient.protocolAdapter.getActivities(type: PomodoroActivityAttributes.self).first + let pendingBGTaskRequest = await backgroundTaskClient.pendingTaskRequests().first + if let pomodoroActivity { + if let pendingBGTaskRequest { + if pendingBGTaskRequest.earliestBeginDate != pomodoroActivity.content.state.goalDatetime { + backgroundTaskClient.cancel(identifier: pendingBGTaskRequest.identifier) + await pomodoroActivity.update(pomodoroActivity.content) + try submitUpdateLiveActivityBGTask(earliestBeginDate: pomodoroActivity.content.state.goalDatetime) + } + } else { + try submitUpdateLiveActivityBGTask(earliestBeginDate: pomodoroActivity.content.state.goalDatetime) + } + } else { + if let pendingBGTaskRequest { + backgroundTaskClient.cancel(identifier: pendingBGTaskRequest.identifier) + } + } } case .didChangeScenePhase: return .none - + case .splash(.moveToHome): state.splash = nil state.home = HomeCore.State() return .none - + case .splash(.moveToOnboarding): state.splash = nil state.onboarding = OnboardingCore.State() return .none - + case .splash: return .none - + case .home: return .none - + case .onboarding(.selectCat(.presented(.namingCat(.presented(.moveToHome))))): state.onboarding = nil state.home = HomeCore.State() return .none - + case .onboarding: return .none - + case .networkError: return .none - + case .requestError(.presented(.moveToHome)): if state.onboarding != nil { state.onboarding = OnboardingCore.State() @@ -158,10 +167,10 @@ public struct AppCore { state.home = HomeCore.State() } return .none - + case .requestError: return .none - + case .serverState(let serverState): switch serverState { case .requestStarted: @@ -178,4 +187,12 @@ public struct AppCore { return .none } } + + func submitUpdateLiveActivityBGTask(earliestBeginDate: Date) throws { + let request = BGProcessingTaskRequest(identifier: "com.pomonyang.mohanyang.update_LiveActivity") + request.requiresExternalPower = false + request.requiresNetworkConnectivity = false + request.earliestBeginDate = earliestBeginDate + try backgroundTaskClient.submit(taskRequest: request) + } }