mirror of
https://github.com/metabolist/metatext.git
synced 2024-11-22 00:01:00 +00:00
Refactoring
This commit is contained in:
parent
62ddaac083
commit
9f80ac6360
2 changed files with 20 additions and 13 deletions
|
@ -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> {
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue