|
| 1 | +// |
| 2 | +// UpdatePassword.swift |
| 3 | +// FirebaseUI |
| 4 | +// |
| 5 | +// Created by Russell Wheatley on 24/04/2025. |
| 6 | +// |
| 7 | + |
| 8 | +import SwiftUI |
| 9 | + |
| 10 | +private enum FocusableField: Hashable { |
| 11 | + case password |
| 12 | + case confirmPassword |
| 13 | +} |
| 14 | + |
| 15 | +@MainActor |
| 16 | +public struct UpdatePasswordView { |
| 17 | + @Environment(AuthService.self) private var authService |
| 18 | + @State private var password = "" |
| 19 | + @State private var confirmPassword = "" |
| 20 | + |
| 21 | + @FocusState private var focus: FocusableField? |
| 22 | + private var isValid: Bool { |
| 23 | + !password.isEmpty && password == confirmPassword |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +extension UpdatePasswordView: View { |
| 28 | + private var isShowingPasswordPrompt: Binding<Bool> { |
| 29 | + Binding( |
| 30 | + get: { authService.passwordPrompt.isPromptingPassword }, |
| 31 | + set: { authService.passwordPrompt.isPromptingPassword = $0 } |
| 32 | + ) |
| 33 | + } |
| 34 | + |
| 35 | + public var body: some View { |
| 36 | + VStack { |
| 37 | + LabeledContent { |
| 38 | + SecureField("Password", text: $password) |
| 39 | + .focused($focus, equals: .password) |
| 40 | + .submitLabel(.go) |
| 41 | + } label: { |
| 42 | + Image(systemName: "lock") |
| 43 | + } |
| 44 | + .padding(.vertical, 6) |
| 45 | + .background(Divider(), alignment: .bottom) |
| 46 | + .padding(.bottom, 8) |
| 47 | + |
| 48 | + Divider() |
| 49 | + |
| 50 | + LabeledContent { |
| 51 | + SecureField("Confirm password", text: $confirmPassword) |
| 52 | + .focused($focus, equals: .confirmPassword) |
| 53 | + .submitLabel(.go) |
| 54 | + } label: { |
| 55 | + Image(systemName: "lock") |
| 56 | + } |
| 57 | + .padding(.vertical, 6) |
| 58 | + .background(Divider(), alignment: .bottom) |
| 59 | + .padding(.bottom, 8) |
| 60 | + |
| 61 | + Divider() |
| 62 | + |
| 63 | + Button(action: { |
| 64 | + Task { |
| 65 | + try await authService.updatePassword(to: confirmPassword) |
| 66 | + authService.authView = .authPicker |
| 67 | + } |
| 68 | + }, label: { |
| 69 | + Text("Update password") |
| 70 | + .padding(.vertical, 8) |
| 71 | + .frame(maxWidth: .infinity) |
| 72 | + |
| 73 | + }) |
| 74 | + .disabled(!isValid) |
| 75 | + .padding([.top, .bottom], 8) |
| 76 | + .frame(maxWidth: .infinity) |
| 77 | + .buttonStyle(.borderedProminent) |
| 78 | + }.sheet(isPresented: isShowingPasswordPrompt) { |
| 79 | + PasswordPromptSheet(coordinator: authService.passwordPrompt) |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments