2020-07-13 15:41:02 +02:00
|
|
|
//
|
2020-07-13 17:10:50 +02:00
|
|
|
// AccountsPreferencesModel.swift
|
2020-07-13 15:41:02 +02:00
|
|
|
// Multiplatform macOS
|
|
|
|
//
|
|
|
|
// Created by Stuart Breckenridge on 13/7/20.
|
|
|
|
// Copyright © 2020 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Account
|
|
|
|
import Combine
|
|
|
|
|
2020-07-13 17:10:50 +02:00
|
|
|
class AccountsPreferencesModel: ObservableObject {
|
2020-07-13 15:41:02 +02:00
|
|
|
|
2020-07-14 08:57:55 +02:00
|
|
|
enum AccountConfigurationSheets {
|
|
|
|
case add, credentials, none
|
|
|
|
}
|
|
|
|
|
|
|
|
public private(set) var account: Account?
|
|
|
|
|
2020-07-14 04:44:59 +02:00
|
|
|
// Configured Accounts
|
2020-07-13 15:41:02 +02:00
|
|
|
@Published var sortedAccounts: [Account] = []
|
2020-07-14 04:44:59 +02:00
|
|
|
@Published var selectedConfiguredAccountID: String? = AccountManager.shared.defaultAccount.accountID {
|
|
|
|
didSet {
|
|
|
|
if let accountID = selectedConfiguredAccountID {
|
|
|
|
account = sortedAccounts.first(where: { $0.accountID == accountID })
|
|
|
|
accountIsActive = account?.isActive ?? false
|
|
|
|
accountName = account?.name ?? ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-13 15:41:02 +02:00
|
|
|
@Published var showAddAccountView: Bool = false
|
2020-07-14 08:57:55 +02:00
|
|
|
var selectedAccountIsDefault: Bool {
|
|
|
|
guard let selected = selectedConfiguredAccountID else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if selected == AccountManager.shared.defaultAccount.accountID {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2020-07-13 15:41:02 +02:00
|
|
|
|
2020-07-14 04:44:59 +02:00
|
|
|
// Edit Account
|
|
|
|
@Published var accountIsActive: Bool = false {
|
|
|
|
didSet {
|
|
|
|
account?.isActive = accountIsActive
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Published var accountName: String = "" {
|
|
|
|
didSet {
|
|
|
|
account?.name = accountName
|
|
|
|
}
|
|
|
|
}
|
2020-07-14 08:57:55 +02:00
|
|
|
@Published var showAddCredentialsView: Bool = false
|
2020-07-14 04:44:59 +02:00
|
|
|
|
2020-07-14 08:57:55 +02:00
|
|
|
// Sheets
|
|
|
|
@Published var showSheet: Bool = false
|
|
|
|
@Published var sheetToShow: AccountConfigurationSheets = .none {
|
|
|
|
didSet {
|
|
|
|
showSheet = sheetToShow != .none
|
2020-07-13 15:41:02 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-14 08:57:55 +02:00
|
|
|
@Published var showDeleteConfirmation: Bool = false
|
|
|
|
|
|
|
|
|
2020-07-13 15:41:02 +02:00
|
|
|
|
|
|
|
// Subscriptions
|
2020-07-13 16:29:33 +02:00
|
|
|
var notificationSubscriptions = Set<AnyCancellable>()
|
2020-07-13 15:41:02 +02:00
|
|
|
|
|
|
|
init() {
|
|
|
|
sortedAccounts = AccountManager.shared.sortedAccounts
|
|
|
|
|
|
|
|
NotificationCenter.default.publisher(for: .UserDidAddAccount).sink(receiveValue: { _ in
|
|
|
|
self.sortedAccounts = AccountManager.shared.sortedAccounts
|
2020-07-13 16:29:33 +02:00
|
|
|
}).store(in: ¬ificationSubscriptions)
|
2020-07-13 15:41:02 +02:00
|
|
|
|
|
|
|
NotificationCenter.default.publisher(for: .UserDidDeleteAccount).sink(receiveValue: { _ in
|
|
|
|
self.selectedConfiguredAccountID = nil
|
|
|
|
self.sortedAccounts = AccountManager.shared.sortedAccounts
|
2020-07-14 04:44:59 +02:00
|
|
|
self.selectedConfiguredAccountID = AccountManager.shared.defaultAccount.accountID
|
|
|
|
}).store(in: ¬ificationSubscriptions)
|
|
|
|
|
|
|
|
NotificationCenter.default.publisher(for: .AccountStateDidChange).sink(receiveValue: { notification in
|
|
|
|
guard let account = notification.object as? Account else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if account.accountID == self.account?.accountID {
|
|
|
|
self.account = account
|
|
|
|
self.accountIsActive = account.isActive
|
|
|
|
self.accountName = account.name ?? ""
|
|
|
|
}
|
2020-07-13 16:29:33 +02:00
|
|
|
}).store(in: ¬ificationSubscriptions)
|
2020-07-13 15:41:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|