From 7631b92afbc357b39943f847824f6930f106ba5a Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Wed, 12 Aug 2020 01:45:01 -0700 Subject: [PATCH] Refactoring --- Shared/Extensions/Publisher+Extensions.swift | 20 -------------- Shared/Services/IdentityService.swift | 8 +++--- .../PostingReadingPreferencesViewModel.swift | 26 ++++++++----------- 3 files changed, 14 insertions(+), 40 deletions(-) diff --git a/Shared/Extensions/Publisher+Extensions.swift b/Shared/Extensions/Publisher+Extensions.swift index bd983f0..7a35c60 100644 --- a/Shared/Extensions/Publisher+Extensions.swift +++ b/Shared/Extensions/Publisher+Extensions.swift @@ -16,24 +16,4 @@ extension Publisher { } .eraseToAnyPublisher() } - - func continuingIfWeakReferenceIsStillAlive(to object: T) -> AnyPublisher<(Output, T), Error> { - tryMap { [weak object] in - guard let object = object else { throw WeakReferenceError.deallocated } - - return ($0, object) - } - .tryCatch { error -> Empty<(Output, T), Never> in - if case WeakReferenceError.deallocated = error { - return Empty() - } - - throw error - } - .eraseToAnyPublisher() - } -} - -private enum WeakReferenceError: Error { - case deallocated } diff --git a/Shared/Services/IdentityService.swift b/Shared/Services/IdentityService.swift index d9ddde9..ff4fd2f 100644 --- a/Shared/Services/IdentityService.swift +++ b/Shared/Services/IdentityService.swift @@ -54,15 +54,14 @@ extension IdentityService { func verifyCredentials() -> AnyPublisher { networkClient.request(AccountEndpoint.verifyCredentials) - .continuingIfWeakReferenceIsStillAlive(to: self) - .map { ($0, $1.identity.id) } + .zip(Just(identity.id).first().setFailureType(to: Error.self)) .flatMap(identityDatabase.updateAccount) .eraseToAnyPublisher() } func refreshServerPreferences() -> AnyPublisher { networkClient.request(PreferencesEndpoint.preferences) - .continuingIfWeakReferenceIsStillAlive(to: self) + .zip(Just(self).first().setFailureType(to: Error.self)) .map { ($1.identity.preferences.updated(from: $0), $1.identity.id) } .flatMap(identityDatabase.updatePreferences) .eraseToAnyPublisher() @@ -70,8 +69,7 @@ extension IdentityService { func refreshInstance() -> AnyPublisher { networkClient.request(InstanceEndpoint.instance) - .continuingIfWeakReferenceIsStillAlive(to: self) - .map { ($0, $1.identity.id) } + .zip(Just(identity.id).first().setFailureType(to: Error.self)) .flatMap(identityDatabase.updateInstance) .eraseToAnyPublisher() } diff --git a/Shared/View Models/PostingReadingPreferencesViewModel.swift b/Shared/View Models/PostingReadingPreferencesViewModel.swift index 4e4c212..5ba0f61 100644 --- a/Shared/View Models/PostingReadingPreferencesViewModel.swift +++ b/Shared/View Models/PostingReadingPreferencesViewModel.swift @@ -4,7 +4,17 @@ import Foundation import Combine class PostingReadingPreferencesViewModel: ObservableObject { - @Published var preferences: Identity.Preferences + @Published var preferences: Identity.Preferences { + didSet { + if preferences.useServerPostingReadingPreferences { + identityService.refreshServerPreferences() + .assignErrorsToAlertItem(to: \.alertItem, on: self) + .sink(receiveValue: {}) + .store(in: &cancellables) + } + } + } + @Published var alertItem: AlertItem? let handle: String @@ -19,11 +29,6 @@ class PostingReadingPreferencesViewModel: ObservableObject { identityService.$identity.map(\.preferences) .dropFirst() .removeDuplicates() - .handleEvents(receiveOutput: { [weak self] in - if $0.useServerPostingReadingPreferences { - self?.refreshServerPreferences() - } - }) .assign(to: &$preferences) $preferences.dropFirst() @@ -33,12 +38,3 @@ class PostingReadingPreferencesViewModel: ObservableObject { .store(in: &cancellables) } } - -extension PostingReadingPreferencesViewModel { - private func refreshServerPreferences() { - identityService.refreshServerPreferences() - .assignErrorsToAlertItem(to: \.alertItem, on: self) - .sink(receiveValue: {}) - .store(in: &cancellables) - } -}