feat: add validated semester and lesson calculations
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import 'semester.dart';
|
||||
|
||||
enum ColorMode { single, multicolor }
|
||||
|
||||
class AppSettings {
|
||||
const AppSettings({
|
||||
this.semester,
|
||||
this.remindersEnabled = true,
|
||||
this.reminderMinutes = 15,
|
||||
this.colorMode = ColorMode.multicolor,
|
||||
this.singleColorArgb = 0xFF348F91,
|
||||
this.calendarEnabled = false,
|
||||
this.calendarId,
|
||||
});
|
||||
final Semester? semester;
|
||||
final bool remindersEnabled, calendarEnabled;
|
||||
final int reminderMinutes, singleColorArgb;
|
||||
final ColorMode colorMode;
|
||||
final String? calendarId;
|
||||
AppSettings copyWith({
|
||||
Semester? semester,
|
||||
bool? remindersEnabled,
|
||||
int? reminderMinutes,
|
||||
ColorMode? colorMode,
|
||||
int? singleColorArgb,
|
||||
bool? calendarEnabled,
|
||||
String? calendarId,
|
||||
}) => AppSettings(
|
||||
semester: semester ?? this.semester,
|
||||
remindersEnabled: remindersEnabled ?? this.remindersEnabled,
|
||||
reminderMinutes: reminderMinutes ?? this.reminderMinutes,
|
||||
colorMode: colorMode ?? this.colorMode,
|
||||
singleColorArgb: singleColorArgb ?? this.singleColorArgb,
|
||||
calendarEnabled: calendarEnabled ?? this.calendarEnabled,
|
||||
calendarId: calendarId ?? this.calendarId,
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import 'package:timezone/timezone.dart' as tz;
|
||||
|
||||
import 'course.dart';
|
||||
|
||||
class CourseOccurrence {
|
||||
const CourseOccurrence({
|
||||
required this.key,
|
||||
required this.course,
|
||||
required this.week,
|
||||
required this.start,
|
||||
required this.end,
|
||||
});
|
||||
final String key;
|
||||
final Course course;
|
||||
final int week;
|
||||
final tz.TZDateTime start, end;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
class LessonSlot {
|
||||
const LessonSlot(this.period, this.startMinute, this.endMinute);
|
||||
final int period, startMinute, endMinute;
|
||||
}
|
||||
|
||||
const lessonSlots = [
|
||||
LessonSlot(1, 510, 555),
|
||||
LessonSlot(2, 560, 605),
|
||||
LessonSlot(3, 620, 665),
|
||||
LessonSlot(4, 670, 715),
|
||||
LessonSlot(5, 870, 915),
|
||||
LessonSlot(6, 920, 965),
|
||||
LessonSlot(7, 980, 1025),
|
||||
LessonSlot(8, 1030, 1075),
|
||||
LessonSlot(9, 1170, 1215),
|
||||
LessonSlot(10, 1220, 1265),
|
||||
LessonSlot(11, 1270, 1315),
|
||||
];
|
||||
String minuteLabel(int minute) =>
|
||||
'${(minute ~/ 60).toString().padLeft(2, '0')}:${(minute % 60).toString().padLeft(2, '0')}';
|
||||
@@ -0,0 +1,79 @@
|
||||
import 'course.dart';
|
||||
import 'semester.dart';
|
||||
import 'course_occurrence.dart';
|
||||
import 'lesson_times.dart';
|
||||
|
||||
import 'package:timezone/timezone.dart' as tz;
|
||||
|
||||
export 'course.dart';
|
||||
export 'semester.dart';
|
||||
export 'lesson_times.dart';
|
||||
export 'course_occurrence.dart';
|
||||
|
||||
class ScheduleEngine {
|
||||
static DateTime schoolDate(DateTime instant) {
|
||||
final local = tz.TZDateTime.from(instant, tz.getLocation('Asia/Shanghai'));
|
||||
return DateTime.utc(local.year, local.month, local.day);
|
||||
}
|
||||
|
||||
static int? weekNumber(Semester semester, DateTime schoolDate) {
|
||||
final date = DateTime.utc(
|
||||
schoolDate.year,
|
||||
schoolDate.month,
|
||||
schoolDate.day,
|
||||
);
|
||||
final delta = date.difference(semester.firstMonday).inDays;
|
||||
if (delta < 0 || delta >= semester.totalWeeks * 7) return null;
|
||||
return delta ~/ 7 + 1;
|
||||
}
|
||||
|
||||
static List<CourseOccurrence> occurrences(
|
||||
List<Course> courses,
|
||||
Semester semester,
|
||||
) {
|
||||
final result = <CourseOccurrence>[];
|
||||
final zone = tz.getLocation('Asia/Shanghai');
|
||||
for (final course in courses) {
|
||||
for (final week in course.weeks) {
|
||||
if (week > semester.totalWeeks) continue;
|
||||
final day = semester.firstMonday.add(
|
||||
Duration(days: (week - 1) * 7 + course.weekday - 1),
|
||||
);
|
||||
if (day.isBefore(semester.startDate)) continue;
|
||||
final first = lessonSlots[course.startPeriod - 1].startMinute;
|
||||
final last = lessonSlots[course.endPeriod - 1].endMinute;
|
||||
result.add(
|
||||
CourseOccurrence(
|
||||
key: '${course.id}/${day.toIso8601String().substring(0, 10)}',
|
||||
course: course,
|
||||
week: week,
|
||||
start: tz.TZDateTime(
|
||||
zone,
|
||||
day.year,
|
||||
day.month,
|
||||
day.day,
|
||||
first ~/ 60,
|
||||
first % 60,
|
||||
),
|
||||
end: tz.TZDateTime(
|
||||
zone,
|
||||
day.year,
|
||||
day.month,
|
||||
day.day,
|
||||
last ~/ 60,
|
||||
last % 60,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
result.sort((a, b) {
|
||||
final order = a.start.compareTo(b.start);
|
||||
return order == 0 ? a.key.compareTo(b.key) : order;
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool overlaps(CourseOccurrence a, CourseOccurrence b) =>
|
||||
a.start.isBefore(b.end) && b.start.isBefore(a.end);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
class Semester {
|
||||
Semester({required DateTime startDate, required this.totalWeeks})
|
||||
: startDate = DateTime.utc(startDate.year, startDate.month, startDate.day) {
|
||||
if (totalWeeks < 1 || totalWeeks > 30) {
|
||||
throw ArgumentError('学期周数应为 1–30');
|
||||
}
|
||||
}
|
||||
DateTime get firstMonday =>
|
||||
startDate.subtract(Duration(days: startDate.weekday - 1));
|
||||
final DateTime startDate;
|
||||
final int totalWeeks;
|
||||
}
|
||||
Reference in New Issue
Block a user