
- 更新Yana项目文档,调整适用版本至iOS 17,确保与最新开发环境兼容。 - 在多个视图中重构代码,优化状态管理和视图逻辑,提升用户体验。 - 添加默认初始化器以简化状态管理,确保各个Feature的状态一致性。 - 更新视图组件,移除不必要的硬编码,增强代码可读性和维护性。 - 修复多个视图中的逻辑错误,确保功能正常运行。
115 lines
4.0 KiB
Swift
115 lines
4.0 KiB
Swift
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
|
||
|
||
// 新增:导航目标
|
||
var navigationDestination: NavigationDestination?
|
||
|
||
init() {
|
||
// 默认初始化
|
||
}
|
||
}
|
||
|
||
// 新增:导航目标枚举
|
||
enum NavigationDestination: Equatable {
|
||
case login // 跳转到登录页面
|
||
case main // 跳转到主页面
|
||
}
|
||
|
||
enum Action: Equatable {
|
||
case onAppear
|
||
case splashFinished
|
||
case checkAuthentication
|
||
case authenticationChecked(UserInfoManager.AuthenticationStatus)
|
||
|
||
// 新增:用户信息获取 actions
|
||
case fetchUserInfo
|
||
case userInfoFetched(Bool)
|
||
|
||
// 新增:导航 actions
|
||
case navigateToLogin
|
||
case navigateToMain
|
||
}
|
||
|
||
@Dependency(\.apiService) var apiService // 新增:API服务依赖
|
||
|
||
var body: some ReducerOf<Self> {
|
||
Reduce { state, action in
|
||
switch action {
|
||
case .onAppear:
|
||
state.isLoading = true
|
||
state.shouldShowMainApp = false
|
||
state.authenticationStatus = UserInfoManager.AuthenticationStatus.notFound
|
||
state.isCheckingAuthentication = false
|
||
state.navigationDestination = nil
|
||
|
||
// 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
|
||
|
||
// 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):
|
||
state.isCheckingAuthentication = false
|
||
state.authenticationStatus = status
|
||
|
||
// 根据认证状态决定下一步操作
|
||
if status.canAutoLogin {
|
||
debugInfoSync("🎉 自动登录成功,开始获取用户信息")
|
||
// 新增:认证成功后自动获取用户信息
|
||
return .send(.fetchUserInfo)
|
||
} else {
|
||
debugInfoSync("🔑 需要手动登录")
|
||
return .send(.navigateToLogin)
|
||
}
|
||
|
||
case .fetchUserInfo:
|
||
// 新增:获取用户信息
|
||
return .run { send in
|
||
let success = await UserInfoManager.autoFetchUserInfoOnAppLaunch(apiService: apiService)
|
||
await send(.userInfoFetched(success))
|
||
}
|
||
|
||
case let .userInfoFetched(success):
|
||
if success {
|
||
debugInfoSync("✅ 用户信息获取成功,进入主页")
|
||
} else {
|
||
debugInfoSync("⚠️ 用户信息获取失败,但仍进入主页")
|
||
}
|
||
return .send(.navigateToMain)
|
||
|
||
case .navigateToLogin:
|
||
state.navigationDestination = .login
|
||
return .none
|
||
|
||
case .navigateToMain:
|
||
state.navigationDestination = .main
|
||
state.shouldShowMainApp = true
|
||
return .none
|
||
}
|
||
}
|
||
}
|
||
}
|