2020-07-29 23:50:30 +00:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Combine
|
|
|
|
|
|
|
|
class AddIdentityViewModel: ObservableObject {
|
|
|
|
@Published var urlFieldText = ""
|
|
|
|
@Published var alertItem: AlertItem?
|
|
|
|
@Published private(set) var loading = false
|
2020-08-14 01:24:53 +00:00
|
|
|
let addedIdentityID: AnyPublisher<UUID, Never>
|
2020-07-29 23:50:30 +00:00
|
|
|
|
2020-08-26 21:35:06 +00:00
|
|
|
private let allIdentitiesService: AllIdentitiesService
|
2020-08-14 01:24:53 +00:00
|
|
|
private let addedIdentityIDInput = PassthroughSubject<UUID, Never>()
|
2020-08-03 15:20:51 +00:00
|
|
|
private var cancellables = Set<AnyCancellable>()
|
2020-07-29 23:50:30 +00:00
|
|
|
|
2020-08-26 21:35:06 +00:00
|
|
|
init(allIdentitiesService: AllIdentitiesService) {
|
|
|
|
self.allIdentitiesService = allIdentitiesService
|
2020-08-14 01:24:53 +00:00
|
|
|
addedIdentityID = addedIdentityIDInput.eraseToAnyPublisher()
|
2020-07-29 23:50:30 +00:00
|
|
|
}
|
|
|
|
|
2020-08-09 11:27:38 +00:00
|
|
|
func logInTapped() {
|
|
|
|
let identityID = UUID()
|
|
|
|
let instanceURL: URL
|
|
|
|
|
|
|
|
do {
|
|
|
|
try instanceURL = urlFieldText.url()
|
|
|
|
} catch {
|
|
|
|
alertItem = AlertItem(error: error)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-26 21:35:06 +00:00
|
|
|
allIdentitiesService.authorizeIdentity(id: identityID, instanceURL: instanceURL)
|
2020-08-26 09:19:38 +00:00
|
|
|
.collect()
|
|
|
|
.map { _ in (identityID, instanceURL) }
|
2020-08-26 21:35:06 +00:00
|
|
|
.flatMap(allIdentitiesService.createIdentity(id:instanceURL:))
|
2020-07-29 23:50:30 +00:00
|
|
|
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
2020-08-02 07:02:03 +00:00
|
|
|
.receive(on: RunLoop.main)
|
2020-07-29 23:50:30 +00:00
|
|
|
.handleEvents(
|
|
|
|
receiveSubscription: { [weak self] _ in self?.loading = true },
|
|
|
|
receiveCompletion: { [weak self] _ in self?.loading = false })
|
2020-08-26 09:19:38 +00:00
|
|
|
.sink { [weak self] in
|
|
|
|
guard let self = self, case .finished = $0 else { return }
|
|
|
|
|
|
|
|
self.addedIdentityIDInput.send(identityID)
|
|
|
|
} receiveValue: { _ in }
|
2020-08-03 15:20:51 +00:00
|
|
|
.store(in: &cancellables)
|
2020-07-29 23:50:30 +00:00
|
|
|
}
|
2020-08-09 11:27:38 +00:00
|
|
|
|
|
|
|
func browseAnonymouslyTapped() {
|
|
|
|
let identityID = UUID()
|
|
|
|
let instanceURL: URL
|
|
|
|
|
|
|
|
do {
|
|
|
|
try instanceURL = urlFieldText.url()
|
|
|
|
} catch {
|
|
|
|
alertItem = AlertItem(error: error)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-09 20:40:14 +00:00
|
|
|
// TODO: Ensure instance has not disabled public preview
|
2020-08-26 21:35:06 +00:00
|
|
|
allIdentitiesService.createIdentity(id: identityID, instanceURL: instanceURL)
|
2020-08-09 11:27:38 +00:00
|
|
|
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
2020-08-26 09:19:38 +00:00
|
|
|
.sink { [weak self] in
|
|
|
|
guard let self = self, case .finished = $0 else { return }
|
|
|
|
|
|
|
|
self.addedIdentityIDInput.send(identityID)
|
|
|
|
} receiveValue: { _ in }
|
2020-08-09 11:27:38 +00:00
|
|
|
.store(in: &cancellables)
|
|
|
|
}
|
2020-07-29 23:50:30 +00:00
|
|
|
}
|