mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2026-10-24 03:37:55 +00:00
4scene
This commit is contained in:
@@ -4,10 +4,12 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:syncrow_app/features/devices/bloc/four_scene_bloc/four_scene_event.dart';
|
||||
import 'package:syncrow_app/features/devices/bloc/four_scene_bloc/four_scene_state.dart';
|
||||
import 'package:syncrow_app/features/devices/model/device_control_model.dart';
|
||||
import 'package:syncrow_app/features/devices/model/device_model.dart';
|
||||
import 'package:syncrow_app/features/devices/model/device_report_model.dart';
|
||||
import 'package:syncrow_app/features/devices/model/four_scene_model.dart';
|
||||
import 'package:syncrow_app/features/devices/model/four_scene_question_model.dart';
|
||||
import 'package:syncrow_app/features/devices/model/four_scene_switch_model.dart';
|
||||
import 'package:syncrow_app/features/devices/model/group_devices_model.dart';
|
||||
import 'package:syncrow_app/features/devices/model/scene_switch_model.dart';
|
||||
import 'package:syncrow_app/features/devices/model/status_model.dart';
|
||||
@@ -43,6 +45,8 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
|
||||
on<RemoveDeviceFromGroup>(_removeDeviceFromGroup);
|
||||
on<FourSceneInitialQuestion>(_onFourSceneInitial);
|
||||
on<FetchDeviceScene>(_fetchDeviceScene);
|
||||
on<ControlDeviceScene>(_controlDevice);
|
||||
on<FourSceneSwitchInitial>(_fetchFourSceneSwitches);
|
||||
}
|
||||
|
||||
final TextEditingController nameController =
|
||||
@@ -55,7 +59,7 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
|
||||
static String deviceName = '';
|
||||
static String selectedRoomId = '';
|
||||
|
||||
FourSceneModel deviceStatus = FourSceneModel(
|
||||
FourSceneModelState deviceStatus = FourSceneModelState(
|
||||
scene_1: '',
|
||||
scene_2: '',
|
||||
scene_3: '',
|
||||
@@ -96,28 +100,6 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
|
||||
),
|
||||
);
|
||||
|
||||
// void changeDeviceName(
|
||||
// SaveNameEvent event, Emitter<FourSceneState> emit) async {
|
||||
// emit(FourSceneLoadingState());
|
||||
// try {
|
||||
// var response = await DevicesAPI.putDeviceName(
|
||||
// deviceId: fourSceneId, deviceName: event.deviceName!);
|
||||
// List<StatusModel> statusModelList = [];
|
||||
// for (var status in response['status']) {
|
||||
// statusModelList.add(StatusModel.fromJson(status));
|
||||
// }
|
||||
// deviceStatus = FourSceneModel.fromJson(
|
||||
// statusModelList,
|
||||
// );
|
||||
// emit(UpdateState(sensor: deviceStatus));
|
||||
// Future.delayed(const Duration(milliseconds: 500));
|
||||
// // _listenToChanges();
|
||||
// } catch (e) {
|
||||
// emit(FourSceneFailedState(errorMessage: e.toString()));
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
|
||||
Future<void> saveName(
|
||||
SaveNameEvent event, Emitter<FourSceneState> emit) async {
|
||||
if (_validateInputs()) return;
|
||||
@@ -137,6 +119,34 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
|
||||
}
|
||||
}
|
||||
|
||||
List<FourSceneSwitchModel> fourScene = [];
|
||||
|
||||
void _fetchFourSceneSwitches(
|
||||
FourSceneSwitchInitial event, Emitter<FourSceneState> emit) async {
|
||||
emit(FourSceneLoadingState());
|
||||
try {
|
||||
List<dynamic> response = await DevicesAPI.getFourSceneInfo(fourSceneId);
|
||||
fourScene =
|
||||
response.map((item) => FourSceneSwitchModel.fromJson(item)).toList();
|
||||
print('Fetched response: $fourScene');
|
||||
_rankFourSceneSwitches();
|
||||
|
||||
emit(UpdateState(device: deviceStatus));
|
||||
} catch (e) {
|
||||
print('Error in _fetchFourSceneSwitches: $e');
|
||||
emit(FourSceneFailedState(errorMessage: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
void _rankFourSceneSwitches() {
|
||||
const switchOrder = ['scene_1', 'scene_2', 'scene_3', 'scene_4'];
|
||||
fourScene.sort((a, b) {
|
||||
return switchOrder
|
||||
.indexOf(a.switchName)
|
||||
.compareTo(switchOrder.indexOf(b.switchName));
|
||||
});
|
||||
}
|
||||
|
||||
void _fetchStatus(
|
||||
FourSceneInitial event, Emitter<FourSceneState> emit) async {
|
||||
emit(FourSceneLoadingState());
|
||||
@@ -146,10 +156,10 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
|
||||
for (var status in response['status']) {
|
||||
statusModelList.add(StatusModel.fromJson(status));
|
||||
}
|
||||
deviceStatus = FourSceneModel.fromJson(
|
||||
deviceStatus = FourSceneModelState.fromJson(
|
||||
statusModelList,
|
||||
);
|
||||
emit(UpdateState(sensor: deviceStatus));
|
||||
emit(UpdateState(device: deviceStatus));
|
||||
Future.delayed(const Duration(milliseconds: 500));
|
||||
// _listenToChanges();
|
||||
} catch (e) {
|
||||
@@ -167,10 +177,10 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
|
||||
for (var status in response['status']) {
|
||||
statusModelList.add(StatusModel.fromJson(status));
|
||||
}
|
||||
deviceStatus = FourSceneModel.fromJson(
|
||||
deviceStatus = FourSceneModelState.fromJson(
|
||||
statusModelList,
|
||||
);
|
||||
emit(UpdateState(sensor: deviceStatus));
|
||||
emit(UpdateState(device: deviceStatus));
|
||||
Future.delayed(const Duration(milliseconds: 500));
|
||||
// _listenToChanges();
|
||||
} catch (e) {
|
||||
@@ -179,6 +189,27 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
|
||||
}
|
||||
}
|
||||
|
||||
void _controlDevice(
|
||||
ControlDeviceScene event, Emitter<FourSceneState> emit) async {
|
||||
emit(FourSceneLoadingState());
|
||||
try {
|
||||
deviceStatus.switch_backlight = !event.backLight!;
|
||||
emit(UpdateState(device: deviceStatus));
|
||||
final response = await DevicesAPI.controlDevice(
|
||||
DeviceControlModel(
|
||||
deviceId: fourSceneId,
|
||||
code: 'switch_backlight',
|
||||
value: !event.backLight!),
|
||||
fourSceneId);
|
||||
|
||||
if (!response['success']) {
|
||||
// add(InitialEvent(groupScreen: oneTouchGroup));
|
||||
}
|
||||
} catch (_) {
|
||||
// add(InitialEvent(groupScreen: oneTouchGroup));
|
||||
}
|
||||
}
|
||||
|
||||
Future fetchDeviceData(
|
||||
FourSceneInitialInfo event, Emitter<FourSceneState> emit) async {
|
||||
emit(FourSceneLoadingState());
|
||||
@@ -213,10 +244,10 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
|
||||
|
||||
void _toggleNotification(
|
||||
ToggleNotificationEvent event, Emitter<FourSceneState> emit) async {
|
||||
emit(LoadingNewSate(sosSensor: deviceStatus));
|
||||
emit(LoadingNewSate(device: deviceStatus));
|
||||
try {
|
||||
closingReminder = event.isClosingEnabled;
|
||||
emit(UpdateState(sensor: deviceStatus));
|
||||
emit(UpdateState(device: deviceStatus));
|
||||
// API call to update the state, if necessary
|
||||
// await DevicesAPI.controlDevice(
|
||||
// DeviceControlModel(
|
||||
@@ -249,7 +280,7 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
|
||||
code: 'sossensor_state',
|
||||
);
|
||||
recordGroups = response;
|
||||
emit(UpdateState(sensor: deviceStatus));
|
||||
emit(UpdateState(device: deviceStatus));
|
||||
} on DioException catch (e) {
|
||||
final errorData = e.response!.data;
|
||||
String errorMessage = errorData['message'];
|
||||
@@ -304,7 +335,6 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
|
||||
emit(FourSceneLoadingState());
|
||||
roomsList = await SpacesAPI.getSubSpaceBySpaceId(
|
||||
event.unit.community.uuid, event.unit.id);
|
||||
print(roomsList);
|
||||
emit(FetchRoomsState(devicesList: allDevices, roomsList: roomsList));
|
||||
} catch (e) {
|
||||
emit(const FourSceneFailedState(errorMessage: 'Something went wrong'));
|
||||
@@ -313,11 +343,26 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
|
||||
}
|
||||
|
||||
bool switchStatus = true;
|
||||
void changeSwitchStatus(
|
||||
ChangeSwitchStatusEvent event, Emitter<FourSceneState> emit) {
|
||||
emit(FourSceneLoadingState());
|
||||
switchStatus = !switchStatus;
|
||||
emit(ChangeSwitchState(isEnable: switchStatus));
|
||||
Future<void> changeSwitchStatus(
|
||||
ChangeSwitchStatusEvent event, Emitter<FourSceneState> emit) async {
|
||||
try {
|
||||
emit(FourSceneLoadingState());
|
||||
switchStatus = deviceStatus.switch_backlight;
|
||||
switchStatus = !switchStatus;
|
||||
final response = await DevicesAPI.controlDevice(
|
||||
DeviceControlModel(
|
||||
deviceId: fourSceneId,
|
||||
code: 'switch_backlight',
|
||||
value: switchStatus),
|
||||
fourSceneId);
|
||||
deviceStatus.switch_backlight = switchStatus;
|
||||
if (!response['success']) {
|
||||
add(const FourSceneInitial());
|
||||
}
|
||||
emit(ChangeSwitchState(isEnable: switchStatus));
|
||||
} catch (_) {
|
||||
add(const FourSceneInitial());
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onLoadScenes(
|
||||
@@ -385,19 +430,6 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
|
||||
dec: 'Syncroom', icon: Assets.addDevicesIcon, name: '6 Scene Switch')
|
||||
];
|
||||
|
||||
// @override
|
||||
// Stream<FourSceneState> mapEventToState(FourSceneEvent event) async* {
|
||||
// if (event is AddDeviceToGroup) {
|
||||
// devices.remove(event.device);
|
||||
// groupDevices.add(event.device);
|
||||
// yield UpdateStateList(groupDevices: groupDevices, devices: devices);
|
||||
// } else if (event is RemoveDeviceFromGroup) {
|
||||
// groupDevices.remove(event.device);
|
||||
// devices.add(event.device);
|
||||
// yield UpdateStateList(groupDevices: groupDevices, devices: devices);
|
||||
// }
|
||||
// }
|
||||
|
||||
// Handler for AddDeviceToGroup
|
||||
void _addDeviceToGroup(AddDeviceToGroup event, Emitter<FourSceneState> emit) {
|
||||
devices.remove(event.device);
|
||||
@@ -466,4 +498,9 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
String scene1 = 'scene_1';
|
||||
String scene2 = 'scene_2';
|
||||
String scene3 = 'scene_3';
|
||||
String scene4 = 'scene_4';
|
||||
}
|
||||
|
||||
@@ -25,7 +25,9 @@ class FourSceneSwitch extends FourSceneEvent {
|
||||
|
||||
class FourSceneUpdated extends FourSceneEvent {}
|
||||
|
||||
class FourSceneInitialInfo extends FourSceneEvent {}
|
||||
class FourSceneInitialInfo extends FourSceneEvent {
|
||||
const FourSceneInitialInfo();
|
||||
}
|
||||
|
||||
class FourSceneInitial extends FourSceneEvent {
|
||||
const FourSceneInitial();
|
||||
@@ -200,6 +202,13 @@ class AssignRoomEvent extends FourSceneEvent {
|
||||
];
|
||||
}
|
||||
|
||||
class FetchDeviceScene extends FourSceneEvent {
|
||||
const FetchDeviceScene();
|
||||
class FetchDeviceScene extends FourSceneEvent {}
|
||||
|
||||
class ControlDeviceScene extends FourSceneEvent {
|
||||
final bool? backLight;
|
||||
const ControlDeviceScene({this.backLight});
|
||||
}
|
||||
|
||||
class FourSceneSwitchInitial extends FourSceneEvent {
|
||||
const FourSceneSwitchInitial();
|
||||
}
|
||||
|
||||
@@ -36,19 +36,19 @@ class FourSceneFailedState extends FourSceneState {
|
||||
}
|
||||
|
||||
class UpdateState extends FourSceneState {
|
||||
final FourSceneModel sensor;
|
||||
const UpdateState({required this.sensor});
|
||||
final FourSceneModelState device;
|
||||
const UpdateState({required this.device});
|
||||
|
||||
@override
|
||||
List<Object> get props => [sensor];
|
||||
List<Object> get props => [device];
|
||||
}
|
||||
|
||||
class LoadingNewSate extends FourSceneState {
|
||||
final FourSceneModel sosSensor;
|
||||
const LoadingNewSate({required this.sosSensor});
|
||||
final FourSceneModelState device;
|
||||
const LoadingNewSate({required this.device});
|
||||
|
||||
@override
|
||||
List<Object> get props => [sosSensor];
|
||||
List<Object> get props => [device];
|
||||
}
|
||||
|
||||
class NameEditingState extends FourSceneState {
|
||||
|
||||
Reference in New Issue
Block a user