Refactoring

This commit is contained in:
Justin Mazzocchi 2020-09-12 12:09:54 -07:00
parent b0b713bd53
commit d061d17cb7
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C
3 changed files with 35 additions and 32 deletions

View file

@ -38,14 +38,12 @@ public extension InstanceURLService {
return url return url
} }
func instance(url: URL) -> AnyPublisher<Instance?, Never> { func instance(url: URL) -> AnyPublisher<Instance, Error> {
httpClient.request( httpClient.request(
MastodonAPITarget( MastodonAPITarget(
baseURL: url, baseURL: url,
endpoint: InstanceEndpoint.instance, endpoint: InstanceEndpoint.instance,
accessToken: nil)) accessToken: nil))
.map { $0 as Instance? }
.catch { _ in Just(nil) }
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }

View file

@ -13,7 +13,8 @@ public final class AddIdentityViewModel: ObservableObject {
@Published public var urlFieldText = "" @Published public var urlFieldText = ""
@Published public var alertItem: AlertItem? @Published public var alertItem: AlertItem?
@Published public private(set) var loading = false @Published public private(set) var loading = false
@Published public private(set) var instanceAndURL: (Instance, URL)? @Published public private(set) var url: URL?
@Published public private(set) var instance: Instance?
@Published public private(set) var isPublicTimelineAvailable = false @Published public private(set) var isPublicTimelineAvailable = false
public let addedIdentityID: AnyPublisher<UUID, Never> public let addedIdentityID: AnyPublisher<UUID, Never>
@ -26,32 +27,7 @@ public final class AddIdentityViewModel: ObservableObject {
self.allIdentitiesService = allIdentitiesService self.allIdentitiesService = allIdentitiesService
self.instanceURLService = instanceURLService self.instanceURLService = instanceURLService
addedIdentityID = addedIdentityIDSubject.eraseToAnyPublisher() addedIdentityID = addedIdentityIDSubject.eraseToAnyPublisher()
setupURLObservation()
let url = $urlFieldText
.debounce(for: 0.5, scheduler: DispatchQueue.global())
.removeDuplicates()
.map(instanceURLService.url(text:))
.share()
url.compactMap { $0 }
.flatMap(instanceURLService.instance(url:))
.combineLatest(url)
.map {
if let instance = $0, let url = $1 {
return (instance, url)
}
return nil
}
.receive(on: DispatchQueue.main)
.assign(to: &$instanceAndURL)
url.compactMap { $0 }
.flatMap(instanceURLService.isPublicTimelineAvailable(url:))
.combineLatest(url.map { $0 != nil })
.map { $0 && $1 }
.receive(on: DispatchQueue.main)
.assign(to: &$isPublicTimelineAvailable)
} }
} }
@ -76,6 +52,35 @@ public extension AddIdentityViewModel {
} }
private extension AddIdentityViewModel { private extension AddIdentityViewModel {
func setupURLObservation() {
let url = $urlFieldText
.debounce(for: 0.5, scheduler: DispatchQueue.global())
.removeDuplicates()
.map(instanceURLService.url(text:))
.share()
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
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)
}
func addIdentity(authenticated: Bool) { func addIdentity(authenticated: Bool) {
let identityID = UUID() let identityID = UUID()

View file

@ -19,7 +19,7 @@ struct AddIdentityView: View {
.autocapitalization(.none) .autocapitalization(.none)
.disableAutocorrection(true) .disableAutocorrection(true)
.keyboardType(.URL) .keyboardType(.URL)
if let (instance, _) = viewModel.instanceAndURL { if let instance = viewModel.instance {
VStack(alignment: .center) { VStack(alignment: .center) {
KFImage(instance.thumbnail) KFImage(instance.thumbnail)
.placeholder { .placeholder {
@ -45,7 +45,7 @@ struct AddIdentityView: View {
} else { } else {
Button("add-identity.log-in", Button("add-identity.log-in",
action: viewModel.logInTapped) action: viewModel.logInTapped)
if let (instance, url) = viewModel.instanceAndURL, if let instance = viewModel.instance, let url = viewModel.url,
instance.registrations { instance.registrations {
ZStack { ZStack {
NavigationLink( NavigationLink(