mirror of
https://github.com/Ranchero-Software/NetNewsWire.git
synced 2025-01-09 08:23:42 +01:00
96 lines
2.6 KiB
Swift
96 lines
2.6 KiB
Swift
//
|
|
// AccountsPreferencesModel.swift
|
|
// Multiplatform macOS
|
|
//
|
|
// Created by Stuart Breckenridge on 13/7/20.
|
|
// Copyright © 2020 Ranchero Software. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import Account
|
|
import Combine
|
|
|
|
class AccountsPreferencesModel: ObservableObject {
|
|
|
|
enum AccountConfigurationSheets {
|
|
case add, credentials, none
|
|
}
|
|
|
|
public private(set) var account: Account?
|
|
|
|
// Configured Accounts
|
|
@Published var sortedAccounts: [Account] = []
|
|
@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 ?? ""
|
|
}
|
|
}
|
|
}
|
|
@Published var showAddAccountView: Bool = false
|
|
var selectedAccountIsDefault: Bool {
|
|
guard let selected = selectedConfiguredAccountID else {
|
|
return true
|
|
}
|
|
if selected == AccountManager.shared.defaultAccount.accountID {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// Edit Account
|
|
@Published var accountIsActive: Bool = false {
|
|
didSet {
|
|
account?.isActive = accountIsActive
|
|
}
|
|
}
|
|
@Published var accountName: String = "" {
|
|
didSet {
|
|
account?.name = accountName
|
|
}
|
|
}
|
|
@Published var showAddCredentialsView: Bool = false
|
|
|
|
// Sheets
|
|
@Published var showSheet: Bool = false
|
|
@Published var sheetToShow: AccountConfigurationSheets = .none {
|
|
didSet {
|
|
showSheet = sheetToShow != .none
|
|
}
|
|
}
|
|
@Published var showDeleteConfirmation: Bool = false
|
|
|
|
|
|
|
|
// Subscriptions
|
|
var notificationSubscriptions = Set<AnyCancellable>()
|
|
|
|
init() {
|
|
sortedAccounts = AccountManager.shared.sortedAccounts
|
|
|
|
NotificationCenter.default.publisher(for: .UserDidAddAccount).sink(receiveValue: { _ in
|
|
self.sortedAccounts = AccountManager.shared.sortedAccounts
|
|
}).store(in: ¬ificationSubscriptions)
|
|
|
|
NotificationCenter.default.publisher(for: .UserDidDeleteAccount).sink(receiveValue: { _ in
|
|
self.selectedConfiguredAccountID = nil
|
|
self.sortedAccounts = AccountManager.shared.sortedAccounts
|
|
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 ?? ""
|
|
}
|
|
}).store(in: ¬ificationSubscriptions)
|
|
}
|
|
|
|
}
|