summaryrefslogtreecommitdiff
path: root/Rune/Views/Settings/SettingsView.swift
blob: a776956db352699be734c76266c7e6b8cf770ad5 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import SwiftUI

struct SettingsView: View {
    @ObservedObject var viewModel: SettingsViewModel
    @ObservedObject var walletViewModel: WalletViewModel
    let onLogout: () -> Void
    @State private var showingLogoutConfirmation = false

    var body: some View {
        NavigationStack {
            List {
                Section("Wallet") {
                    if let balance = viewModel.balance {
                        HStack {
                            Text("Balance")
                            Spacer()
                            Text("€\(balance.balance)")
                                .foregroundStyle(.secondary)
                        }
                    } else if viewModel.isLoadingBalance {
                        ProgressView()
                    } else {
                        Text("Wallet balance unavailable.")
                            .foregroundStyle(.secondary)
                    }

                    Button("Refresh Balance") {
                        Task {
                            do {
                                try await viewModel.refreshBalance()
                            } catch {
                                viewModel.errorMessage = error.localizedDescription
                            }
                        }
                    }
                    .disabled(viewModel.client == nil || viewModel.isLoadingBalance)

                    if let client = viewModel.client {
                        NavigationLink("Transactions") {
                            WalletTransactionsView(viewModel: walletViewModel, client: client)
                        }
                    }
                }

                Section("Account") {
                    Button("Logout", role: .destructive) {
                        showingLogoutConfirmation = true
                    }
                    .foregroundStyle(.red)
                }

                if let errorMessage = viewModel.errorMessage {
                    Section {
                        Text(errorMessage)
                            .foregroundStyle(.red)
                    }
                }
            }
            .navigationTitle("Settings")
        }
        .confirmationDialog(
            "Log out of Rune?",
            isPresented: $showingLogoutConfirmation,
            titleVisibility: .visible
        ) {
            Button("Log Out", role: .destructive) {
                Task {
                    do {
                        try await viewModel.logout()
                        onLogout()
                    } catch {
                        viewModel.errorMessage = error.localizedDescription
                    }
                }
            }

            Button("Cancel", role: .cancel) {}
        } message: {
            Text("Your API token will be removed from this device.")
        }
    }
}