|
| 1 | +// |
| 2 | +// AppReducer.swift |
| 3 | +// NomadSpot |
| 4 | +// |
| 5 | +// Created by Wonji Suh on 3/1/26. |
| 6 | +// |
| 7 | + |
| 8 | +import Presentation |
| 9 | +import ComposableArchitecture |
| 10 | +import Entity |
| 11 | +import LogMacro |
| 12 | + |
| 13 | +@Reducer |
| 14 | +public struct AppReducer: Sendable { |
| 15 | + public init() {} |
| 16 | + |
| 17 | + @ObservableState |
| 18 | + public enum State { |
| 19 | + case splash(SplashReducer.State) |
| 20 | + |
| 21 | + |
| 22 | + public init() { |
| 23 | + self = .splash(SplashReducer.State()) |
| 24 | + } |
| 25 | + |
| 26 | + // Animation identifier for SwiftUI transitions |
| 27 | + var animationID: String { |
| 28 | + switch self { |
| 29 | + case .splash: return "splash" |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + //MARK: - Action |
| 35 | + public enum Action: ViewAction { |
| 36 | + case view(View) |
| 37 | + case async(AsyncAction) |
| 38 | + case inner(InnerAction) |
| 39 | + case navigation(NavigationAction) |
| 40 | + case scope(ScopeAction) |
| 41 | + } |
| 42 | + |
| 43 | + @CasePathable |
| 44 | + public enum View { |
| 45 | + case presentView |
| 46 | + case presentRoot |
| 47 | + case presentAuth |
| 48 | + case presentStaff |
| 49 | + case presentMember |
| 50 | + } |
| 51 | + |
| 52 | + //MARK: - 앱내에서 사용하는 액션 |
| 53 | + public enum InnerAction: Equatable { |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + //MARK: - 비동기 처리 액션 |
| 58 | + public enum AsyncAction: Equatable { |
| 59 | + case startNotificationListener |
| 60 | + case refreshTokenExpired |
| 61 | + } |
| 62 | + |
| 63 | + //MARK: - 네비게이션 연결 액션 |
| 64 | + public enum NavigationAction: Equatable { |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + //MARK: - 스코프 액션 |
| 69 | + @CasePathable |
| 70 | + public enum ScopeAction { |
| 71 | + case splash(SplashReducer.Action) |
| 72 | + } |
| 73 | + |
| 74 | + @Dependency(\.continuousClock) var clock |
| 75 | + |
| 76 | + private enum CancelID { |
| 77 | + case refreshTokenExpiredListener |
| 78 | + case splashRouting |
| 79 | + case authEffects |
| 80 | + case staffEffects |
| 81 | + case memberEffects |
| 82 | + } |
| 83 | + |
| 84 | + public var body: some ReducerOf<Self> { |
| 85 | + Reduce { state, action in |
| 86 | + switch action { |
| 87 | + case .view(let viewAction): |
| 88 | + return handleViewAction(state: &state, action: viewAction) |
| 89 | + |
| 90 | + case .inner(let innerAction): |
| 91 | + return handleInnerAction(state: &state, action: innerAction) |
| 92 | + |
| 93 | + case .async(let asyncAction): |
| 94 | + return handleAsyncAction(state: &state, action: asyncAction) |
| 95 | + |
| 96 | + case .navigation(let navigationAction): |
| 97 | + return handleNavigationAction(state: &state, action: navigationAction) |
| 98 | + |
| 99 | + case .scope(let scopeAction): |
| 100 | + return handleScopeAction(state: &state, action: scopeAction) |
| 101 | + } |
| 102 | + } |
| 103 | + .ifCaseLet(\.splash, action: \.scope.splash) { |
| 104 | + SplashReducer() |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private func handleViewAction( |
| 109 | + state: inout State, |
| 110 | + action: View |
| 111 | + ) -> Effect<Action> { |
| 112 | + switch action { |
| 113 | + case .presentView: |
| 114 | + return .run { send in |
| 115 | +// await send(.scope(.splash(.view(.onAppear)))) |
| 116 | + } |
| 117 | + |
| 118 | + case .presentRoot: |
| 119 | +// |
| 120 | + return .none |
| 121 | + |
| 122 | + case .presentAuth: |
| 123 | +// state = .auth(.init()) |
| 124 | + return .concatenate( |
| 125 | + .cancel(id: CancelID.staffEffects), |
| 126 | + .cancel(id: CancelID.memberEffects) |
| 127 | + ) |
| 128 | + |
| 129 | + case .presentStaff: |
| 130 | +// state = .staff(.init()) |
| 131 | + return .concatenate( |
| 132 | + .cancel(id: CancelID.authEffects), |
| 133 | + .cancel(id: CancelID.memberEffects) |
| 134 | + ) |
| 135 | + |
| 136 | + case .presentMember: |
| 137 | +// state = .member(.init()) |
| 138 | + return .concatenate( |
| 139 | + .cancel(id: CancelID.authEffects), |
| 140 | + .cancel(id: CancelID.staffEffects) |
| 141 | + ) |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private func handleAsyncAction( |
| 146 | + state: inout State, |
| 147 | + action: AsyncAction |
| 148 | + ) -> Effect<Action> { |
| 149 | + switch action { |
| 150 | + case .startNotificationListener: |
| 151 | + return setupRefreshTokenExpiredListener() |
| 152 | + .cancellable(id: CancelID.refreshTokenExpiredListener, cancelInFlight: true) |
| 153 | + |
| 154 | + case .refreshTokenExpired: |
| 155 | + // Refresh token이 만료된 경우 로그인 화면으로 이동 |
| 156 | + |
| 157 | +// state = .auth(.init()) |
| 158 | + |
| 159 | + return .concatenate( |
| 160 | + .cancel(id: CancelID.splashRouting), |
| 161 | + .cancel(id: CancelID.staffEffects), |
| 162 | + .cancel(id: CancelID.memberEffects) |
| 163 | + ) |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + private func handleInnerAction( |
| 168 | + state: inout State, |
| 169 | + action: InnerAction |
| 170 | + ) -> Effect<Action> { |
| 171 | + return .none |
| 172 | + } |
| 173 | + |
| 174 | + private func handleNavigationAction( |
| 175 | + state: inout State, |
| 176 | + action: NavigationAction |
| 177 | + ) -> Effect<Action> { |
| 178 | + return .none |
| 179 | + } |
| 180 | + |
| 181 | + private func handleScopeAction( |
| 182 | + state: inout State, |
| 183 | + action: ScopeAction |
| 184 | + ) -> Effect<Action> { |
| 185 | + switch action { |
| 186 | + |
| 187 | + |
| 188 | + default: |
| 189 | + return .none |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + /// Refresh token 만료 감지 리스너 설정 |
| 194 | + private func setupRefreshTokenExpiredListener() -> Effect<Action> { |
| 195 | + #logDebug("🔔 [AppReducer] 🚨 SETTING UP REFRESH TOKEN EXPIRED LISTENER...") |
| 196 | + return .publisher { |
| 197 | + NotificationCenter.default |
| 198 | + .publisher(for: NSNotification.Name("RefreshTokenExpired")) |
| 199 | + .map { notification in |
| 200 | + #logDebug("🔔 [AppReducer] 🔥 🎯 REFRESH TOKEN EXPIRED NOTIFICATION RECEIVED!") |
| 201 | + #logDebug("🔔 [AppReducer] Notification details: \(notification)") |
| 202 | + return Action.async(.refreshTokenExpired) |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | +} |
0 commit comments