mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-11-27 05:44:55 +00:00
Fetched devices based on the unit id
This commit is contained in:
@ -25,11 +25,10 @@ class DeviceManagerBloc extends Bloc<DeviceManagerEvent, DeviceManagerState> {
|
||||
|
||||
static List<DevicesCategoryModel>? allCategories;
|
||||
|
||||
Future<void> _onFetchAllDevices(
|
||||
FetchAllDevices event, Emitter<DeviceManagerState> emit) async {
|
||||
Future<void> _onFetchAllDevices(FetchAllDevices event, Emitter<DeviceManagerState> emit) async {
|
||||
emit(state.copyWith(loading: true));
|
||||
try {
|
||||
final allDevices = await HomeManagementAPI.fetchDevicesByUserId();
|
||||
final allDevices = await HomeManagementAPI.fetchDevicesByUnitId();
|
||||
emit(state.copyWith(devices: allDevices, loading: false));
|
||||
} catch (e) {
|
||||
emit(state.copyWith(error: e.toString(), loading: false));
|
||||
@ -47,16 +46,14 @@ class DeviceManagerBloc extends Bloc<DeviceManagerEvent, DeviceManagerState> {
|
||||
}
|
||||
}
|
||||
|
||||
void _onSelectCategory(
|
||||
SelectCategory event, Emitter<DeviceManagerState> emit) {
|
||||
void _onSelectCategory(SelectCategory event, Emitter<DeviceManagerState> emit) {
|
||||
for (var i = 0; i < allCategories!.length; i++) {
|
||||
allCategories![i].isSelected = i == event.index;
|
||||
}
|
||||
emit(state.copyWith(categoryChanged: true));
|
||||
}
|
||||
|
||||
void _onUnselectAllCategories(
|
||||
UnselectAllCategories event, Emitter<DeviceManagerState> emit) {
|
||||
void _onUnselectAllCategories(UnselectAllCategories event, Emitter<DeviceManagerState> emit) {
|
||||
for (var category in allCategories!) {
|
||||
category.isSelected = false;
|
||||
}
|
||||
@ -100,8 +97,7 @@ class DeviceManagerBloc extends Bloc<DeviceManagerEvent, DeviceManagerState> {
|
||||
_updateDevicesStatus(category, emit);
|
||||
}
|
||||
|
||||
void _onTurnOnOffDevice(
|
||||
TurnOnOffDevice event, Emitter<DeviceManagerState> emit) {
|
||||
void _onTurnOnOffDevice(TurnOnOffDevice event, Emitter<DeviceManagerState> emit) {
|
||||
var device = event.device;
|
||||
device.isOnline = !device.isOnline!;
|
||||
DevicesCategoryModel category = allCategories!.firstWhere((category) {
|
||||
@ -124,8 +120,7 @@ class DeviceManagerBloc extends Bloc<DeviceManagerEvent, DeviceManagerState> {
|
||||
emit(state.copyWith(categoryChanged: true)); // Set category changed state
|
||||
}
|
||||
|
||||
void _updateDevicesStatus(
|
||||
DevicesCategoryModel category, Emitter<DeviceManagerState> emit) {
|
||||
void _updateDevicesStatus(DevicesCategoryModel category, Emitter<DeviceManagerState> emit) {
|
||||
if (category.devices != null && category.devices!.isNotEmpty) {
|
||||
bool? tempStatus = category.devices![0].isOnline;
|
||||
for (var device in category.devices!) {
|
||||
@ -145,8 +140,7 @@ class DeviceManagerBloc extends Bloc<DeviceManagerEvent, DeviceManagerState> {
|
||||
try {
|
||||
final deviceFunctions = await DevicesAPI.deviceFunctions(event.deviceId);
|
||||
|
||||
emit(state.copyWith(
|
||||
functionsLoading: false, deviceFunctions: deviceFunctions));
|
||||
emit(state.copyWith(functionsLoading: false, deviceFunctions: deviceFunctions));
|
||||
} catch (e) {
|
||||
emit(state.copyWith(functionsLoading: false, error: e.toString()));
|
||||
}
|
||||
|
||||
@ -18,15 +18,18 @@ class SceneBloc extends Bloc<SceneEvent, SceneState> {
|
||||
emit(SceneLoading());
|
||||
|
||||
try {
|
||||
final scenes = await SceneApi.getScenesByUnitId(event.unitId);
|
||||
emit(SceneLoaded(scenes));
|
||||
if (event.unitId.isNotEmpty) {
|
||||
final scenes = await SceneApi.getScenesByUnitId(event.unitId);
|
||||
emit(SceneLoaded(scenes));
|
||||
} else {
|
||||
const SceneError(message: '');
|
||||
}
|
||||
} catch (e) {
|
||||
emit(const SceneError(message: 'Something went wrong'));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onSceneTrigger(
|
||||
SceneTrigger event, Emitter<SceneState> emit) async {
|
||||
Future<void> _onSceneTrigger(SceneTrigger event, Emitter<SceneState> emit) async {
|
||||
final currentState = state;
|
||||
if (currentState is SceneLoaded) {
|
||||
emit(SceneLoaded(currentState.scenes, loadingSceneId: event.sceneId));
|
||||
|
||||
@ -16,8 +16,8 @@ class SceneView extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocProvider(
|
||||
create: (BuildContext context) => SceneBloc()
|
||||
..add(LoadScenes(HomeCubit.getInstance().selectedSpace!.id!)),
|
||||
create: (BuildContext context) =>
|
||||
SceneBloc()..add(LoadScenes(HomeCubit.getInstance().selectedSpace?.id ?? '')),
|
||||
child: BlocBuilder<CreateSceneBloc, CreateSceneState>(
|
||||
builder: (context, state) {
|
||||
if (state is DeleteSceneSuccess) {
|
||||
@ -36,8 +36,7 @@ class SceneView extends StatelessWidget {
|
||||
listener: (context, state) {
|
||||
if (state is SceneTriggerSuccess) {
|
||||
context.showCustomSnackbar(
|
||||
message:
|
||||
'Scene ${state.sceneName} triggered successfully!');
|
||||
message: 'Scene ${state.sceneName} triggered successfully!');
|
||||
}
|
||||
},
|
||||
child: HomeCubit.getInstance().spaces?.isEmpty ?? true
|
||||
|
||||
Reference in New Issue
Block a user