Refactoring

This commit is contained in:
Justin Mazzocchi 2020-08-12 01:45:01 -07:00
parent d2b12e0b99
commit 7631b92afb
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C
3 changed files with 14 additions and 40 deletions

View file

@ -16,24 +16,4 @@ extension Publisher {
} }
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }
func continuingIfWeakReferenceIsStillAlive<T: AnyObject>(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
} }

View file

@ -54,15 +54,14 @@ extension IdentityService {
func verifyCredentials() -> AnyPublisher<Void, Error> { func verifyCredentials() -> AnyPublisher<Void, Error> {
networkClient.request(AccountEndpoint.verifyCredentials) networkClient.request(AccountEndpoint.verifyCredentials)
.continuingIfWeakReferenceIsStillAlive(to: self) .zip(Just(identity.id).first().setFailureType(to: Error.self))
.map { ($0, $1.identity.id) }
.flatMap(identityDatabase.updateAccount) .flatMap(identityDatabase.updateAccount)
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }
func refreshServerPreferences() -> AnyPublisher<Void, Error> { func refreshServerPreferences() -> AnyPublisher<Void, Error> {
networkClient.request(PreferencesEndpoint.preferences) 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) } .map { ($1.identity.preferences.updated(from: $0), $1.identity.id) }
.flatMap(identityDatabase.updatePreferences) .flatMap(identityDatabase.updatePreferences)
.eraseToAnyPublisher() .eraseToAnyPublisher()
@ -70,8 +69,7 @@ extension IdentityService {
func refreshInstance() -> AnyPublisher<Void, Error> { func refreshInstance() -> AnyPublisher<Void, Error> {
networkClient.request(InstanceEndpoint.instance) networkClient.request(InstanceEndpoint.instance)
.continuingIfWeakReferenceIsStillAlive(to: self) .zip(Just(identity.id).first().setFailureType(to: Error.self))
.map { ($0, $1.identity.id) }
.flatMap(identityDatabase.updateInstance) .flatMap(identityDatabase.updateInstance)
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }

View file

@ -4,7 +4,17 @@ import Foundation
import Combine import Combine
class PostingReadingPreferencesViewModel: ObservableObject { 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? @Published var alertItem: AlertItem?
let handle: String let handle: String
@ -19,11 +29,6 @@ class PostingReadingPreferencesViewModel: ObservableObject {
identityService.$identity.map(\.preferences) identityService.$identity.map(\.preferences)
.dropFirst() .dropFirst()
.removeDuplicates() .removeDuplicates()
.handleEvents(receiveOutput: { [weak self] in
if $0.useServerPostingReadingPreferences {
self?.refreshServerPreferences()
}
})
.assign(to: &$preferences) .assign(to: &$preferences)
$preferences.dropFirst() $preferences.dropFirst()
@ -33,12 +38,3 @@ class PostingReadingPreferencesViewModel: ObservableObject {
.store(in: &cancellables) .store(in: &cancellables)
} }
} }
extension PostingReadingPreferencesViewModel {
private func refreshServerPreferences() {
identityService.refreshServerPreferences()
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.sink(receiveValue: {})
.store(in: &cancellables)
}
}