Skip to content

Commit

Permalink
[timetable] riverpod in TimetableStyleProv
Browse files Browse the repository at this point in the history
  • Loading branch information
liplum committed Apr 17, 2024
1 parent 4edafcc commit b4acd63
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 27 deletions.
21 changes: 17 additions & 4 deletions lib/timetable/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,34 @@ class TimetableSettings {

TimetableSettings(this.box);

bool get autoUseImported => box.safeGet(_K.autoUseImported) ?? _kAutoUseImported;
bool get autoUseImported => box.safeGet<bool>(_K.autoUseImported) ?? _kAutoUseImported;

set autoUseImported(bool newV) => box.safePut(_K.autoUseImported, newV);
set autoUseImported(bool newV) => box.safePut<bool>(_K.autoUseImported, newV);

CourseCellStyle? get cellStyle =>
decodeJsonObject(box.safeGet<String>(_K.cellStyle), (obj) => CourseCellStyle.fromJson(obj));

set cellStyle(CourseCellStyle? newV) => box.safePut(_K.cellStyle, encodeJsonObject(newV, (obj) => obj.toJson()));
set cellStyle(CourseCellStyle? newV) =>
box.safePut<String>(_K.cellStyle, encodeJsonObject(newV, (obj) => obj.toJson()));

late final $cellStyle = box.provider(
_K.cellStyle,
get: () => cellStyle,
set: (v) => cellStyle = v,
);

ValueListenable listenCellStyle() => box.listenable(keys: [_K.cellStyle]);

BackgroundImage? get backgroundImage =>
decodeJsonObject(box.safeGet<String>(_K.backgroundImage), (obj) => BackgroundImage.fromJson(obj));

set backgroundImage(BackgroundImage? newV) => box.safePut(_K.backgroundImage, jsonEncode(newV?.toJson()));
set backgroundImage(BackgroundImage? newV) => box.safePut<String>(_K.backgroundImage, jsonEncode(newV?.toJson()));

late final $backgroundImage = box.provider(
_K.backgroundImage,
get: () => backgroundImage,
set: (v) => backgroundImage = v,
);

ValueListenable listenBackgroundImage() => box.listenable(keys: [_K.backgroundImage]);
}
29 changes: 6 additions & 23 deletions lib/timetable/widgets/style.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:copy_with_extension/copy_with_extension.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:sit/settings/settings.dart';
import 'package:sit/timetable/entity/background.dart';
import 'package:sit/timetable/entity/platte.dart';
Expand Down Expand Up @@ -59,7 +60,7 @@ class TimetableStyle extends InheritedWidget {
}
}

class TimetableStyleProv extends StatefulWidget {
class TimetableStyleProv extends ConsumerStatefulWidget {
final Widget? child;
final TimetablePalette? palette;
final CourseCellStyle? cellStyle;
Expand All @@ -77,30 +78,22 @@ class TimetableStyleProv extends StatefulWidget {
}) : assert(builder != null || child != null, "TimetableStyleProv should have at least one child.");

@override
TimetableStyleProvState createState() => TimetableStyleProvState();
ConsumerState createState() => TimetableStyleProvState();
}

class TimetableStyleProvState extends State<TimetableStyleProv> {
class TimetableStyleProvState extends ConsumerState<TimetableStyleProv> {
final $palette = TimetableInit.storage.palette.$selected;
final $cellStyle = Settings.timetable.listenCellStyle();
final $background = Settings.timetable.listenBackgroundImage();
var palette = TimetableInit.storage.palette.selectedRow ?? BuiltinTimetablePalettes.classic;
var cellStyle = Settings.timetable.cellStyle ?? const CourseCellStyle();
var background = Settings.timetable.backgroundImage ?? const BackgroundImage.disabled();

@override
void initState() {
super.initState();
$palette.addListener(refreshPalette);
$cellStyle.addListener(refreshCellStyle);
$background.addListener(refreshBackground);
}

@override
void dispose() {
$palette.removeListener(refreshPalette);
$cellStyle.removeListener(refreshCellStyle);
$background.removeListener(refreshBackground);
super.dispose();
}

Expand All @@ -110,20 +103,10 @@ class TimetableStyleProvState extends State<TimetableStyleProv> {
});
}

void refreshCellStyle() {
setState(() {
cellStyle = Settings.timetable.cellStyle ?? const CourseCellStyle();
});
}

void refreshBackground() {
setState(() {
background = Settings.timetable.backgroundImage ?? const BackgroundImage.disabled();
});
}

@override
Widget build(BuildContext context) {
final background = ref.watch(Settings.timetable.$backgroundImage) ?? const BackgroundImage.disabled();
final cellStyle = ref.watch(Settings.timetable.$cellStyle) ?? const CourseCellStyle();
final data = TimetableStyleData(
platte: palette,
cellStyle: cellStyle,
Expand Down

0 comments on commit b4acd63

Please sign in to comment.