From 06f7e306e046f3cafea57ac387a3bc837ce54a54 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 28 May 2023 21:28:20 -0700 Subject: [PATCH] Provide UserAccountInfo to MastodonController at initialization --- Tusker/API/MastodonController.swift | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/Tusker/API/MastodonController.swift b/Tusker/API/MastodonController.swift index 9c35d8ce..1790720c 100644 --- a/Tusker/API/MastodonController.swift +++ b/Tusker/API/MastodonController.swift @@ -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(_ request: Request, completion: @escaping Client.Callback) -> URLSessionTask? { return client.run(request, completion: completion)