From b4acd63b06bd811caf9570e1074b898bc2bc83f2 Mon Sep 17 00:00:00 2001 From: Liplum Date: Thu, 18 Apr 2024 07:55:22 +0800 Subject: [PATCH] [timetable] riverpod in TimetableStyleProv --- lib/timetable/settings.dart | 21 +++++++++++++++++---- lib/timetable/widgets/style.dart | 29 ++++++----------------------- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/lib/timetable/settings.dart b/lib/timetable/settings.dart index eb84e4042..b67d197b0 100644 --- a/lib/timetable/settings.dart +++ b/lib/timetable/settings.dart @@ -22,21 +22,34 @@ class TimetableSettings { TimetableSettings(this.box); - bool get autoUseImported => box.safeGet(_K.autoUseImported) ?? _kAutoUseImported; + bool get autoUseImported => box.safeGet(_K.autoUseImported) ?? _kAutoUseImported; - set autoUseImported(bool newV) => box.safePut(_K.autoUseImported, newV); + set autoUseImported(bool newV) => box.safePut(_K.autoUseImported, newV); CourseCellStyle? get cellStyle => decodeJsonObject(box.safeGet(_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(_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(_K.backgroundImage), (obj) => BackgroundImage.fromJson(obj)); - set backgroundImage(BackgroundImage? newV) => box.safePut(_K.backgroundImage, jsonEncode(newV?.toJson())); + set backgroundImage(BackgroundImage? newV) => box.safePut(_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]); } diff --git a/lib/timetable/widgets/style.dart b/lib/timetable/widgets/style.dart index 6c56ee1ba..b7ad57efe 100644 --- a/lib/timetable/widgets/style.dart +++ b/lib/timetable/widgets/style.dart @@ -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'; @@ -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; @@ -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 { +class TimetableStyleProvState extends ConsumerState { 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(); } @@ -110,20 +103,10 @@ class TimetableStyleProvState extends State { }); } - 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,