56 lines
1.5 KiB
Swift
56 lines
1.5 KiB
Swift
//
|
|
// LoginViewModel.swift
|
|
// medmap
|
|
//
|
|
|
|
import Foundation
|
|
import SwiftUI
|
|
import Combine
|
|
@MainActor
|
|
final class LoginViewModel: ViewModel {
|
|
|
|
@Published var email = "katia.carvalho564@mail.pt"
|
|
@Published var password = "2fuckpprod"
|
|
@Published private(set) var errorMessage: String?
|
|
@Published private(set) var authenticatedPatient: LoginResponse?
|
|
|
|
private let api: MedMapAPI = DIContainer.shared.resolve()
|
|
|
|
|
|
|
|
|
|
var canSubmit: Bool {
|
|
!email.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty && !password.isEmpty && !isLoading
|
|
}
|
|
|
|
func showSignup() {
|
|
appStateManager.navigateTo(state: .signup)
|
|
}
|
|
|
|
func login() async {
|
|
guard canSubmit else { return }
|
|
|
|
errorMessage = nil
|
|
isLoading = true
|
|
defer { isLoading = false }
|
|
|
|
do {
|
|
let response = try await api.login(
|
|
email: email.trimmingCharacters(in: .whitespacesAndNewlines),
|
|
password: password
|
|
)
|
|
|
|
guard response.role == .patient else {
|
|
errorMessage = "Esta aplicacao esta disponivel apenas para doentes."
|
|
return
|
|
}
|
|
|
|
authenticatedPatient = response
|
|
appStateManager.navigateTo(state: .dashboard)
|
|
} catch {
|
|
errorMessage = "Nao foi possivel iniciar sessao. Confirme o email e a palavra-passe."
|
|
fluxlog("Patient login failed: \(error.localizedDescription)")
|
|
}
|
|
}
|
|
}
|