NetNewsWire/Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/AccountsPreferencesModel.swift

94 lines
2.5 KiB
Swift
Raw Normal View History

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 {
2020-12-02 14:09:59 +01:00
case addAccountPicker, credentials, none
2020-07-14 08:57:55 +02:00
}
// Selected Account
2020-07-14 08:57:55 +02:00
public private(set) var account: Account?
// All 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
// 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
var cancellables = Set<AnyCancellable>()
2020-07-13 15:41:02 +02:00
init() {
sortedAccounts = AccountManager.shared.sortedAccounts
NotificationCenter.default.publisher(for: .UserDidAddAccount).sink { [weak self] _ in
2020-07-14 15:14:16 +02:00
self?.sortedAccounts = AccountManager.shared.sortedAccounts
}.store(in: &cancellables)
2020-07-13 15:41:02 +02:00
NotificationCenter.default.publisher(for: .UserDidDeleteAccount).sink { [weak self] _ in
2020-07-14 15:14:16 +02:00
self?.selectedConfiguredAccountID = nil
self?.sortedAccounts = AccountManager.shared.sortedAccounts
self?.selectedConfiguredAccountID = AccountManager.shared.defaultAccount.accountID
}.store(in: &cancellables)
2020-07-14 04:44:59 +02:00
NotificationCenter.default.publisher(for: .AccountStateDidChange).sink { [weak self] notification in
2020-07-14 04:44:59 +02:00
guard let account = notification.object as? Account else {
return
}
2020-07-14 15:14:16 +02:00
if account.accountID == self?.account?.accountID {
self?.account = account
self?.accountIsActive = account.isActive
self?.accountName = account.name ?? ""
2020-07-14 04:44:59 +02:00
}
}.store(in: &cancellables)
2020-07-13 15:41:02 +02:00
}
}