push fixes

This commit is contained in:
ashrafzarkanisala
2024-08-01 03:11:03 +03:00
parent a242876e16
commit b66fbc32e7
21 changed files with 647 additions and 331 deletions
@@ -92,30 +92,59 @@ class CreateSceneBloc extends Bloc<CreateSceneEvent, CreateSceneState>
TempHoldSceneTasksEvent event, Emitter<CreateSceneState> emit) {
emit(CreateSceneLoading());
bool updated = false;
// Check and update if the task exists in tempTasksList
for (var element in tempTasksList) {
if (element.code == event.deviceControlModel.code) {
// Update the existing function with new values
var updatedElement = element.copyWith(
operationName: event.operation,
deviceName: event.deviceName,
icon: event.icon,
code: event.deviceControlModel.code ?? '',
deviceId: event.deviceId,
functionValue: event.deviceControlModel.value,
operationDialogType: event.operationType,
operationalValues: [
SceneOperationalValue(
value: event.deviceControlModel.value,
icon: '',
),
],
);
tempTasksList[tempTasksList.indexOf(element)] = updatedElement;
selectedValues[updatedElement.code] = event.deviceControlModel.value;
updated = true;
break;
var updatedElement = element.copyWith(
operationName: event.operation,
deviceName: event.deviceName,
icon: event.icon,
code: event.deviceControlModel.code ?? '',
deviceId: event.deviceId,
functionValue: event.deviceControlModel.value,
operationDialogType: event.operationType,
operationalValues: [
SceneOperationalValue(
value: event.deviceControlModel.value,
icon: '',
),
],
);
tempTasksList[tempTasksList.indexOf(element)] = updatedElement;
selectedValues[updatedElement.code] = event.deviceControlModel.value;
updated = true;
break;
}
if (!updated) {
/// for smart scene add to view
for (var element in tasksList) {
if (element.deviceId == event.deviceControlModel.deviceId &&
element.code == event.deviceControlModel.code) {
var updatedElement = element.copyWith(
operationName: event.operation,
deviceName: event.deviceName,
icon: event.icon,
code: event.deviceControlModel.code ?? '',
deviceId: event.deviceId,
functionValue: event.deviceControlModel.value,
operationDialogType: event.operationType,
operationalValues: [
SceneOperationalValue(
value: event.deviceControlModel.value,
icon: '',
),
],
);
tasksList[tasksList.indexOf(element)] = updatedElement;
selectedValues[updatedElement.code] = event.deviceControlModel.value;
updated = true;
break;
}
}
}
// Add new element if it doesn't exist in either list
if (!updated) {
var newElement = SceneStaticFunction(
operationName: event.operation,
@@ -319,7 +348,8 @@ class CreateSceneBloc extends Bloc<CreateSceneEvent, CreateSceneState>
automationTempTasksList.clear();
automationSelectedValues.clear();
automationComparatorValues.clear();
effectiveTime = null;
effectiveTime =
EffectiveTime(start: '00:00', end: '23:59', loops: '1111111');
sceneType = CreateSceneEnum.none;
conditionRule = 'or';
emit(const CreateSceneWithTasks(success: true));
@@ -369,14 +399,31 @@ class CreateSceneBloc extends Bloc<CreateSceneEvent, CreateSceneState>
conditionRule = response.decisionExpr ?? conditionRule;
if (response.effectiveTime != null) {
BlocProvider.of<EffectPeriodBloc>(
NavigationService.navigatorKey.currentState!.context)
.add(SetCustomTime(response.effectiveTime!.start,
response.effectiveTime!.end));
BlocProvider.of<EffectPeriodBloc>(
NavigationService.navigatorKey.currentState!.context)
.add(ToggleDay(response.effectiveTime!.loops));
effectiveTime = response.effectiveTime != null
? EffectiveTime(
start: response.effectiveTime!.start,
end: response.effectiveTime!.end,
loops: response.effectiveTime!.loops,
)
: EffectiveTime(start: '00:00', end: '23:59', loops: '1111111');
// Set Custom Time and reset days first
BlocProvider.of<EffectPeriodBloc>(
NavigationService.navigatorKey.currentContext!)
.add(SetCustomTime(effectiveTime!.start, effectiveTime!.end));
// Reset all days to not selected before toggling
BlocProvider.of<EffectPeriodBloc>(
NavigationService.navigatorKey.currentContext!)
.add(ResetDays());
// Iterate over the loops and toggle each day
for (int i = 0; i < effectiveTime!.loops.length; i++) {
if (effectiveTime!.loops[i] == '1') {
BlocProvider.of<EffectPeriodBloc>(
NavigationService.navigatorKey.currentContext!)
.add(ToggleDay(_getDayFromIndex(i)));
}
}
emit(AddSceneTask(
@@ -401,6 +448,11 @@ class CreateSceneBloc extends Bloc<CreateSceneEvent, CreateSceneState>
}
}
String _getDayFromIndex(int index) {
const days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
return days[index];
}
FutureOr<void> _clearTempTaskList(
ClearTempTaskListEvent event, Emitter<CreateSceneState> emit) {
emit(CreateSceneLoading());
@@ -408,20 +460,15 @@ class CreateSceneBloc extends Bloc<CreateSceneEvent, CreateSceneState>
automationTempTasksList.clear();
automationSelectedValues.clear();
automationComparatorValues.clear();
emit(AddSceneTask(
tasksList: tasksList,
automationTasksList: automationTasksList,
condition: conditionRule,
));
} else {
tempTasksList.clear();
selectedValues.clear();
emit(AddSceneTask(
tasksList: tasksList,
automationTasksList: automationTasksList,
condition: conditionRule,
));
}
emit(AddSceneTask(
tasksList: tasksList,
automationTasksList: automationTasksList,
condition: conditionRule,
));
}
FutureOr<void> _removeFromSelectedValueById(
@@ -12,6 +12,8 @@ class EffectPeriodBloc extends Bloc<EffectPeriodEvent, EffectPeriodState> {
on<SetPeriod>(_onSetPeriod);
on<ToggleDay>(_onToggleDay);
on<SetCustomTime>(_onSetCustomTime);
on<ResetEffectivePeriod>(_onResetEffectivePeriod);
on<ResetDays>(_onResetDays);
}
void _onSetPeriod(SetPeriod event, Emitter<EffectPeriodState> emit) {
@@ -39,13 +41,15 @@ class EffectPeriodBloc extends Bloc<EffectPeriodEvent, EffectPeriodState> {
break;
}
// Update CreateSceneBloc
BlocProvider.of<CreateSceneBloc>(
NavigationService.navigatorKey.currentState!.context)
NavigationService.navigatorKey.currentContext!)
.add(EffectiveTimePeriodEvent(EffectiveTime(
start: startTime, end: endTime, loops: state.selectedDaysBinary)));
emit(state.copyWith(selectedPeriod: event.period));
emit(state.copyWith(
selectedPeriod: event.period,
customStartTime: startTime,
customEndTime: endTime));
}
void _onToggleDay(ToggleDay event, Emitter<EffectPeriodState> emit) {
@@ -60,7 +64,7 @@ class EffectPeriodBloc extends Bloc<EffectPeriodEvent, EffectPeriodState> {
emit(state.copyWith(selectedDaysBinary: newDaysBinary));
BlocProvider.of<CreateSceneBloc>(
NavigationService.navigatorKey.currentState!.context)
NavigationService.navigatorKey.currentContext!)
.add(EffectiveTimePeriodEvent(EffectiveTime(
start: state.customStartTime ?? '00:00',
end: state.customEndTime ?? '23:59',
@@ -68,15 +72,48 @@ class EffectPeriodBloc extends Bloc<EffectPeriodEvent, EffectPeriodState> {
}
void _onSetCustomTime(SetCustomTime event, Emitter<EffectPeriodState> emit) {
String startTime = event.startTime;
String endTime = event.endTime;
EnumEffectivePeriodOptions period;
// Determine the period based on start and end times
if (startTime == '00:00' && endTime == '23:59') {
period = EnumEffectivePeriodOptions.allDay;
} else if (startTime == '06:00' && endTime == '18:00') {
period = EnumEffectivePeriodOptions.daytime;
} else if (startTime == '18:00' && endTime == '06:00') {
period = EnumEffectivePeriodOptions.night;
} else {
period = EnumEffectivePeriodOptions.custom;
}
emit(state.copyWith(
customStartTime: event.startTime, customEndTime: event.endTime));
customStartTime: startTime,
customEndTime: endTime,
selectedPeriod: period));
BlocProvider.of<CreateSceneBloc>(
NavigationService.navigatorKey.currentState!.context)
NavigationService.navigatorKey.currentContext!)
.add(EffectiveTimePeriodEvent(EffectiveTime(
start: event.startTime,
end: event.endTime,
loops: state.selectedDaysBinary)));
start: startTime, end: endTime, loops: state.selectedDaysBinary)));
}
void _onResetEffectivePeriod(
ResetEffectivePeriod event, Emitter<EffectPeriodState> emit) {
emit(state.copyWith(
selectedPeriod: EnumEffectivePeriodOptions.allDay,
customStartTime: '00:00',
customEndTime: '23:59',
selectedDaysBinary: '1111111'));
BlocProvider.of<CreateSceneBloc>(
NavigationService.navigatorKey.currentContext!)
.add(EffectiveTimePeriodEvent(
EffectiveTime(start: '00:00', end: '23:59', loops: '1111111')));
}
void _onResetDays(ResetDays event, Emitter<EffectPeriodState> emit) {
emit(state.copyWith(selectedDaysBinary: '1111111'));
}
int _getDayIndex(String day) {
@@ -35,3 +35,10 @@ class SetCustomTime extends EffectPeriodEvent {
@override
List<Object> get props => [startTime, endTime];
}
class ResetEffectivePeriod extends EffectPeriodEvent {}
class ResetDays extends EffectPeriodEvent {
@override
List<Object> get props => [];
}
@@ -37,6 +37,18 @@ class EffectPeriodState extends Equatable {
);
}
EnumEffectivePeriodOptions getEffectivePeriod() {
if (customStartTime == '00:00' && customEndTime == '23:59') {
return EnumEffectivePeriodOptions.allDay;
} else if (customStartTime == '06:00' && customEndTime == '18:00') {
return EnumEffectivePeriodOptions.daytime;
} else if (customStartTime == '18:00' && customEndTime == '06:00') {
return EnumEffectivePeriodOptions.night;
} else {
return EnumEffectivePeriodOptions.custom;
}
}
@override
List<Object?> get props =>
[selectedPeriod, selectedDaysBinary, customStartTime, customEndTime];
@@ -18,6 +18,7 @@ class SmartSceneSelectBloc
SmartSceneSelectBloc() : super(SmartSceneSelectInitial()) {
on<SmartSceneEnableEvent>(_onSmartSceneEnable);
on<SmartSceneClearEvent>(_smartSceneClear);
on<SmartSceneConfirmSelectionEvent>(_smartSceneConfirmSelection);
}
SmartSceneEnable? smartSceneEnable;
@@ -25,29 +26,6 @@ class SmartSceneSelectBloc
FutureOr<void> _onSmartSceneEnable(
SmartSceneEnableEvent event, Emitter<SmartSceneSelectState> emit) {
smartSceneEnable = event.smartSceneEnable;
NavigationService.navigatorKey.currentState!.context
.read<CreateSceneBloc>()
.add(TempHoldSceneTasksEvent(
deviceControlModel: DeviceControlModel(
deviceId: smartSceneEnable?.entityId ?? '',
code: CreateSceneEnum.smartSceneSelect.name,
value: '',
),
deviceId: smartSceneEnable?.sceneORAutomationName ?? '',
operation: smartSceneEnable?.actionExecutor ?? '',
icon: smartSceneEnable?.isAutomation == true
? Assets.player
: Assets.handClickIcon,
deviceName: smartSceneEnable?.sceneORAutomationName ?? '',
uniqueId: '',
operationType: OperationDialogType.none,
isAutomation: false,
));
emit(SmartSceneSelected(smartSceneEnable: smartSceneEnable!));
NavigationService.navigatorKey.currentState!.context
.read<CreateSceneBloc>()
.add(const AddTaskEvent(isAutomation: false));
}
FutureOr<void> _smartSceneClear(
@@ -55,4 +33,33 @@ class SmartSceneSelectBloc
smartSceneEnable = null;
emit(SmartSceneSelectInitial());
}
FutureOr<void> _smartSceneConfirmSelection(
SmartSceneConfirmSelectionEvent event,
Emitter<SmartSceneSelectState> emit) {
final createSceneBloc = NavigationService.navigatorKey.currentState!.context
.read<CreateSceneBloc>();
createSceneBloc.add(TempHoldSceneTasksEvent(
deviceControlModel: DeviceControlModel(
deviceId: smartSceneEnable?.entityId ?? '',
code: CreateSceneEnum.smartSceneSelect.name,
value: smartSceneEnable?.actionExecutor ?? '',
),
deviceId: smartSceneEnable?.entityId ?? '',
operation: smartSceneEnable?.type ?? '',
icon: smartSceneEnable?.isAutomation == true
? Assets.player
: Assets.handClickIcon,
deviceName: smartSceneEnable?.sceneORAutomationName ?? '',
uniqueId: smartSceneEnable?.entityId ?? '',
operationType: OperationDialogType.none,
isAutomation: false,
));
emit(SmartSceneSelected(smartSceneEnable: smartSceneEnable!));
NavigationService.navigatorKey.currentState!.context
.read<CreateSceneBloc>()
.add(const AddTaskEvent(isAutomation: false));
}
}
@@ -19,3 +19,7 @@ class SmartSceneEnableEvent extends SmartSceneSelectEvent {
class SmartSceneClearEvent extends SmartSceneSelectEvent {
const SmartSceneClearEvent();
}
class SmartSceneConfirmSelectionEvent extends SmartSceneSelectEvent {
const SmartSceneConfirmSelectionEvent();
}