aboutsummaryrefslogtreecommitdiffstats
path: root/lib/data/data_providers/units_notifier.dart
diff options
context:
space:
mode:
authorLudwig Schwiedrzik <ludwig.schwiedrzik@d-fine.com>2024-08-14 11:50:33 +0200
committerScott Murray <scott.murray@konsulko.com>2024-09-12 16:58:42 +0000
commit7ea2d37528da61ff40a50da5843397d51fc0e789 (patch)
treede3b9dcafda931aa3d0297c60bc2586c89e03782 /lib/data/data_providers/units_notifier.dart
parenta2dcd701777968a65d3176eaf28aa7023d97c16b (diff)
Integration of Storage API to flutter - Users, Units
Updated user and units notifiers to make use of persistent storage. New users are stored with a name and id in the default namespace, and their unit preferences (distance, temperature, tire pressure) are stored in namespaces corresponding to each user's id. Added new initialize_settings to load user and unit settings from storage on startup. For first-time startup, default users with ids have been added to storage as well. Added unit tests for new user and unit handling. Bug-AGL: [SPEC-5228] Change-Id: I9cbcfc3ea5045dcb27db1b05e332f89abfc284a1 Signed-off-by: Tom Kronsbein <tom.kronsbein@d-fine.com> Signed-off-by: Ludwig Schwiedrzik <ludwig.schwiedrzik@d-fine.com>
Diffstat (limited to 'lib/data/data_providers/units_notifier.dart')
-rw-r--r--lib/data/data_providers/units_notifier.dart83
1 files changed, 80 insertions, 3 deletions
diff --git a/lib/data/data_providers/units_notifier.dart b/lib/data/data_providers/units_notifier.dart
index 68c9e65..daf9c92 100644
--- a/lib/data/data_providers/units_notifier.dart
+++ b/lib/data/data_providers/units_notifier.dart
@@ -1,12 +1,53 @@
import 'package:flutter_ics_homescreen/export.dart';
import 'package:protos/val_api.dart';
+import 'package:protos/storage-api.dart' as storage_api;
+import 'initialize_settings.dart';
+
class UnitsNotifier extends Notifier<Units> {
@override
Units build() {
return const Units.initial();
}
+ // Load Units state of the selected user from the storage API.
+ Future <void> loadSettingsUnits() async {
+ final storageClient = ref.read(storageClientProvider);
+ final userClient = ref.read(usersProvider);
+
+ try {
+ await initializeSettingsUser(ref);
+ } catch (e) {
+ print('Error loading settings of user: $e');
+ }
+
+ try {
+ // Read unit values from the selected user namespace.
+ final distanceResponse = await storageClient.read(storage_api.Key(key: VSSPath.vehicleHmiDistanceUnit, namespace: userClient.selectedUser.id));
+ final temperatureResponse = await storageClient.read(storage_api.Key(key: VSSPath.vehicleHmiTemperatureUnit, namespace: userClient.selectedUser.id));
+ final pressureResponse = await storageClient.read(storage_api.Key(key: VSSPath.vehicleHmiPressureUnit, namespace: userClient.selectedUser.id));
+
+ // Prepare state declaration and fall back to default values if the key is not present in the storage API.
+ final distanceUnit = distanceResponse.result == 'MILES'
+ ? DistanceUnit.miles
+ : DistanceUnit.kilometers;
+
+ final temperatureUnit = temperatureResponse.result == 'F'
+ ? TemperatureUnit.fahrenheit
+ : TemperatureUnit.celsius;
+
+ final pressureUnit = pressureResponse.result == 'PSI'
+ ? PressureUnit.psi
+ : PressureUnit.kilopascals;
+
+ state = Units(distanceUnit, temperatureUnit, pressureUnit);
+ } catch (e) {
+ // Fallback to initial defaults if error occurs.
+ print('Error loading settings for units: $e');
+ state = const Units.initial();
+ }
+ }
+
bool handleSignalUpdate(DataEntry entry) {
bool handled = true;
switch (entry.path) {
@@ -40,7 +81,7 @@ class UnitsNotifier extends Notifier<Units> {
return handled;
}
- void setDistanceUnit(DistanceUnit unit) {
+ Future <void> setDistanceUnit(DistanceUnit unit) async {
state = state.copyWith(distanceUnit: unit);
var valClient = ref.read(valClientProvider);
valClient.setString(
@@ -48,9 +89,21 @@ class UnitsNotifier extends Notifier<Units> {
unit == DistanceUnit.kilometers ? "KILOMETERS" : "MILES",
true,
);
+ // Write to storage API (to selected user namespace).
+ var storageClient = ref.read(storageClientProvider);
+ final userClient = ref.read(usersProvider);
+ try {
+ await storageClient.write(storage_api.KeyValue(
+ key: VSSPath.vehicleHmiDistanceUnit,
+ value: unit == DistanceUnit.kilometers ? 'KILOMETERS' : 'MILES',
+ namespace: userClient.selectedUser.id
+ ));
+ } catch (e) {
+ print('Error saving distance unit: $e');
+ }
}
- void setTemperatureUnit(TemperatureUnit unit) {
+ Future <void> setTemperatureUnit(TemperatureUnit unit) async {
state = state.copyWith(temperatureUnit: unit);
var valClient = ref.read(valClientProvider);
valClient.setString(
@@ -58,9 +111,21 @@ class UnitsNotifier extends Notifier<Units> {
unit == TemperatureUnit.celsius ? "C" : "F",
true,
);
+ // Write to storage API (to selected user namespace).
+ var storageClient = ref.read(storageClientProvider);
+ final userClient = ref.read(usersProvider);
+ try {
+ await storageClient.write(storage_api.KeyValue(
+ key: VSSPath.vehicleHmiTemperatureUnit,
+ value: unit == TemperatureUnit.celsius ? "C" : "F",
+ namespace: userClient.selectedUser.id
+ ));
+ } catch (e) {
+ print('Error saving distance unit: $e');
+ }
}
- void setPressureUnit(PressureUnit unit) {
+ Future <void> setPressureUnit(PressureUnit unit) async {
state = state.copyWith(pressureUnit: unit);
var valClient = ref.read(valClientProvider);
valClient.setString(
@@ -68,5 +133,17 @@ class UnitsNotifier extends Notifier<Units> {
unit == PressureUnit.kilopascals ? "KPA" : "PSI",
true,
);
+ // Write to storage API (to selected user namespace).
+ var storageClient = ref.read(storageClientProvider);
+ final userClient = ref.read(usersProvider);
+ try {
+ await storageClient.write(storage_api.KeyValue(
+ key: VSSPath.vehicleHmiPressureUnit,
+ value: unit == PressureUnit.kilopascals ? "KPA" : "PSI",
+ namespace: userClient.selectedUser.id
+ ));
+ } catch (e) {
+ print('Error saving distance unit: $e');
+ }
}
}