Refactoring

This commit is contained in:
Justin Mazzocchi 2020-09-12 18:12:33 -07:00
parent 62ddaac083
commit 9f80ac6360
No known key found for this signature in database
GPG Key ID: E223E6937AAFB01C
2 changed files with 20 additions and 13 deletions

View File

@ -18,8 +18,10 @@ public struct InstanceURLService {
}
public extension InstanceURLService {
func url(text: String) -> URL? {
guard text.count >= Self.shortestPossibleURLLength else { return nil }
func url(text: String) -> Result<URL, Error> {
guard text.count >= Self.shortestPossibleURLLength else {
return .failure(URLError(.badURL))
}
let url: URL
@ -28,14 +30,14 @@ public extension InstanceURLService {
} else if let unprefixedURL = URL(string: Self.httpsPrefix + text) {
url = unprefixedURL
} else {
return nil
return .failure(URLError(.badURL))
}
if isFiltered(url: url) {
return nil
return .failure(URLError(.badURL))
}
return url
return .success(url)
}
func instance(url: URL) -> AnyPublisher<Instance, Error> {

View File

@ -53,7 +53,16 @@ private extension AddIdentityViewModel {
let url = $urlFieldText
.debounce(for: 0.5, scheduler: DispatchQueue.global())
.removeDuplicates()
.map(instanceURLService.url(text:))
.flatMap { [weak self] text -> AnyPublisher<URL?, Never> 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)
@ -85,13 +94,9 @@ private extension AddIdentityViewModel {
}
func addIdentity(authenticated: Bool) {
guard let url = instanceURLService.url(text: urlFieldText) else {
alertItem = AlertItem(error: AddIdentityError.unableToConnectToInstance)
return
}
allIdentitiesService.createIdentity(url: url, authenticated: authenticated)
instanceURLService.url(text: urlFieldText).publisher
.map { ($0, authenticated) }
.flatMap(allIdentitiesService.createIdentity(url:authenticated:))
.receive(on: DispatchQueue.main)
.handleEvents(receiveSubscription: { [weak self] _ in self?.loading = true })
.sink { [weak self] in