diff --git a/Tusker/Controllers/MastodonController.swift b/Tusker/Controllers/MastodonController.swift index 899b35cd6d..3fc56033f0 100644 --- a/Tusker/Controllers/MastodonController.swift +++ b/Tusker/Controllers/MastodonController.swift @@ -33,9 +33,9 @@ class MastodonController { private(set) lazy var cache = MastodonCache(mastodonController: self) private(set) lazy var persistentContainer = MastodonCachePersistentStore(for: self) - + let instanceURL: URL - private(set) var accountInfo: LocalData.UserAccountInfo? + var accountInfo: LocalData.UserAccountInfo? let client: Client! diff --git a/Tusker/LocalData.swift b/Tusker/LocalData.swift index f1fbfbd386..821b395587 100644 --- a/Tusker/LocalData.swift +++ b/Tusker/LocalData.swift @@ -44,11 +44,10 @@ class LocalData: ObservableObject { let url = URL(string: instanceURL), let clientId = info["clientID"], let secret = info["clientSecret"], - let username = info["username"], let accessToken = info["accessToken"] else { return nil } - return UserAccountInfo(id: id, instanceURL: url, clientID: clientId, clientSecret: secret, username: username, accessToken: accessToken) + return UserAccountInfo(id: id, instanceURL: url, clientID: clientId, clientSecret: secret, username: info["username"], accessToken: accessToken) } } else { return [] @@ -56,15 +55,18 @@ class LocalData: ObservableObject { } set { objectWillChange.send() - let array = newValue.map { (info) in - return [ + let array = newValue.map { (info) -> [String: String] in + var res = [ "id": info.id, "instanceURL": info.instanceURL.absoluteString, "clientID": info.clientID, "clientSecret": info.clientSecret, - "username": info.username, "accessToken": info.accessToken ] + if let username = info.username { + res["username"] = username + } + return res } defaults.set(array, forKey: accountsKey) } @@ -85,7 +87,7 @@ class LocalData: ObservableObject { return !accounts.isEmpty } - func addAccount(instanceURL url: URL, clientID: String, clientSecret secret: String, username: String, accessToken: String) -> UserAccountInfo { + func addAccount(instanceURL url: URL, clientID: String, clientSecret secret: String, username: String?, accessToken: String) -> UserAccountInfo { var accounts = self.accounts if let index = accounts.firstIndex(where: { $0.instanceURL == url && $0.username == username }) { accounts.remove(at: index) @@ -97,6 +99,13 @@ class LocalData: ObservableObject { return info } + func setUsername(for info: UserAccountInfo, username: String) { + var info = info + info.username = username + removeAccount(info) + accounts.append(info) + } + func removeAccount(_ info: UserAccountInfo) { accounts.removeAll(where: { $0.id == info.id }) } @@ -128,7 +137,7 @@ extension LocalData { let instanceURL: URL let clientID: String let clientSecret: String - let username: String + fileprivate(set) var username: String! let accessToken: String func hash(into hasher: inout Hasher) { diff --git a/Tusker/Screens/Onboarding/OnboardingViewController.swift b/Tusker/Screens/Onboarding/OnboardingViewController.swift index 9bb2af607d..b94a9a4cde 100644 --- a/Tusker/Screens/Onboarding/OnboardingViewController.swift +++ b/Tusker/Screens/Onboarding/OnboardingViewController.swift @@ -68,10 +68,13 @@ extension OnboardingViewController: InstanceSelectorTableViewControllerDelegate let authCode = item.value else { return } mastodonController.authorize(authorizationCode: authCode) { (accessToken) in + let accountInfo = LocalData.shared.addAccount(instanceURL: instanceURL, clientID: clientID, clientSecret: clientSecret, username: nil, accessToken: accessToken) + mastodonController.accountInfo = accountInfo + mastodonController.getOwnAccount { (account) in + LocalData.shared.setUsername(for: accountInfo, username: account.username) + DispatchQueue.main.async { - let accountInfo = LocalData.shared.addAccount(instanceURL: instanceURL, clientID: clientID, clientSecret: clientSecret, username: account.username, accessToken: accessToken) - self.onboardingDelegate?.didFinishOnboarding(account: accountInfo) } }