aboutsummaryrefslogtreecommitdiffstats
path: root/lib/data/data_providers/units_notifier.dart
blob: 9f4a1ac7a2a7f26b91499dac32700a84989a6595 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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 {
      // 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.
      debugPrint('Error loading settings for units: $e');
      state = const Units.initial();
    }
  }

  bool handleSignalUpdate(DataEntry entry) {
    bool handled = true;
    switch (entry.path) {
      case VSSPath.vehicleHmiDistanceUnit:
        if (entry.value.hasString()) {
          String value = entry.value.string;
          DistanceUnit unit = DistanceUnit.kilometers;
          if (value != "KILOMETERS") unit = DistanceUnit.miles;
          state = state.copyWith(distanceUnit: unit);
        }
        break;
      case VSSPath.vehicleHmiTemperatureUnit:
        if (entry.value.hasString()) {
          String value = entry.value.string;
          TemperatureUnit unit = TemperatureUnit.celsius;
          if (value != "C") unit = TemperatureUnit.fahrenheit;
          state = state.copyWith(temperatureUnit: unit);
        }
        break;
      case VSSPath.vehicleHmiPressureUnit:
        if (entry.value.hasString()) {
          String value = entry.value.string;
          PressureUnit unit = PressureUnit.kilopascals;
          if (value != "KPA") unit = PressureUnit.psi;
          state = state.copyWith(pressureUnit: unit);
        }
        break;
      default:
        handled = false;
    }
    return handled;
  }

  Future<void> setDistanceUnit(DistanceUnit unit) async {
    state = state.copyWith(distanceUnit: unit);

    var valClient = ref.read(valClientProvider);
    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));
    } catch (e) {
      debugPrint('Error saving distance unit: $e');
    }
  }

  Future<void> setTemperatureUnit(TemperatureUnit unit) async {
    state = state.copyWith(temperatureUnit: unit);

    var valClient = ref.read(valClientProvider);
    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));
    } catch (e) {
      debugPrint('Error saving distance unit: $e');
    }
  }

  Future<void> setPressureUnit(PressureUnit unit) async {
    state = state.copyWith(pressureUnit: unit);

    var valClient = ref.read(valClientProvider);
    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));
    } catch (e) {
      debugPrint('Error saving pressure unit: $e');
    }
  }
}