krz/hutch

an ios client for sourcehut

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

v2.7.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                } footer: {
19                    Text("Generate a token at meta.sr.ht → OAuth2 clients.")
20                }
21
22                if let errorMessage {
23                    Section {
24                        Text(errorMessage)
25                            .foregroundStyle(.red)
26                    }
27                }
28            }
29            .navigationTitle("Add Account")
30            .navigationBarTitleDisplayMode(.inline)
31            .toolbar {
32                ToolbarItem(placement: .cancellationAction) {
33                    Button("Cancel") { dismiss() }
34                        .disabled(isConnecting)
35                }
36                ToolbarItem(placement: .confirmationAction) {
37                    Button("Connect") { connect() }
38                        .disabled(token.trimmingCharacters(in: .whitespaces).isEmpty || isConnecting)
39                }
40            }
41            .interactiveDismissDisabled(isConnecting)
42        }
43    }
44
45    private func connect() {
46        isConnecting = true
47        errorMessage = nil
48        let trimmed = token.trimmingCharacters(in: .whitespaces)
49        Task {
50            do {
51                try await appState.addAccount(token: trimmed)
52                dismiss()
53            } catch {
54                errorMessage = error.localizedDescription
55            }
56            isConnecting = false
57        }
58    }
59}