Skip to content

Commit

Permalink
rebuild actions when glowtip detected
Browse files Browse the repository at this point in the history
  • Loading branch information
Codel1417 committed Jun 28, 2024
1 parent f4eeb82 commit 4c95a16
Showing 1 changed file with 40 additions and 23 deletions.
63 changes: 40 additions & 23 deletions lib/Backend/action_registry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

import 'Bluetooth/bluetooth_manager.dart';
import 'Definitions/Action/base_action.dart';
import 'Definitions/Device/device_definition.dart';
import 'audio.dart';
Expand Down Expand Up @@ -270,33 +271,49 @@ class ActionRegistry {
}

@Riverpod(keepAlive: true)
BuiltMap<ActionCategory, BuiltSet<BaseAction>> getAvailableActions(GetAvailableActionsRef ref) {
Map<ActionCategory, Set<BaseAction>> sortedActions = {};
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) {
// check if command matches device type
if (baseAction.deviceCategory.contains(baseStatefulDevice.baseDeviceDefinition.deviceType) && ((baseAction.actionCategory == ActionCategory.glowtip && baseStatefulDevice.hasGlowtip.value == GlowtipStatus.glowtip) || baseAction.actionCategory != ActionCategory.glowtip)) {
// get category if it exists
if (sortedActions.containsKey(baseAction.actionCategory)) {
baseActions = sortedActions[baseAction.actionCategory];
class GetAvailableActions extends _$GetAvailableActions {
@override
BuiltMap<ActionCategory, BuiltSet<BaseAction>> build() {
for (BaseStatefulDevice baseStatefulDevice in ref.watch(knownDevicesProvider).values) {
baseStatefulDevice.hasGlowtip
..removeListener(_listener)
..addListener(_listener);
}
return getState();
}

BuiltMap<ActionCategory, BuiltSet<BaseAction>> getState() {
Map<ActionCategory, Set<BaseAction>> sortedActions = {};
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) {
// check if command matches device type
if (baseAction.deviceCategory.contains(baseStatefulDevice.baseDeviceDefinition.deviceType) && ((baseAction.actionCategory == ActionCategory.glowtip && baseStatefulDevice.hasGlowtip.value == GlowtipStatus.glowtip) || baseAction.actionCategory != ActionCategory.glowtip)) {
// get category if it exists
if (sortedActions.containsKey(baseAction.actionCategory)) {
baseActions = sortedActions[baseAction.actionCategory];
}
// add action to category
baseActions?.add(baseAction);
}
// add action to category
baseActions?.add(baseAction);
}
// store result
if (baseActions != null && baseActions.isNotEmpty) {
sortedActions[baseAction.actionCategory] = baseActions;
}
}
// store result
if (baseActions != null && baseActions.isNotEmpty) {
sortedActions[baseAction.actionCategory] = baseActions;
}
return BuiltMap(
sortedActions.map(
(key, value) => MapEntry(key, value.build()),
),
);
}

void _listener() {
state = getState();
}
return BuiltMap(
sortedActions.map(
(key, value) => MapEntry(key, value.build()),
),
);
}

@Riverpod(keepAlive: true)
Expand Down

0 comments on commit 4c95a16

Please sign in to comment.