1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 06:40:32 +05:30
yattee/Model/Accounts/AccountsModel.swift

148 lines
3.9 KiB
Swift
Raw Normal View History

2021-10-17 04:18:58 +05:30
import Combine
import Defaults
import Foundation
final class AccountsModel: ObservableObject {
static let shared = AccountsModel()
2021-10-21 03:51:50 +05:30
@Published private(set) var current: Account!
2021-10-17 04:18:58 +05:30
2021-10-21 03:51:50 +05:30
@Published private var invidious = InvidiousAPI()
@Published private var piped = PipedAPI()
2022-12-09 05:45:19 +05:30
@Published private var peerTube = PeerTubeAPI()
2021-10-17 04:18:58 +05:30
@Published var publicAccount: Account?
2021-10-17 04:18:58 +05:30
private var cancellables = [AnyCancellable]()
2021-10-21 03:51:50 +05:30
var all: [Account] {
Defaults[.accounts]
}
2021-10-21 03:51:50 +05:30
var lastUsed: Account? {
guard let id = Defaults[.lastAccountID] else {
return nil
}
2023-04-22 18:38:33 +05:30
return Self.find(id)
2021-10-17 04:18:58 +05:30
}
2021-12-25 00:50:05 +05:30
var any: Account? {
lastUsed ?? all.randomElement()
}
2021-10-21 03:51:50 +05:30
var app: VideosApp {
2022-12-09 05:45:19 +05:30
current?.instance?.app ?? .local
2021-10-21 03:51:50 +05:30
}
2022-12-09 05:45:19 +05:30
var api: VideosAPI! {
switch app {
case .piped:
return piped
case .invidious:
return invidious
2022-12-09 05:45:19 +05:30
default:
2022-12-10 05:53:13 +05:30
return peerTube
}
2021-10-21 03:51:50 +05:30
}
2021-10-18 04:36:00 +05:30
var isEmpty: Bool {
current.isNil
2021-10-18 04:36:00 +05:30
}
2021-10-17 04:18:58 +05:30
var signedIn: Bool {
!isEmpty && !current.anonymous && api.signedIn
2021-10-17 04:18:58 +05:30
}
init() {
cancellables.append(
invidious.objectWillChange.sink { [weak self] _ in self?.objectWillChange.send() }
)
cancellables.append(
piped.objectWillChange.sink { [weak self] _ in self?.objectWillChange.send() }
)
}
2024-02-02 04:24:16 +05:30
func find(_ id: Account.ID) -> Account? {
all.first { $0.id == id }
}
func configureAccount() {
if let account = lastUsed ??
InstancesModel.shared.lastUsed?.anonymousAccount ??
InstancesModel.shared.all.first?.anonymousAccount
{
setCurrent(account)
}
}
2021-10-21 03:51:50 +05:30
func setCurrent(_ account: Account! = nil) {
guard account != current else {
2021-10-17 04:18:58 +05:30
return
}
current = account
2021-10-17 04:18:58 +05:30
2021-10-18 04:36:00 +05:30
guard !account.isNil else {
2022-11-12 01:04:20 +05:30
current = nil
2021-10-18 04:36:00 +05:30
return
}
2021-10-17 04:18:58 +05:30
switch account.instance.app {
2022-12-09 05:45:19 +05:30
case .local:
return
2021-10-17 04:18:58 +05:30
case .invidious:
invidious.setAccount(account)
case .piped:
piped.setAccount(account)
2022-12-09 05:45:19 +05:30
case .peerTube:
peerTube.setAccount(account)
2021-10-17 04:18:58 +05:30
}
Defaults[.lastAccountIsPublic] = account.isPublic
if !account.isPublic {
Defaults[.lastAccountID] = account.anonymous ? nil : account.id
Defaults[.lastInstanceID] = account.instanceID
}
}
2021-10-21 03:51:50 +05:30
static func find(_ id: Account.ID) -> Account? {
Defaults[.accounts].first { $0.id == id }
}
2024-02-02 04:24:16 +05:30
static func add(instance: Instance, id: String? = UUID().uuidString, name: String, username: String, password: String) -> Account {
let account = Account(id: id, instanceID: instance.id, name: name, urlString: instance.apiURLString)
Defaults[.accounts].append(account)
setCredentials(account, username: username, password: password)
return account
}
2021-10-21 03:51:50 +05:30
static func remove(_ account: Account) {
if let accountIndex = Defaults[.accounts].firstIndex(where: { $0.id == account.id }) {
let account = Defaults[.accounts][accountIndex]
KeychainModel.shared.removeAccountKeys(account)
Defaults[.accounts].remove(at: accountIndex)
}
2021-10-17 04:18:58 +05:30
}
static func setToken(_ account: Account, _ token: String) {
KeychainModel.shared.updateAccountKey(account, "token", token)
}
static func setCredentials(_ account: Account, username: String, password: String) {
KeychainModel.shared.updateAccountKey(account, "username", username)
KeychainModel.shared.updateAccountKey(account, "password", password)
}
static func getCredentials(_ account: Account) -> (String?, String?) {
(
KeychainModel.shared.getAccountKey(account, "username"),
KeychainModel.shared.getAccountKey(account, "password")
)
}
2021-10-17 04:18:58 +05:30
}