292 lines
11 KiB
Swift
292 lines
11 KiB
Swift
//
|
||
// PreviewData.swift
|
||
// TeachMate
|
||
//
|
||
// Created by Hongli on 2025/3/18.
|
||
//
|
||
|
||
import SwiftUI
|
||
import SwiftData
|
||
|
||
/// 提供预览所需的示例数据
|
||
@MainActor
|
||
enum PreviewData {
|
||
|
||
// MARK: - 共享的日期和时间
|
||
|
||
/// 当前学期开始日期(2025年春季学期)
|
||
static let currentSemesterStartDate = Calendar.current.date(from: DateComponents(year: 2025, month: 2, day: 24))!
|
||
|
||
/// 当前学期结束日期
|
||
static let currentSemesterEndDate = Calendar.current.date(byAdding: .day, value: 18 * 7, to: currentSemesterStartDate)!
|
||
|
||
/// 上一学期开始日期(2024年秋季学期)
|
||
static let previousSemesterStartDate = Calendar.current.date(from: DateComponents(year: 2024, month: 9, day: 1))!
|
||
|
||
/// 上一学期结束日期
|
||
static let previousSemesterEndDate = Calendar.current.date(from: DateComponents(year: 2025, month: 1, day: 15))!
|
||
|
||
// MARK: - 示例颜色
|
||
|
||
/// 示例课程颜色
|
||
static let courseColors = [
|
||
"#D6EAF8", // 浅蓝色
|
||
"#FADBD8", // 浅红色
|
||
"#D5F5E3", // 浅绿色
|
||
"#FCF3CF", // 浅黄色
|
||
"#F5EEF8", // 浅紫色
|
||
"#EAEDED", // 浅灰色
|
||
"#FDEBD0" // 浅橙色
|
||
]
|
||
|
||
// MARK: - 示例课程名称和位置
|
||
|
||
/// 常见课程名称
|
||
static let courseNames = [
|
||
"高等数学", "线性代数", "程序设计", "数据结构",
|
||
"计算机网络", "操作系统", "软件工程", "数据库原理",
|
||
"计算机组成原理", "编译原理", "人工智能", "机器学习",
|
||
"离散数学", "概率论与数理统计", "算法设计与分析"
|
||
]
|
||
|
||
/// 常见教室位置
|
||
static let locations = [
|
||
"教学楼A-101", "教学楼A-102", "教学楼A-201", "教学楼A-202",
|
||
"教学楼B-101", "教学楼B-102", "教学楼B-201", "教学楼B-202",
|
||
"实验楼C-301", "实验楼C-302", "实验楼C-303", "实验楼C-304",
|
||
"图书馆-多媒体教室", "综合楼-报告厅", "科技楼-机房"
|
||
]
|
||
|
||
/// 常见班级
|
||
static let classes = [
|
||
"云计算G23-1", "软件G22-3", "软件G22-4", "软件工程2班",
|
||
"网络工程1班", "人工智能1班", "数据科学1班", "信息安全1班"
|
||
]
|
||
|
||
// MARK: - 创建预览容器
|
||
|
||
/// 创建预览用的ModelContainer(包含所有示例数据)
|
||
static func createContainer() -> ModelContainer {
|
||
let container = try! ModelContainer(for: Semester.self, CourseModel.self, ClassSession.self,
|
||
configurations: ModelConfiguration(isStoredInMemoryOnly: true))
|
||
|
||
// 创建示例学期
|
||
let currentSemester = Semester(
|
||
title: "2024-2025-2",
|
||
startDate: currentSemesterStartDate,
|
||
endDate: currentSemesterEndDate,
|
||
weeksCount: 18,
|
||
isCurrent: true
|
||
)
|
||
|
||
let previousSemester = Semester(
|
||
title: "2024-2025-1",
|
||
startDate: previousSemesterStartDate,
|
||
endDate: previousSemesterEndDate,
|
||
weeksCount: 18
|
||
)
|
||
|
||
container.mainContext.insert(currentSemester)
|
||
container.mainContext.insert(previousSemester)
|
||
|
||
// 创建示例课程
|
||
let course1 = CourseModel(name: courseNames[0], colorHex: courseColors[0])
|
||
course1.semester = currentSemester
|
||
course1.addSession(weekday: 1, timeSlot: 1, location: locations[0], schoolClass: classes[0])
|
||
course1.addSession(weekday: 3, timeSlot: 2, location: locations[0], schoolClass: classes[0])
|
||
|
||
let course2 = CourseModel(name: courseNames[1], colorHex: courseColors[1])
|
||
course2.semester = currentSemester
|
||
course2.addSession(weekday: 2, timeSlot: 3, location: locations[4], schoolClass: classes[0])
|
||
|
||
let course3 = CourseModel(name: courseNames[2], colorHex: courseColors[2], isNew: true)
|
||
course3.semester = currentSemester
|
||
course3.addSession(weekday: 4, timeSlot: 4, location: locations[8], schoolClass: classes[0])
|
||
course3.addSession(weekday: 5, timeSlot: 5, location: locations[8], schoolClass: classes[0])
|
||
|
||
let course4 = CourseModel(name: courseNames[3], colorHex: courseColors[3])
|
||
course4.semester = previousSemester
|
||
course4.addSession(weekday: 1, timeSlot: 2, location: locations[1], schoolClass: classes[0])
|
||
|
||
// 添加更多课程以展示丰富的数据
|
||
let course5 = CourseModel(name: courseNames[4], colorHex: courseColors[4])
|
||
course5.semester = currentSemester
|
||
course5.addSession(weekday: 2, timeSlot: 1, location: locations[5], schoolClass: classes[1])
|
||
|
||
let course6 = CourseModel(name: courseNames[5], colorHex: courseColors[5])
|
||
course6.semester = currentSemester
|
||
course6.addSession(weekday: 3, timeSlot: 3, location: locations[9], schoolClass: classes[1])
|
||
course6.addSession(weekday: 5, timeSlot: 4, location: locations[9], schoolClass: classes[1])
|
||
|
||
container.mainContext.insert(course1)
|
||
container.mainContext.insert(course2)
|
||
container.mainContext.insert(course3)
|
||
container.mainContext.insert(course4)
|
||
container.mainContext.insert(course5)
|
||
container.mainContext.insert(course6)
|
||
|
||
return container
|
||
}
|
||
|
||
/// 创建空的预览容器(用于添加新数据的表单)
|
||
static func createEmptyContainer() -> ModelContainer {
|
||
return try! ModelContainer(for: Semester.self, CourseModel.self, ClassSession.self,
|
||
configurations: ModelConfiguration(isStoredInMemoryOnly: true))
|
||
}
|
||
|
||
/// 创建包含大量数据的预览容器(用于性能测试)
|
||
static func createLargeDataContainer() -> ModelContainer {
|
||
let container = try! ModelContainer(for: Semester.self, CourseModel.self, ClassSession.self,
|
||
configurations: ModelConfiguration(isStoredInMemoryOnly: true))
|
||
|
||
// 创建多个学期
|
||
let oldStartDate1 = Calendar.current.date(byAdding: .year, value: -2, to: currentSemesterStartDate)!
|
||
let oldEndDate1 = Calendar.current.date(byAdding: .day, value: 18 * 7, to: oldStartDate1)!
|
||
|
||
let oldStartDate2 = Calendar.current.date(byAdding: .year, value: -1, to: currentSemesterStartDate)!
|
||
let oldEndDate2 = Calendar.current.date(byAdding: .day, value: 18 * 7, to: oldStartDate2)!
|
||
|
||
let prevEndDate = Calendar.current.date(byAdding: .day, value: 18 * 7, to: previousSemesterStartDate)!
|
||
|
||
let semesters = [
|
||
Semester(title: "2023-2024-1", startDate: oldStartDate1, endDate: oldEndDate1, weeksCount: 18),
|
||
Semester(title: "2023-2024-2", startDate: oldStartDate2, endDate: oldEndDate2, weeksCount: 18),
|
||
Semester(title: "2024-2025-1", startDate: previousSemesterStartDate, endDate: prevEndDate, weeksCount: 18),
|
||
Semester(title: "2024-2025-2", startDate: currentSemesterStartDate, endDate: currentSemesterEndDate, weeksCount: 18, isCurrent: true)
|
||
]
|
||
|
||
for semester in semesters {
|
||
container.mainContext.insert(semester)
|
||
}
|
||
|
||
// 为每个学期创建多个课程
|
||
for semester in semesters {
|
||
for i in 0..<10 {
|
||
let courseIndex = i % courseNames.count
|
||
let colorIndex = i % courseColors.count
|
||
|
||
let course = CourseModel(name: courseNames[courseIndex], colorHex: courseColors[colorIndex])
|
||
course.semester = semester
|
||
|
||
// 为每个课程添加1-3个课时
|
||
let sessionCount = (i % 3) + 1
|
||
for j in 0..<sessionCount {
|
||
let weekday = (j % 5) + 1
|
||
let timeSlot = (j % 5) + 1
|
||
let locationIndex = (i + j) % locations.count
|
||
let classIndex = i % classes.count
|
||
|
||
course.addSession(
|
||
weekday: weekday,
|
||
timeSlot: timeSlot,
|
||
location: locations[locationIndex],
|
||
schoolClass: classes[classIndex]
|
||
)
|
||
}
|
||
|
||
container.mainContext.insert(course)
|
||
}
|
||
}
|
||
|
||
return container
|
||
}
|
||
|
||
// MARK: - 专用预览容器
|
||
|
||
/// 创建仅包含学期的预览容器(用于ContentView预览)
|
||
static func createSemesterContainer() -> ModelContainer {
|
||
let config = ModelConfiguration(isStoredInMemoryOnly: true)
|
||
let container = try! ModelContainer(for: Semester.self, configurations: config)
|
||
|
||
// 创建示例学期
|
||
let currentSemester = Semester(
|
||
title: "2024-2025-2",
|
||
startDate: currentSemesterStartDate,
|
||
endDate: currentSemesterEndDate,
|
||
weeksCount: 18,
|
||
isCurrent: true
|
||
)
|
||
|
||
let previousSemester = Semester(
|
||
title: "2024-2025-1",
|
||
startDate: previousSemesterStartDate,
|
||
endDate: previousSemesterEndDate,
|
||
weeksCount: 18,
|
||
isCurrent: false
|
||
)
|
||
|
||
container.mainContext.insert(currentSemester)
|
||
container.mainContext.insert(previousSemester)
|
||
|
||
return container
|
||
}
|
||
|
||
// MARK: - 示例模型
|
||
|
||
/// 创建示例学期(用于日历视图和课程表视图)
|
||
static func createSampleSemester() -> Semester {
|
||
return Semester(
|
||
title: "2024-2025-2",
|
||
startDate: currentSemesterStartDate,
|
||
endDate: currentSemesterEndDate,
|
||
weeksCount: 18,
|
||
isCurrent: true
|
||
)
|
||
}
|
||
|
||
/// 创建示例课程(用于课程详情视图)
|
||
static func createSampleCourse() -> CourseModel {
|
||
let course = CourseModel(name: courseNames[0], colorHex: courseColors[0])
|
||
course.addSession(weekday: 1, timeSlot: 1, location: locations[0], schoolClass: classes[0])
|
||
course.addSession(weekday: 3, timeSlot: 2, location: locations[0], schoolClass: classes[0])
|
||
return course
|
||
}
|
||
|
||
/// 创建示例课时(用于课时详情视图)
|
||
static func createSampleSession() -> ClassSession {
|
||
let session = ClassSession(
|
||
weekday: 1,
|
||
timeSlot: 1,
|
||
location: locations[0],
|
||
schoolClass: classes[0]
|
||
)
|
||
|
||
// 设置关联的课程
|
||
let course = createSampleCourse()
|
||
session.course = course
|
||
|
||
return session
|
||
}
|
||
|
||
// MARK: - 辅助方法
|
||
|
||
/// 获取随机课程颜色
|
||
static func randomCourseColor() -> String {
|
||
return courseColors.randomElement() ?? courseColors[0]
|
||
}
|
||
|
||
/// 获取随机课程名称
|
||
static func randomCourseName() -> String {
|
||
return courseNames.randomElement() ?? courseNames[0]
|
||
}
|
||
|
||
/// 获取随机教室位置
|
||
static func randomLocation() -> String {
|
||
return locations.randomElement() ?? locations[0]
|
||
}
|
||
|
||
/// 获取随机班级
|
||
static func randomClass() -> String {
|
||
return classes.randomElement() ?? classes[0]
|
||
}
|
||
|
||
// MARK: - 预览修饰器
|
||
|
||
/// 为预览添加标准边距和背景
|
||
static func standardPreviewStyle<Content: View>(_ content: Content) -> some View {
|
||
content
|
||
.padding()
|
||
.background(Color.white) // 使用简单的白色背景
|
||
}
|
||
}
|