Skip to content

Commit

Permalink
Try Converter
Browse files Browse the repository at this point in the history
  • Loading branch information
StarProxima committed Apr 19, 2023
1 parent 57f82c9 commit d109419
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 20 deletions.
62 changes: 55 additions & 7 deletions lib/core/state_notifiers/hive_state_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,55 @@ import 'package:hive/hive.dart';
import 'package:cube_system/core/state_notifiers/open_state_notifier.dart';

class HiveStateNotifier<T> extends OpenStateNotifier<T> {
String boxName;
final String _boxName;
String get boxName => _boxName;

late Box _box;

HiveStateNotifier(super.state, {required this.boxName}) {
final Converter<T, Object?>? _converter;

HiveStateNotifier(
super.state, {
required String boxName,
Converter<T, Object?>? converter,
}) : _boxName = boxName,
_converter = converter {
_init();
}

void _init() async {
_box = await Hive.openBox(boxName);
state = _box.get(boxName) ?? state;
await _openBox();
_getData();
addListener(_saveData);
}

Future<void> _openBox() async {
_box = await Hive.openBox(boxName);
}

void _getData() {
final data = _box.get(boxName);

if (data != null) {
if (_converter != null) {
final newState = _converter!.from(data);
state = newState;
} else {
state = data;
}
}
}

Future<void> _saveData(T data) async {
await _box.put(boxName, data);
void put(data) {
_box.put(boxName, data);
}

if (_converter != null) {
put(_converter!.to(data));
} else {
put(data);
}
}

@override
Expand All @@ -27,7 +61,21 @@ class HiveStateNotifier<T> extends OpenStateNotifier<T> {
}
}

class SingleHiveStateNotifier<T> extends HiveStateNotifier<T?>
abstract class SingleHiveStateNotifier<T> extends HiveStateNotifier<T>
with ChangeStateMixin {
SingleHiveStateNotifier(super.state, {required super.boxName});
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,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:collection';

import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'package:cube_system/models/lesson/lesson.dart';
import 'package:cube_system/models/timetable_day/timetable_day_event.dart';

import 'package:cube_system/features/timetable_page/state_holders/timetable_page_events.dart';
Expand All @@ -12,13 +11,13 @@ import 'package:cube_system/models/timetable_day/timetable_day_type.dart';

final timetableDayEventManager = Provider<TimetableDayEventManager>((ref) {
return TimetableDayEventManager(
timetable: ref.watch(timetablePageTimetable.notifier),
timetable: ref.watch(timetablePageLessons.notifier),
events: ref.watch(timetablePageLessonEvents.notifier),
);
});

class TimetableDayEventManager {
final StateController<SplayTreeMap<DateTime, List<Lesson>>> timetable;
final TimetablePageLessonsNotifier timetable;
final StateController<SplayTreeMap<DateTime, TimetableDayEvent>> events;

TimetableDayEventManager({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final timetableLessonsManager = Provider<TimetableLessonsManager>((ref) {
lessonConvertor: ref.watch(lessonConvertor),
eventManager: ref.watch(timetableDayEventManager),
selectedTimetable: ref.watch(selectedTimetableStateHolder.notifier),
timetable: ref.watch(timetablePageTimetable.notifier),
timetable: ref.watch(timetablePageLessons.notifier),
events: ref.watch(timetablePageLessonEvents.notifier),
currentDateTime: ref.watch(currentDateTimeQuick.notifier),
selectedDate: ref.watch(selectedDate.notifier),
Expand All @@ -42,7 +42,7 @@ class TimetableLessonsManager {
final TimetableDayEventManager eventManager;

final SelectedTimetableNotifier selectedTimetable;
final StateController<SplayTreeMap<DateTime, List<Lesson>>> timetable;
final TimetablePageLessonsNotifier timetable;
final StateController<SplayTreeMap<DateTime, TimetableDayEvent>> events;
final StateController<DateTime> currentDateTime;
final StateController<DateTime> selectedDate;
Expand All @@ -65,7 +65,7 @@ class TimetableLessonsManager {
});

void clear() {
timetable.state = SplayTreeMap();
timetable.change(SplayTreeMap());
events.state = SplayTreeMap();
}

Expand All @@ -92,8 +92,7 @@ class TimetableLessonsManager {
}

void _setLessons(List<LessonFullNamesInDb> lessons) {
SplayTreeMap<DateTime, List<Lesson>> timetableMap =
SplayTreeMap.of(timetable.state.cast());
TimetableLessons timetableMap = SplayTreeMap.of(timetable.state.cast());

for (final lesson in lessons) {
timetableMap[lesson.date] = [];
Expand All @@ -113,7 +112,7 @@ class TimetableLessonsManager {
timetableMap[lesson.date]!.add(l);
}

timetable.state = timetableMap;
timetable.change(timetableMap);
}

Future<void> updateCurrentTimetable() async {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
import 'dart:collection';

import 'package:cube_system/models/app_box_names/app_box_names.dart';
import 'package:cube_system/models/lesson/lesson.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'package:cube_system/core/state_notifiers/hive_state_notifier.dart';

typedef TimetableLessons = SplayTreeMap<DateTime, List<Lesson>>;

final timetablePageDayLessons =
Provider.family<List<Lesson>?, DateTime>((ref, date) {
return ref.watch(timetablePageTimetable)[date];
return ref.watch(timetablePageLessons)[date];
});

final timetablePageTimetable =
StateProvider<SplayTreeMap<DateTime, List<Lesson>>>((ref) {
return SplayTreeMap();
// 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,
});
}
1 change: 1 addition & 0 deletions lib/models/app_box_names/app_box_names.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class AppBoxNames {
static const appSettings = 'appSettings';
static const selectedTimetable = 'selectedTimetable';
static const timetablePageLessons = 'timetablePageLessons';
}

0 comments on commit d109419

Please sign in to comment.