Refactoring

This commit is contained in:
Justin Mazzocchi 2020-09-12 12:58:23 -07:00
parent d061d17cb7
commit 5927d5f27a
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C
2 changed files with 15 additions and 12 deletions

View file

@ -47,22 +47,19 @@ public extension InstanceURLService {
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }
func isPublicTimelineAvailable(url: URL) -> AnyPublisher<Bool, Never> { func isPublicTimelineAvailable(url: URL) -> AnyPublisher<Bool, Error> {
httpClient.request( httpClient.request(
MastodonAPITarget( MastodonAPITarget(
baseURL: url, baseURL: url,
endpoint: TimelinesEndpoint.public(local: true), endpoint: TimelinesEndpoint.public(local: true),
accessToken: nil)) accessToken: nil))
.map { _ in true } .map { _ in true }
.catch { _ in Just(false) }
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }
func updateFilter() -> AnyPublisher<Never, Never> { func updateFilter() -> AnyPublisher<Never, Error> {
httpClient.request(UpdatedFilterTarget()) httpClient.request(UpdatedFilterTarget())
.handleEvents(receiveOutput: { userDefaultsClient.updatedInstanceFilter = $0 }) .handleEvents(receiveOutput: { userDefaultsClient.updatedInstanceFilter = $0 })
.map { _ in () }
.replaceError(with: ())
.ignoreOutput() .ignoreOutput()
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }

View file

@ -42,7 +42,7 @@ public extension AddIdentityViewModel {
func refreshFilter() { func refreshFilter() {
instanceURLService.updateFilter() instanceURLService.updateFilter()
.sink { _ in } .sink { _ in } receiveValue: { _ in }
.store(in: &cancellables) .store(in: &cancellables)
} }
@ -61,12 +61,6 @@ private extension AddIdentityViewModel {
url.receive(on: DispatchQueue.main).assign(to: &$url) url.receive(on: DispatchQueue.main).assign(to: &$url)
url.compactMap { $0 }
.flatMap(instanceURLService.isPublicTimelineAvailable(url:))
.replaceError(with: false)
.receive(on: DispatchQueue.main)
.assign(to: &$isPublicTimelineAvailable)
url.flatMap { [weak self] url -> AnyPublisher<Instance?, Never> in url.flatMap { [weak self] url -> AnyPublisher<Instance?, Never> in
guard let self = self, let url = url else { guard let self = self, let url = url else {
return Just(nil).eraseToAnyPublisher() return Just(nil).eraseToAnyPublisher()
@ -79,6 +73,18 @@ private extension AddIdentityViewModel {
} }
.receive(on: DispatchQueue.main) .receive(on: DispatchQueue.main)
.assign(to: &$instance) .assign(to: &$instance)
url.flatMap { [weak self] url -> AnyPublisher<Bool, Never> in
guard let self = self, let url = url else {
return Just(false).eraseToAnyPublisher()
}
return self.instanceURLService.isPublicTimelineAvailable(url: url)
.replaceError(with: false)
.eraseToAnyPublisher()
}
.receive(on: DispatchQueue.main)
.assign(to: &$isPublicTimelineAvailable)
} }
func addIdentity(authenticated: Bool) { func addIdentity(authenticated: Bool) {