From 1e877172b0263b205afba772d1c4428c20f70867 Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Sun, 13 Sep 2020 18:36:03 -0700 Subject: [PATCH] Refactoring --- .../ViewModels/AddIdentityViewModel.swift | 82 +++++++++---------- 1 file changed, 37 insertions(+), 45 deletions(-) diff --git a/ViewModels/Sources/ViewModels/AddIdentityViewModel.swift b/ViewModels/Sources/ViewModels/AddIdentityViewModel.swift index 8fed8a2..67fcab9 100644 --- a/ViewModels/Sources/ViewModels/AddIdentityViewModel.swift +++ b/ViewModels/Sources/ViewModels/AddIdentityViewModel.swift @@ -24,7 +24,43 @@ public final class AddIdentityViewModel: ObservableObject { init(allIdentitiesService: AllIdentitiesService, instanceURLService: InstanceURLService) { self.allIdentitiesService = allIdentitiesService self.instanceURLService = instanceURLService - setupURLObservation() + + let url = $urlFieldText + .debounce(for: 0.5, scheduler: DispatchQueue.global()) + .removeDuplicates() + .flatMap { + instanceURLService.url(text: $0).publisher + .map { $0 as URL? } + .replaceError(with: nil) + } + .share() + + url.receive(on: DispatchQueue.main).assign(to: &$url) + + url.flatMap { url -> AnyPublisher in + guard let url = url else { + return Just(nil).eraseToAnyPublisher() + } + + return instanceURLService.instance(url: url) + .map { $0 as Instance? } + .replaceError(with: nil) + .eraseToAnyPublisher() + } + .receive(on: DispatchQueue.main) + .assign(to: &$instance) + + url.flatMap { url -> AnyPublisher in + guard let url = url else { + return Just(false).eraseToAnyPublisher() + } + + return instanceURLService.isPublicTimelineAvailable(url: url) + .replaceError(with: false) + .eraseToAnyPublisher() + } + .receive(on: DispatchQueue.main) + .assign(to: &$isPublicTimelineAvailable) } } @@ -49,50 +85,6 @@ public extension AddIdentityViewModel { } private extension AddIdentityViewModel { - func setupURLObservation() { - let url = $urlFieldText - .debounce(for: 0.5, scheduler: DispatchQueue.global()) - .removeDuplicates() - .flatMap { [weak self] text -> AnyPublisher in - guard let self = self else { - return Just(nil).eraseToAnyPublisher() - } - - return self.instanceURLService.url(text: text).publisher - .map { $0 as URL? } - .replaceError(with: nil) - .eraseToAnyPublisher() - } - .share() - - url.receive(on: DispatchQueue.main).assign(to: &$url) - - url.flatMap { [weak self] url -> AnyPublisher in - guard let self = self, let url = url else { - return Just(nil).eraseToAnyPublisher() - } - - return self.instanceURLService.instance(url: url) - .map { $0 as Instance? } - .replaceError(with: nil) - .eraseToAnyPublisher() - } - .receive(on: DispatchQueue.main) - .assign(to: &$instance) - - url.flatMap { [weak self] url -> AnyPublisher 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(kind: AllIdentitiesService.IdentityCreation) { instanceURLService.url(text: urlFieldText).publisher .map { ($0, kind) }