Provide UserAccountInfo to MastodonController at initialization

This commit is contained in:
Shadowfacts 2023-05-28 21:28:20 -07:00
parent 878744b636
commit 06f7e306e0
1 changed files with 13 additions and 8 deletions

View File

@ -27,11 +27,7 @@ class MastodonController: ObservableObject {
if let controller = all[account] {
return controller
} else {
let controller = MastodonController(instanceURL: account.instanceURL)
controller.accountInfo = account
controller.client.clientID = account.clientID
controller.client.clientSecret = account.clientSecret
controller.client.accessToken = account.accessToken
let controller = MastodonController(instanceURL: account.instanceURL, accountInfo: account)
all[account] = controller
return controller
}
@ -72,11 +68,15 @@ class MastodonController: ObservableObject {
accountInfo != nil
}
init(instanceURL: URL, transient: Bool = false) {
init(instanceURL: URL, accountInfo: UserAccountInfo?) {
self.instanceURL = instanceURL
self.accountInfo = nil
self.accountInfo = accountInfo
self.client = Client(baseURL: instanceURL, session: .appDefault)
self.transient = transient
self.transient = accountInfo == nil
self.client.clientID = accountInfo?.clientID
self.client.clientSecret = accountInfo?.clientSecret
self.client.accessToken = accountInfo?.accessToken
$instance
.combineLatest($nodeInfo)
@ -103,6 +103,11 @@ class MastodonController: ObservableObject {
.store(in: &cancellables)
}
convenience init(instanceURL: URL, transient: Bool) {
precondition(transient, "account info must be provided if transient is false")
self.init(instanceURL: instanceURL, accountInfo: nil)
}
@discardableResult
func run<Result>(_ request: Request<Result>, completion: @escaping Client.Callback<Result>) -> URLSessionTask? {
return client.run(request, completion: completion)