-
Notifications
You must be signed in to change notification settings - Fork 0
[#571] PushNotificationSettingsView에 TCA를 적용한다 #590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
opficdev
merged 2 commits into
develop
from
refactor/#571-PushNotificationSettingsView-TCA
Jun 12, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
205 changes: 205 additions & 0 deletions
205
Application/DevLogPresentation/Sources/Settings/PushNotificationSettingsFeature.swift
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| // | ||
| // PushNotificationSettingsFeature.swift | ||
| // DevLogPresentation | ||
| // | ||
| // Created by opfic on 6/12/26. | ||
| // | ||
|
|
||
| import ComposableArchitecture | ||
| import DevLogDomain | ||
| import Foundation | ||
| import SwiftUI | ||
|
|
||
| @Reducer | ||
| struct PushNotificationSettingsFeature { | ||
| @ObservableState | ||
| struct State: Equatable { | ||
| @Presents var alert: AlertState<Never>? | ||
| @Presents var timePicker: TimePickerState? | ||
| var pushNotificationEnable = false | ||
| var viewPushNotificationTime = Date() | ||
| var loading = LoadingFeature.State() | ||
|
|
||
| var isLoading: Bool { | ||
| loading.isLoading | ||
| } | ||
| var pushNotificationHour: Int { | ||
| Calendar.current.component(.hour, from: viewPushNotificationTime) | ||
| } | ||
| var pushNotificationMinute: Int { | ||
| Calendar.current.component(.minute, from: viewPushNotificationTime) | ||
| } | ||
| } | ||
|
|
||
| @ObservableState | ||
| struct TimePickerState: Equatable { | ||
| var time: Date | ||
| var height: CGFloat = .pi | ||
| } | ||
|
|
||
| enum Action: BindableAction { | ||
| case alert(PresentationAction<Never>) | ||
| case binding(BindingAction<State>) | ||
| case timePicker(PresentationAction<TimePicker>) | ||
| case fetchSettings | ||
| case setAlert | ||
| case tapCustomTime | ||
| case selectPresetTime(Date) | ||
| case loading(LoadingFeature.Action) | ||
|
|
||
| enum TimePicker: BindableAction, Equatable { | ||
| case binding(BindingAction<TimePickerState>) | ||
| case tapCloseButton | ||
| case tapDoneButton | ||
| } | ||
| } | ||
|
|
||
| @Dependency(\.fetchPushSettingsUseCase) var fetchPushSettingsUseCase | ||
| @Dependency(\.updatePushSettingsUseCase) var updatePushSettingsUseCase | ||
|
|
||
| var body: some ReducerOf<Self> { | ||
| Scope(state: \.loading, action: \.loading) { | ||
| LoadingFeature() | ||
| } | ||
| BindingReducer() | ||
| Reduce { state, action in | ||
| switch action { | ||
| case .alert: | ||
| break | ||
| case .binding(\.pushNotificationEnable): | ||
| return updatePushNotificationSettingsEffect(settings: settings(from: state)) | ||
| case .binding(\.viewPushNotificationTime): | ||
| let time = state.viewPushNotificationTime | ||
| state.timePicker?.time = time | ||
| case .binding: | ||
| break | ||
| case .timePicker(.dismiss): | ||
| state.timePicker = nil | ||
| case .timePicker(.presented(.tapCloseButton)): | ||
| state.timePicker = nil | ||
| case .timePicker(.presented(.tapDoneButton)): | ||
| guard let time = state.timePicker?.time else { break } | ||
| state.timePicker = nil | ||
| state.viewPushNotificationTime = time | ||
| return updatePushNotificationSettingsEffect(settings: settings(from: state)) | ||
| case .timePicker: | ||
| break | ||
| case .fetchSettings: | ||
| return fetchPushNotificationSettingsEffect() | ||
| case .setAlert: | ||
| state.alert = alertState() | ||
| case .tapCustomTime: | ||
| state.timePicker = TimePickerState(time: state.viewPushNotificationTime) | ||
| case .selectPresetTime(let date): | ||
| state.viewPushNotificationTime = date | ||
| state.timePicker?.time = date | ||
| return updatePushNotificationSettingsEffect(settings: settings(from: state)) | ||
| case .loading: | ||
| break | ||
| } | ||
|
|
||
| return .none | ||
| } | ||
| .ifLet(\.$alert, action: \.alert) | ||
| .ifLet(\.$timePicker, action: \.timePicker) { | ||
| TimePickerFeature() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private struct TimePickerFeature: Reducer { | ||
| typealias State = PushNotificationSettingsFeature.TimePickerState | ||
| typealias Action = PushNotificationSettingsFeature.Action.TimePicker | ||
|
|
||
| var body: some ReducerOf<Self> { | ||
| BindingReducer() | ||
| } | ||
| } | ||
|
|
||
| extension DependencyValues { | ||
| var fetchPushSettingsUseCase: FetchPushSettingsUseCase { | ||
| get { self[FetchPushSettingsUseCaseKey.self] } | ||
| set { self[FetchPushSettingsUseCaseKey.self] = newValue } | ||
| } | ||
|
|
||
| var updatePushSettingsUseCase: UpdatePushSettingsUseCase { | ||
| get { self[UpdatePushSettingsUseCaseKey.self] } | ||
| set { self[UpdatePushSettingsUseCaseKey.self] = newValue } | ||
| } | ||
| } | ||
|
|
||
| private enum FetchPushSettingsUseCaseKey: DependencyKey { | ||
| static var liveValue: FetchPushSettingsUseCase { | ||
| preconditionFailure("FetchPushSettingsUseCase must be provided.") | ||
| } | ||
|
|
||
| static var testValue: FetchPushSettingsUseCase { | ||
| liveValue | ||
| } | ||
| } | ||
|
|
||
| private enum UpdatePushSettingsUseCaseKey: DependencyKey { | ||
| static var liveValue: UpdatePushSettingsUseCase { | ||
| preconditionFailure("UpdatePushSettingsUseCase must be provided.") | ||
| } | ||
|
|
||
| static var testValue: UpdatePushSettingsUseCase { | ||
| liveValue | ||
| } | ||
| } | ||
|
|
||
| private extension PushNotificationSettingsFeature { | ||
| func fetchPushNotificationSettingsEffect() -> Effect<Action> { | ||
| .run { [fetchPushSettingsUseCase] send in | ||
| await send(.loading(.begin(target: .default, mode: .delayed))) | ||
| do { | ||
| let settings = try await fetchPushSettingsUseCase.execute() | ||
| await send(.binding(.set(\.pushNotificationEnable, settings.isEnabled))) | ||
| if let hour = settings.scheduledTime.hour, | ||
| let minute = settings.scheduledTime.minute, | ||
| let date = Calendar.current.date(bySettingHour: hour, minute: minute, second: 0, of: Date()) { | ||
| await send(.binding(.set(\.viewPushNotificationTime, date))) | ||
| } | ||
| await send(.loading(.end(target: .default, mode: .delayed))) | ||
| } catch { | ||
| await send(.loading(.end(target: .default, mode: .delayed))) | ||
| await send(.setAlert) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func updatePushNotificationSettingsEffect(settings: PushNotificationSettings) -> Effect<Action> { | ||
| .run { [updatePushSettingsUseCase] send in | ||
| await send(.loading(.begin(target: .default, mode: .delayed))) | ||
| do { | ||
| try await updatePushSettingsUseCase.execute(settings) | ||
| await send(.loading(.end(target: .default, mode: .delayed))) | ||
| } catch { | ||
| await send(.loading(.end(target: .default, mode: .delayed))) | ||
| await send(.setAlert) | ||
| await send(.fetchSettings) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func settings(from state: State) -> PushNotificationSettings { | ||
| let date = state.timePicker?.time ?? state.viewPushNotificationTime | ||
| let dateComponents = Calendar.current.dateComponents([.hour, .minute], from: date) | ||
| return PushNotificationSettings( | ||
| isEnabled: state.pushNotificationEnable, | ||
| scheduledTime: dateComponents | ||
| ) | ||
| } | ||
|
opficdev marked this conversation as resolved.
|
||
|
|
||
| func alertState() -> AlertState<Never> { | ||
| AlertState { | ||
| TextState(String(localized: "common_error_title")) | ||
| } actions: { | ||
| ButtonState(role: .cancel) { | ||
| TextState(String(localized: "common_close")) | ||
| } | ||
| } message: { | ||
| TextState(String(localized: "common_error_message")) | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.