punchnet-macos/punchnet/Views/RootView.swift
2026-03-24 15:10:58 +08:00

90 lines
3.3 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.

//
// RootView.swift
// punchnet
//
// Created by on 2026/1/19.
//
import SwiftUI
struct RootView: View {
@Environment(AppContext.self) var appContext: AppContext
@State private var updateManager = AppUpdateManager.shared
var body: some View {
ZStack {
// 1.
// 使 ZStack Group
ZStack(alignment: .center) {
switch appContext.appScene {
case .login(username: let username):
LoginView(username: username)
.id("scene_login") // ID
case .logined:
NetworkView()
.id("scene_logined")
case .register:
RegisterRootView()
.id("scene_register")
case .resetPassword:
ResetPasswordRootView()
.id("scene_reset")
}
}
// 2.
// maxWidth/Height .infinity
// minWidth/minHeight
.frame(maxWidth: .infinity, maxHeight: .infinity)
// 3. RootView
.clipped()
.transition(.asymmetric(
insertion: .move(edge: .trailing).combined(with: .opacity),
removal: .move(edge: .leading).combined(with: .opacity) // leading
))
//
if updateManager.showUpdateOverlay, let info = updateManager.updateInfo {
updateOverlay(info: info)
}
}
// 4. Scene
.animation(.spring(duration: 0.5), value: appContext.appScene)
.animation(.spring(duration: 0.4), value: updateManager.showUpdateOverlay)
// macOS
.background(VisualEffectView(material: .hudWindow, blendingMode: .behindWindow))
.task {
let checkUpdateResult = await updateManager.checkUpdate(isManual: false)
NSLog("[RootView] checkUpdateResult: \(checkUpdateResult)")
}
}
// body
@ViewBuilder
private func updateOverlay(info: SDLAPIClient.AppUpgradeInfo) -> some View {
ZStack {
Color.black.opacity(0.4)
.ignoresSafeArea()
.onTapGesture {
if !info.forceUpdate {
updateManager.showUpdateOverlay = false
}
}
AppUpdateView(info: info) {
updateManager.showUpdateOverlay = false
}
.clipShape(RoundedRectangle(cornerRadius: 16))
.shadow(color: .black.opacity(0.3), radius: 20)
}
.transition(.asymmetric(
insertion: .scale(scale: 0.9).combined(with: .opacity),
removal: .opacity
))
.zIndex(100) //
}
}
#Preview {
RootView()
}