feat: persist timetable and validate confirmed imports

This commit is contained in:
Codex
2026-10-12 23:18:51 +08:00
parent ae8e347e49
commit 808af007a2
13 changed files with 852 additions and 0 deletions
+39
View File
@@ -27,4 +27,43 @@ class Course {
final int weekday, startPeriod, endPeriod;
final Set<int> weeks;
final CourseSource source;
Map<String, dynamic> toJson() => {
'id': id,
'name': name,
'teacher': teacher,
'room': room,
'weekday': weekday,
'startPeriod': startPeriod,
'endPeriod': endPeriod,
'weeks': weeks.toList()..sort(),
'source': source.name,
};
static Course fromJson(Map<String, dynamic> json, {CourseSource? source}) {
try {
String text(String key) {
final v = json[key];
if (v is! String) throw const FormatException('课程字段缺失');
return v.trim();
}
final rawWeeks = json['weeks'];
if (rawWeeks is! List || rawWeeks.any((w) => w is! int))
throw const FormatException('周次必须是整数数组');
return Course(
id: text('id'),
name: text('name'),
teacher: text('teacher'),
room: text('room'),
weekday: json['weekday'] as int,
startPeriod: json['startPeriod'] as int,
endPeriod: json['endPeriod'] as int,
weeks: rawWeeks.cast<int>().toSet(),
source: source ?? CourseSource.values.byName(json['source'] as String),
);
} catch (_) {
throw const FormatException('课程字段、节次或周次无效');
}
}
}