krz/rune

an ios client for njalla

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

v1.0.0: Rune/Views/Onboarding/OnboardingView.swift · raw

 1import SwiftUI
 2
 3struct OnboardingView: View {
 4    @ObservedObject var viewModel: SettingsViewModel
 5
 6    @State private var token = ""
 7    @State private var isSubmitting = false
 8    @State private var localErrorMessage: String?
 9
10    var body: some View {
11        NavigationStack {
12            Form {
13                Section {
14                    SecureField("Enter Njalla API token", text: $token)
15                        .textInputAutocapitalization(.never)
16                        .autocorrectionDisabled()
17
18                    Button("Validate and Save") {
19                        Task {
20                            await submit()
21                        }
22                    }
23                    .disabled(isSubmitting || token.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
24                } header: {
25                    Text("API Token")
26                } footer: {
27                    Text("Rune validates the token with `get-balance` before saving it to Keychain.")
28                }
29
30                if let localErrorMessage {
31                    Section {
32                        Text(localErrorMessage)
33                            .foregroundStyle(.red)
34                    }
35                }
36            }
37            .navigationTitle("Welcome")
38            .overlay {
39                if isSubmitting {
40                    ProgressView()
41                        .controlSize(.large)
42                }
43            }
44        }
45    }
46
47    private func submit() async {
48        isSubmitting = true
49        defer {
50            isSubmitting = false
51        }
52
53        do {
54            try await viewModel.login(token: token)
55            localErrorMessage = nil
56        } catch {
57            localErrorMessage = error.localizedDescription
58        }
59    }
60}