diff --git a/lib/Backend/action_registry.dart b/lib/Backend/action_registry.dart index 9d041d73..c4e1209b 100644 --- a/lib/Backend/action_registry.dart +++ b/lib/Backend/action_registry.dart @@ -270,10 +270,10 @@ class ActionRegistry { } @Riverpod(keepAlive: true) -Map> getAvailableActions(GetAvailableActionsRef ref) { +BuiltMap> getAvailableActions(GetAvailableActionsRef ref) { Map> sortedActions = {}; - final Map> allActions = ref.watch(getAllActionsProvider); - final Iterable availableGear = ref.watch(getAvailableGearProvider); + final BuiltMap> allActions = ref.watch(getAllActionsProvider); + final BuiltList availableGear = ref.watch(getAvailableGearProvider); for (BaseAction baseAction in allActions.values.flattened) { Set? baseActions = {}; for (BaseStatefulDevice baseStatefulDevice in availableGear) { @@ -292,11 +292,15 @@ Map> getAvailableActions(GetAvailableActionsRef sortedActions[baseAction.actionCategory] = baseActions; } } - return sortedActions; + return BuiltMap( + sortedActions.map( + (key, value) => MapEntry(key, value.build()), + ), + ); } @Riverpod(keepAlive: true) -BuiltMap> getAllActions(GetAllActionsRef ref) { +BuiltMap> getAllActions(GetAllActionsRef ref) { Map> sortedActions = {}; final BuiltList moveLists = ref.watch(moveListsProvider); final BuiltList audioActions = ref.watch(userAudioActionsProvider); @@ -315,13 +319,17 @@ BuiltMap> getAllActions(GetAllActionsRef ref) { sortedActions[baseAction.actionCategory] = baseActions; } } - return BuiltMap(sortedActions); + return BuiltMap( + sortedActions.map( + (key, value) => MapEntry(key, value.build()), + ), + ); } @Riverpod(keepAlive: true) -Map> getAllActionsFiltered(GetAllActionsFilteredRef ref, BuiltSet deviceType) { +BuiltMap> getAllActionsFiltered(GetAllActionsFilteredRef ref, BuiltSet deviceType) { Map> sortedActions = {}; - final Map> read = ref.watch(getAllActionsProvider); + final BuiltMap> read = ref.watch(getAllActionsProvider); for (BaseAction baseAction in read.values.flattened) { Set? baseActions = {}; // check if command matches device type @@ -338,12 +346,16 @@ Map> getAllActionsFiltered(GetAllActionsFiltered sortedActions[baseAction.actionCategory] = baseActions; } } - return sortedActions; + return BuiltMap( + sortedActions.map( + (key, value) => MapEntry(key, value.build()), + ), + ); } @Riverpod(keepAlive: true) BuiltList getAllActionsForCategory(GetAllActionsForCategoryRef ref, ActionCategory actionCategory) { - final Map> allActions = ref.watch(getAllActionsProvider); + final BuiltMap> allActions = ref.watch(getAllActionsProvider); if (allActions.containsKey(actionCategory)) { return allActions[actionCategory]!.toBuiltList(); } @@ -362,6 +374,6 @@ BaseAction? getActionFromUUID(GetActionFromUUIDRef ref, String? uuid) { if (uuid == null) { return null; } - final Map> watch = ref.watch(getAllActionsProvider); + final BuiltMap> watch = ref.watch(getAllActionsProvider); return watch.values.flattened.where((element) => element.uuid == uuid).firstOrNull; } diff --git a/lib/Backend/sensors.dart b/lib/Backend/sensors.dart index abd69155..1671a197 100644 --- a/lib/Backend/sensors.dart +++ b/lib/Backend/sensors.dart @@ -713,7 +713,7 @@ class TriggerAction { ) class TriggerList extends _$TriggerList { @override - List build() { + BuiltList build() { List results = []; try { results = HiveProxy.getAll(triggerBox).map((trigger) { @@ -743,19 +743,23 @@ class TriggerList extends _$TriggerList { trigger.actions.firstWhere((element) => element.uuid == '7424097d-ba24-4d85-b963-bf58e85e289d').actions.add(ActionRegistry.allCommands.firstWhere((element) => element.uuid == 'd8384bcf-31ed-4b5d-a25a-da3a2f96e406').uuid); unawaited(store()); - return [trigger]; + return [trigger].build(); } - return results; + return results.build(); } Future add(Trigger trigger) async { - state.add(trigger); + state = state.rebuild( + (p0) => p0.add(trigger), + ); await store(); } Future remove(Trigger trigger) async { trigger.enabled = false; - state.remove(trigger); + state = state.rebuild( + (p0) => p0.remove(trigger), + ); await store(); } diff --git a/lib/Frontend/pages/action_selector.dart b/lib/Frontend/pages/action_selector.dart index f0ae8cc2..08e68cce 100644 --- a/lib/Frontend/pages/action_selector.dart +++ b/lib/Frontend/pages/action_selector.dart @@ -1,3 +1,4 @@ +import 'package:built_collection/built_collection.dart'; import 'package:collection/collection.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; @@ -36,7 +37,7 @@ class ActionSelector extends ConsumerStatefulWidget { } class _ActionSelectorState extends ConsumerState { - Map> actionsCatMap = {}; + BuiltMap> actionsCatMap = BuiltMap(); List catList = []; List selected = []; Set knownDeviceTypes = {}; @@ -51,7 +52,7 @@ class _ActionSelectorState extends ConsumerState { (e) => e.baseDeviceDefinition.deviceType, ) .toSet(); - actionsCatMap = Map.fromEntries( + actionsCatMap = BuiltMap( ref.read(getAllActionsProvider).entries.sorted( (a, b) { int first = a.value diff --git a/lib/Frontend/pages/actions.dart b/lib/Frontend/pages/actions.dart index 87d783dd..183410ab 100644 --- a/lib/Frontend/pages/actions.dart +++ b/lib/Frontend/pages/actions.dart @@ -48,7 +48,7 @@ class _ActionPageBuilderState extends ConsumerState { List knownDevicesFiltered = ref.watch(getAvailableGearProvider).toList(); return MultiValueListenableBuilder( builder: (context, values, child) { - Map> actionsCatMap = ref.watch(getAvailableActionsProvider); + BuiltMap> actionsCatMap = ref.watch(getAvailableActionsProvider); List catList = actionsCatMap.keys.toList(); return AnimatedCrossFade( firstChild: const Home(), diff --git a/lib/Frontend/pages/triggers.dart b/lib/Frontend/pages/triggers.dart index 7ee27be5..8a5034b0 100644 --- a/lib/Frontend/pages/triggers.dart +++ b/lib/Frontend/pages/triggers.dart @@ -1,4 +1,5 @@ import 'package:back_button_interceptor/back_button_interceptor.dart'; +import 'package:built_collection/built_collection.dart'; import 'package:choice/choice.dart'; import 'package:collection/collection.dart'; import 'package:flutter/material.dart'; @@ -29,7 +30,7 @@ class Triggers extends ConsumerStatefulWidget { class _TriggersState extends ConsumerState { @override Widget build(BuildContext context) { - final List triggersList = ref.watch(triggerListProvider); + final BuiltList triggersList = ref.watch(triggerListProvider); return Scaffold( floatingActionButton: Builder( builder: (context) { @@ -377,10 +378,9 @@ class _TriggerEditState extends ConsumerState { children: [ TextButton( onPressed: () async { + await ref.watch(triggerListProvider.notifier).remove(widget.trigger); setState( () { - ref.watch(triggerListProvider).remove(widget.trigger); - ref.watch(triggerListProvider.notifier).store(); Navigator.of(context).pop(); }, ); diff --git a/test/Backend/action_registry_test.dart b/test/Backend/action_registry_test.dart index 8c5adfaf..d045f970 100644 --- a/test/Backend/action_registry_test.dart +++ b/test/Backend/action_registry_test.dart @@ -25,7 +25,7 @@ void main() { group('GetAvailableActions', () { test('Tail Actions', () async { ProviderContainer providerContainer = await testGearAdd('MiTail'); - Map> actions = providerContainer.read(getAvailableActionsProvider); + BuiltMap> actions = providerContainer.read(getAvailableActionsProvider); expect(actions.length, 3); expect(actions[ActionCategory.calm]?.length, 3); expect(actions[ActionCategory.fast]?.length, 4); @@ -33,7 +33,7 @@ void main() { providerContainer.read(knownDevicesProvider).values.first.hasGlowtip.value = GlowtipStatus.glowtip; providerContainer.invalidate(getAvailableActionsProvider); - Map> actions2 = providerContainer.read(getAvailableActionsProvider); + BuiltMap> actions2 = providerContainer.read(getAvailableActionsProvider); expect(actions2.length, 4); expect(actions2[ActionCategory.calm]?.length, 3); expect(actions2[ActionCategory.fast]?.length, 4); @@ -42,20 +42,20 @@ void main() { }); test('Ear Actions', () async { ProviderContainer providerContainer = await testGearAdd('EG2'); - Map> actions = providerContainer.read(getAvailableActionsProvider); + BuiltMap> actions = providerContainer.read(getAvailableActionsProvider); expect(actions.length, 1); expect(actions[ActionCategory.ears]?.length, 8); }); test('Mini Tail Actions', () async { ProviderContainer providerContainer = await testGearAdd('minitail'); - Map> actions = providerContainer.read(getAvailableActionsProvider); + BuiltMap> actions = providerContainer.read(getAvailableActionsProvider); expect(actions.length, 2); expect(actions[ActionCategory.calm]?.length, 3); expect(actions[ActionCategory.fast]?.length, 1); }); test('Wings Actions', () async { ProviderContainer providerContainer = await testGearAdd('flutter'); - Map> actions = providerContainer.read(getAvailableActionsProvider); + BuiltMap> actions = providerContainer.read(getAvailableActionsProvider); expect(actions.length, 3); expect(actions[ActionCategory.calm]?.length, 3); expect(actions[ActionCategory.fast]?.length, 4); @@ -65,7 +65,7 @@ void main() { final providerContainer = ProviderContainer( overrides: [], ); - Map> actions = providerContainer.read(getAvailableActionsProvider); + BuiltMap> actions = providerContainer.read(getAvailableActionsProvider); expect(actions.length, 0); }); });