Skip to content

Commit

Permalink
TimetablePageLessonsNotifier with Hive
Browse files Browse the repository at this point in the history
  • Loading branch information
StarProxima committed Apr 19, 2023
1 parent d109419 commit f5a306d
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 52 deletions.
8 changes: 7 additions & 1 deletion lib/core/hive_initializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(TypeAdapter<T> adapter) => Hive.registerAdapter(adapter);

Expand All @@ -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),
]);
}
}
37 changes: 6 additions & 31 deletions lib/core/state_notifiers/hive_state_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@ class HiveStateNotifier<T> extends OpenStateNotifier<T> {

late Box _box;

final Converter<T, Object?>? _converter;

HiveStateNotifier(
super.state, {
required String boxName,
Converter<T, Object?>? 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();
Expand All @@ -33,25 +32,12 @@ class HiveStateNotifier<T> extends OpenStateNotifier<T> {
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<void> _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
Expand All @@ -66,16 +52,5 @@ abstract class SingleHiveStateNotifier<T> extends HiveStateNotifier<T>
SingleHiveStateNotifier(
super.state, {
required super.boxName,
super.converter,
});
}

class Converter<T, K> {
K Function(T data) to;
T Function(K data) from;

Converter({
required this.to,
required this.from,
});
}
7 changes: 2 additions & 5 deletions lib/core/state_notifiers/open_state_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ class OpenStateNotifier<T> extends StateNotifier<T> {
T get state => super.state;
}

class SingleOpenStateNotifier<T> extends StateNotifier<T>
class SingleStateNotifier<T> extends OpenStateNotifier<T>
with ChangeStateMixin {
SingleOpenStateNotifier(super.state);

@override
T get state => super.state;
SingleStateNotifier(super.state);
}

mixin ChangeStateMixin<T> on StateNotifier<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,22 @@ class TimetableDayEventManager {
SplayTreeMap<DateTime, TimetableDayEvent> 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,
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,30 @@ final timetablePageDayLessons =
return ref.watch(timetablePageLessons)[date];
});

// final timetablePageTimetable = StateProvider<TimetableLessons>((ref) {
// return SplayTreeMap();
// });

final timetablePageLessons =
StateNotifierProvider<TimetablePageLessonsNotifier, TimetableLessons>(
(ref) {
return TimetablePageLessonsNotifier(
SplayTreeMap(),
boxName: AppBoxNames.timetablePageLessons,
converter: Converter<TimetableLessons, Map<DateTime, List<Lesson>>>(
to: (data) => data,
from: (data) => SplayTreeMap.from(data),
),
);
});

class TimetablePageLessonsNotifier
extends SingleHiveStateNotifier<TimetableLessons> {
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<Lesson>.from(value));
});

return SplayTreeMap.from(typedMap);
}
}
13 changes: 13 additions & 0 deletions lib/models/adapters/color_adapter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:flutter/cupertino.dart';
import 'package:hive/hive.dart';

class ColorAdapter extends TypeAdapter<Color> {
@override
final typeId = 222;

@override
Color read(BinaryReader reader) => Color(reader.readUint32());

@override
void write(BinaryWriter writer, Color obj) => writer.writeUint32(obj.value);
}

0 comments on commit f5a306d

Please sign in to comment.