From 41c0d4265f710b94e4ad90e5fe8566305364efe1 Mon Sep 17 00:00:00 2001 From: AlexVincent525 Date: Mon, 17 Feb 2020 23:22:08 +0800 Subject: [PATCH] :label: Add `rawDay` and `rawTime` to course in order to modify correctly. --- lib/model/beans.g.dart | 10 +++- lib/model/course.dart | 23 ++++++-- lib/pages/home/course_schedule_page.dart | 73 +++++++++++++----------- lib/providers/courses_provider.dart | 2 +- 4 files changed, 66 insertions(+), 42 deletions(-) diff --git a/lib/model/beans.g.dart b/lib/model/beans.g.dart index f7a3a6ff..3158c26e 100644 --- a/lib/model/beans.g.dart +++ b/lib/model/beans.g.dart @@ -107,13 +107,15 @@ class CourseAdapter extends TypeAdapter { classesName: (fields[10] as List)?.cast(), isEleven: fields[11] as bool, oddEven: fields[9] as int, + rawDay: fields[12] as int, + rawTime: fields[13] as String, ); } @override void write(BinaryWriter writer, Course obj) { writer - ..writeByte(12) + ..writeByte(14) ..writeByte(0) ..write(obj.isCustom) ..writeByte(1) @@ -137,7 +139,11 @@ class CourseAdapter extends TypeAdapter { ..writeByte(10) ..write(obj.classesName) ..writeByte(11) - ..write(obj.isEleven); + ..write(obj.isEleven) + ..writeByte(12) + ..write(obj.rawDay) + ..writeByte(13) + ..write(obj.rawTime); } } diff --git a/lib/model/course.dart b/lib/model/course.dart index 5cd30bf7..b52d6397 100644 --- a/lib/model/course.dart +++ b/lib/model/course.dart @@ -9,9 +9,11 @@ part of 'beans.dart'; /// [isCustom] **必需**是否自定义课程, /// [name] 课程名称, [time] 上课时间, [location] 上课地点, [className] 班级名称, /// [teacher] 教师名称, [day] 上课日, [startWeek] 开始周, [endWeek] 结束周, -/// [classesName] 共同上课的班级, -/// [isEleven] 是否第十一节, -/// [oddEven] 是否为单双周, 0为普通, 1为单周, 2为双周 +/// [oddEven] 是否为单双周, 0为普通, 1为单周, 2为双周, +/// [classesName] 共同上课的班级, [isEleven] 是否第十一节, +/// +/// [rawDay] 原始天数 [rawTime] 原始课时 +/// 以上两项用于编辑课程信息。由于课程表的数据错乱,需要保存原始数据,否则会造成编辑错误。 @HiveType(typeId: HiveAdapterTypeIds.course) class Course { @HiveField(0) @@ -38,6 +40,10 @@ class Course { List classesName; @HiveField(11) bool isEleven; + @HiveField(12) + int rawDay; + @HiveField(13) + String rawTime; Color color; Course({ @@ -53,8 +59,13 @@ class Course { this.classesName, this.isEleven, this.oddEven, + this.rawDay, + this.rawTime, }); + /// Whether we should use raw data to modify. + bool get shouldUseRaw => day != rawDay || time != rawTime; + static int judgeOddEven(Map json) { int _oddEven = 0; final _split = (json['allWeek'] as String).split(' '); @@ -96,17 +107,17 @@ class Course { location: json['couRoom'], className: json['className'], teacher: json['couTeaName'], - day: json[!isCustom ? 'couDayTime' : 'courseDaytime'].toString().substring(0, 1).toInt(), + day: json[isCustom ? 'courseDaytime' : 'couDayTime'].toString().substring(0, 1).toInt(), startWeek: !isCustom ? weeks[0].toInt() : null, endWeek: !isCustom ? weeks[1].toInt() : null, classesName: !isCustom ? json['comboClassName'].split(',') : null, isEleven: json['three'] == 'y', oddEven: _oddEven, + rawDay: json[isCustom ? 'courseDaytime' : 'couDayTime'].toString().toInt(), + rawTime: json[isCustom ? 'courseTime' : 'coudeTime'].toString(), ); if (_c.isEleven && _c.time == '90') _c.time = '911'; - uniqueColor(_c, CourseAPI.randomCourseColor()); - return _c; } diff --git a/lib/pages/home/course_schedule_page.dart b/lib/pages/home/course_schedule_page.dart index 64780844..eee87127 100755 --- a/lib/pages/home/course_schedule_page.dart +++ b/lib/pages/home/course_schedule_page.dart @@ -663,23 +663,19 @@ class _CoursesDialogState extends State { setState(() { deleting = true; }); + final _course = widget.courseList[0]; Future.wait( [ CourseAPI.setCustomCourse({ 'content': Uri.encodeComponent(''), - 'couDayTime': widget.courseList[0].day, - 'coudeTime': widget.courseList[0].time, + 'couDayTime': _course.day, + 'coudeTime': _course.time, }), - CourseAPI.setCustomCourse({ - 'content': Uri.encodeComponent(''), - 'couDayTime': widget.courseList[0].day, - 'coudeTime': widget.courseList[0].time.toString().substring(0, 1), - }), - if (widget.courseList[0].time.toString().length > 1) + if (_course.shouldUseRaw) CourseAPI.setCustomCourse({ 'content': Uri.encodeComponent(''), - 'couDayTime': widget.courseList[0].day, - 'coudeTime': widget.courseList[0].time.toString().substring(1, 2), + 'couDayTime': _course.rawDay, + 'coudeTime': _course.rawTime, }), ], eagerError: true, @@ -965,6 +961,39 @@ class _CourseEditDialogState extends State { _controller = TextEditingController(text: content); } + void editCourse() { + loading = true; + if (mounted) setState(() {}); + Future editFuture; + + if (widget.course?.shouldUseRaw ?? false) { + editFuture = CourseAPI.setCustomCourse({ + 'content': Uri.encodeComponent(content), + 'couDayTime': widget.course?.rawDay ?? widget.coordinate[0], + 'coudeTime': widget.course?.rawTime ?? widget.coordinate[1], + }); + } else { + editFuture = CourseAPI.setCustomCourse({ + 'content': Uri.encodeComponent(content), + 'couDayTime': widget.course?.day ?? widget.coordinate[0], + 'coudeTime': widget.course?.time ?? widget.coordinate[1], + }); + } + editFuture.then((response) { + loading = false; + if (mounted) setState(() {}); + if (jsonDecode(response.data)['isOk']) { + navigatorState.popUntil((_) => _.isFirst); + } + Instances.eventBus.fire(CourseScheduleRefreshEvent()); + }).catchError((e) { + debugPrint('Failed when editing custom course: $e'); + showCenterErrorToast('编辑自定义课程失败'); + loading = false; + if (mounted) setState(() {}); + }); + } + Widget get courseEditField => Container( padding: EdgeInsets.all(suSetWidth(12.0)), decoration: BoxDecoration( @@ -1049,29 +1078,7 @@ class _CourseEditDialogState extends State { ? Colors.black.withOpacity(0.15) : Colors.black, ), - onPressed: content == widget.course?.name || loading - ? null - : () { - loading = true; - if (mounted) setState(() {}); - CourseAPI.setCustomCourse({ - 'content': Uri.encodeComponent(content), - 'couDayTime': widget.course?.day ?? widget.coordinate[0], - 'coudeTime': widget.course?.time ?? widget.coordinate[1], - }).then((response) { - loading = false; - if (mounted) setState(() {}); - if (jsonDecode(response.data)['isOk']) { - navigatorState.popUntil((_) => _.isFirst); - } - Instances.eventBus.fire(CourseScheduleRefreshEvent()); - }).catchError((e) { - debugPrint('Failed when editing custom course: $e'); - showCenterErrorToast('编辑自定义课程失败'); - loading = false; - if (mounted) setState(() {}); - }); - }, + onPressed: content == widget.course?.name || loading ? null : editCourse, ), ], ), diff --git a/lib/providers/courses_provider.dart b/lib/providers/courses_provider.dart index e8e32e55..ac2ed307 100644 --- a/lib/providers/courses_provider.dart +++ b/lib/providers/courses_provider.dart @@ -178,7 +178,7 @@ class CoursesProvider extends ChangeNotifier { } void addCourse(Course course, Map courses) { - final courseDay = course.day.toString().substring(0, 1).toInt(); + final courseDay = course.day; final courseTime = course.time.toInt(); assert(courseDay != null && courseTime != null); try {