wake-ios/wake/ContentView.swift
2025-08-21 19:40:15 +08:00

228 lines
10 KiB
Swift

import SwiftUI
import SwiftData
// MARK: -
extension AnyTransition {
///
static var slideFromLeading: AnyTransition {
.asymmetric(
insertion: .move(edge: .trailing).combined(with: .opacity), //
removal: .move(edge: .leading).combined(with: .opacity) //
)
}
}
// MARK: -
struct ContentView: View {
// MARK: -
@State private var showModal = false //
@State private var showSettings = false //
@State private var contentOffset: CGFloat = 0 //
@State private var showLogin = false
//
@Environment(\.modelContext) private var modelContext
// -
@Query private var login: [Login]
// MARK: -
var body: some View {
NavigationView {
ZStack {
//
VStack {
VStack(spacing: 20) {
//
Spacer().frame(height: UIApplication.shared.windows.first?.safeAreaInsets.top ?? 0)
//
HStack {
//
Button(action: showUserProfile) {
Image(systemName: "gearshape")
.font(.title2)
.padding()
}
Spacer()
Text("Wake")
.font(.largeTitle)
.fontWeight(.bold)
.onTapGesture {
if login.isEmpty {
print("⚠️ 没有登录记录,正在创建新记录...")
let newLogin = Login(
email: "jyq@example.com",
name: "New User"
)
modelContext.insert(newLogin)
try? modelContext.save()
print("✅ 已创建新登录记录")
} else if let firstLogin = login.first {
// 2.
print("🔍 找到现有记录,正在更新...")
firstLogin.email = "updated@example.com"
firstLogin.name = "Updated Name"
try? modelContext.save()
print("✅ 记录已更新")
}
}
//
Button(action: {
showLogin = true
}) {
Text("登录")
.font(.headline)
.padding(.horizontal, 16)
.padding(.vertical, 8)
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(8)
}
.padding(.trailing)
.fullScreenCover(isPresented: $showLogin) {
LoginView()
}
}
Spacer()
//
List {
Section(header: Text("我的收藏")) {
ForEach(1...5, id: \.self) { item in
HStack {
Image(systemName: "photo")
.foregroundColor(.blue)
.frame(width: 40, height: 40)
.background(Color.blue.opacity(0.1))
.cornerRadius(8)
VStack(alignment: .leading, spacing: 4) {
Text("项目 \(item)")
.font(.headline)
Text("这是第\(item)个项目的描述")
.font(.subheadline)
.foregroundColor(.secondary)
}
Spacer()
Image(systemName: "chevron.right")
.foregroundColor(.gray)
}
.padding(.vertical, 4)
}
}
Section(header: Text("最近活动")) {
ForEach(6...10, id: \.self) { item in
HStack {
Image(systemName: "clock")
.foregroundColor(.orange)
.frame(width: 40, height: 40)
.background(Color.orange.opacity(0.1))
.cornerRadius(8)
VStack(alignment: .leading, spacing: 4) {
Text("活动 \(item)")
.font(.headline)
Text("\(item)分钟前更新")
.font(.subheadline)
.foregroundColor(.secondary)
}
Spacer()
Text("查看")
.font(.caption)
.padding(6)
.background(Color.blue.opacity(0.1))
.foregroundColor(.blue)
.cornerRadius(4)
}
.padding(.vertical, 4)
}
}
}
.listStyle(GroupedListStyle())
.padding(.top, 0)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color(.systemBackground))
.offset(x: showModal ? UIScreen.main.bounds.width * 0.8 : 0)
.animation(.spring(response: 0.6, dampingFraction: 0.8), value: showModal)
.edgesIgnoringSafeArea(.all)
}
//
SlideInModal(
isPresented: $showModal,
onDismiss: hideUserProfile
) {
UserProfileModal(
showModal: $showModal,
showSettings: $showSettings
)
}
.offset(x: showSettings ? UIScreen.main.bounds.width : 0)
.animation(.spring(response: 0.6, dampingFraction: 0.8), value: showSettings)
//
ZStack {
if showSettings {
Color.black.opacity(0.3)
.edgesIgnoringSafeArea(.all)
.onTapGesture(perform: hideSettings)
.transition(.opacity)
}
if showSettings {
SettingsView(isPresented: $showSettings)
.transition(.move(edge: .leading))
.zIndex(1)
}
}
.animation(.spring(response: 0.6, dampingFraction: 0.8), value: showSettings)
}
.navigationBarHidden(true)
}
}
///
private func showUserProfile() {
withAnimation(.spring(response: 0.6, dampingFraction: 0.8)) {
// print(": \(login.count)")
// for (index, item) in login.enumerated() {
// print(" \(index + 1): =\(item.email), =\(item.name)")
// }
print("当前登录记录:")
for (index, item) in login.enumerated() {
print("记录 \(index + 1): 邮箱=\(item.email), 姓名=\(item.name)")
}
// showModal.toggle()
}
}
///
private func hideUserProfile() {
withAnimation(.spring(response: 0.6, dampingFraction: 0.8)) {
showModal = false
}
}
///
private func hideSettings() {
withAnimation(.spring(response: 0.6, dampingFraction: 0.8)) {
showSettings = false
}
}
}
// MARK: -
#Preview {
ContentView()
}