Files
TPSchedule/mobile/lib/domain/course.dart
T

70 lines
2.0 KiB
Dart

enum CourseSource { school, file, demo }
class Course {
Course({
required this.id,
required this.name,
required this.teacher,
required this.room,
required this.weekday,
required this.startPeriod,
required this.endPeriod,
required Set<int> weeks,
required this.source,
}) : weeks = Set.unmodifiable(weeks) {
if ([id, name, teacher, room].any((value) => value.trim().isEmpty) ||
weekday < 1 ||
weekday > 7 ||
startPeriod < 1 ||
endPeriod > 11 ||
endPeriod < startPeriod ||
weeks.isEmpty ||
weeks.any((week) => week < 1 || week > 30)) {
throw ArgumentError('课程字段、节次或周次无效');
}
}
final String id, name, teacher, room;
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('课程字段、节次或周次无效');
}
}
}