Refactor device control logic and add temperature and fan speed enums

- Refactor device control logic in the app to improve readability and maintainability.
- Add temperature modes (hot, cold, wind) and fan speeds (auto, low, middle, high) enums.
- Update icon mappings and utility functions for temperature modes and fan speeds.
This commit is contained in:
Mohammad Salameh
2024-04-03 18:54:21 +03:00
parent 6577652702
commit bff4b9493c
30 changed files with 183 additions and 107 deletions

View File

@ -1,5 +1,6 @@
//ignore_for_file: constant_identifier_names
import 'package:syncrow_app/features/devices/model/function_model.dart';
import 'package:syncrow_app/generated/assets.dart';
abstract class Constants {
static const String languageCode = "en";
@ -129,3 +130,71 @@ Map<DeviceType, List<FunctionModel>> devicesFunctionsMap = {
values: '{"unit":"s","min":0,"max":43200,"scale":0,"step":1}'),
],
};
enum TempModes { hot, cold, wind }
enum FanSpeeds { auto, low, middle, high }
final Map<FanSpeeds, String> fanSpeedsIconMap = {
FanSpeeds.auto: Assets.iconsFan0,
FanSpeeds.low: Assets.iconsFan1,
FanSpeeds.middle: Assets.iconsFan2,
FanSpeeds.high: Assets.iconsFan3,
};
final Map<TempModes, String> tempModesIconMap = {
TempModes.hot: Assets.iconsSunnyMode,
TempModes.cold: Assets.iconsColdMode,
TempModes.wind: Assets.iconsWindyMode,
};
final Map<String, FanSpeeds> fanSpeedsMap = {
'auto': FanSpeeds.auto,
'low': FanSpeeds.low,
'middle': FanSpeeds.middle,
'high': FanSpeeds.high,
};
final Map<String, TempModes> tempModesMap = {
'hot': TempModes.hot,
'cold': TempModes.cold,
'wind': TempModes.wind,
};
final Map<FanSpeeds, String> reversedFanSpeedsMap =
fanSpeedsMap.map((key, value) => MapEntry(value, key));
final Map<TempModes, String> reversedTempModesMap =
tempModesMap.map((key, value) => MapEntry(value, key));
String getNextFanSpeedKey(FanSpeeds currentFanSpeed) {
const List<FanSpeeds> speeds = FanSpeeds.values;
final int currentIndex = speeds.indexOf(currentFanSpeed);
// Return null if currentFanSpeed is the last value
if (currentIndex == speeds.length - 1) {
return reversedFanSpeedsMap[FanSpeeds.auto]!;
}
final FanSpeeds nextSpeed = speeds[currentIndex + 1];
return reversedFanSpeedsMap[nextSpeed]!;
}
K? getNextItem<K, V>(Map<K, V> map, V value) {
// Iterate over the map entries
for (var entry in map.entries) {
// If the current value matches the provided value
if (entry.value == value) {
// Get the index of the current entry
final index = map.keys.toList().indexOf(entry.key);
// If the index is not the last item, return the next item
if (index < map.length - 1) {
return map.keys.elementAt(index + 1);
}
// If it's the last item, return null
return map.keys.elementAt(0);
}
}
// If the value is not found, return null
return null;
}