Bubble/Threaded/Data/Accounts/AccountManager.swift

54 lines
1.3 KiB
Swift
Raw Normal View History

2023-12-29 11:17:37 +01:00
//Made by Lumaa
import Foundation
@Observable
2024-02-11 17:00:29 +01:00
public class AccountManager: ObservableObject {
private var client: Client?
private var account: Account?
2024-02-24 13:21:31 +01:00
public static var shared: AccountManager = AccountManager()
2024-02-11 17:00:29 +01:00
init(client: Client? = nil, account: Account? = nil) {
self.client = client
self.account = account
}
public func clear() {
self.client = nil
self.account = nil
}
public func setClient(_ client: Client) {
self.client = client
}
public func getClient() -> Client? {
return client
}
2024-02-11 17:00:29 +01:00
public func setAccount(_ account: Account) {
self.account = account
}
public func getAccount() -> Account? {
return account
}
public func forceClient() -> Client {
2024-01-05 12:54:57 +01:00
guard client != nil else { fatalError("Client is not existant in that context") }
return client!
}
public func forceAccount() -> Account {
2024-01-05 12:54:57 +01:00
guard account != nil else { fatalError("Account is not existant in that context") }
return account!
}
public func fetchAccount() async -> Account? {
guard client != nil else { fatalError("Client is not existant in that context") }
account = try? await client!.get(endpoint: Accounts.verifyCredentials)
return account
}
}