curtain wizard

This commit is contained in:
mohammad
2024-10-08 17:02:24 +03:00
parent f2172bd6e8
commit e0049c0aec
16 changed files with 412 additions and 15 deletions
@@ -1,7 +1,11 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
import 'package:syncrow_app/features/devices/bloc/curtain_bloc/curtain_event.dart';
import 'package:syncrow_app/features/devices/bloc/curtain_bloc/curtain_state.dart';
import 'package:syncrow_app/features/devices/model/curtain_model.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/group_curtain_model.dart';
import 'package:syncrow_app/features/devices/model/status_model.dart';
import 'package:syncrow_app/services/api/devices_api.dart';
import 'package:syncrow_app/utils/resource_manager/constants.dart';
@@ -22,9 +26,14 @@ class CurtainBloc extends Bloc<CurtainEvent, CurtainState> {
on<OpenCurtain>(_onOpenCurtain);
on<CloseCurtain>(_onCloseCurtain);
on<PauseCurtain>(_onPauseCurtain);
on<ChangeFirstWizardSwitchStatusEvent>(_changeFirstWizardSwitch);
on<InitialWizardEvent>(_fetchOneTouchWizardStatus);
on<GroupAllOffEvent>(_groupAllOff);
on<GroupAllOnEvent>(_groupAllOn);
}
Future<void> _onOpenCurtain(OpenCurtain event, Emitter<CurtainState> emit) async {
Future<void> _onOpenCurtain(
OpenCurtain event, Emitter<CurtainState> emit) async {
isMoving = true;
while (openPercentage < 100.0) {
if (state is CurtainsClosing) {
@@ -71,7 +80,8 @@ class CurtainBloc extends Bloc<CurtainEvent, CurtainState> {
}
}
Future<void> _onCloseCurtain(CloseCurtain event, Emitter<CurtainState> emit) async {
Future<void> _onCloseCurtain(
CloseCurtain event, Emitter<CurtainState> emit) async {
isMoving = true;
while (openPercentage > 0.0) {
if (state is CurtainsOpening) {
@@ -118,7 +128,8 @@ class CurtainBloc extends Bloc<CurtainEvent, CurtainState> {
}
}
Future<void> _onPauseCurtain(PauseCurtain event, Emitter<CurtainState> emit) async {
Future<void> _onPauseCurtain(
PauseCurtain event, Emitter<CurtainState> emit) async {
_pauseCurtain(emit);
await DevicesAPI.controlDevice(
DeviceControlModel(
@@ -172,4 +183,157 @@ class CurtainBloc extends Bloc<CurtainEvent, CurtainState> {
return;
}
}
List<GroupCurtainModel> groupList = [];
bool allSwitchesOn = true;
List<DeviceModel> devicesList = [];
CurtainModel deviceStatus = CurtainModel(
control: 'stop',
);
void _fetchOneTouchWizardStatus(
InitialWizardEvent event, Emitter<CurtainState> emit) async {
emit(CurtainLoadingState());
try {
devicesList = [];
groupList = [];
allSwitchesOn = true;
devicesList = await DevicesAPI.getDeviceByGroupName(
HomeCubit.getInstance().selectedSpace?.id ?? '', 'CUR');
for (int i = 0; i < devicesList.length; i++) {
var response =
await DevicesAPI.getDeviceStatus(devicesList[i].uuid ?? '');
List<StatusModel> statusModelList = [];
for (var status in response['status']) {
statusModelList.add(StatusModel.fromJson(status));
}
deviceStatus = CurtainModel.fromJson(statusModelList);
groupList.add(GroupCurtainModel(
deviceId: devicesList[i].uuid ?? '',
deviceName: devicesList[i].name ?? '',
firstSwitch: deviceStatus.control,
percentControl: 0));
}
if (groupList.isNotEmpty) {
groupList.firstWhere((element) {
print('object=====${element.firstSwitch}');
if (element.firstSwitch == 'close') {
allSwitchesOn = false;
}
return true;
});
}
emit(
UpdateGroupState(curtainList: groupList, allSwitches: allSwitchesOn));
} catch (e) {
emit(FailedState());
return;
}
}
void _changeFirstWizardSwitch(ChangeFirstWizardSwitchStatusEvent event,
Emitter<CurtainState> emit) async {
emit(LoadingNewSate(curtainModel: deviceStatus));
try {
bool allSwitchesValue = true;
// Update the firstSwitch value in the groupList based on the deviceId
groupList.forEach((element) {
if (element.deviceId == event.deviceId) {
element.firstSwitch = event.value; // Set the new value from the event
}
if (element.firstSwitch != 'open') {
allSwitchesValue = false; // Check if any switch is not 'open'
}
});
final response = await DevicesAPI.deviceBatchController(
code: 'control',
devicesUuid: [event.deviceId],
value: event.value, // Use the value from the event
);
emit(UpdateGroupState(
curtainList: groupList, allSwitches: allSwitchesValue));
if (response['success']) {
// Optionally add an initial event if needed.
}
} catch (_) {
// Handle the error if needed.
}
}
void _groupAllOn(GroupAllOnEvent event, Emitter<CurtainState> emit) async {
emit(LoadingNewSate(curtainModel: deviceStatus));
try {
// Set all devices to 'open'
for (int i = 0; i < groupList.length; i++) {
groupList[i].firstSwitch = 'open';
}
emit(UpdateGroupState(curtainList: groupList, allSwitches: true));
List<String> allDeviceIds =
groupList.map((device) => device.deviceId).toList();
final response1 = await DevicesAPI.deviceBatchController(
code: 'control',
devicesUuid: allDeviceIds,
value: 'open', // Set the devices to 'open'
);
final response2 = await DevicesAPI.deviceBatchController(
code: 'control',
devicesUuid: allDeviceIds,
value: 'open',
);
if (response1['failedResults'].toString() != '[]') {
await Future.delayed(const Duration(milliseconds: 500));
// Handle retry or error if needed.
}
} catch (_) {
emit(FailedState());
await Future.delayed(const Duration(milliseconds: 500));
// Optionally add an initial event if needed.
}
}
void _groupAllOff(GroupAllOffEvent event, Emitter<CurtainState> emit) async {
emit(LoadingNewSate(curtainModel: deviceStatus));
try {
// Set all devices to 'close'
for (int i = 0; i < groupList.length; i++) {
groupList[i].firstSwitch = 'close';
}
emit(UpdateGroupState(curtainList: groupList, allSwitches: false));
List<String> allDeviceIds =
groupList.map((device) => device.deviceId).toList();
final response1 = await DevicesAPI.deviceBatchController(
code: 'control',
devicesUuid: allDeviceIds,
value: 'close', // Set the devices to 'close'
);
final response2 = await DevicesAPI.deviceBatchController(
code: 'control',
devicesUuid: allDeviceIds,
value: 'close',
);
if (response1['failedResults'].toString() != '[]') {
await Future.delayed(const Duration(milliseconds: 500));
// Handle retry or error if needed.
}
} catch (_) {
emit(FailedState());
await Future.delayed(const Duration(milliseconds: 500));
// Optionally add an initial event if needed.
}
}
}
@@ -31,4 +31,21 @@ class CloseCurtain extends CurtainEvent {
class InitCurtain extends CurtainEvent {}
class PauseCurtain extends CurtainEvent {}
class useCurtainEvent extends CurtainEvent {}
class useCurtainEvent extends CurtainEvent {}
class InitialWizardEvent extends CurtainEvent {}
class ChangeFirstWizardSwitchStatusEvent extends CurtainEvent {
final String value;
final String deviceId;
const ChangeFirstWizardSwitchStatusEvent(
{required this.value, this.deviceId = ''});
@override
List<Object> get props => [value, deviceId];
}
class GroupAllOnEvent extends CurtainEvent {}
class GroupAllOffEvent extends CurtainEvent {}
@@ -1,5 +1,7 @@
// curtain_state.dart
import 'package:equatable/equatable.dart';
import 'package:syncrow_app/features/devices/model/curtain_model.dart';
import 'package:syncrow_app/features/devices/model/group_curtain_model.dart';
abstract class CurtainState extends Equatable {
const CurtainState();
@@ -10,8 +12,9 @@ abstract class CurtainState extends Equatable {
class CurtainInitial extends CurtainState {}
class UpdateCurtain extends CurtainState {
class LoadingInitialState extends CurtainState {}
class UpdateCurtain extends CurtainState {
final double curtainWidth;
final double blindHeight;
final double openPercentage;
@@ -73,4 +76,23 @@ class CurtainsPaused extends CurtainState {
@override
List<Object?> get props => [curtainWidth, blindHeight, openPercentage];
}
}
class UpdateGroupState extends CurtainState {
final List<GroupCurtainModel> curtainList;
final bool allSwitches;
const UpdateGroupState(
{required this.curtainList, required this.allSwitches});
@override
List<Object> get props => [curtainList, allSwitches];
}
class LoadingNewSate extends CurtainState {
final CurtainModel curtainModel;
const LoadingNewSate({required this.curtainModel});
@override
List<Object> get props => [curtainModel];
}