Only refetch information about the active user’s instance, not all of them.

contributes to iOS-319
This commit is contained in:
shannon 2024-11-19 10:05:29 -05:00
parent 54d04aed4e
commit ae7aaf091d

View File

@ -28,11 +28,13 @@ public final class InstanceService {
self.backgroundManagedObjectContext = apiService.backgroundManagedObjectContext
self.apiService = apiService
AuthenticationServiceProvider.shared.$mastodonAuthenticationBoxes
AuthenticationServiceProvider.shared.currentActiveUser
.receive(on: DispatchQueue.main)
.compactMap { $0.first?.domain }
.removeDuplicates() // prevent infinity loop
.asyncMap { [weak self] in await self?.updateInstance(domain: $0) }
.asyncMap { [weak self] in
if let domain = $0?.domain {
await self?.updateInstance(domain: domain)
}
}
.sink {}
.store(in: &disposeBag)
}
@ -44,17 +46,18 @@ extension InstanceService {
@MainActor
func updateInstance(domain: String) async {
guard let apiService else { return }
guard let authBox = AuthenticationServiceProvider.shared.currentActiveUser.value, authBox.domain == domain else { return }
let response = try? await apiService.instance(domain: domain, authenticationBox: AuthenticationServiceProvider.shared.currentActiveUser.value)
let response = try? await apiService.instance(domain: domain, authenticationBox: authBox)
.singleOutput()
if response?.value.version?.majorServerVersion(greaterThanOrEquals: 4) == true {
guard let instanceV2 = try? await apiService.instanceV2(domain: domain, authenticationBox: AuthenticationServiceProvider.shared.currentActiveUser.value).singleOutput() else {
guard let instanceV2 = try? await apiService.instanceV2(domain: domain, authenticationBox: authBox).singleOutput() else {
return
}
self.updateInstanceV2(domain: domain, response: instanceV2)
if let translationResponse = try? await apiService.translationLanguages(domain: domain, authenticationBox: AuthenticationServiceProvider.shared.currentActiveUser.value).singleOutput() {
if let translationResponse = try? await apiService.translationLanguages(domain: domain, authenticationBox: authBox).singleOutput() {
updateTranslationLanguages(domain: domain, response: translationResponse)
}
} else if let response {