payfrit-works-ios/PayfritWorks/ViewModels/AppState.swift
2026-02-01 23:38:34 -08:00

41 lines
1.2 KiB
Swift

import SwiftUI
@MainActor
final class AppState: ObservableObject {
@Published var userId: Int?
@Published var userName: String?
@Published var userPhotoUrl: String?
@Published var userToken: String?
@Published var businessId: Int = 0
@Published var isAuthenticated = false
var isLoggedIn: Bool { userId != nil && userToken != nil }
func setAuth(userId: Int, token: String, userName: String? = nil, photoUrl: String? = nil) {
self.userId = userId
self.userToken = token
self.userName = userName
self.userPhotoUrl = photoUrl
self.isAuthenticated = true
}
func setBusinessId(_ id: Int) {
self.businessId = id
}
func clearAuth() {
userId = nil
userToken = nil
userName = nil
userPhotoUrl = nil
isAuthenticated = false
businessId = 0
}
func loadSavedAuth() async {
let creds = await AuthStorage.shared.loadAuth()
guard let creds = creds else { return }
await APIService.shared.setAuth(token: creds.token, userId: creds.userId)
setAuth(userId: creds.userId, token: creds.token, userName: creds.userName, photoUrl: creds.photoUrl)
}
}