import Foundation import ComposableArchitecture @Reducer struct SplashFeature { @ObservableState struct State: Equatable { var isLoading = true var shouldShowMainApp = false var authenticationStatus: UserInfoManager.AuthenticationStatus = .notFound var isCheckingAuthentication = false } enum Action: Equatable { case onAppear case splashFinished case checkAuthentication case authenticationChecked(UserInfoManager.AuthenticationStatus) } var body: some ReducerOf { Reduce { state, action in switch action { case .onAppear: state.isLoading = true state.shouldShowMainApp = false state.authenticationStatus = UserInfoManager.AuthenticationStatus.notFound state.isCheckingAuthentication = false // 1秒延迟后显示主应用 (iOS 15.5+ 兼容) return .run { send in try await Task.sleep(nanoseconds: 1_000_000_000) // 1秒 = 1,000,000,000 纳秒 await send(.splashFinished) } case .splashFinished: state.isLoading = false state.shouldShowMainApp = true // Splash 完成后,开始检查认证状态 return .send(.checkAuthentication) case .checkAuthentication: state.isCheckingAuthentication = true // 异步检查认证状态 return .run { send in let authStatus = await UserInfoManager.checkAuthenticationStatus() await send(.authenticationChecked(authStatus)) } case let .authenticationChecked(status): #if DEBUG debugInfoSync("🔑 需要手动登录") return .none #endif state.isCheckingAuthentication = false state.authenticationStatus = status // 根据认证状态发送相应的导航通知 if status.canAutoLogin { debugInfoSync("🎉 自动登录成功,进入主页") } else { debugInfoSync("🔑 需要手动登录") } return .none } } } }