summaryrefslogtreecommitdiff
path: root/Rune/Views/Onboarding/OnboardingView.swift
blob: 3fba6c9a6abfcccf8bfd745dfa92a56fa67340e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import SwiftUI

struct OnboardingView: View {
    @ObservedObject var viewModel: SettingsViewModel

    @State private var token = ""
    @State private var isSubmitting = false
    @State private var localErrorMessage: String?

    var body: some View {
        NavigationStack {
            Form {
                Section {
                    SecureField("Enter Njalla API token", text: $token)
                        .textInputAutocapitalization(.never)
                        .autocorrectionDisabled()

                    Button("Validate and Save") {
                        Task {
                            await submit()
                        }
                    }
                    .disabled(isSubmitting || token.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
                } header: {
                    Text("API Token")
                } footer: {
                    Text("Rune validates the token with `get-balance` before saving it to Keychain.")
                }

                if let localErrorMessage {
                    Section {
                        Text(localErrorMessage)
                            .foregroundStyle(.red)
                    }
                }
            }
            .navigationTitle("Welcome")
            .overlay {
                if isSubmitting {
                    ProgressView()
                        .controlSize(.large)
                }
            }
        }
    }

    private func submit() async {
        isSubmitting = true
        defer {
            isSubmitting = false
        }

        do {
            try await viewModel.login(token: token)
            localErrorMessage = nil
        } catch {
            localErrorMessage = error.localizedDescription
        }
    }
}