Skip to content

Commit

Permalink
🏷️ Add rawDay and rawTime to course in order to modify correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 committed Feb 17, 2020
1 parent f2951fa commit 41c0d42
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 42 deletions.
10 changes: 8 additions & 2 deletions lib/model/beans.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 17 additions & 6 deletions lib/model/course.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -38,6 +40,10 @@ class Course {
List<String> classesName;
@HiveField(11)
bool isEleven;
@HiveField(12)
int rawDay;
@HiveField(13)
String rawTime;
Color color;

Course({
Expand All @@ -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<String, dynamic> json) {
int _oddEven = 0;
final _split = (json['allWeek'] as String).split(' ');
Expand Down Expand Up @@ -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;
}

Expand Down
73 changes: 40 additions & 33 deletions lib/pages/home/course_schedule_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -663,23 +663,19 @@ class _CoursesDialogState extends State<CoursesDialog> {
setState(() {
deleting = true;
});
final _course = widget.courseList[0];
Future.wait(
<Future>[
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,
Expand Down Expand Up @@ -965,6 +961,39 @@ class _CourseEditDialogState extends State<CourseEditDialog> {
_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(
Expand Down Expand Up @@ -1049,29 +1078,7 @@ class _CourseEditDialogState extends State<CourseEditDialog> {
? 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,
),
],
),
Expand Down
2 changes: 1 addition & 1 deletion lib/providers/courses_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class CoursesProvider extends ChangeNotifier {
}

void addCourse(Course course, Map<int, 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 {
Expand Down

0 comments on commit 41c0d42

Please sign in to comment.