LoginView.swift (6637B)
1 // SPDX-License-Identifier: GPL-2.0-only 2 import SwiftUI 3 4 struct LoginView: View { 5 @Environment(AppModel.self) private var model 6 @State private var email = "" 7 @State private var password = "" 8 @State private var busy = false 9 @State private var creating = LaunchOptions.flag("-createAccount") 10 @State private var error: String? 11 @FocusState private var focus: Field? 12 13 private enum Field { case email, password } 14 15 private var disclaimer: String { 16 String(localized: "The account is your Subsurface cloud account (cloud.subsurface-divelog.org), the same one you use in Subsurface. Your credentials are sent only to that service, and the password stays in the iPhone keychain.\n\n\(Brand.name) is an independent app, not affiliated with the Subsurface team.") 17 } 18 19 var body: some View { 20 NavigationStack { 21 ScrollView { 22 VStack(spacing: 20) { 23 Image("Logo") 24 .resizable() 25 .frame(width: 120, height: 120) 26 .clipShape(RoundedRectangle(cornerRadius: 27, style: .continuous)) 27 .shadow(radius: 8, y: 4) 28 .padding(.top, 32) 29 Text(Brand.name) 30 .font(.largeTitle.bold()) 31 Text("Your dive log, on iPhone.") 32 .font(.subheadline) 33 .foregroundStyle(.secondary) 34 .multilineTextAlignment(.center) 35 36 VStack(spacing: 12) { 37 TextField("Subsurface account email", text: $email) 38 .textContentType(.username) 39 .keyboardType(.emailAddress) 40 .textInputAutocapitalization(.never) 41 .autocorrectionDisabled() 42 .focused($focus, equals: .email) 43 .submitLabel(.next) 44 .onSubmit { focus = .password } 45 Divider() 46 SecureField("Password", text: $password) 47 .textContentType(.password) 48 .focused($focus, equals: .password) 49 .submitLabel(.go) 50 .onSubmit { signIn() } 51 } 52 .padding() 53 .background(Color(.secondarySystemGroupedBackground), in: RoundedRectangle(cornerRadius: 14)) 54 55 if let error { 56 Label(error, systemImage: "exclamationmark.triangle.fill") 57 .font(.footnote) 58 .foregroundStyle(.red) 59 .frame(maxWidth: .infinity, alignment: .leading) 60 } 61 62 Button(action: signIn) { 63 HStack { 64 if busy { ProgressView().tint(.white) } 65 Text(busy ? String(localized: "Signing in…") : String(localized: "Sign in")) 66 .fontWeight(.semibold) 67 } 68 .frame(maxWidth: .infinity) 69 .padding(.vertical, 6) 70 } 71 .buttonStyle(.borderedProminent) 72 .disabled(busy || email.isEmpty || password.isEmpty) 73 74 HStack(spacing: 24) { 75 Button("Create an account") { creating = true } 76 .disabled(busy) 77 Link("Forgot password?", destination: URL(string: "https://cloud.subsurface-divelog.org/passwordreset")!) 78 } 79 .font(.subheadline) 80 81 VStack(spacing: 10) { 82 Text("Or, without an account") 83 .font(.footnote) 84 .foregroundStyle(.secondary) 85 if let found = model.backup.available { 86 Button { 87 run { try await model.restoreFromBackup() } 88 } label: { 89 Label("Restore the iCloud backup (\(found.dives) dives, \(found.saved.formatted(date: .abbreviated, time: .omitted)))", 90 systemImage: "icloud.and.arrow.down") 91 .frame(maxWidth: .infinity) 92 } 93 .buttonStyle(.bordered) 94 } 95 Button { 96 run { try await model.startLocal() } 97 } label: { 98 Text("Keep my logbook on this iPhone").frame(maxWidth: .infinity) 99 } 100 .buttonStyle(.bordered) 101 Button("Try a demo logbook") { 102 Task { await model.startDemo() } 103 } 104 .font(.subheadline) 105 } 106 .disabled(busy) 107 .padding(.top, 8) 108 109 Text(disclaimer) 110 .font(.footnote) 111 .foregroundStyle(.secondary) 112 .multilineTextAlignment(.center) 113 .padding(.top, 8) 114 } 115 .padding(.horizontal, 24) 116 } 117 .background(Color(.systemGroupedBackground)) 118 .scrollDismissesKeyboard(.interactively) 119 .task { await model.backup.refresh() } 120 .sheet(isPresented: $creating) { 121 CreateAccountView { newEmail, newPassword in 122 email = newEmail 123 password = newPassword 124 signIn() 125 } 126 } 127 } 128 } 129 130 private func run(_ work: @escaping () async throws -> Void) { 131 guard !busy else { return } 132 busy = true 133 error = nil 134 Task { 135 do { 136 try await work() 137 } catch { 138 self.error = AppModel.describe(error) 139 } 140 busy = false 141 } 142 } 143 144 private func signIn() { 145 guard !busy, !email.isEmpty, !password.isEmpty else { return } 146 busy = true 147 error = nil 148 focus = nil 149 Task { 150 do { 151 try await model.signIn(email: email, password: password) 152 } catch { 153 self.error = AppModel.describe(error) 154 } 155 busy = false 156 } 157 } 158 }