feat: 使用swiftdata
This commit is contained in:
parent
35e949cf17
commit
9e0a4a92c3
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
/* Begin PBXBuildFile section */
|
/* Begin PBXBuildFile section */
|
||||||
AB8773632E4E04400071CB53 /* .gitignore in Resources */ = {isa = PBXBuildFile; fileRef = AB8773622E4E040E0071CB53 /* .gitignore */; };
|
AB8773632E4E04400071CB53 /* .gitignore in Resources */ = {isa = PBXBuildFile; fileRef = AB8773622E4E040E0071CB53 /* .gitignore */; };
|
||||||
ABB4E2182E4B761400660198 /* Alamofire in Frameworks */ = {isa = PBXBuildFile; productRef = ABB4E2172E4B761400660198 /* Alamofire */; };
|
ABE8998E2E533A7100CD7BA6 /* Alamofire in Frameworks */ = {isa = PBXBuildFile; productRef = ABE8998D2E533A7100CD7BA6 /* Alamofire */; };
|
||||||
/* End PBXBuildFile section */
|
/* End PBXBuildFile section */
|
||||||
|
|
||||||
/* Begin PBXCopyFilesBuildPhase section */
|
/* Begin PBXCopyFilesBuildPhase section */
|
||||||
@ -27,6 +27,7 @@
|
|||||||
/* Begin PBXFileReference section */
|
/* Begin PBXFileReference section */
|
||||||
AB8773622E4E040E0071CB53 /* .gitignore */ = {isa = PBXFileReference; lastKnownFileType = text; path = .gitignore; sourceTree = "<group>"; };
|
AB8773622E4E040E0071CB53 /* .gitignore */ = {isa = PBXFileReference; lastKnownFileType = text; path = .gitignore; sourceTree = "<group>"; };
|
||||||
ABB4E2082E4B75D900660198 /* wake.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = wake.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
ABB4E2082E4B75D900660198 /* wake.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = wake.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||||
|
ABE899102E5328F100CD7BA6 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = System/Library/Frameworks/CoreData.framework; sourceTree = SDKROOT; };
|
||||||
/* End PBXFileReference section */
|
/* End PBXFileReference section */
|
||||||
|
|
||||||
/* Begin PBXFileSystemSynchronizedBuildFileExceptionSet section */
|
/* Begin PBXFileSystemSynchronizedBuildFileExceptionSet section */
|
||||||
@ -55,7 +56,7 @@
|
|||||||
isa = PBXFrameworksBuildPhase;
|
isa = PBXFrameworksBuildPhase;
|
||||||
buildActionMask = 2147483647;
|
buildActionMask = 2147483647;
|
||||||
files = (
|
files = (
|
||||||
ABB4E2182E4B761400660198 /* Alamofire in Frameworks */,
|
ABE8998E2E533A7100CD7BA6 /* Alamofire in Frameworks */,
|
||||||
);
|
);
|
||||||
runOnlyForDeploymentPostprocessing = 0;
|
runOnlyForDeploymentPostprocessing = 0;
|
||||||
};
|
};
|
||||||
@ -67,6 +68,7 @@
|
|||||||
children = (
|
children = (
|
||||||
AB8773622E4E040E0071CB53 /* .gitignore */,
|
AB8773622E4E040E0071CB53 /* .gitignore */,
|
||||||
ABB4E20A2E4B75D900660198 /* wake */,
|
ABB4E20A2E4B75D900660198 /* wake */,
|
||||||
|
ABE8990F2E5328F100CD7BA6 /* Frameworks */,
|
||||||
ABB4E2092E4B75D900660198 /* Products */,
|
ABB4E2092E4B75D900660198 /* Products */,
|
||||||
);
|
);
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
@ -79,6 +81,14 @@
|
|||||||
name = Products;
|
name = Products;
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
};
|
};
|
||||||
|
ABE8990F2E5328F100CD7BA6 /* Frameworks */ = {
|
||||||
|
isa = PBXGroup;
|
||||||
|
children = (
|
||||||
|
ABE899102E5328F100CD7BA6 /* CoreData.framework */,
|
||||||
|
);
|
||||||
|
name = Frameworks;
|
||||||
|
sourceTree = "<group>";
|
||||||
|
};
|
||||||
/* End PBXGroup section */
|
/* End PBXGroup section */
|
||||||
|
|
||||||
/* Begin PBXNativeTarget section */
|
/* Begin PBXNativeTarget section */
|
||||||
@ -100,7 +110,7 @@
|
|||||||
);
|
);
|
||||||
name = wake;
|
name = wake;
|
||||||
packageProductDependencies = (
|
packageProductDependencies = (
|
||||||
ABB4E2172E4B761400660198 /* Alamofire */,
|
ABE8998D2E533A7100CD7BA6 /* Alamofire */,
|
||||||
);
|
);
|
||||||
productName = wake;
|
productName = wake;
|
||||||
productReference = ABB4E2082E4B75D900660198 /* wake.app */;
|
productReference = ABB4E2082E4B75D900660198 /* wake.app */;
|
||||||
@ -131,7 +141,7 @@
|
|||||||
mainGroup = ABB4E1FF2E4B75D900660198;
|
mainGroup = ABB4E1FF2E4B75D900660198;
|
||||||
minimizedProjectReferenceProxies = 1;
|
minimizedProjectReferenceProxies = 1;
|
||||||
packageReferences = (
|
packageReferences = (
|
||||||
ABB4E2162E4B761400660198 /* XCRemoteSwiftPackageReference "Alamofire" */,
|
ABE8998C2E533A7100CD7BA6 /* XCRemoteSwiftPackageReference "Alamofire" */,
|
||||||
);
|
);
|
||||||
preferredProjectObjectVersion = 77;
|
preferredProjectObjectVersion = 77;
|
||||||
productRefGroup = ABB4E2092E4B75D900660198 /* Products */;
|
productRefGroup = ABB4E2092E4B75D900660198 /* Products */;
|
||||||
@ -372,7 +382,7 @@
|
|||||||
/* End XCConfigurationList section */
|
/* End XCConfigurationList section */
|
||||||
|
|
||||||
/* Begin XCRemoteSwiftPackageReference section */
|
/* Begin XCRemoteSwiftPackageReference section */
|
||||||
ABB4E2162E4B761400660198 /* XCRemoteSwiftPackageReference "Alamofire" */ = {
|
ABE8998C2E533A7100CD7BA6 /* XCRemoteSwiftPackageReference "Alamofire" */ = {
|
||||||
isa = XCRemoteSwiftPackageReference;
|
isa = XCRemoteSwiftPackageReference;
|
||||||
repositoryURL = "https://github.com/Alamofire/Alamofire.git";
|
repositoryURL = "https://github.com/Alamofire/Alamofire.git";
|
||||||
requirement = {
|
requirement = {
|
||||||
@ -383,9 +393,9 @@
|
|||||||
/* End XCRemoteSwiftPackageReference section */
|
/* End XCRemoteSwiftPackageReference section */
|
||||||
|
|
||||||
/* Begin XCSwiftPackageProductDependency section */
|
/* Begin XCSwiftPackageProductDependency section */
|
||||||
ABB4E2172E4B761400660198 /* Alamofire */ = {
|
ABE8998D2E533A7100CD7BA6 /* Alamofire */ = {
|
||||||
isa = XCSwiftPackageProductDependency;
|
isa = XCSwiftPackageProductDependency;
|
||||||
package = ABB4E2162E4B761400660198 /* XCRemoteSwiftPackageReference "Alamofire" */;
|
package = ABE8998C2E533A7100CD7BA6 /* XCRemoteSwiftPackageReference "Alamofire" */;
|
||||||
productName = Alamofire;
|
productName = Alamofire;
|
||||||
};
|
};
|
||||||
/* End XCSwiftPackageProductDependency section */
|
/* End XCSwiftPackageProductDependency section */
|
||||||
|
|||||||
Binary file not shown.
@ -14,8 +14,8 @@
|
|||||||
filePath = "wake/ContentView.swift"
|
filePath = "wake/ContentView.swift"
|
||||||
startingColumnNumber = "9223372036854775807"
|
startingColumnNumber = "9223372036854775807"
|
||||||
endingColumnNumber = "9223372036854775807"
|
endingColumnNumber = "9223372036854775807"
|
||||||
startingLineNumber = "30"
|
startingLineNumber = "37"
|
||||||
endingLineNumber = "30"
|
endingLineNumber = "37"
|
||||||
landmarkName = "body"
|
landmarkName = "body"
|
||||||
landmarkType = "24">
|
landmarkType = "24">
|
||||||
</BreakpointContent>
|
</BreakpointContent>
|
||||||
|
|||||||
BIN
wake/.DS_Store
vendored
BIN
wake/.DS_Store
vendored
Binary file not shown.
@ -1,4 +1,5 @@
|
|||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
import SwiftData
|
||||||
|
|
||||||
// MARK: - 自定义过渡动画
|
// MARK: - 自定义过渡动画
|
||||||
extension AnyTransition {
|
extension AnyTransition {
|
||||||
@ -19,6 +20,12 @@ struct ContentView: View {
|
|||||||
@State private var contentOffset: CGFloat = 0 // 内容偏移量
|
@State private var contentOffset: CGFloat = 0 // 内容偏移量
|
||||||
@State private var showLogin = false
|
@State private var showLogin = false
|
||||||
|
|
||||||
|
// 获取模型上下文
|
||||||
|
@Environment(\.modelContext) private var modelContext
|
||||||
|
|
||||||
|
// 查询数据 - 简单查询
|
||||||
|
@Query private var login: [Login]
|
||||||
|
|
||||||
// MARK: - 主体视图
|
// MARK: - 主体视图
|
||||||
var body: some View {
|
var body: some View {
|
||||||
NavigationView {
|
NavigationView {
|
||||||
@ -40,6 +47,28 @@ struct ContentView: View {
|
|||||||
|
|
||||||
Spacer()
|
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("✅ 记录已更新")
|
||||||
|
}
|
||||||
|
}
|
||||||
// 登录按钮
|
// 登录按钮
|
||||||
NavigationLink(destination: LoginView()) {
|
NavigationLink(destination: LoginView()) {
|
||||||
Text("登录")
|
Text("登录")
|
||||||
@ -160,7 +189,15 @@ struct ContentView: View {
|
|||||||
/// 显示用户资料弹窗
|
/// 显示用户资料弹窗
|
||||||
private func showUserProfile() {
|
private func showUserProfile() {
|
||||||
withAnimation(.spring(response: 0.6, dampingFraction: 0.8)) {
|
withAnimation(.spring(response: 0.6, dampingFraction: 0.8)) {
|
||||||
showModal.toggle()
|
// 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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,9 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||||
|
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="23788.4" systemVersion="24G84" minimumToolsVersion="Automatic" sourceLanguage="Swift" usedWithSwiftData="YES" userDefinedModelVersionIdentifier="">
|
||||||
|
<entity name="Entity" representedClassName="Entity" syncable="YES" codeGenerationType="class">
|
||||||
|
<attribute name="accessToken" optional="YES" attributeType="String"/>
|
||||||
|
<attribute name="fulname" optional="YES" attributeType="String"/>
|
||||||
|
<attribute name="refreshToken" optional="YES" attributeType="String"/>
|
||||||
|
<attribute name="username" optional="YES" attributeType="String"/>
|
||||||
|
</entity>
|
||||||
|
</model>
|
||||||
22
wake/SwiftData/LoginModel.swift
Normal file
22
wake/SwiftData/LoginModel.swift
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import Foundation
|
||||||
|
import SwiftData
|
||||||
|
|
||||||
|
@Model
|
||||||
|
final class Login {
|
||||||
|
@Attribute(.unique) // 添加唯一性约束
|
||||||
|
|
||||||
|
// 基本属性
|
||||||
|
// fulname
|
||||||
|
var name: String
|
||||||
|
// 邮箱
|
||||||
|
var email: String
|
||||||
|
// 描述
|
||||||
|
var description: String {
|
||||||
|
return "Login(name: \(name), email: \(email))"
|
||||||
|
}
|
||||||
|
// 初始化方法
|
||||||
|
init(email:String="", name: String = "") {
|
||||||
|
self.email = email
|
||||||
|
self.name = name
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,12 +1,7 @@
|
|||||||
//
|
|
||||||
// WakeApp.swift
|
|
||||||
// Wake
|
|
||||||
//
|
|
||||||
// Created by elliwood on 2025/8/11.
|
|
||||||
//
|
|
||||||
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
import UIKit
|
import UIKit
|
||||||
|
import SwiftData
|
||||||
|
|
||||||
@main
|
@main
|
||||||
struct WakeApp: App {
|
struct WakeApp: App {
|
||||||
@ -20,6 +15,25 @@ struct WakeApp: App {
|
|||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
// 使用更简单的方式创建模型容器
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
var body: some Scene {
|
var body: some Scene {
|
||||||
WindowGroup {
|
WindowGroup {
|
||||||
@ -37,5 +51,7 @@ struct WakeApp: App {
|
|||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
// 注入模型容器到环境中
|
||||||
|
.modelContainer(container)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user