aboutsummaryrefslogtreecommitdiffstats
path: root/lib/data/data_providers/units_notifier.dart
diff options
context:
space:
mode:
authorScott Murray <scott.murray@konsulko.com>2024-09-24 16:10:43 -0400
committerScott Murray <scott.murray@konsulko.com>2024-09-24 16:20:40 -0400
commitbdc33c218e3e62e5a3121d3ab0de6e26ef7ad3eb (patch)
tree29c01094aafeb9aaab6d3aef04354502fefb9cd9 /lib/data/data_providers/units_notifier.dart
parent7ea2d37528da61ff40a50da5843397d51fc0e789 (diff)
Fix storage API and databroker interaction on start up
Changes: - The persisted values read in by the forced early init of the storage API were getting overridden by the defaults coming from the databroker in the case of preferences that map directly to VSS signals. Refactor the preference updating code to move the VSS updating calls to reusable functions in ValClient, and then use those to update the VSS signal values right after connecting to the databroker. - Remove the recursive initialization call from UnitsNotifier's loadSettingsUnits function, as in my testing it broke start up, and the recursion seems incorrect. There are definitely issues with racing with the storage API daemon, and the homescreen completely hangs if it is not running. The fix for that is to switch to a more Flutter typical asynchronous initialization of the storage API connection, and future rework will be in that direction. - Change various prints in the storage API code to debugPrint to start trying to clean up complaints from "flutter analyze". Bug-AGL: SPEC-5250 Change-Id: I6dc9a45569453254d1565038048305f94d121db6 Signed-off-by: Scott Murray <scott.murray@konsulko.com>
Diffstat (limited to 'lib/data/data_providers/units_notifier.dart')
-rw-r--r--lib/data/data_providers/units_notifier.dart83
1 files changed, 37 insertions, 46 deletions
diff --git a/lib/data/data_providers/units_notifier.dart b/lib/data/data_providers/units_notifier.dart
index daf9c92..9f4a1ac 100644
--- a/lib/data/data_providers/units_notifier.dart
+++ b/lib/data/data_providers/units_notifier.dart
@@ -11,21 +11,21 @@ class UnitsNotifier extends Notifier<Units> {
}
// Load Units state of the selected user from the storage API.
- Future <void> loadSettingsUnits() async {
+ 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));
+ 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'
@@ -40,10 +40,10 @@ class UnitsNotifier extends Notifier<Units> {
? PressureUnit.psi
: PressureUnit.kilopascals;
- state = Units(distanceUnit, temperatureUnit, pressureUnit);
+ state = Units(distanceUnit, temperatureUnit, pressureUnit);
} catch (e) {
// Fallback to initial defaults if error occurs.
- print('Error loading settings for units: $e');
+ debugPrint('Error loading settings for units: $e');
state = const Units.initial();
}
}
@@ -81,69 +81,60 @@ class UnitsNotifier extends Notifier<Units> {
return handled;
}
- Future <void> setDistanceUnit(DistanceUnit unit) async {
+ Future<void> setDistanceUnit(DistanceUnit unit) async {
state = state.copyWith(distanceUnit: unit);
+
var valClient = ref.read(valClientProvider);
- valClient.setString(
- VSSPath.vehicleHmiDistanceUnit,
- unit == DistanceUnit.kilometers ? "KILOMETERS" : "MILES",
- true,
- );
+ valClient.setDistanceUnit(unit);
+
// 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
- ));
+ key: VSSPath.vehicleHmiDistanceUnit,
+ value: unit == DistanceUnit.kilometers ? 'KILOMETERS' : 'MILES',
+ namespace: userClient.selectedUser.id));
} catch (e) {
- print('Error saving distance unit: $e');
+ debugPrint('Error saving distance unit: $e');
}
}
- Future <void> setTemperatureUnit(TemperatureUnit unit) async {
+ Future<void> setTemperatureUnit(TemperatureUnit unit) async {
state = state.copyWith(temperatureUnit: unit);
+
var valClient = ref.read(valClientProvider);
- valClient.setString(
- VSSPath.vehicleHmiTemperatureUnit,
- unit == TemperatureUnit.celsius ? "C" : "F",
- true,
- );
+ valClient.setTemperatureUnit(unit);
+
// 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
- ));
+ key: VSSPath.vehicleHmiTemperatureUnit,
+ value: unit == TemperatureUnit.celsius ? "C" : "F",
+ namespace: userClient.selectedUser.id));
} catch (e) {
- print('Error saving distance unit: $e');
+ debugPrint('Error saving distance unit: $e');
}
}
- Future <void> setPressureUnit(PressureUnit unit) async {
+ Future<void> setPressureUnit(PressureUnit unit) async {
state = state.copyWith(pressureUnit: unit);
+
var valClient = ref.read(valClientProvider);
- valClient.setString(
- VSSPath.vehicleHmiPressureUnit,
- unit == PressureUnit.kilopascals ? "KPA" : "PSI",
- true,
- );
+ valClient.setPressureUnit(unit);
+
// 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
- ));
+ key: VSSPath.vehicleHmiPressureUnit,
+ value: unit == PressureUnit.kilopascals ? "KPA" : "PSI",
+ namespace: userClient.selectedUser.id));
} catch (e) {
- print('Error saving distance unit: $e');
+ debugPrint('Error saving pressure unit: $e');
}
}
}