yattee/Model/AccountsModel.swift

46 lines
1.1 KiB
Swift
Raw Normal View History

2021-10-16 22:48:58 +00:00
import Combine
import Defaults
import Foundation
final class AccountsModel: ObservableObject {
@Published private(set) var account: Instance.Account!
@Published private(set) var invidious = InvidiousAPI()
@Published private(set) var piped = PipedAPI()
private var cancellables = [AnyCancellable]()
var all: [Instance.Account] {
Defaults[.instances].map(\.anonymousAccount) + Defaults[.accounts]
}
var signedIn: Bool {
!account.isNil && !account.anonymous
}
init() {
cancellables.append(
invidious.objectWillChange.sink { [weak self] _ in self?.objectWillChange.send() }
)
cancellables.append(
piped.objectWillChange.sink { [weak self] _ in self?.objectWillChange.send() }
)
}
func setAccount(_ account: Instance.Account) {
guard account != self.account else {
return
}
self.account = account
switch account.instance.app {
case .invidious:
invidious.setAccount(account)
case .piped:
piped.setAccount(account)
}
}
}