| 1 | import SwiftUI |
| 2 | |
| 3 | @MainActor public struct NativeAccountsView: View { |
| 4 | @ObservedObject private var connection: NativeConnection |
| 5 | private let name: String |
| 6 | private let symbol: String |
| 7 | private let addAccount: (() -> Void)? |
| 8 | @State private var adding = false |
| 9 | |
| 10 | public init(_ name: String, systemImage: String, connection: NativeConnection, addAccount: (() -> Void)? = nil) { |
| 11 | self.addAccount = addAccount |
| 12 | self.name = name |
| 13 | self.symbol = systemImage |
| 14 | self.connection = connection |
| 15 | } |
| 16 | |
| 17 | public var body: some View { |
| 18 | NavigationStack { |
| 19 | List { |
| 20 | ForEach(connection.accounts) { account in |
| 21 | Button { |
| 22 | connection.selectAccount(account) |
| 23 | } label: { |
| 24 | Label { |
| 25 | VStack(alignment: .leading) { |
| 26 | Text(verbatim: account.name).foregroundStyle(.primary) |
| 27 | Text(verbatim: account.origin.host ?? account.origin.absoluteString) |
| 28 | .font(.caption).foregroundStyle(.secondary) |
| 29 | } |
| 30 | } icon: { Image(systemName: "person.crop.circle") } |
| 31 | } |
| 32 | } |
| 33 | Button("Add account", systemImage: "plus") { |
| 34 | connection.accountName = "" |
| 35 | connection.error = "" |
| 36 | if let addAccount { addAccount() } else { adding = true } |
| 37 | } |
| 38 | if !connection.error.isEmpty { |
| 39 | Text(connection.error).foregroundStyle(.red) |
| 40 | } |
| 41 | } |
| 42 | .navigationTitle("Accounts") |
| 43 | .sheet(isPresented: $adding, onDismiss: { connection.cancel() }) { |
| 44 | NavigationStack { |
| 45 | NativeSignInView(name, systemImage: symbol, connection: connection) |
| 46 | .toolbar { |
| 47 | ToolbarItem(placement: .cancellationAction) { |
| 48 | Button("Cancel", role: .cancel) { adding = false } |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |