Skip to content

Commit

Permalink
[ywb] riverpod
Browse files Browse the repository at this point in the history
  • Loading branch information
liplum committed Mar 27, 2024
1 parent c5c6af8 commit 736d9bf
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 30 deletions.
32 changes: 8 additions & 24 deletions lib/school/ywb/index.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:sit/design/widgets/app.dart';
import 'package:sit/school/ywb/entity/application.dart';
Expand All @@ -12,36 +13,21 @@ import 'widgets/application.dart';

const _applicationLength = 2;

class YwbAppCard extends StatefulWidget {
class YwbAppCard extends ConsumerStatefulWidget {
const YwbAppCard({super.key});

@override
State<YwbAppCard> createState() => _YwbAppCardState();
ConsumerState<YwbAppCard> createState() => _YwbAppCardState();
}

class _YwbAppCardState extends State<YwbAppCard> {
final $running = YwbInit.applicationStorage.listenApplicationListOf(YwbApplicationType.running);
@override
void initState() {
super.initState();
$running.addListener(refresh);
}

@override
void dispose() {
$running.removeListener(refresh);
super.dispose();
}

void refresh() {
setState(() {});
}

class _YwbAppCardState extends ConsumerState<YwbAppCard> {
@override
Widget build(BuildContext context) {
final storage = YwbInit.applicationStorage;
final running = ref.watch(storage.$applicationFamily(YwbApplicationType.running));
return AppCard(
title: i18n.title.text(),
view: buildRunningCard(),
view: running == null ? null : buildRunningCard(running),
leftActions: [
FilledButton.icon(
onPressed: () {
Expand All @@ -61,9 +47,7 @@ class _YwbAppCardState extends State<YwbAppCard> {
);
}

Widget buildRunningCard() {
final running = YwbInit.applicationStorage.getApplicationListOf(YwbApplicationType.running);
if (running == null) return const SizedBox();
Widget buildRunningCard(List<YwbApplication> running) {
final applications = running.sublist(0, min(_applicationLength, running.length));
return applications
.map((e) => YwbApplicationTile(e).inCard(
Expand Down
2 changes: 1 addition & 1 deletion lib/school/ywb/init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ class YwbInit {
serviceService = Dev.demoMode ? const DemoYwbServiceService() : const YwbServiceService();
serviceStorage = const YwbServiceStorage();
applicationService = Dev.demoMode ? const DemoYwbApplicationService() : const YwbApplicationService();
applicationStorage = const YwbApplicationStorage();
applicationStorage = YwbApplicationStorage();
}
}
5 changes: 4 additions & 1 deletion lib/school/ywb/storage/application.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class _K {
class YwbApplicationStorage {
Box get box => HiveInit.ywb;

const YwbApplicationStorage();
YwbApplicationStorage();

List<YwbApplication>? getApplicationListOf(YwbApplicationType type) =>
(box.safeGet(_K.applicationListOf(type)) as List?)?.cast<YwbApplication>();
Expand All @@ -23,4 +23,7 @@ class YwbApplicationStorage {
box.safePut(_K.applicationListOf(type), newV);

Listenable listenApplicationListOf(YwbApplicationType type) => box.listenable(keys: [_K.applicationListOf(type)]);

late final $applicationFamily =
box.providerFamily<List<YwbApplication>, YwbApplicationType>(_K.applicationListOf, getApplicationListOf);
}
2 changes: 1 addition & 1 deletion lib/storage/hive/init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ class HiveInit {
settings = await core.openBox('settings'),
meta = await core.openBox('meta'),
timetable = await core.openBox('timetable'),
dev = await core.openBox("dev"),
...cacheBoxes = [
yellowPages = await cache.openBox('yellow-pages'),
eduEmail = await cache.openBox('edu-email'),
dev = await core.openBox("dev"),
game = await core.openBox("game"),
if (!kIsWeb) cookies = await cache.openBox('cookies'),
if (!kIsWeb) expense = await cache.openBox('expense'),
Expand Down
19 changes: 16 additions & 3 deletions lib/utils/hive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ class BoxChangeStreamNotifier extends ChangeNotifier {
}
}


extension BoxProviderX on Box {
/// For generic class, like [List] or [Map], please specify the [get] for type conversion.
AutoDisposeStateNotifierProvider<BoxFieldNotifier<T>, T?> provider<T>(dynamic key, [T? Function()? get]) {
Expand All @@ -94,6 +93,20 @@ extension BoxProviderX on Box {
});
}

/// For generic class, like [List] or [Map], please specify the [get] for type conversion.
AutoDisposeStateNotifierProviderFamily<BoxFieldNotifier<T>, T?, Arg> providerFamily<T, Arg>(
dynamic Function(Arg arg) keyOf,
T? Function(Arg arg) get,
) {
return StateNotifierProvider.autoDispose.family<BoxFieldNotifier<T>, T?, Arg>((ref, arg) {
return BoxFieldNotifier(
get(arg),
listenable(keys: [keyOf(arg)]),
() => get(arg),
);
});
}

AutoDisposeChangeNotifierProvider changeProvider(List<dynamic> keys) {
return ChangeNotifierProvider.autoDispose((ref) {
return BoxChangeNotifier(listenable(keys: keys));
Expand All @@ -107,8 +120,8 @@ extension BoxProviderX on Box {
}

ChangeNotifierProviderFamily<BoxChangeStreamNotifier, BoxEventFilter> streamProviderFamily({dynamic key}) {
return ChangeNotifierProvider.family((ref, filter) {
return BoxChangeStreamNotifier(watch(key: key), filter);
return ChangeNotifierProvider.family((ref, arg) {
return BoxChangeStreamNotifier(watch(key: key), arg);
});
}
}

0 comments on commit 736d9bf

Please sign in to comment.