Skip to content

Commit

Permalink
move some hive boxes to consts
Browse files Browse the repository at this point in the history
  • Loading branch information
Codel1417 committed Jun 10, 2024
1 parent 5239c14 commit 144df86
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 22 deletions.
6 changes: 3 additions & 3 deletions lib/Backend/Bluetooth/bluetooth_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final log.Logger bluetoothLog = log.Logger('Bluetooth');
class KnownDevices extends _$KnownDevices {
@override
Map<String, BaseStatefulDevice> build() {
List<BaseStoredDevice> storedDevices = HiveProxy.getAll<BaseStoredDevice>('devices').toList();
List<BaseStoredDevice> storedDevices = HiveProxy.getAll<BaseStoredDevice>(devicesBox).toList();
Map<String, BaseStatefulDevice> results = {};
try {
if (storedDevices.isNotEmpty) {
Expand Down Expand Up @@ -50,7 +50,7 @@ class KnownDevices extends _$KnownDevices {
}

Future<void> store() async {
await HiveProxy.clear<BaseStoredDevice>('devices');
await HiveProxy.addAll<BaseStoredDevice>('devices', state.values.map((e) => e.baseStoredDevice));
await HiveProxy.clear<BaseStoredDevice>(devicesBox);
await HiveProxy.addAll<BaseStoredDevice>(devicesBox, state.values.map((e) => e.baseStoredDevice));
}
}
6 changes: 3 additions & 3 deletions lib/Backend/move_lists.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class MoveLists extends _$MoveLists {
List<MoveList> build() {
List<MoveList> results = [];
try {
results = HiveProxy.getAll<MoveList>('sequences').toList(growable: true);
results = HiveProxy.getAll<MoveList>(sequencesBox).toList(growable: true);
} catch (e, s) {
sequencesLogger.severe("Unable to load sequences: $e", e, s);
}
Expand All @@ -189,8 +189,8 @@ class MoveLists extends _$MoveLists {

Future<void> store() async {
sequencesLogger.info("Storing sequences");
await HiveProxy.clear<MoveList>('sequences');
await HiveProxy.addAll<MoveList>('sequences', state);
await HiveProxy.clear<MoveList>(sequencesBox);
await HiveProxy.addAll<MoveList>(sequencesBox, state);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Backend/plausible_dio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class PlausibleDio extends Plausible {
page = "app://localhost/$page?utm_source=${(await InstallReferrer.referrer).name}";
referrer = "app://localhost/$referrer";
props = Map.of(props);
props['Number Of Devices'] = HiveProxy.getAll<BaseStoredDevice>('devices').length.toString();
props['Number Of Sequences'] = HiveProxy.getAll<MoveList>('sequences').length.toString();
props['Number Of Devices'] = HiveProxy.getAll<BaseStoredDevice>(devicesBox).length.toString();
props['Number Of Sequences'] = HiveProxy.getAll<MoveList>(sequencesBox).length.toString();
props['Number Of Triggers'] = HiveProxy.getAll<Trigger>(triggerBox).length.toString();
props['App Version'] = (await PackageInfo.fromPlatform()).version;
props['App Build'] = (await PackageInfo.fromPlatform()).buildNumber;
Expand Down
6 changes: 4 additions & 2 deletions lib/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const String hasCompletedOnboarding = 'hasCompletedOnboardingVersion';
const String showDebugging = 'showDebugging';
const String alwaysScanning = 'alwaysScanning';
const String showDemoGear = 'showDemoGear';
const String showAdvancedSettings = 'showAdvancedSettings';

// Notification Settings
const String allowNewsletterNotifications = 'allowNewsletterNotifications';
Expand All @@ -43,10 +44,11 @@ const bool showDebuggingDefault = false;
const bool alwaysScanningDefault = true;
const bool allowNewsletterNotificationsDefault = false;
const bool showDemoGearDefault = false;
const bool showAdvancedSettingsDefault = false;

// Triggers labels
const String triggerBox = 'triggers';

const String sequencesBox = 'sequences';
const String devicesBox = 'devices';
const String favoriteActionsBox = 'favoriteActions';
const String audioActionsBox = 'audioActions';
const int hasCompletedOnboardingVersionToAgree = 1;
Expand Down
4 changes: 2 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ Future<void> initHive() async {
await SentryHive.openBox<Trigger>(triggerBox);
await SentryHive.openBox<FavoriteAction>(favoriteActionsBox);
await SentryHive.openBox<AudioAction>(audioActionsBox);
await SentryHive.openBox<MoveList>('sequences');
await SentryHive.openBox<BaseStoredDevice>('devices');
await SentryHive.openBox<MoveList>(sequencesBox);
await SentryHive.openBox<BaseStoredDevice>(devicesBox);
await SentryHive.openBox(notificationBox); // Do not set type here
}

Expand Down
10 changes: 5 additions & 5 deletions test/Backend/device_registry_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,20 @@ void main() {
String name = 'MiTail';
String name2 = 'EG2';
expect(container.read(knownDevicesProvider).length, 0);
expect(HiveProxy.getAll<BaseStoredDevice>('devices').length, 0);
expect(HiveProxy.getAll<BaseStoredDevice>(devicesBox).length, 0);
BaseStatefulDevice baseStatefulDevice = await createAndStoreGear(name, container);
expect(baseStatefulDevice.baseDeviceDefinition.btName, name);
expect(container.read(knownDevicesProvider).length, 1);
expect(container.read(knownDevicesProvider).values.first, baseStatefulDevice);
expect(HiveProxy.getAll<BaseStoredDevice>('devices').length, 1);
expect(HiveProxy.getAll<BaseStoredDevice>('devices').first, baseStatefulDevice.baseStoredDevice);
expect(HiveProxy.getAll<BaseStoredDevice>(devicesBox).length, 1);
expect(HiveProxy.getAll<BaseStoredDevice>(devicesBox).first, baseStatefulDevice.baseStoredDevice);

BaseStatefulDevice baseStatefulDevice2 = await createAndStoreGear(name2, container);
expect(baseStatefulDevice2.baseDeviceDefinition.btName, name2);
expect(container.read(knownDevicesProvider).length, 2);
expect(container.read(knownDevicesProvider).values.contains(baseStatefulDevice2), true);
expect(HiveProxy.getAll<BaseStoredDevice>('devices').length, 2);
expect(HiveProxy.getAll<BaseStoredDevice>('devices').contains(baseStatefulDevice2.baseStoredDevice), true);
expect(HiveProxy.getAll<BaseStoredDevice>(devicesBox).length, 2);
expect(HiveProxy.getAll<BaseStoredDevice>(devicesBox).contains(baseStatefulDevice2.baseStoredDevice), true);

BaseAction baseAction = BaseAction(name: "name", deviceCategory: [DeviceType.tail], actionCategory: ActionCategory.hidden, uuid: "uuid");
BaseAction baseAction2 = BaseAction(name: "name1", deviceCategory: [DeviceType.ears], actionCategory: ActionCategory.hidden, uuid: "uuid1");
Expand Down
4 changes: 2 additions & 2 deletions test/Backend/move_lists_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ void main() {
await container.read(moveListsProvider.notifier).add(moveList);
expect(container.read(moveListsProvider).length, 1);
expect(container.read(moveListsProvider).first, moveList);
expect(HiveProxy.getAll<MoveList>('sequences').length, 1);
expect(HiveProxy.getAll<MoveList>(sequencesBox).length, 1);
//verify movelists are read again from hive
container.invalidate(moveListsProvider);
expect(container.read(moveListsProvider).length, 1);
expect(container.read(moveListsProvider).first, moveList);

await container.read(moveListsProvider.notifier).remove(moveList);
expect(container.read(moveListsProvider).isEmpty, true);
expect(HiveProxy.getAll<MoveList>('sequences').isEmpty, true);
expect(HiveProxy.getAll<MoveList>(sequencesBox).isEmpty, true);
});
});
test('Editing Move List record', () {
Expand Down
7 changes: 4 additions & 3 deletions test/testing_utils/gear_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:tail_app/Backend/Bluetooth/bluetooth_manager_plus.dart';
import 'package:tail_app/Backend/Definitions/Device/device_definition.dart';
import 'package:tail_app/Backend/LoggingWrappers.dart';
import 'package:tail_app/Backend/device_registry.dart';
import 'package:tail_app/constants.dart';

Future<BaseStatefulDevice> createAndStoreGear(String gearBtName, ProviderContainer ref, {String gearMacPrefix = 'Dev'}) async {
BaseDeviceDefinition baseDeviceDefinition = DeviceRegistry.getByName(gearBtName)!;
Expand All @@ -26,12 +27,12 @@ Future<ProviderContainer> testGearAdd(String name, {String gearMacPrefix = 'DEV'
overrides: [],
);
expect(container.read(knownDevicesProvider).length, 0);
expect(HiveProxy.getAll<BaseStoredDevice>('devices').length, 0);
expect(HiveProxy.getAll<BaseStoredDevice>(devicesBox).length, 0);
BaseStatefulDevice baseStatefulDevice = await createAndStoreGear(name, container, gearMacPrefix: gearMacPrefix);
expect(baseStatefulDevice.baseDeviceDefinition.btName, name);
expect(container.read(knownDevicesProvider).length, 1);
expect(container.read(knownDevicesProvider).values.first, baseStatefulDevice);
expect(HiveProxy.getAll<BaseStoredDevice>('devices').length, 1);
expect(HiveProxy.getAll<BaseStoredDevice>('devices').first, baseStatefulDevice.baseStoredDevice);
expect(HiveProxy.getAll<BaseStoredDevice>(devicesBox).length, 1);
expect(HiveProxy.getAll<BaseStoredDevice>(devicesBox).first, baseStatefulDevice.baseStoredDevice);
return container;
}

0 comments on commit 144df86

Please sign in to comment.