diff --git a/assets/icons/functions_icons/sensitivity.svg b/assets/icons/functions_icons/sensitivity.svg
new file mode 100644
index 0000000..b75ebd3
--- /dev/null
+++ b/assets/icons/functions_icons/sensitivity.svg
@@ -0,0 +1,14 @@
+
diff --git a/lib/features/devices/bloc/device_manager_bloc/device_manager_bloc.dart b/lib/features/devices/bloc/device_manager_bloc/device_manager_bloc.dart
index 033e57b..f648578 100644
--- a/lib/features/devices/bloc/device_manager_bloc/device_manager_bloc.dart
+++ b/lib/features/devices/bloc/device_manager_bloc/device_manager_bloc.dart
@@ -1,3 +1,5 @@
+import 'dart:async';
+
import 'package:bloc/bloc.dart';
import 'package:flutter/material.dart';
@@ -9,7 +11,7 @@ import 'package:syncrow_app/services/api/devices_api.dart';
import 'package:syncrow_app/services/api/home_management_api.dart';
class DeviceManagerBloc extends Bloc {
- DeviceManagerBloc() : super(DeviceManagerInitial()) {
+ DeviceManagerBloc() : super(DeviceManagerState.initial()) {
on(_onFetchAllDevices);
on(_onFetchDevicesByRoomId);
on(_onSelectCategory);
@@ -18,30 +20,30 @@ class DeviceManagerBloc extends Bloc {
on(_onChangeCategorySwitchValue);
on(_onTurnOnOffDevice);
on(_onClearCategoriesSelection);
- // on(_onDeviceControl);
+ on(_getDeviceFunctions);
}
static List? allCategories;
Future _onFetchAllDevices(
FetchAllDevices event, Emitter emit) async {
- emit(GetDevicesLoading());
+ emit(state.copyWith(loading: true));
try {
final allDevices = await HomeManagementAPI.fetchDevicesByUserId();
- emit(GetDevicesSuccess(allDevices));
+ emit(state.copyWith(devices: allDevices, loading: false));
} catch (e) {
- emit(GetDevicesError(e.toString()));
+ emit(state.copyWith(error: e.toString(), loading: false));
}
}
Future _onFetchDevicesByRoomId(
FetchDevicesByRoomId event, Emitter emit) async {
- emit(GetDevicesLoading());
+ emit(state.copyWith(loading: true));
try {
final devices = await DevicesAPI.getDevicesByRoomId(event.roomId);
- emit(GetDevicesSuccess(devices));
+ emit(state.copyWith(devices: devices, loading: false));
} catch (e) {
- emit(GetDevicesError(e.toString()));
+ emit(state.copyWith(error: e.toString(), loading: false));
}
}
@@ -50,7 +52,7 @@ class DeviceManagerBloc extends Bloc {
for (var i = 0; i < allCategories!.length; i++) {
allCategories![i].isSelected = i == event.index;
}
- emit(DevicesCategoryChanged());
+ emit(state.copyWith(categoryChanged: true));
}
void _onUnselectAllCategories(
@@ -58,7 +60,7 @@ class DeviceManagerBloc extends Bloc {
for (var category in allCategories!) {
category.isSelected = false;
}
- emit(DevicesCategoryChanged());
+ emit(state.copyWith(categoryChanged: true));
}
void _onSelectDevice(SelectDevice event, Emitter emit) {
@@ -67,14 +69,14 @@ class DeviceManagerBloc extends Bloc {
for (var device in category.devices!) {
if (device.isSelected) {
category.isSelected = false;
- emit(DeviceSelected());
+ emit(state.copyWith(deviceSelected: true));
return;
}
}
}
}
event.device.isSelected = !event.device.isSelected;
- emit(DeviceSelected());
+ emit(state.copyWith(deviceSelected: true));
}
void _onChangeCategorySwitchValue(
@@ -119,28 +121,9 @@ class DeviceManagerBloc extends Bloc {
}
}
Navigator.popUntil(event.context, (route) => route.isFirst);
- emit(DevicesCategoryChanged());
+ emit(state.copyWith(categoryChanged: true)); // Set category changed state
}
- // Future _onDeviceControl(
- // DeviceControl event, Emitter emit) async {
- // emit(DeviceControlLoading(code: event.control.code));
- // try {
- // var response =
- // await DevicesAPI.controlDevice(event.control, event.deviceId);
- // if (response['success'] ?? false) {
- // emit(DeviceControlSuccess(code: event.control.code));
- // await Future.delayed(const Duration(milliseconds: 400), () {
- // add(FetchDevicesByRoomId(event.deviceId));
- // });
- // } else {
- // emit(DeviceControlError('Failed to control the device'));
- // }
- // } catch (e) {
- // emit(DeviceControlError(e.toString()));
- // }
- // }
-
void _updateDevicesStatus(
DevicesCategoryModel category, Emitter emit) {
if (category.devices != null && category.devices!.isNotEmpty) {
@@ -152,7 +135,20 @@ class DeviceManagerBloc extends Bloc {
}
category.devicesStatus = tempStatus;
}
- emit(CategorySwitchChanged());
+ emit(state.copyWith(categoryChanged: true));
+ }
+ }
+
+ Future _getDeviceFunctions(
+ DeviceFunctionsEvent event, Emitter emit) async {
+ emit(state.copyWith(functionsLoading: true));
+ try {
+ final deviceFunctions = await DevicesAPI.deviceFunctions(event.deviceId);
+
+ emit(state.copyWith(
+ functionsLoading: false, deviceFunctions: deviceFunctions));
+ } catch (e) {
+ emit(state.copyWith(functionsLoading: false, error: e.toString()));
}
}
}
diff --git a/lib/features/devices/bloc/device_manager_bloc/device_manager_event.dart b/lib/features/devices/bloc/device_manager_bloc/device_manager_event.dart
index 44de0a2..867027d 100644
--- a/lib/features/devices/bloc/device_manager_bloc/device_manager_event.dart
+++ b/lib/features/devices/bloc/device_manager_bloc/device_manager_event.dart
@@ -78,3 +78,11 @@ class DeviceControl extends DeviceManagerEvent {
@override
List