Skip to content
Snippets Groups Projects
SFTPViewModel.swift 1.95 KiB
Newer Older
import HUD
import Combine
import Foundation
import DependencyInjection

Bruno Muniz's avatar
Bruno Muniz committed
struct SFTPViewState {
    var host: String = ""
    var username: String = ""
    var password: String = ""
    var isButtonEnabled: Bool = false
}

Bruno Muniz's avatar
Bruno Muniz committed
final class SFTPViewModel {
    @Dependency private var service: SFTPService

    var hudPublisher: AnyPublisher<HUDStatus, Never> {
        hudSubject.eraseToAnyPublisher()
    }

Bruno Muniz's avatar
Bruno Muniz committed
    var statePublisher: AnyPublisher<SFTPViewState, Never> {
        stateSubject.eraseToAnyPublisher()
Bruno Muniz's avatar
Bruno Muniz committed
    var authPublisher: AnyPublisher<Void, Never> {
        authSubject.eraseToAnyPublisher()
Bruno Muniz's avatar
Bruno Muniz committed
    private let authSubject = PassthroughSubject<Void, Never>()
    private let hudSubject = CurrentValueSubject<HUDStatus, Never>(.none)
Bruno Muniz's avatar
Bruno Muniz committed
    private let stateSubject = CurrentValueSubject<SFTPViewState, Never>(.init())

    func didEnterHost(_ string: String) {
        stateSubject.value.host = string
        validate()
    }

    func didEnterUsername(_ string: String) {
        stateSubject.value.username = string
        validate()
    }

    func didEnterPassword(_ string: String) {
        stateSubject.value.password = string
        validate()
    }

    func didTapLogin() {
        hudSubject.send(.on(nil))

        let host = stateSubject.value.host
        let username = stateSubject.value.username
        let password = stateSubject.value.password

Bruno Muniz's avatar
Bruno Muniz committed
        DispatchQueue.global().async { [weak self] in
            guard let self = self else { return }
            do {
                try self.service.authenticate(host, username, password)
                self.hudSubject.send(.none)
                self.authSubject.send(())
            } catch {
                self.hudSubject.send(.error(.init(with: error)))
            }
    }

    private func validate() {
        stateSubject.value.isButtonEnabled =
        !stateSubject.value.host.isEmpty &&
        !stateSubject.value.username.isEmpty &&
        !stateSubject.value.password.isEmpty
    }
}