forked from shadowfacts/Tusker
Use cached logged-in account for things
Fixes various race conditions with loading account Closes #251
This commit is contained in:
parent
da88303a22
commit
f0b8f92791
|
@ -52,8 +52,7 @@ class MastodonController: ObservableObject {
|
|||
let client: Client!
|
||||
let instanceFeatures = InstanceFeatures()
|
||||
|
||||
@Published private(set) var accountID: String?
|
||||
@Published private(set) var account: Account!
|
||||
@Published private(set) var account: AccountMO?
|
||||
@Published private(set) var instance: Instance?
|
||||
@Published private(set) var instanceInfo: InstanceInfo!
|
||||
@Published private(set) var nodeInfo: NodeInfo!
|
||||
|
@ -85,7 +84,7 @@ class MastodonController: ObservableObject {
|
|||
self.client.accessToken = accountInfo?.accessToken
|
||||
|
||||
if !transient {
|
||||
fetchActiveAccountID()
|
||||
fetchActiveAccount()
|
||||
fetchActiveInstance()
|
||||
}
|
||||
|
||||
|
@ -234,8 +233,8 @@ class MastodonController: ObservableObject {
|
|||
}
|
||||
}
|
||||
|
||||
func getOwnAccount(completion: ((Result<Account, Client.Error>) -> Void)? = nil) {
|
||||
if account != nil {
|
||||
func getOwnAccount(completion: ((Result<AccountMO, Client.Error>) -> Void)? = nil) {
|
||||
if let account {
|
||||
completion?(.success(account))
|
||||
} else {
|
||||
let request = Client.getSelfAccount()
|
||||
|
@ -245,26 +244,25 @@ class MastodonController: ObservableObject {
|
|||
completion?(.failure(error))
|
||||
|
||||
case let .success(account, _):
|
||||
DispatchQueue.main.async {
|
||||
self.account = account
|
||||
}
|
||||
let context = self.persistentContainer.backgroundContext
|
||||
let context = self.persistentContainer.viewContext
|
||||
context.perform {
|
||||
if let accountMO = self.persistentContainer.account(for: account.id, in: context) {
|
||||
accountMO.updateFrom(apiAccount: account, container: self.persistentContainer)
|
||||
accountMO.active = true
|
||||
let accountMO: AccountMO
|
||||
if let existing = self.persistentContainer.account(for: account.id, in: context) {
|
||||
accountMO = existing
|
||||
existing.updateFrom(apiAccount: account, container: self.persistentContainer)
|
||||
} else {
|
||||
let account = self.persistentContainer.addOrUpdateSynchronously(account: account, in: context)
|
||||
account.active = true
|
||||
accountMO = self.persistentContainer.addOrUpdateSynchronously(account: account, in: context)
|
||||
}
|
||||
completion?(.success(account))
|
||||
accountMO.active = true
|
||||
self.account = accountMO
|
||||
completion?(.success(accountMO))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getOwnAccount() async throws -> Account {
|
||||
func getOwnAccount() async throws -> AccountMO {
|
||||
if let account = account {
|
||||
return account
|
||||
} else {
|
||||
|
@ -375,11 +373,11 @@ class MastodonController: ObservableObject {
|
|||
}
|
||||
|
||||
@MainActor
|
||||
private func fetchActiveAccountID() {
|
||||
private func fetchActiveAccount() {
|
||||
let req = AccountMO.fetchRequest()
|
||||
req.predicate = NSPredicate(format: "active = YES")
|
||||
if let activeAccount = try? persistentContainer.viewContext.fetch(req).first {
|
||||
accountID = activeAccount.id
|
||||
account = activeAccount
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -150,7 +150,7 @@ class OnboardingViewController: UINavigationController {
|
|||
mastodonController.accountInfo = tempAccountInfo
|
||||
|
||||
updateStatus("Checking Credentials")
|
||||
let ownAccount: Account
|
||||
let ownAccount: AccountMO
|
||||
do {
|
||||
ownAccount = try await retrying("Getting own account") {
|
||||
try await mastodonController.getOwnAccount()
|
||||
|
|
Loading…
Reference in New Issue