| 1 | import RastrilloApple |
| 2 | import SheetsKit |
| 3 | import SwiftUI |
| 4 | |
| 5 | /// Each window owns its navigation and model lifetime. Switching an account in |
| 6 | /// one window must never discard another window's unconfirmed edits. |
| 7 | struct SheetsAccountWindow: View { |
| 8 | @StateObject private var connection = NativeConnection(defaultServer: "https://sheets.oficina.works") |
| 9 | #if os(macOS) |
| 10 | @Bindable var windows: SheetsWindows |
| 11 | @Environment(\.openWindow) private var openWindow |
| 12 | #endif |
| 13 | var body: some View { |
| 14 | Group { |
| 15 | if let client = connection.client { |
| 16 | #if os(macOS) |
| 17 | if let account = connection.activeAccountID { |
| 18 | SheetsWorkspace(client: client, accounts: connection.showAccounts, signOut: { |
| 19 | guard connection.activeAccountID == account, windows.canSignOut(account) else { return } |
| 20 | windows.entries.removeAll { $0.account == account } |
| 21 | connection.signOut() |
| 22 | }, canSignOut: windows.canSignOut(account), createRequested: $windows.createRequested, accountsRequested: $windows.accountsRequested, openDocument: { document, title in |
| 23 | guard connection.activeAccountID == account else { return } |
| 24 | let id = windows.reserve(account: account, document: document, title: title, client: client) |
| 25 | openWindow(id: "document", value: id) |
| 26 | }, updateRecents: { documents in |
| 27 | guard connection.activeAccountID == account else { return } |
| 28 | windows.updateRecents(documents, account: account, client: client) |
| 29 | }) |
| 30 | .id(account) |
| 31 | } |
| 32 | #else |
| 33 | SheetsWorkspace(client: client, accounts: connection.showAccounts, signOut: connection.signOut) |
| 34 | .id(connection.activeAccountID) |
| 35 | #endif |
| 36 | } else { |
| 37 | NativeAccountsView("Oficina Sheets", systemImage: "tablecells", connection: connection) |
| 38 | } |
| 39 | }.onOpenURL(perform: connection.handleOpenURL) |
| 40 | #if os(macOS) |
| 41 | .onChange(of: windows.accountsRequested, initial: true) { _, requested in |
| 42 | if requested && connection.client == nil { windows.accountsRequested = false } |
| 43 | } |
| 44 | .focusedSceneValue(\.sheetsWindowAccount, connection.activeAccountID) |
| 45 | .onChange(of: connection.activeAccountID, initial: true) { _, value in windows.activeAccount = value } |
| 46 | #endif |
| 47 | .tint(.green) |
| 48 | #if os(macOS) |
| 49 | .frame(minWidth: 760, minHeight: 600) |
| 50 | #endif |
| 51 | } |
| 52 | } |
| 53 | |