diff --git a/lib/core/hive_initializer.dart b/lib/core/hive_initializer.dart index 9d1a612..05931d6 100644 --- a/lib/core/hive_initializer.dart +++ b/lib/core/hive_initializer.dart @@ -17,6 +17,8 @@ import 'package:cube_system/models/timetable/timetable_type.dart'; import 'package:cube_system/models/timetable_day/timetable_day_event.dart'; import 'package:cube_system/models/timetable_day/timetable_day_type.dart'; +import 'package:cube_system/models/adapters/color_adapter.dart'; + abstract class HiveInitializer { static void _reg(TypeAdapter adapter) => Hive.registerAdapter(adapter); @@ -36,7 +38,11 @@ abstract class HiveInitializer { _reg(TimetableDayEventAdapter()); _reg(TimetableDayEventTypeAdapter()); _reg(AppThemeModeAdapter()); + _reg(ColorAdapter()); - await Hive.openBox(AppBoxNames.selectedTimetable); + await Future.wait([ + Hive.openBox(AppBoxNames.selectedTimetable), + Hive.openBox(AppBoxNames.timetablePageLessons), + ]); } } diff --git a/lib/core/state_notifiers/hive_state_notifier.dart b/lib/core/state_notifiers/hive_state_notifier.dart index bf14369..d6b6d03 100644 --- a/lib/core/state_notifiers/hive_state_notifier.dart +++ b/lib/core/state_notifiers/hive_state_notifier.dart @@ -8,17 +8,16 @@ class HiveStateNotifier extends OpenStateNotifier { late Box _box; - final Converter? _converter; - HiveStateNotifier( super.state, { required String boxName, - Converter? converter, - }) : _boxName = boxName, - _converter = converter { + }) : _boxName = boxName { _init(); } + dynamic serialize(T value) => value; + T deserialize(dynamic value) => value as T; + void _init() async { await _openBox(); _getData(); @@ -33,25 +32,12 @@ class HiveStateNotifier extends OpenStateNotifier { final data = _box.get(boxName); if (data != null) { - if (_converter != null) { - final newState = _converter!.from(data); - state = newState; - } else { - state = data; - } + state = deserialize(data); } } Future _saveData(T data) async { - void put(data) { - _box.put(boxName, data); - } - - if (_converter != null) { - put(_converter!.to(data)); - } else { - put(data); - } + _box.put(boxName, serialize(data)); } @override @@ -66,16 +52,5 @@ abstract class SingleHiveStateNotifier extends HiveStateNotifier SingleHiveStateNotifier( super.state, { required super.boxName, - super.converter, - }); -} - -class Converter { - K Function(T data) to; - T Function(K data) from; - - Converter({ - required this.to, - required this.from, }); } diff --git a/lib/core/state_notifiers/open_state_notifier.dart b/lib/core/state_notifiers/open_state_notifier.dart index 2b706d3..0e9a0b7 100644 --- a/lib/core/state_notifiers/open_state_notifier.dart +++ b/lib/core/state_notifiers/open_state_notifier.dart @@ -7,12 +7,9 @@ class OpenStateNotifier extends StateNotifier { T get state => super.state; } -class SingleOpenStateNotifier extends StateNotifier +class SingleStateNotifier extends OpenStateNotifier with ChangeStateMixin { - SingleOpenStateNotifier(super.state); - - @override - T get state => super.state; + SingleStateNotifier(super.state); } mixin ChangeStateMixin on StateNotifier { diff --git a/lib/features/timetable_page/managers/timetable_day_event_manager.dart b/lib/features/timetable_page/managers/timetable_day_event_manager.dart index 5755e34..29b03de 100644 --- a/lib/features/timetable_page/managers/timetable_day_event_manager.dart +++ b/lib/features/timetable_page/managers/timetable_day_event_manager.dart @@ -86,10 +86,22 @@ class TimetableDayEventManager { SplayTreeMap eventMap = SplayTreeMap.of(events.state.cast()); + final timetableMap = timetable.state; + for (final entry in eventMap.entries) { if (entry.value.type == TimetableDayEventType.loading) { - eventMap[entry.key] = - TimetableDayEvent(type: TimetableDayEventType.error); + final TimetableDayEventType type; + final lessons = timetableMap[entry.key]; + if (lessons == null) { + type = TimetableDayEventType.error; + } else if (lessons.isEmpty) { + type = TimetableDayEventType.weekend; + } else { + type = TimetableDayEventType.lessons; + } + eventMap[entry.key] = TimetableDayEvent( + type: type, + ); } } diff --git a/lib/features/timetable_page/managers/timetable_lessons_manager.dart b/lib/features/timetable_page/managers/timetable_lessons_manager.dart index cd1912a..9dd4d3c 100644 --- a/lib/features/timetable_page/managers/timetable_lessons_manager.dart +++ b/lib/features/timetable_page/managers/timetable_lessons_manager.dart @@ -142,6 +142,11 @@ class TimetableLessonsManager { final lessons = await _getLessons(startDate: startDate, endDate: endDate); + for (int day = 0; day <= endDate.difference(startDate).inDays; day++) { + final date = startDate.add(Duration(days: day)); + timetable.state[date] ??= []; + } + _setLessons(lessons); eventManager.setLessonsAfterLoading(); diff --git a/lib/features/timetable_page/state_holders/timetable_page_lessons.dart b/lib/features/timetable_page/state_holders/timetable_page_lessons.dart index c20cb0e..9264f11 100644 --- a/lib/features/timetable_page/state_holders/timetable_page_lessons.dart +++ b/lib/features/timetable_page/state_holders/timetable_page_lessons.dart @@ -13,28 +13,30 @@ final timetablePageDayLessons = return ref.watch(timetablePageLessons)[date]; }); -// final timetablePageTimetable = StateProvider((ref) { -// return SplayTreeMap(); -// }); - final timetablePageLessons = StateNotifierProvider( (ref) { return TimetablePageLessonsNotifier( SplayTreeMap(), boxName: AppBoxNames.timetablePageLessons, - converter: Converter>>( - to: (data) => data, - from: (data) => SplayTreeMap.from(data), - ), ); }); class TimetablePageLessonsNotifier extends SingleHiveStateNotifier { - TimetablePageLessonsNotifier( - super.state, { - required super.boxName, - required super.converter, - }); + TimetablePageLessonsNotifier(super.state, {required super.boxName}); + + @override + serialize(value) { + return value.cast(); + } + + @override + deserialize(value) { + final typedMap = (value as Map).map((key, value) { + return MapEntry(key as DateTime, List.from(value)); + }); + + return SplayTreeMap.from(typedMap); + } } diff --git a/lib/models/adapters/color_adapter.dart b/lib/models/adapters/color_adapter.dart new file mode 100644 index 0000000..78bbe8b --- /dev/null +++ b/lib/models/adapters/color_adapter.dart @@ -0,0 +1,13 @@ +import 'package:flutter/cupertino.dart'; +import 'package:hive/hive.dart'; + +class ColorAdapter extends TypeAdapter { + @override + final typeId = 222; + + @override + Color read(BinaryReader reader) => Color(reader.readUint32()); + + @override + void write(BinaryWriter writer, Color obj) => writer.writeUint32(obj.value); +}