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-07 23:19:13 +00:00
|
|
|
let addedIdentityID: AnyPublisher<UUID, Never>
|
2020-07-29 23:50:30 +00:00
|
|
|
|
2020-08-09 05:37:04 +00:00
|
|
|
private let authenticationService: AuthenticationService
|
2020-08-07 23:19:13 +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-09 05:37:04 +00:00
|
|
|
init(authenticationService: AuthenticationService) {
|
|
|
|
self.authenticationService = authenticationService
|
2020-08-03 15:20:51 +00:00
|
|
|
addedIdentityID = addedIdentityIDInput.eraseToAnyPublisher()
|
2020-07-29 23:50:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func goTapped() {
|
2020-08-09 05:37:04 +00:00
|
|
|
Just(urlFieldText)
|
|
|
|
.tryMap { try $0.url() }
|
|
|
|
.flatMap(authenticationService.authenticate(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-03 15:20:51 +00:00
|
|
|
.sink(receiveValue: addedIdentityIDInput.send)
|
|
|
|
.store(in: &cancellables)
|
2020-07-29 23:50:30 +00:00
|
|
|
}
|
|
|
|
}
|