From ae7aaf091d93b6c1c8af622e35cd22059065770e Mon Sep 17 00:00:00 2001 From: shannon Date: Tue, 19 Nov 2024 10:05:29 -0500 Subject: [PATCH] =?UTF-8?q?Only=20refetch=20information=20about=20the=20ac?= =?UTF-8?q?tive=20user=E2=80=99s=20instance,=20not=20all=20of=20them.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit contributes to iOS-319 --- .../MastodonCore/Service/InstanceService.swift | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/MastodonSDK/Sources/MastodonCore/Service/InstanceService.swift b/MastodonSDK/Sources/MastodonCore/Service/InstanceService.swift index ba3e910be..25539a6cc 100644 --- a/MastodonSDK/Sources/MastodonCore/Service/InstanceService.swift +++ b/MastodonSDK/Sources/MastodonCore/Service/InstanceService.swift @@ -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 {