Skip to content

Commit

Permalink
feat: background 진입시 LiveActivity 업데이트 로직 실행
Browse files Browse the repository at this point in the history
  • Loading branch information
devMinseok committed Dec 25, 2024
1 parent 7885f52 commit bea48db
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 36 deletions.
13 changes: 2 additions & 11 deletions Projects/Domain/PomodoroService/Sources/PomodoroService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ extension PomodoroService: DependencyKey {
public static let liveValue: PomodoroService = .live()

private static func live() -> PomodoroService {


return .init(
syncCategoryList: {
apiClient, databaseClient in
Expand Down Expand Up @@ -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)
}
Expand Down
67 changes: 42 additions & 25 deletions Projects/Feature/Feature/Sources/AppCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import UserDefaultsClientInterface
import UserNotificationClientInterface
import CatServiceInterface
import UserServiceInterface
import PomodoroServiceInterface
import DatabaseClientInterface
import StreamListenerInterface
import BackgroundTaskClientInterface
import LiveActivityClientInterface

import ComposableArchitecture

Expand All @@ -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() {}
}

Expand All @@ -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<Self> {
Expand Down Expand Up @@ -101,67 +104,73 @@ 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()
} else if state.home != nil {
state.home = HomeCore.State()
}
return .none

case .requestError:
return .none

case .serverState(let serverState):
switch serverState {
case .requestStarted:
Expand All @@ -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)
}
}

0 comments on commit bea48db

Please sign in to comment.