mirror of
https://github.com/yattee/yattee.git
synced 2025-08-09 20:24:06 +00:00
New account selection menu
This commit is contained in:
36
Shared/Navigation/AccountsViewModel.swift
Normal file
36
Shared/Navigation/AccountsViewModel.swift
Normal file
@@ -0,0 +1,36 @@
|
||||
import Foundation
|
||||
|
||||
final class AccountsViewModel: ObservableObject {
|
||||
typealias AreInIncreasingOrder = (Account, Account) -> Bool
|
||||
|
||||
var accounts: [Account] { AccountsModel.shared.all }
|
||||
|
||||
var sortedAccounts: [Account] {
|
||||
accounts.sorted { lhs, rhs in
|
||||
let predicates: [AreInIncreasingOrder] = [
|
||||
{ ($0.app ?? .local).rawValue < ($1.app ?? .local).rawValue },
|
||||
{ $0.urlHost < $1.urlHost },
|
||||
{ $0.description < $1.description }
|
||||
]
|
||||
|
||||
for predicate in predicates {
|
||||
if !predicate(lhs, rhs), !predicate(rhs, lhs) {
|
||||
continue
|
||||
}
|
||||
|
||||
return predicate(lhs, rhs)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
var publicAccount: Account? { AccountsModel.shared.publicAccount }
|
||||
var currentAccount: Account? { AccountsModel.shared.current }
|
||||
|
||||
var instances: [Instance] { InstancesModel.shared.all }
|
||||
|
||||
func accountsOfInstance(_ instance: Instance) -> [Account] {
|
||||
accounts.filter { $0.instance.apiURL == instance.apiURL }.sorted { $0.name < $1.name }
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user