131 lines
4.4 KiB
Swift
131 lines
4.4 KiB
Swift
//
|
||
// PreviewExampleView.swift
|
||
// TeachMate
|
||
//
|
||
// Created by Hongli on 2025/3/18.
|
||
//
|
||
|
||
import SwiftUI
|
||
import SwiftData
|
||
|
||
/// 示例视图,展示如何使用预览数据
|
||
/// 此视图仅用于开发和测试,不会包含在最终应用中
|
||
struct PreviewExampleView: View {
|
||
@Environment(\.modelContext) private var modelContext
|
||
@Query private var semesters: [Semester]
|
||
|
||
var body: some View {
|
||
NavigationStack {
|
||
List {
|
||
Section("学期") {
|
||
ForEach(semesters) { semester in
|
||
HStack {
|
||
Text(semester.title)
|
||
.font(.headline)
|
||
|
||
Spacer()
|
||
|
||
if semester.isCurrent {
|
||
Text("当前")
|
||
.font(.caption)
|
||
.foregroundStyle(.secondary)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
Section("使用预览数据的示例") {
|
||
NavigationLink("标准预览容器") {
|
||
Text("此视图使用 PreviewData.createContainer()")
|
||
.font(.headline)
|
||
}
|
||
|
||
NavigationLink("大数据集预览") {
|
||
Text("此视图使用 PreviewData.createLargeDataContainer()")
|
||
.font(.headline)
|
||
}
|
||
|
||
NavigationLink("空容器预览") {
|
||
Text("此视图使用 PreviewData.createEmptyContainer()")
|
||
.font(.headline)
|
||
}
|
||
}
|
||
|
||
Section("辅助方法示例") {
|
||
HStack {
|
||
Text("随机课程颜色:")
|
||
|
||
RoundedRectangle(cornerRadius: 4)
|
||
.fill(ColorFromHex(hex: PreviewData.randomCourseColor()))
|
||
.frame(width: 20, height: 20)
|
||
}
|
||
|
||
HStack {
|
||
Text("随机课程名称:")
|
||
Text(PreviewData.randomCourseName())
|
||
.foregroundStyle(.secondary)
|
||
}
|
||
|
||
HStack {
|
||
Text("随机教室位置:")
|
||
Text(PreviewData.randomLocation())
|
||
.foregroundStyle(.secondary)
|
||
}
|
||
|
||
HStack {
|
||
Text("随机班级:")
|
||
Text(PreviewData.randomClass())
|
||
.foregroundStyle(.secondary)
|
||
}
|
||
}
|
||
}
|
||
.navigationTitle("预览数据示例")
|
||
}
|
||
}
|
||
|
||
// 内部函数,用于从十六进制字符串创建颜色
|
||
// 避免与 ColorExtension.swift 中的扩展冲突
|
||
private func ColorFromHex(hex: String) -> Color {
|
||
let hex = hex.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
|
||
var int: UInt64 = 0
|
||
Scanner(string: hex).scanHexInt64(&int)
|
||
let a, r, g, b: UInt64
|
||
switch hex.count {
|
||
case 3: // RGB (12-bit)
|
||
(a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
|
||
case 6: // RGB (24-bit)
|
||
(a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
|
||
case 8: // ARGB (32-bit)
|
||
(a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
|
||
default:
|
||
(a, r, g, b) = (1, 1, 1, 0)
|
||
}
|
||
|
||
return Color(
|
||
.sRGB,
|
||
red: Double(r) / 255,
|
||
green: Double(g) / 255,
|
||
blue: Double(b) / 255,
|
||
opacity: Double(a) / 255
|
||
)
|
||
}
|
||
}
|
||
|
||
// 标准预览
|
||
#Preview("标准预览") {
|
||
PreviewExampleView()
|
||
.modelContainer(PreviewData.createContainer())
|
||
}
|
||
|
||
// 大数据集预览
|
||
#Preview("大数据集") {
|
||
PreviewExampleView()
|
||
.modelContainer(PreviewData.createLargeDataContainer())
|
||
}
|
||
|
||
// 空容器预览
|
||
#Preview("空容器") {
|
||
PreviewExampleView()
|
||
.modelContainer(PreviewData.createEmptyContainer())
|
||
}
|