krz/hutch

an ios client for sourcehut

clone: git clone https://gitbay.org/krz/hutch.git

v3.4.0: Hutch/Views/More/AddAccountView.swift · raw

 1import SwiftUI
 2
 3struct AddAccountView: View {
 4    @Environment(AppState.self) private var appState
 5    @Environment(\.dismiss) private var dismiss
 6
 7    @State private var token = ""
 8    @State private var isConnecting = false
 9    @State private var errorMessage: String?
10
11    var body: some View {
12        NavigationStack {
13            Form {
14                Section {
15                    SecureField("Personal Access Token", text: $token)
16                        .autocorrectionDisabled()
17                        .textInputAutocapitalization(.never)
18                        .themedRow()
19                } footer: {
20                    Text("Generate a token at meta.sr.ht → OAuth2 clients.")
21                }
22
23                if let errorMessage {
24                    Section {
25                        Text(errorMessage)
26                            .foregroundStyle(.red)
27                            .themedRow()
28                    }
29                }
30            }
31            .themedList()
32            .navigationTitle("Add Account")
33            .navigationBarTitleDisplayMode(.inline)
34            .toolbar {
35                ToolbarItem(placement: .cancellationAction) {
36                    Button("Cancel") { dismiss() }
37                        .disabled(isConnecting)
38                }
39                ToolbarItem(placement: .confirmationAction) {
40                    Button("Connect") { connect() }
41                        .disabled(token.trimmingCharacters(in: .whitespaces).isEmpty || isConnecting)
42                }
43            }
44            .interactiveDismissDisabled(isConnecting)
45        }
46    }
47
48    private func connect() {
49        isConnecting = true
50        errorMessage = nil
51        let trimmed = token.trimmingCharacters(in: .whitespaces)
52        Task {
53            do {
54                try await appState.addAccount(token: trimmed)
55                dismiss()
56            } catch {
57                errorMessage = error.localizedDescription
58            }
59            isConnecting = false
60        }
61    }
62}