
- 在AppDelegate中集成数据迁移管理器,支持从UserDefaults迁移到Keychain。 - 重构UserInfoManager,使用Keychain存储用户信息,增加内存缓存以提升性能。 - 添加API加载效果视图,增强用户体验。 - 更新SplashFeature以支持自动登录和认证状态检查。 - 语言设置迁移至Keychain,确保用户设置的安全性。
272 lines
11 KiB
Swift
272 lines
11 KiB
Swift
import SwiftUI
|
||
import ComposableArchitecture
|
||
|
||
struct EMailLoginView: View {
|
||
let store: StoreOf<EMailLoginFeature>
|
||
let onBack: () -> Void
|
||
|
||
// 使用本地@State管理UI状态
|
||
@State private var email: String = ""
|
||
@State private var verificationCode: String = ""
|
||
@State private var codeCountdown: Int = 0
|
||
@State private var timer: Timer?
|
||
|
||
// 管理输入框焦点状态
|
||
@FocusState private var focusedField: Field?
|
||
|
||
enum Field {
|
||
case email
|
||
case verificationCode
|
||
}
|
||
|
||
// 计算登录按钮是否可用
|
||
private var isLoginButtonEnabled: Bool {
|
||
return !store.isLoading && !email.isEmpty && !verificationCode.isEmpty
|
||
}
|
||
|
||
// 计算获取验证码按钮文本
|
||
private var getCodeButtonText: String {
|
||
if store.isCodeLoading {
|
||
return ""
|
||
} else if codeCountdown > 0 {
|
||
return "\(codeCountdown)S"
|
||
} else {
|
||
return "email_login.get_code".localized
|
||
}
|
||
}
|
||
|
||
// 计算获取验证码按钮是否可用
|
||
private var isCodeButtonEnabled: Bool {
|
||
return !store.isCodeLoading && codeCountdown == 0
|
||
}
|
||
|
||
var body: some View {
|
||
GeometryReader { geometry in
|
||
ZStack {
|
||
// 背景图片
|
||
Image("bg")
|
||
.resizable()
|
||
.aspectRatio(contentMode: .fill)
|
||
.ignoresSafeArea(.all)
|
||
|
||
VStack(spacing: 0) {
|
||
// 顶部导航栏
|
||
HStack {
|
||
Button(action: {
|
||
onBack()
|
||
}) {
|
||
Image(systemName: "chevron.left")
|
||
.font(.system(size: 24, weight: .medium))
|
||
.foregroundColor(.white)
|
||
.frame(width: 44, height: 44)
|
||
}
|
||
|
||
Spacer()
|
||
}
|
||
.padding(.horizontal, 16)
|
||
.padding(.top, 8)
|
||
|
||
Spacer()
|
||
.frame(height: 60)
|
||
|
||
// 标题
|
||
Text("email_login.title".localized)
|
||
.font(.system(size: 28, weight: .medium))
|
||
.foregroundColor(.white)
|
||
.padding(.bottom, 80)
|
||
|
||
// 输入框区域
|
||
VStack(spacing: 24) {
|
||
// 邮箱输入框
|
||
ZStack {
|
||
RoundedRectangle(cornerRadius: 25)
|
||
.fill(Color.white.opacity(0.1))
|
||
.overlay(
|
||
RoundedRectangle(cornerRadius: 25)
|
||
.stroke(Color.white.opacity(0.3), lineWidth: 1)
|
||
)
|
||
.frame(height: 56)
|
||
|
||
TextField("", text: $email)
|
||
.placeholder(when: email.isEmpty) {
|
||
Text("placeholder.enter_email".localized)
|
||
.foregroundColor(.white.opacity(0.6))
|
||
}
|
||
.foregroundColor(.white)
|
||
.font(.system(size: 16))
|
||
.padding(.horizontal, 24)
|
||
.keyboardType(.emailAddress)
|
||
.autocapitalization(.none)
|
||
.disableAutocorrection(true)
|
||
.focused($focusedField, equals: .email)
|
||
}
|
||
|
||
// 验证码输入框
|
||
ZStack {
|
||
RoundedRectangle(cornerRadius: 25)
|
||
.fill(Color.white.opacity(0.1))
|
||
.overlay(
|
||
RoundedRectangle(cornerRadius: 25)
|
||
.stroke(Color.white.opacity(0.3), lineWidth: 1)
|
||
)
|
||
.frame(height: 56)
|
||
|
||
HStack {
|
||
TextField("", text: $verificationCode)
|
||
.placeholder(when: verificationCode.isEmpty) {
|
||
Text("placeholder.enter_verification_code".localized)
|
||
.foregroundColor(.white.opacity(0.6))
|
||
}
|
||
.foregroundColor(.white)
|
||
.font(.system(size: 16))
|
||
.keyboardType(.numberPad)
|
||
.focused($focusedField, equals: .verificationCode)
|
||
|
||
// 获取验证码按钮
|
||
Button(action: {
|
||
// 发送API请求
|
||
store.send(.getVerificationCodeTapped)
|
||
// 立即开始倒计时
|
||
startCountdown()
|
||
}) {
|
||
ZStack {
|
||
if store.isCodeLoading {
|
||
ProgressView()
|
||
.progressViewStyle(CircularProgressViewStyle(tint: .white))
|
||
.scaleEffect(0.7)
|
||
} else {
|
||
Text(getCodeButtonText)
|
||
.font(.system(size: 14, weight: .medium))
|
||
.foregroundColor(.white)
|
||
}
|
||
}
|
||
.frame(width: 60, height: 36)
|
||
.background(
|
||
RoundedRectangle(cornerRadius: 18)
|
||
.fill(Color.white.opacity(isCodeButtonEnabled && !email.isEmpty ? 0.2 : 0.1))
|
||
)
|
||
}
|
||
.disabled(!isCodeButtonEnabled || email.isEmpty || store.isCodeLoading)
|
||
}
|
||
.padding(.horizontal, 24)
|
||
}
|
||
}
|
||
.padding(.horizontal, 32)
|
||
|
||
Spacer()
|
||
.frame(height: 60)
|
||
|
||
// 登录按钮
|
||
Button(action: {
|
||
store.send(.loginButtonTapped(email: email, verificationCode: verificationCode))
|
||
}) {
|
||
ZStack {
|
||
// 渐变背景
|
||
LinearGradient(
|
||
colors: [
|
||
Color(red: 0.85, green: 0.37, blue: 1.0), // #D85EFF
|
||
Color(red: 0.54, green: 0.31, blue: 1.0) // #8A4FFF
|
||
],
|
||
startPoint: .leading,
|
||
endPoint: .trailing
|
||
)
|
||
.clipShape(RoundedRectangle(cornerRadius: 28))
|
||
|
||
HStack {
|
||
if store.isLoading {
|
||
ProgressView()
|
||
.progressViewStyle(CircularProgressViewStyle(tint: .white))
|
||
.scaleEffect(0.8)
|
||
}
|
||
Text(store.isLoading ? "email_login.logging_in".localized : "email_login.login_button".localized)
|
||
.font(.system(size: 18, weight: .semibold))
|
||
.foregroundColor(.white)
|
||
}
|
||
}
|
||
.frame(height: 56)
|
||
}
|
||
.disabled(store.isLoading || email.isEmpty || verificationCode.isEmpty)
|
||
.opacity(isLoginButtonEnabled ? 1.0 : 0.5)
|
||
.padding(.horizontal, 32)
|
||
|
||
// 错误信息
|
||
if let errorMessage = store.errorMessage {
|
||
Text(errorMessage)
|
||
.font(.system(size: 14))
|
||
.foregroundColor(.red)
|
||
.padding(.top, 16)
|
||
.padding(.horizontal, 32)
|
||
}
|
||
|
||
Spacer()
|
||
}
|
||
}
|
||
}
|
||
.onAppear {
|
||
// 每次进入页面都重置状态
|
||
store.send(.resetState)
|
||
|
||
email = ""
|
||
verificationCode = ""
|
||
codeCountdown = 0
|
||
stopCountdown()
|
||
|
||
#if DEBUG
|
||
email = "exzero@126.com"
|
||
store.send(.emailChanged(email))
|
||
#endif
|
||
}
|
||
.onDisappear {
|
||
stopCountdown()
|
||
}
|
||
.onChange(of: email) { newEmail in
|
||
store.send(.emailChanged(newEmail))
|
||
}
|
||
.onChange(of: verificationCode) { newCode in
|
||
store.send(.verificationCodeChanged(newCode))
|
||
}
|
||
.onChange(of: store.isCodeLoading) { isCodeLoading in
|
||
// 当API请求完成且成功时,自动将焦点切换到验证码输入框
|
||
if !isCodeLoading && store.errorMessage == nil {
|
||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
|
||
focusedField = .verificationCode
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// MARK: - 倒计时管理
|
||
private func startCountdown() {
|
||
stopCountdown()
|
||
|
||
// 立即设置倒计时
|
||
codeCountdown = 60
|
||
|
||
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in
|
||
DispatchQueue.main.async {
|
||
if codeCountdown > 0 {
|
||
codeCountdown -= 1
|
||
} else {
|
||
stopCountdown()
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private func stopCountdown() {
|
||
timer?.invalidate()
|
||
timer = nil
|
||
}
|
||
}
|
||
|
||
#Preview {
|
||
EMailLoginView(
|
||
store: Store(
|
||
initialState: EMailLoginFeature.State()
|
||
) {
|
||
EMailLoginFeature()
|
||
},
|
||
onBack: {}
|
||
)
|
||
}
|