ios_app/Shared/ViewModels/LoginViewModel.swift
2025-06-10 05:23:59 +03:00

60 lines
1.5 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// LoginViewModel.swift
// VolnahubApp
//
// Created by cheykrym on 09/06/2025.
//
import Foundation
import Combine
class LoginViewModel: ObservableObject {
@Published var username: String = ""
@Published var password: String = ""
@Published var isLoading: Bool = true // сразу true, чтобы вызывался автологин
@Published var showError: Bool = false
@Published var errorMessage: String = ""
@Published var isLoggedIn: Bool = false
private let authService = AuthService()
init() {
autoLogin()
}
func autoLogin() {
authService.autoLogin { [weak self] success in
DispatchQueue.main.async {
self?.isLoading = false
if success {
self?.isLoggedIn = true
}
}
}
}
func login() {
isLoading = true
showError = false
authService.login(username: username, password: password) { [weak self] success, error in
DispatchQueue.main.async {
self?.isLoading = false
if success {
self?.isLoggedIn = true
} else {
self?.errorMessage = error ?? "Неизвестная ошибка"
self?.showError = true
}
}
}
}
func logout() {
username = ""
password = ""
isLoggedIn = false
}
}