wake-ios/wake/WakeApp.swift
2025-08-22 18:11:30 +08:00

102 lines
3.2 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.

import SwiftUI
import UIKit
import SwiftData
@main
struct WakeApp: App {
@StateObject private var authState = AuthState.shared
@State private var showSplash = true
// 使
let container: ModelContainer
init() {
do {
// 1.
container = try ModelContainer(for: Login.self)
} catch {
// 2.
let url = URL.applicationSupportDirectory.appending(path: "default.store")
if FileManager.default.fileExists(atPath: url.path) {
try? FileManager.default.removeItem(at: url)
}
// 3.
container = try! ModelContainer(for: Login.self)
}
//
configureNetwork()
}
var body: some Scene {
WindowGroup {
ZStack {
if showSplash {
//
SplashView()
.environmentObject(authState)
// .onAppear {
// // token
// checkTokenValidity()
// }
} else {
//
if authState.isAuthenticated {
// userInfo
UserInfo()
.environmentObject(authState)
} else {
//
// ContentView()
// .environmentObject(authState)
UserInfo()
.environmentObject(authState)
}
}
}
// .onAppear {
// //3
// DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
// withAnimation {
// showSplash = false
// }
// }
// }
}
.modelContainer(container)
}
// MARK: -
///
private func configureNetwork() {
//
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 30
configuration.timeoutIntervalForResource = 60
//
}
/// token
private func checkTokenValidity() {
guard TokenManager.shared.hasToken,
let token = KeychainHelper.getAccessToken() else {
showSplash = false
return
}
// token
if TokenManager.shared.isTokenValid(token) {
authState.isAuthenticated = true
}
// 3
// DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
// withAnimation {
// showSplash = false
// }
// }
}
}