Skip to content

Commit

Permalink
Move triggers & more to built lists
Browse files Browse the repository at this point in the history
  • Loading branch information
Codel1417 committed Jun 18, 2024
1 parent 2acf8ad commit 3fe773e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 28 deletions.
34 changes: 23 additions & 11 deletions lib/Backend/action_registry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,10 @@ class ActionRegistry {
}

@Riverpod(keepAlive: true)
Map<ActionCategory, Set<BaseAction>> getAvailableActions(GetAvailableActionsRef ref) {
BuiltMap<ActionCategory, BuiltSet<BaseAction>> getAvailableActions(GetAvailableActionsRef ref) {
Map<ActionCategory, Set<BaseAction>> sortedActions = {};
final Map<ActionCategory, Set<BaseAction>> allActions = ref.watch(getAllActionsProvider);
final Iterable<BaseStatefulDevice> availableGear = ref.watch(getAvailableGearProvider);
final BuiltMap<ActionCategory, BuiltSet<BaseAction>> allActions = ref.watch(getAllActionsProvider);
final BuiltList<BaseStatefulDevice> availableGear = ref.watch(getAvailableGearProvider);
for (BaseAction baseAction in allActions.values.flattened) {
Set<BaseAction>? baseActions = {};
for (BaseStatefulDevice baseStatefulDevice in availableGear) {
Expand All @@ -292,11 +292,15 @@ Map<ActionCategory, Set<BaseAction>> getAvailableActions(GetAvailableActionsRef
sortedActions[baseAction.actionCategory] = baseActions;
}
}
return sortedActions;
return BuiltMap(
sortedActions.map(
(key, value) => MapEntry(key, value.build()),
),
);
}

@Riverpod(keepAlive: true)
BuiltMap<ActionCategory, Set<BaseAction>> getAllActions(GetAllActionsRef ref) {
BuiltMap<ActionCategory, BuiltSet<BaseAction>> getAllActions(GetAllActionsRef ref) {
Map<ActionCategory, Set<BaseAction>> sortedActions = {};
final BuiltList<MoveList> moveLists = ref.watch(moveListsProvider);
final BuiltList<AudioAction> audioActions = ref.watch(userAudioActionsProvider);
Expand All @@ -315,13 +319,17 @@ BuiltMap<ActionCategory, Set<BaseAction>> getAllActions(GetAllActionsRef ref) {
sortedActions[baseAction.actionCategory] = baseActions;
}
}
return BuiltMap(sortedActions);
return BuiltMap(
sortedActions.map(
(key, value) => MapEntry(key, value.build()),
),
);
}

@Riverpod(keepAlive: true)
Map<ActionCategory, Set<BaseAction>> getAllActionsFiltered(GetAllActionsFilteredRef ref, BuiltSet<DeviceType> deviceType) {
BuiltMap<ActionCategory, BuiltSet<BaseAction>> getAllActionsFiltered(GetAllActionsFilteredRef ref, BuiltSet<DeviceType> deviceType) {
Map<ActionCategory, Set<BaseAction>> sortedActions = {};
final Map<ActionCategory, Set<BaseAction>> read = ref.watch(getAllActionsProvider);
final BuiltMap<ActionCategory, BuiltSet<BaseAction>> read = ref.watch(getAllActionsProvider);
for (BaseAction baseAction in read.values.flattened) {
Set<BaseAction>? baseActions = {};
// check if command matches device type
Expand All @@ -338,12 +346,16 @@ Map<ActionCategory, Set<BaseAction>> getAllActionsFiltered(GetAllActionsFiltered
sortedActions[baseAction.actionCategory] = baseActions;
}
}
return sortedActions;
return BuiltMap(
sortedActions.map(
(key, value) => MapEntry(key, value.build()),
),
);
}

@Riverpod(keepAlive: true)
BuiltList<BaseAction> getAllActionsForCategory(GetAllActionsForCategoryRef ref, ActionCategory actionCategory) {
final Map<ActionCategory, Set<BaseAction>> allActions = ref.watch(getAllActionsProvider);
final BuiltMap<ActionCategory, BuiltSet<BaseAction>> allActions = ref.watch(getAllActionsProvider);
if (allActions.containsKey(actionCategory)) {
return allActions[actionCategory]!.toBuiltList();
}
Expand All @@ -362,6 +374,6 @@ BaseAction? getActionFromUUID(GetActionFromUUIDRef ref, String? uuid) {
if (uuid == null) {
return null;
}
final Map<ActionCategory, Set<BaseAction>> watch = ref.watch(getAllActionsProvider);
final BuiltMap<ActionCategory, BuiltSet<BaseAction>> watch = ref.watch(getAllActionsProvider);
return watch.values.flattened.where((element) => element.uuid == uuid).firstOrNull;
}
14 changes: 9 additions & 5 deletions lib/Backend/sensors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ class TriggerAction {
)
class TriggerList extends _$TriggerList {
@override
List<Trigger> build() {
BuiltList<Trigger> build() {
List<Trigger> results = [];
try {
results = HiveProxy.getAll<Trigger>(triggerBox).map((trigger) {
Expand Down Expand Up @@ -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<void> add(Trigger trigger) async {
state.add(trigger);
state = state.rebuild(
(p0) => p0.add(trigger),
);
await store();
}

Future<void> remove(Trigger trigger) async {
trigger.enabled = false;
state.remove(trigger);
state = state.rebuild(
(p0) => p0.remove(trigger),
);
await store();
}

Expand Down
5 changes: 3 additions & 2 deletions lib/Frontend/pages/action_selector.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -36,7 +37,7 @@ class ActionSelector extends ConsumerStatefulWidget {
}

class _ActionSelectorState extends ConsumerState<ActionSelector> {
Map<ActionCategory, Set<BaseAction>> actionsCatMap = {};
BuiltMap<ActionCategory, BuiltSet<BaseAction>> actionsCatMap = BuiltMap();
List<ActionCategory> catList = [];
List<BaseAction> selected = [];
Set<DeviceType> knownDeviceTypes = {};
Expand All @@ -51,7 +52,7 @@ class _ActionSelectorState extends ConsumerState<ActionSelector> {
(e) => e.baseDeviceDefinition.deviceType,
)
.toSet();
actionsCatMap = Map.fromEntries(
actionsCatMap = BuiltMap(
ref.read(getAllActionsProvider).entries.sorted(
(a, b) {
int first = a.value
Expand Down
2 changes: 1 addition & 1 deletion lib/Frontend/pages/actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class _ActionPageBuilderState extends ConsumerState<ActionPageBuilder> {
List<BaseStatefulDevice> knownDevicesFiltered = ref.watch(getAvailableGearProvider).toList();
return MultiValueListenableBuilder(
builder: (context, values, child) {
Map<ActionCategory, Set<BaseAction>> actionsCatMap = ref.watch(getAvailableActionsProvider);
BuiltMap<ActionCategory, BuiltSet<BaseAction>> actionsCatMap = ref.watch(getAvailableActionsProvider);
List<ActionCategory> catList = actionsCatMap.keys.toList();
return AnimatedCrossFade(
firstChild: const Home(),
Expand Down
6 changes: 3 additions & 3 deletions lib/Frontend/pages/triggers.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -29,7 +30,7 @@ class Triggers extends ConsumerStatefulWidget {
class _TriggersState extends ConsumerState<Triggers> {
@override
Widget build(BuildContext context) {
final List<Trigger> triggersList = ref.watch(triggerListProvider);
final BuiltList<Trigger> triggersList = ref.watch(triggerListProvider);
return Scaffold(
floatingActionButton: Builder(
builder: (context) {
Expand Down Expand Up @@ -377,10 +378,9 @@ class _TriggerEditState extends ConsumerState<TriggerEdit> {
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();
},
);
Expand Down
12 changes: 6 additions & 6 deletions test/Backend/action_registry_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ void main() {
group('GetAvailableActions', () {
test('Tail Actions', () async {
ProviderContainer providerContainer = await testGearAdd('MiTail');
Map<ActionCategory, Set<BaseAction>> actions = providerContainer.read(getAvailableActionsProvider);
BuiltMap<ActionCategory, BuiltSet<BaseAction>> actions = providerContainer.read(getAvailableActionsProvider);
expect(actions.length, 3);
expect(actions[ActionCategory.calm]?.length, 3);
expect(actions[ActionCategory.fast]?.length, 4);
expect(actions[ActionCategory.tense]?.length, 4);

providerContainer.read(knownDevicesProvider).values.first.hasGlowtip.value = GlowtipStatus.glowtip;
providerContainer.invalidate(getAvailableActionsProvider);
Map<ActionCategory, Set<BaseAction>> actions2 = providerContainer.read(getAvailableActionsProvider);
BuiltMap<ActionCategory, BuiltSet<BaseAction>> actions2 = providerContainer.read(getAvailableActionsProvider);
expect(actions2.length, 4);
expect(actions2[ActionCategory.calm]?.length, 3);
expect(actions2[ActionCategory.fast]?.length, 4);
Expand All @@ -42,20 +42,20 @@ void main() {
});
test('Ear Actions', () async {
ProviderContainer providerContainer = await testGearAdd('EG2');
Map<ActionCategory, Set<BaseAction>> actions = providerContainer.read(getAvailableActionsProvider);
BuiltMap<ActionCategory, BuiltSet<BaseAction>> 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<ActionCategory, Set<BaseAction>> actions = providerContainer.read(getAvailableActionsProvider);
BuiltMap<ActionCategory, BuiltSet<BaseAction>> 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<ActionCategory, Set<BaseAction>> actions = providerContainer.read(getAvailableActionsProvider);
BuiltMap<ActionCategory, BuiltSet<BaseAction>> actions = providerContainer.read(getAvailableActionsProvider);
expect(actions.length, 3);
expect(actions[ActionCategory.calm]?.length, 3);
expect(actions[ActionCategory.fast]?.length, 4);
Expand All @@ -65,7 +65,7 @@ void main() {
final providerContainer = ProviderContainer(
overrides: [],
);
Map<ActionCategory, Set<BaseAction>> actions = providerContainer.read(getAvailableActionsProvider);
BuiltMap<ActionCategory, BuiltSet<BaseAction>> actions = providerContainer.read(getAvailableActionsProvider);
expect(actions.length, 0);
});
});
Expand Down

0 comments on commit 3fe773e

Please sign in to comment.