Skip to content

Commit

Permalink
[timetable] student type. max name length. check if timetable can be …
Browse files Browse the repository at this point in the history
…updated.
  • Loading branch information
liplum committed Sep 12, 2024
1 parent 439fd82 commit 8c456f7
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 5 deletions.
7 changes: 7 additions & 0 deletions lib/school/entity/school.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ class SemesterInfo implements Comparable<SemesterInfo> {
}
}

@JsonEnum()
enum StudentType {
undergraduate,
postgraduate,
;
}

@HiveType(typeId: CacheHiveType.courseCat)
enum CourseCat {
@HiveField(0)
Expand Down
30 changes: 29 additions & 1 deletion lib/timetable/entity/timetable.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import 'dart:math';
import 'dart:typed_data';

import 'package:copy_with_extension/copy_with_extension.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:meta/meta.dart';
import 'package:mimir/credentials/entity/user_type.dart';
import 'package:mimir/credentials/init.dart';
import 'package:mimir/entity/campus.dart';
import 'package:mimir/school/entity/school.dart';
Expand All @@ -19,6 +21,12 @@ part 'timetable.g.dart';

DateTime _kNow() => DateTime.now();

StudentType _kStudentType() => switch (CredentialsInit.storage.oa.userType) {
OaUserType.undergraduate => StudentType.undergraduate,
OaUserType.postgraduate => StudentType.postgraduate,
_ => StudentType.undergraduate,
};

List<TimetablePatchEntry> _patchesFromJson(List? list) {
return list
?.map((e) => TimetablePatchEntry.fromJson(e as Map<String, dynamic>))
Expand All @@ -35,11 +43,16 @@ String _defaultStudentId() {
return CredentialsInit.storage.oa.credentials?.account ?? "";
}

String _parseName(String name) {
return name.substring(0, min(SitTimetable.maxNameLength, name.length));
}

@JsonSerializable()
@CopyWith(skipFields: true)
@immutable
class SitTimetable {
@JsonKey()
static int maxNameLength = 50;
@JsonKey(fromJson: _parseName)
final String name;
@JsonKey()
final DateTime startDate;
Expand All @@ -49,6 +62,8 @@ class SitTimetable {
final int schoolYear;
@JsonKey()
final Semester semester;
@JsonKey(defaultValue: _kStudentType)
final StudentType studentType;
@JsonKey()
final int lastCourseKey;
@JsonKey()
Expand Down Expand Up @@ -84,6 +99,7 @@ class SitTimetable {
required this.lastModified,
required this.createdTime,
required this.studentId,
required this.studentType,
this.patches = const [],
this.signature = "",
this.version = 2,
Expand Down Expand Up @@ -112,6 +128,7 @@ class SitTimetable {
"createdTime": createdTime,
"signature": signature,
"studentId": studentId,
"studentType": studentType,
"patches": patches,
}.toString();
}
Expand All @@ -121,11 +138,14 @@ class SitTimetable {
'name:"$name",'
'signature:"$signature",'
'studentId:"$studentId",'
'studentType:"$studentType",'
"campus:$campus,"
'startDate:DateTime.parse("$startDate"),'
'createdTime:DateTime.parse("$createdTime"),'
'lastModified:DateTime.parse("$lastModified"),'
"courses:${courses.map((key, value) => MapEntry('"$key"', value.toDartCode()))},"
"studentId:$studentId,"
"studentType:$studentType,"
"schoolYear:$schoolYear,"
"semester:$semester,"
"lastCourseKey:$lastCourseKey,"
Expand All @@ -146,7 +166,10 @@ class SitTimetable {
name == other.name &&
signature == other.signature &&
startDate == other.startDate &&
studentId == other.studentId &&
studentType == other.studentType &&
lastModified == other.lastModified &&
createdTime == other.createdTime &&
courses.equalsKeysValues(courses.keys, other.courses) &&
patches.equalsElements(other.patches);
}
Expand All @@ -161,6 +184,9 @@ class SitTimetable {
semester,
startDate,
lastModified,
createdTime,
studentId,
studentType,
Object.hashAllUnordered(courses.entries.map((e) => (e.key, e.value))),
Object.hashAll(patches),
version,
Expand All @@ -175,6 +201,7 @@ class SitTimetable {
writer.strUtf8(name, ByteLength.bit8);
writer.strUtf8(signature, ByteLength.bit8);
writer.strUtf8(studentId, ByteLength.bit8);
writer.uint8(studentType.index);
writer.uint8(campus.index);
writer.uint8(schoolYear);
writer.uint8(semester.index);
Expand All @@ -198,6 +225,7 @@ class SitTimetable {
name: reader.strUtf8(ByteLength.bit8),
signature: reader.strUtf8(ByteLength.bit8),
studentId: revision == 1 ? _defaultStudentId() : reader.strUtf8(ByteLength.bit8),
studentType: StudentType.values[reader.uint8()],
campus: Campus.values[reader.uint8()],
schoolYear: reader.uint8(),
semester: Semester.values[reader.uint8()],
Expand Down
13 changes: 13 additions & 0 deletions lib/timetable/entity/timetable.g.dart

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

6 changes: 5 additions & 1 deletion lib/timetable/page/edit/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';

import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
import 'package:go_router/go_router.dart';
import 'package:mimir/design/adaptive/foundation.dart';
Expand Down Expand Up @@ -335,7 +336,10 @@ class _TimetableEditorPageState extends State<TimetableEditorPage> {
children: [
TextFormField(
controller: $name,
maxLines: 1,
maxLines: 2,
inputFormatters: [
LengthLimitingTextInputFormatter(SitTimetable.maxNameLength),
],
decoration: InputDecoration(
labelText: i18n.editor.name,
border: const OutlineInputBorder(),
Expand Down
15 changes: 13 additions & 2 deletions lib/timetable/page/mine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import '../entity/timetable.dart';
import '../init.dart';
import '../utils/export.dart';
import '../utils/import.dart';
import '../utils/update.dart';
import '../widgets/focus.dart';
import '../p13n/widget/style.dart';
import 'import.dart';
Expand Down Expand Up @@ -353,7 +354,7 @@ class TimetableCard extends StatelessWidget {
);
},
),
if (!kIsWeb && kDebugMode)
if (!kIsWeb && canUpdateTimetable(timetable))
EntryAction(
icon: context.icons.refresh,
label: i18n.update,
Expand Down Expand Up @@ -465,10 +466,20 @@ class TimetableDetailsPage extends ConsumerWidget {
subtitle: context.formatYmdText(timetable.startDate).text(),
),
ListTile(
leading: const Icon(Icons.drive_file_rename_outline),
leading: const Icon(Icons.create),
title: "Created when".text(),
subtitle: context.formatYmdText(timetable.createdTime).text(),
),
ListTile(
leading: const Icon(Icons.person),
title: i18n.signature.text(),
subtitle: timetable.signature.text(),
),
ListTile(
leading: const Icon(Icons.school),
title: "Student type".text(),
subtitle: timetable.studentType.toString().text(),
),
]),
if (code2Courses.isNotEmpty) const SliverToBoxAdapter(child: Divider()),
SliverList.builder(
Expand Down
1 change: 1 addition & 0 deletions lib/timetable/service/school.demo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class DemoTimetableService implements TimetableService {
return SitTimetable(
campus: Campus.fengxian,
studentId: CredentialsInit.storage.oa.credentials?.account ?? "",
studentType: StudentType.undergraduate,
createdTime: DateTime.now(),
courses: {
"$key": SitCourse(
Expand Down
1 change: 1 addition & 0 deletions lib/timetable/utils/parse.pg.dart
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ SitTimetable parsePostgraduateTimetableFromCourseRaw(
lastCourseKey: counter,
name: "",
studentId: studentId,
studentType: StudentType.postgraduate,
campus: campus,
startDate: DateTime.utc(0),
createdTime: DateTime.now(),
Expand Down
1 change: 1 addition & 0 deletions lib/timetable/utils/parse.ug.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ SitTimetable parseUndergraduateTimetableFromRaw(
return SitTimetable(
courses: courses,
studentId: studentId,
studentType: StudentType.undergraduate,
lastCourseKey: lastCourseKey,
signature: name,
name: i18n.import.defaultName(semester.l10n(), schoolYear.toString(), (schoolYear + 1).toString()),
Expand Down
16 changes: 16 additions & 0 deletions lib/timetable/utils/update.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:mimir/credentials/init.dart';
import 'package:mimir/settings/settings.dart';
import 'package:mimir/timetable/entity/timetable.dart';
import 'package:mimir/timetable/init.dart';

bool canUpdateTimetable(SitTimetable old) {
final credentials = CredentialsInit.storage.oa.credentials;
if (credentials == null) return false;
if (old.studentId != credentials.account) return false;
if (old.campus != Settings.campus) return false;
return true;
}

Future<SitTimetable?> updateTimetable(SitTimetable old) async {
// TimetableInit.service.fetchUgTimetable(info)
}
2 changes: 1 addition & 1 deletion lib/utils/byte_io/writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ByteWriter {
}

void _minimalByteLength(int actualBytes, [ByteLength expectedBytes = ByteLength.bit32, Endian endian = Endian.big]) {
assert(expectedBytes.maxValue >= actualBytes, "Expect $expectedBytes");
assert(expectedBytes.maxValue >= actualBytes, "Expect $expectedBytes but $actualBytes given");
_checkCapacity(requireBytes: actualBytes + expectedBytes.byteLengths);
switch (expectedBytes) {
case ByteLength.bit8:
Expand Down
1 change: 1 addition & 0 deletions test/timetable_entity_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ SitTimetable _get() {
name: "Spring, 2023 Timetable",
signature: "Liplum",
studentId: "114514",
studentType: StudentType.undergraduate,
startDate: DateTime.parse("2024-02-26"),
createdTime: DateTime.now(),
lastModified: DateTime.now(),
Expand Down

0 comments on commit 8c456f7

Please sign in to comment.