Refactoring
This commit is contained in:
parent
9a2fae9e4c
commit
34bca1c8fa
|
@ -5,6 +5,7 @@
|
||||||
"add-identity.instance-url" = "Instance URL";
|
"add-identity.instance-url" = "Instance URL";
|
||||||
"add-identity.log-in" = "Log in";
|
"add-identity.log-in" = "Log in";
|
||||||
"add-identity.browse-anonymously" = "Browse anonymously";
|
"add-identity.browse-anonymously" = "Browse anonymously";
|
||||||
|
"add-identity.unable-to-connect-to-instance" = "Unable to connect to instance";
|
||||||
"secondary-navigation.manage-accounts" = "Manage Accounts";
|
"secondary-navigation.manage-accounts" = "Manage Accounts";
|
||||||
"secondary-navigation.lists" = "Lists";
|
"secondary-navigation.lists" = "Lists";
|
||||||
"secondary-navigation.preferences" = "Preferences";
|
"secondary-navigation.preferences" = "Preferences";
|
||||||
|
|
|
@ -4,6 +4,10 @@ import Combine
|
||||||
import Foundation
|
import Foundation
|
||||||
import ServiceLayer
|
import ServiceLayer
|
||||||
|
|
||||||
|
public enum AddIdentityError: Error {
|
||||||
|
case unableToConnectToInstance
|
||||||
|
}
|
||||||
|
|
||||||
public final class AddIdentityViewModel: ObservableObject {
|
public final class AddIdentityViewModel: ObservableObject {
|
||||||
@Published public var urlFieldText = ""
|
@Published public var urlFieldText = ""
|
||||||
@Published public var alertItem: AlertItem?
|
@Published public var alertItem: AlertItem?
|
||||||
|
@ -24,24 +28,61 @@ public final class AddIdentityViewModel: ObservableObject {
|
||||||
|
|
||||||
public extension AddIdentityViewModel {
|
public extension AddIdentityViewModel {
|
||||||
func logInTapped() {
|
func logInTapped() {
|
||||||
let identityID = UUID()
|
addIdentity(authenticated: true)
|
||||||
let instanceURL: URL
|
}
|
||||||
|
|
||||||
do {
|
func browseAnonymouslyTapped() {
|
||||||
instanceURL = try checkedURL()
|
addIdentity(authenticated: false)
|
||||||
} catch {
|
}
|
||||||
alertItem = AlertItem(error: error)
|
|
||||||
|
func refreshFilter() {
|
||||||
|
instanceFilterService.updateFilter()
|
||||||
|
.sink { _ in }
|
||||||
|
.store(in: &cancellables)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private extension AddIdentityViewModel {
|
||||||
|
private static let filteredURL = URL(string: "https://filtered")!
|
||||||
|
private static let HTTPSPrefix = "https://"
|
||||||
|
|
||||||
|
func addIdentity(authenticated: Bool) {
|
||||||
|
let identityID = UUID()
|
||||||
|
let url: URL
|
||||||
|
|
||||||
|
if urlFieldText.hasPrefix(Self.HTTPSPrefix), let prefixedURL = URL(string: urlFieldText) {
|
||||||
|
url = prefixedURL
|
||||||
|
} else if let unprefixedURL = URL(string: Self.HTTPSPrefix + urlFieldText) {
|
||||||
|
url = unprefixedURL
|
||||||
|
} else {
|
||||||
|
alertItem = AlertItem(error: AddIdentityError.unableToConnectToInstance)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
allIdentitiesService.createIdentity(id: identityID, url: instanceURL, authenticated: true)
|
if instanceFilterService.isFiltered(url: url) {
|
||||||
|
loading = true
|
||||||
|
|
||||||
|
DispatchQueue.main.asyncAfter(deadline: .now() + .random(in: 0.01...0.1)) {
|
||||||
|
self.alertItem = AlertItem(error: AddIdentityError.unableToConnectToInstance)
|
||||||
|
self.loading = false
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
allIdentitiesService.createIdentity(
|
||||||
|
id: identityID,
|
||||||
|
url: url,
|
||||||
|
authenticated: authenticated)
|
||||||
.receive(on: DispatchQueue.main)
|
.receive(on: DispatchQueue.main)
|
||||||
.catch { [weak self] error -> Empty<Never, Never> in
|
.catch { [weak self] error -> Empty<Never, Never> in
|
||||||
if case AuthenticationError.canceled = error {
|
if case AuthenticationError.canceled = error {
|
||||||
// no-op
|
// no-op
|
||||||
} else {
|
} else {
|
||||||
self?.alertItem = AlertItem(error: error)
|
let displayedError = error is URLError ? AddIdentityError.unableToConnectToInstance : error
|
||||||
|
|
||||||
|
self?.alertItem = AlertItem(error: displayedError)
|
||||||
}
|
}
|
||||||
|
|
||||||
return Empty()
|
return Empty()
|
||||||
|
@ -58,56 +99,4 @@ public extension AddIdentityViewModel {
|
||||||
} receiveValue: { _ in }
|
} receiveValue: { _ in }
|
||||||
.store(in: &cancellables)
|
.store(in: &cancellables)
|
||||||
}
|
}
|
||||||
|
|
||||||
func browseAnonymouslyTapped() {
|
|
||||||
let identityID = UUID()
|
|
||||||
let instanceURL: URL
|
|
||||||
|
|
||||||
do {
|
|
||||||
instanceURL = try checkedURL()
|
|
||||||
} catch {
|
|
||||||
alertItem = AlertItem(error: error)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Ensure instance has not disabled public preview
|
|
||||||
allIdentitiesService.createIdentity(id: identityID, url: instanceURL, authenticated: false)
|
|
||||||
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
|
||||||
.sink { [weak self] in
|
|
||||||
guard let self = self, case .finished = $0 else { return }
|
|
||||||
|
|
||||||
self.addedIdentityIDSubject.send(identityID)
|
|
||||||
} receiveValue: { _ in }
|
|
||||||
.store(in: &cancellables)
|
|
||||||
}
|
|
||||||
|
|
||||||
func refreshFilter() {
|
|
||||||
instanceFilterService.updateFilter()
|
|
||||||
.sink { _ in }
|
|
||||||
.store(in: &cancellables)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private extension AddIdentityViewModel {
|
|
||||||
private static let filteredURL = URL(string: "https://filtered")!
|
|
||||||
private static let HTTPSPrefix = "https://"
|
|
||||||
|
|
||||||
func checkedURL() throws -> URL {
|
|
||||||
let url: URL
|
|
||||||
|
|
||||||
if urlFieldText.hasPrefix(Self.HTTPSPrefix), let prefixedURL = URL(string: urlFieldText) {
|
|
||||||
url = prefixedURL
|
|
||||||
} else if let unprefixedURL = URL(string: Self.HTTPSPrefix + urlFieldText) {
|
|
||||||
url = unprefixedURL
|
|
||||||
} else {
|
|
||||||
throw URLError(.badURL)
|
|
||||||
}
|
|
||||||
|
|
||||||
if instanceFilterService.isFiltered(url: url) {
|
|
||||||
return Self.filteredURL
|
|
||||||
}
|
|
||||||
|
|
||||||
return url
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,6 +41,12 @@ extension AddIdentityView {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension AddIdentityError: LocalizedError {
|
||||||
|
public var errorDescription: String? {
|
||||||
|
NSLocalizedString("add-identity.unable-to-connect-to-instance", comment: "")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
import PreviewViewModels
|
import PreviewViewModels
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue