krz/rune

an ios client for njalla

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

v1.0.0: Rune/Views/Settings/SettingsView.swift · raw

 1import SwiftUI
 2
 3struct SettingsView: View {
 4    @ObservedObject var viewModel: SettingsViewModel
 5    let onLogout: () -> Void
 6    @State private var showingLogoutConfirmation = false
 7
 8    var body: some View {
 9        NavigationStack {
10            List {
11                Section("Wallet") {
12                    if let balance = viewModel.balance {
13                        HStack {
14                            Text("Balance")
15                            Spacer()
16                            Text("\(balance.balance)")
17                                .foregroundStyle(.secondary)
18                        }
19                    } else if viewModel.isLoadingBalance {
20                        ProgressView()
21                    } else {
22                        Text("Wallet balance unavailable.")
23                            .foregroundStyle(.secondary)
24                    }
25
26                    Button("Refresh Balance") {
27                        Task {
28                            do {
29                                try await viewModel.refreshBalance()
30                            } catch {
31                                viewModel.errorMessage = error.localizedDescription
32                            }
33                        }
34                    }
35                    .disabled(viewModel.client == nil || viewModel.isLoadingBalance)
36                }
37
38                Section("Account") {
39                    Button("Logout", role: .destructive) {
40                        showingLogoutConfirmation = true
41                    }
42                    .foregroundStyle(.red)
43                }
44
45                if let errorMessage = viewModel.errorMessage {
46                    Section {
47                        Text(errorMessage)
48                            .foregroundStyle(.red)
49                    }
50                }
51            }
52            .navigationTitle("Settings")
53        }
54        .confirmationDialog(
55            "Log out of Rune?",
56            isPresented: $showingLogoutConfirmation,
57            titleVisibility: .visible
58        ) {
59            Button("Log Out", role: .destructive) {
60                do {
61                    try viewModel.logout()
62                    onLogout()
63                } catch {
64                    viewModel.errorMessage = error.localizedDescription
65                }
66            }
67
68            Button("Cancel", role: .cancel) {}
69        } message: {
70            Text("Your API token will be removed from this device.")
71        }
72    }
73}