2023-12-29 11:17:37 +01:00
|
|
|
//Made by Lumaa
|
|
|
|
|
|
|
|
import Foundation
|
2024-01-31 16:47:29 +01:00
|
|
|
|
2024-01-02 14:23:36 +01:00
|
|
|
@Observable
|
2024-02-11 17:00:29 +01:00
|
|
|
public class AccountManager: ObservableObject {
|
2024-01-02 14:23:36 +01:00
|
|
|
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
|
|
|
|
2024-01-02 14:23:36 +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
|
|
|
|
}
|
|
|
|
|
2024-01-02 14:23:36 +01:00
|
|
|
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!
|
2024-01-02 14:23:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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!
|
2024-01-02 14:23:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|