139 lines
6.0 KiB
Swift
139 lines
6.0 KiB
Swift
import SwiftUI
|
|
|
|
struct SignInView: View {
|
|
private enum Field: Hashable { case account, password }
|
|
|
|
let account: AtlasAccount
|
|
@Environment(\.colorScheme) private var colorScheme
|
|
@FocusState private var focusedField: Field?
|
|
@AccessibilityFocusState private var errorFocused: Bool
|
|
@State private var username = ""
|
|
@State private var password = ""
|
|
@State private var passwordVisible = false
|
|
@State private var submission: Task<Void, Never>?
|
|
|
|
private var hasCredentials: Bool {
|
|
!username.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty && !password.isEmpty
|
|
}
|
|
|
|
var body: some View {
|
|
ScrollView {
|
|
VStack(alignment: .leading, spacing: 26) {
|
|
EntryBrand()
|
|
VStack(alignment: .leading, spacing: 8) {
|
|
Text("登录我的空间")
|
|
.font(.largeTitle.bold())
|
|
.accessibilityAddTraits(.isHeader)
|
|
Text("登录后,继续管理你和家人的物品。")
|
|
.font(.subheadline)
|
|
.foregroundStyle(.primary.opacity(0.72))
|
|
}
|
|
VStack(alignment: .leading, spacing: 17) {
|
|
field(label: "账号", active: focusedField == .account) {
|
|
TextField("输入用户名", text: $username)
|
|
.textContentType(.username)
|
|
.textInputAutocapitalization(.never)
|
|
.autocorrectionDisabled()
|
|
.focused($focusedField, equals: .account)
|
|
.submitLabel(.next)
|
|
.onSubmit { focusedField = .password }
|
|
.accessibilityLabel("账号")
|
|
}
|
|
field(label: "密码", active: focusedField == .password) {
|
|
Group {
|
|
if passwordVisible {
|
|
TextField("输入密码", text: $password)
|
|
.textInputAutocapitalization(.never)
|
|
.autocorrectionDisabled()
|
|
} else {
|
|
SecureField("输入密码", text: $password)
|
|
}
|
|
}
|
|
.textContentType(.password)
|
|
.focused($focusedField, equals: .password)
|
|
.submitLabel(.go)
|
|
.onSubmit(submit)
|
|
.accessibilityLabel("密码")
|
|
|
|
Button {
|
|
passwordVisible.toggle()
|
|
} label: {
|
|
Image(systemName: passwordVisible ? "eye.slash" : "eye")
|
|
.frame(width: 44, height: 44)
|
|
}
|
|
.buttonStyle(.plain)
|
|
.foregroundStyle(.secondary)
|
|
.accessibilityLabel(passwordVisible ? "隐藏密码" : "显示密码")
|
|
}
|
|
if let error = account.error {
|
|
Label(error, systemImage: "exclamationmark.circle")
|
|
.font(.footnote)
|
|
.foregroundStyle(Color.red)
|
|
.accessibilityFocused($errorFocused)
|
|
}
|
|
Button(action: submit) {
|
|
HStack(spacing: 8) {
|
|
if account.signingIn {
|
|
ProgressView().tint(.white).accessibilityHidden(true)
|
|
}
|
|
Text(account.signingIn ? "正在登录…" : "登录")
|
|
}
|
|
.font(.body.weight(.semibold))
|
|
.frame(maxWidth: .infinity, minHeight: 29)
|
|
}
|
|
.buttonStyle(.borderedProminent)
|
|
.buttonBorderShape(.roundedRectangle(radius: 14))
|
|
.controlSize(.large)
|
|
.disabled(!hasCredentials || account.signingIn)
|
|
}
|
|
}
|
|
.fixedSize(horizontal: false, vertical: true)
|
|
.frame(maxWidth: 440, alignment: .leading)
|
|
.padding(.horizontal, 24)
|
|
.padding(.top, 19)
|
|
.padding(.bottom, 32)
|
|
.frame(maxWidth: .infinity)
|
|
}
|
|
.scrollDismissesKeyboard(.interactively)
|
|
.background(Color.entryBackground(for: colorScheme).ignoresSafeArea())
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
.toolbar(.visible, for: .navigationBar)
|
|
.onAppear { account.clearSignInError() }
|
|
.onChange(of: account.error) { _, error in
|
|
if error != nil { errorFocused = true }
|
|
}
|
|
.onDisappear {
|
|
submission?.cancel()
|
|
password = ""
|
|
}
|
|
}
|
|
|
|
private func field<Content: View>(label: String, active: Bool, @ViewBuilder content: () -> Content) -> some View {
|
|
VStack(alignment: .leading, spacing: 7) {
|
|
Text(label)
|
|
.font(.subheadline.weight(.semibold))
|
|
.accessibilityHidden(true)
|
|
HStack(spacing: 6, content: content)
|
|
.padding(.leading, 14)
|
|
.padding(.trailing, 8)
|
|
.frame(minHeight: 54)
|
|
.background(Color(uiColor: .secondarySystemGroupedBackground), in: .rect(cornerRadius: 13))
|
|
.overlay {
|
|
RoundedRectangle(cornerRadius: 13)
|
|
.strokeBorder(active ? .atlasBlue : Color(uiColor: .separator), lineWidth: active ? 2 : 1)
|
|
.allowsHitTesting(false)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func submit() {
|
|
guard hasCredentials, !account.signingIn, submission == nil else { return }
|
|
focusedField = nil
|
|
passwordVisible = false
|
|
submission = Task {
|
|
await account.signIn(username: username, password: password)
|
|
submission = nil
|
|
}
|
|
}
|
|
}
|