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()
|
.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> {
|
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()
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue