mirror of
https://github.com/metabolist/metatext.git
synced 2024-11-25 09:41:00 +00:00
Refactoring
This commit is contained in:
parent
d2b12e0b99
commit
7631b92afb
3 changed files with 14 additions and 40 deletions
|
@ -16,24 +16,4 @@ extension Publisher {
|
|||
}
|
||||
.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
|
||||
}
|
||||
|
|
|
@ -54,15 +54,14 @@ extension IdentityService {
|
|||
|
||||
func verifyCredentials() -> AnyPublisher<Void, Error> {
|
||||
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<Void, Error> {
|
||||
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<Void, Error> {
|
||||
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()
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue