Refactoring
This commit is contained in:
parent
62ddaac083
commit
9f80ac6360
|
@ -18,8 +18,10 @@ public struct InstanceURLService {
|
||||||
}
|
}
|
||||||
|
|
||||||
public extension InstanceURLService {
|
public extension InstanceURLService {
|
||||||
func url(text: String) -> URL? {
|
func url(text: String) -> Result<URL, Error> {
|
||||||
guard text.count >= Self.shortestPossibleURLLength else { return nil }
|
guard text.count >= Self.shortestPossibleURLLength else {
|
||||||
|
return .failure(URLError(.badURL))
|
||||||
|
}
|
||||||
|
|
||||||
let url: URL
|
let url: URL
|
||||||
|
|
||||||
|
@ -28,14 +30,14 @@ public extension InstanceURLService {
|
||||||
} else if let unprefixedURL = URL(string: Self.httpsPrefix + text) {
|
} else if let unprefixedURL = URL(string: Self.httpsPrefix + text) {
|
||||||
url = unprefixedURL
|
url = unprefixedURL
|
||||||
} else {
|
} else {
|
||||||
return nil
|
return .failure(URLError(.badURL))
|
||||||
}
|
}
|
||||||
|
|
||||||
if isFiltered(url: url) {
|
if isFiltered(url: url) {
|
||||||
return nil
|
return .failure(URLError(.badURL))
|
||||||
}
|
}
|
||||||
|
|
||||||
return url
|
return .success(url)
|
||||||
}
|
}
|
||||||
|
|
||||||
func instance(url: URL) -> AnyPublisher<Instance, Error> {
|
func instance(url: URL) -> AnyPublisher<Instance, Error> {
|
||||||
|
|
|
@ -53,7 +53,16 @@ private extension AddIdentityViewModel {
|
||||||
let url = $urlFieldText
|
let url = $urlFieldText
|
||||||
.debounce(for: 0.5, scheduler: DispatchQueue.global())
|
.debounce(for: 0.5, scheduler: DispatchQueue.global())
|
||||||
.removeDuplicates()
|
.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()
|
.share()
|
||||||
|
|
||||||
url.receive(on: DispatchQueue.main).assign(to: &$url)
|
url.receive(on: DispatchQueue.main).assign(to: &$url)
|
||||||
|
@ -85,13 +94,9 @@ private extension AddIdentityViewModel {
|
||||||
}
|
}
|
||||||
|
|
||||||
func addIdentity(authenticated: Bool) {
|
func addIdentity(authenticated: Bool) {
|
||||||
guard let url = instanceURLService.url(text: urlFieldText) else {
|
instanceURLService.url(text: urlFieldText).publisher
|
||||||
alertItem = AlertItem(error: AddIdentityError.unableToConnectToInstance)
|
.map { ($0, authenticated) }
|
||||||
|
.flatMap(allIdentitiesService.createIdentity(url:authenticated:))
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
allIdentitiesService.createIdentity(url: url, authenticated: authenticated)
|
|
||||||
.receive(on: DispatchQueue.main)
|
.receive(on: DispatchQueue.main)
|
||||||
.handleEvents(receiveSubscription: { [weak self] _ in self?.loading = true })
|
.handleEvents(receiveSubscription: { [weak self] _ in self?.loading = true })
|
||||||
.sink { [weak self] in
|
.sink { [weak self] in
|
||||||
|
|
Loading…
Reference in New Issue