-
Notifications
You must be signed in to change notification settings - Fork 46
[feat] 为添加课程页面提供更自由的时间选项 #128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hazuki-keatsu
wants to merge
8
commits into
main
Choose a base branch
from
feat/improveAddingCourse
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d752d63
feat: Implement custom class management functionality
hazuki-keatsu 7d3cfee
fix: Repair notification service initialization error when app start …
hazuki-keatsu 661adb6
feat: Add delete functionality to date selector component
hazuki-keatsu 2ec619a
feat: Integrate CustomCourseData into CourseReminder
hazuki-keatsu d65964d
feat: add i18n for date picker and adjust the layout of class add window
hazuki-keatsu 9d928bb
refactor: refactor the way of generating CourseID to avoid conflicts
hazuki-keatsu 934c51a
fix: update the id generator to the new one
hazuki-keatsu f3f7739
fix: avoid that the start time is equal to the end and change snackBa…
hazuki-keatsu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| // Copyright 2026 Hazuki Keatsu. | ||
| // Copyright 2026 Traintime PDA authors. | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| import 'dart:convert'; | ||
| import 'dart:io'; | ||
|
|
||
| import 'package:signals/signals.dart'; | ||
| import 'package:watermeter/model/pda_service/custom_class.dart'; | ||
| import 'package:watermeter/repository/network_session.dart'; | ||
|
|
||
| enum CustomClassState { fetching, fetched, error, none } | ||
|
|
||
| class CustomClassOccurrence { | ||
| final CustomClass customClass; | ||
| final CustomClassTimeRange timeRange; | ||
|
|
||
| const CustomClassOccurrence({ | ||
| required this.customClass, | ||
| required this.timeRange, | ||
| }); | ||
| } | ||
|
|
||
| class CustomClassController { | ||
| static final CustomClassController i = CustomClassController._(); | ||
|
|
||
| static const String _customClassFileName = 'CustomClassesV2.json'; | ||
| static const String _customClassIdPrefix = 'cc'; | ||
| static const String _timeRangeIdPrefix = 'tr'; | ||
|
|
||
| late File customClassFile; | ||
|
|
||
| CustomClassController._() { | ||
| customClassFile = File('${supportPath.path}/$_customClassFileName'); | ||
| _load(); | ||
| } | ||
|
|
||
| int _idSequence = 0; | ||
|
|
||
| String _nextId(String prefix) { | ||
| final int now = DateTime.now().microsecondsSinceEpoch; | ||
| _idSequence = (_idSequence + 1) & 0xFFFFF; | ||
| return '$prefix-${now.toRadixString(36)}-${_idSequence.toRadixString(36)}'; | ||
| } | ||
|
|
||
| String generateCustomClassId() => _nextId(_customClassIdPrefix); | ||
|
|
||
| String generateTimeRangeId() => _nextId(_timeRangeIdPrefix); | ||
|
|
||
| DateTime _dateOnly(DateTime value) => | ||
| DateTime(value.year, value.month, value.day); | ||
|
|
||
| final errorSignal = signal<String?>(null); | ||
| final stateSignal = signal<CustomClassState>(CustomClassState.none); | ||
| final customClassesSignal = signal<List<CustomClass>>([]); | ||
|
|
||
| String? get error => errorSignal.value; | ||
|
|
||
| CustomClassState get state => stateSignal.value; | ||
|
|
||
| List<CustomClass> get customClasses => customClassesSignal.value; | ||
|
|
||
| void _load() { | ||
| stateSignal.value = CustomClassState.fetching; | ||
| errorSignal.value = null; | ||
| try { | ||
| if (!customClassFile.existsSync()) { | ||
| customClassFile.writeAsStringSync('[]'); | ||
| } | ||
| final dynamic decoded = jsonDecode(customClassFile.readAsStringSync()); | ||
| customClassesSignal.value = (decoded as List<dynamic>) | ||
| .map((e) => CustomClass.fromJson(e as Map<String, dynamic>)) | ||
| .toList(); | ||
| stateSignal.value = CustomClassState.fetched; | ||
| } catch (e) { | ||
| stateSignal.value = CustomClassState.error; | ||
| errorSignal.value = e.toString(); | ||
| customClassesSignal.value = []; | ||
| } | ||
| } | ||
|
|
||
| bool _save(List<CustomClass> nextClasses) { | ||
| try { | ||
| customClassFile.writeAsStringSync( | ||
| jsonEncode(nextClasses.map((e) => e.toJson()).toList()), | ||
| ); | ||
| customClassesSignal.value = nextClasses; | ||
| errorSignal.value = null; | ||
| stateSignal.value = CustomClassState.fetched; | ||
| return true; | ||
| } catch (e) { | ||
| stateSignal.value = CustomClassState.error; | ||
| errorSignal.value = 'Failed to save custom classes: $e'; | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| int _indexOfCustomClassById(String customClassId) => customClasses.indexWhere( | ||
| (customClass) => customClass.id == customClassId, | ||
| ); | ||
|
|
||
| /// 添加新的自定义课程 | ||
| Future<void> addCustomClass(CustomClass customClass) async { | ||
| final List<CustomClass> updated = List<CustomClass>.from(customClasses) | ||
| ..add(customClass); | ||
| _save(updated); | ||
| } | ||
|
|
||
| /// 编辑已有的自定义课程 | ||
| Future<void> editCustomClassById( | ||
| String customClassId, | ||
| CustomClass customClass, | ||
| ) async { | ||
| final int index = _indexOfCustomClassById(customClassId); | ||
| if (index < 0) return; | ||
| final List<CustomClass> updated = List<CustomClass>.from(customClasses); | ||
| updated[index] = customClass; | ||
| _save(updated); | ||
| } | ||
|
|
||
| /// 删除已有的自定义课程 | ||
| Future<void> deleteCustomClassById(String customClassId) async { | ||
| final int index = _indexOfCustomClassById(customClassId); | ||
| if (index < 0) return; | ||
| final List<CustomClass> updated = List<CustomClass>.from(customClasses) | ||
| ..removeAt(index); | ||
| _save(updated); | ||
| } | ||
|
|
||
| /// 从已有的自定义课程中一处某个时间段 | ||
| Future<void> deleteCustomClassTimeRange({ | ||
| required String customClassId, | ||
| required String timeRangeId, | ||
| }) async { | ||
| final int customIndex = _indexOfCustomClassById(customClassId); | ||
| if (customIndex < 0) return; | ||
|
|
||
| final CustomClass customClass = customClasses[customIndex]; | ||
| final int timeRangeIndex = customClass.timeRanges.indexWhere( | ||
| (timeRange) => timeRange.id == timeRangeId, | ||
| ); | ||
| if (timeRangeIndex < 0) return; | ||
|
|
||
| final List<CustomClassTimeRange> updatedRanges = | ||
| List<CustomClassTimeRange>.from(customClass.timeRanges) | ||
| ..removeAt(timeRangeIndex); | ||
|
|
||
| if (updatedRanges.isEmpty) { | ||
| final List<CustomClass> updated = List<CustomClass>.from(customClasses) | ||
| ..removeAt(customIndex); | ||
| _save(updated); | ||
| return; | ||
| } | ||
|
|
||
| final CustomClass updatedClass = CustomClass( | ||
| id: customClass.id, | ||
| name: customClass.name, | ||
| teacher: customClass.teacher, | ||
| classroom: customClass.classroom, | ||
| timeRanges: updatedRanges, | ||
| ); | ||
| final List<CustomClass> updated = List<CustomClass>.from(customClasses); | ||
| updated[customIndex] = updatedClass; | ||
| _save(updated); | ||
| } | ||
|
|
||
| /// 通过周索引、日索引和学期开始日期来找到有日程的那天 | ||
| List<CustomClassOccurrence> getOccurrenceOfDay({ | ||
| required int weekIndex, | ||
| required int dayIndex, | ||
| required DateTime semesterStartDay, | ||
| }) { | ||
| final List<CustomClassOccurrence> occurrences = []; | ||
|
|
||
| for (final customClass in customClasses) { | ||
| for (final timeRange in customClass.timeRanges) { | ||
| final int diffDays = _dateOnly( | ||
| timeRange.startTime, | ||
| ).difference(_dateOnly(semesterStartDay)).inDays; | ||
| if (diffDays < 0) continue; | ||
|
|
||
| final int targetWeek = diffDays ~/ 7; | ||
| final int targetDay = diffDays % 7 + 1; | ||
|
|
||
| if (targetWeek == weekIndex && targetDay == dayIndex) { | ||
| occurrences.add( | ||
| CustomClassOccurrence( | ||
| customClass: customClass, | ||
| timeRange: timeRange, | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return occurrences; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.