- Add AppViewModel to manage app-level state and handle 401 responses - Implement automatic logout when API returns 401 Unauthorized - Add persistent logout state using existing hasFinishedSetup flag - Move NavigationStack outside TabView to enable automatic TabBar hiding - Update API classes to send UnauthorizedAPIResponse notifications - TabBar now hides automatically when navigating to detail views
72 lines
1.8 KiB
Swift
72 lines
1.8 KiB
Swift
//
|
|
// AppViewModel.swift
|
|
// readeck
|
|
//
|
|
// Created by Ilyas Hallak on 27.08.25.
|
|
//
|
|
|
|
import Foundation
|
|
import SwiftUI
|
|
|
|
class AppViewModel: ObservableObject {
|
|
private let settingsRepository = SettingsRepository()
|
|
private let logoutUseCase: LogoutUseCase
|
|
|
|
@Published var hasFinishedSetup: Bool = true
|
|
|
|
init(logoutUseCase: LogoutUseCase = LogoutUseCase()) {
|
|
self.logoutUseCase = logoutUseCase
|
|
setupNotificationObservers()
|
|
|
|
Task {
|
|
await loadSetupStatus()
|
|
}
|
|
}
|
|
|
|
private func setupNotificationObservers() {
|
|
NotificationCenter.default.addObserver(
|
|
forName: NSNotification.Name("UnauthorizedAPIResponse"),
|
|
object: nil,
|
|
queue: .main
|
|
) { [weak self] _ in
|
|
Task {
|
|
await self?.handleUnauthorizedResponse()
|
|
}
|
|
}
|
|
|
|
NotificationCenter.default.addObserver(
|
|
forName: NSNotification.Name("SetupStatusChanged"),
|
|
object: nil,
|
|
queue: .main
|
|
) { [weak self] _ in
|
|
self?.loadSetupStatus()
|
|
}
|
|
}
|
|
|
|
@MainActor
|
|
private func handleUnauthorizedResponse() async {
|
|
print("AppViewModel: Handling 401 Unauthorized - logging out user")
|
|
|
|
do {
|
|
// Führe den Logout durch
|
|
try await logoutUseCase.execute()
|
|
|
|
// Update UI state
|
|
loadSetupStatus()
|
|
|
|
print("AppViewModel: User successfully logged out due to 401 error")
|
|
} catch {
|
|
print("AppViewModel: Error during logout: \(error)")
|
|
}
|
|
}
|
|
|
|
@MainActor
|
|
private func loadSetupStatus() {
|
|
hasFinishedSetup = settingsRepository.hasFinishedSetup
|
|
}
|
|
|
|
deinit {
|
|
NotificationCenter.default.removeObserver(self)
|
|
}
|
|
}
|