mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
@ -13,6 +13,7 @@ import 'package:syncrow_web/pages/routiens/models/routine_model.dart';
|
|||||||
import 'package:syncrow_web/services/devices_mang_api.dart';
|
import 'package:syncrow_web/services/devices_mang_api.dart';
|
||||||
import 'package:syncrow_web/services/routines_api.dart';
|
import 'package:syncrow_web/services/routines_api.dart';
|
||||||
import 'package:syncrow_web/utils/constants/assets.dart';
|
import 'package:syncrow_web/utils/constants/assets.dart';
|
||||||
|
import 'package:syncrow_web/utils/snack_bar.dart';
|
||||||
import 'package:uuid/uuid.dart';
|
import 'package:uuid/uuid.dart';
|
||||||
|
|
||||||
part 'routine_event.dart';
|
part 'routine_event.dart';
|
||||||
@ -44,16 +45,17 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
on<FetchDevicesInRoutine>(_fetchDevices);
|
on<FetchDevicesInRoutine>(_fetchDevices);
|
||||||
on<UpdateScene>(_onUpdateScene);
|
on<UpdateScene>(_onUpdateScene);
|
||||||
on<UpdateAutomation>(_onUpdateAutomation);
|
on<UpdateAutomation>(_onUpdateAutomation);
|
||||||
on<SetAutomationActionExecutor>(_onSetAutomationActionExecutor);
|
|
||||||
on<TriggerSwitchTabsEvent>(_triggerSwitchTabsEvent);
|
on<TriggerSwitchTabsEvent>(_triggerSwitchTabsEvent);
|
||||||
on<CreateNewRoutineViewEvent>(_createNewRoutineViewEvent);
|
on<CreateNewRoutineViewEvent>(_createNewRoutineViewEvent);
|
||||||
|
on<ResetErrorMessage>(_resetErrorMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureOr<void> _triggerSwitchTabsEvent(
|
FutureOr<void> _triggerSwitchTabsEvent(
|
||||||
TriggerSwitchTabsEvent event,
|
TriggerSwitchTabsEvent event,
|
||||||
Emitter<RoutineState> emit,
|
Emitter<RoutineState> emit,
|
||||||
) {
|
) {
|
||||||
emit(state.copyWith(routineTab: event.isRoutineTab, createRoutineView: false));
|
emit(state.copyWith(
|
||||||
|
routineTab: event.isRoutineTab, createRoutineView: false));
|
||||||
add(ResetRoutineState());
|
add(ResetRoutineState());
|
||||||
if (event.isRoutineTab) {
|
if (event.isRoutineTab) {
|
||||||
add(const LoadScenes(spaceId, communityId));
|
add(const LoadScenes(spaceId, communityId));
|
||||||
@ -61,6 +63,13 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_resetErrorMessage(
|
||||||
|
ResetErrorMessage event,
|
||||||
|
Emitter<RoutineState> emit,
|
||||||
|
) {
|
||||||
|
emit(state.copyWith(errorMessage: ''));
|
||||||
|
}
|
||||||
|
|
||||||
FutureOr<void> _createNewRoutineViewEvent(
|
FutureOr<void> _createNewRoutineViewEvent(
|
||||||
CreateNewRoutineViewEvent event,
|
CreateNewRoutineViewEvent event,
|
||||||
Emitter<RoutineState> emit,
|
Emitter<RoutineState> emit,
|
||||||
@ -72,8 +81,8 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
final updatedIfItems = List<Map<String, dynamic>>.from(state.ifItems);
|
final updatedIfItems = List<Map<String, dynamic>>.from(state.ifItems);
|
||||||
|
|
||||||
// Find the index of the item in teh current itemsList
|
// Find the index of the item in teh current itemsList
|
||||||
int index =
|
int index = updatedIfItems.indexWhere(
|
||||||
updatedIfItems.indexWhere((map) => map['uniqueCustomId'] == event.item['uniqueCustomId']);
|
(map) => map['uniqueCustomId'] == event.item['uniqueCustomId']);
|
||||||
// Replace the map if the index is valid
|
// Replace the map if the index is valid
|
||||||
if (index != -1) {
|
if (index != -1) {
|
||||||
updatedIfItems[index] = event.item;
|
updatedIfItems[index] = event.item;
|
||||||
@ -82,18 +91,21 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (event.isTabToRun) {
|
if (event.isTabToRun) {
|
||||||
emit(state.copyWith(ifItems: updatedIfItems, isTabToRun: true, isAutomation: false));
|
emit(state.copyWith(
|
||||||
|
ifItems: updatedIfItems, isTabToRun: true, isAutomation: false));
|
||||||
} else {
|
} else {
|
||||||
emit(state.copyWith(ifItems: updatedIfItems, isTabToRun: false, isAutomation: true));
|
emit(state.copyWith(
|
||||||
|
ifItems: updatedIfItems, isTabToRun: false, isAutomation: true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onAddToThenContainer(AddToThenContainer event, Emitter<RoutineState> emit) {
|
void _onAddToThenContainer(
|
||||||
|
AddToThenContainer event, Emitter<RoutineState> emit) {
|
||||||
final currentItems = List<Map<String, dynamic>>.from(state.thenItems);
|
final currentItems = List<Map<String, dynamic>>.from(state.thenItems);
|
||||||
|
|
||||||
// Find the index of the item in teh current itemsList
|
// Find the index of the item in teh current itemsList
|
||||||
int index =
|
int index = currentItems.indexWhere(
|
||||||
currentItems.indexWhere((map) => map['uniqueCustomId'] == event.item['uniqueCustomId']);
|
(map) => map['uniqueCustomId'] == event.item['uniqueCustomId']);
|
||||||
// Replace the map if the index is valid
|
// Replace the map if the index is valid
|
||||||
if (index != -1) {
|
if (index != -1) {
|
||||||
currentItems[index] = event.item;
|
currentItems[index] = event.item;
|
||||||
@ -104,22 +116,26 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
emit(state.copyWith(thenItems: currentItems));
|
emit(state.copyWith(thenItems: currentItems));
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onAddFunctionsToRoutine(AddFunctionToRoutine event, Emitter<RoutineState> emit) {
|
void _onAddFunctionsToRoutine(
|
||||||
|
AddFunctionToRoutine event, Emitter<RoutineState> emit) {
|
||||||
try {
|
try {
|
||||||
if (event.functions.isEmpty) return;
|
if (event.functions.isEmpty) return;
|
||||||
|
|
||||||
List<DeviceFunctionData> selectedFunction = List<DeviceFunctionData>.from(event.functions);
|
List<DeviceFunctionData> selectedFunction =
|
||||||
|
List<DeviceFunctionData>.from(event.functions);
|
||||||
|
|
||||||
Map<String, List<DeviceFunctionData>> currentSelectedFunctions =
|
Map<String, List<DeviceFunctionData>> currentSelectedFunctions =
|
||||||
Map<String, List<DeviceFunctionData>>.from(state.selectedFunctions);
|
Map<String, List<DeviceFunctionData>>.from(state.selectedFunctions);
|
||||||
if (currentSelectedFunctions.containsKey(event.uniqueCustomId)) {
|
if (currentSelectedFunctions.containsKey(event.uniqueCustomId)) {
|
||||||
List<DeviceFunctionData> currentFunctions =
|
List<DeviceFunctionData> currentFunctions =
|
||||||
List<DeviceFunctionData>.from(currentSelectedFunctions[event.uniqueCustomId] ?? []);
|
List<DeviceFunctionData>.from(
|
||||||
|
currentSelectedFunctions[event.uniqueCustomId] ?? []);
|
||||||
|
|
||||||
List<String> functionCode = [];
|
List<String> functionCode = [];
|
||||||
for (int i = 0; i < selectedFunction.length; i++) {
|
for (int i = 0; i < selectedFunction.length; i++) {
|
||||||
for (int j = 0; j < currentFunctions.length; j++) {
|
for (int j = 0; j < currentFunctions.length; j++) {
|
||||||
if (selectedFunction[i].functionCode == currentFunctions[j].functionCode) {
|
if (selectedFunction[i].functionCode ==
|
||||||
|
currentFunctions[j].functionCode) {
|
||||||
currentFunctions[j] = selectedFunction[i];
|
currentFunctions[j] = selectedFunction[i];
|
||||||
if (!functionCode.contains(currentFunctions[j].functionCode)) {
|
if (!functionCode.contains(currentFunctions[j].functionCode)) {
|
||||||
functionCode.add(currentFunctions[j].functionCode);
|
functionCode.add(currentFunctions[j].functionCode);
|
||||||
@ -129,13 +145,15 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < functionCode.length; i++) {
|
for (int i = 0; i < functionCode.length; i++) {
|
||||||
selectedFunction.removeWhere((code) => code.functionCode == functionCode[i]);
|
selectedFunction
|
||||||
|
.removeWhere((code) => code.functionCode == functionCode[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
currentSelectedFunctions[event.uniqueCustomId] = List.from(currentFunctions)
|
currentSelectedFunctions[event.uniqueCustomId] =
|
||||||
..addAll(selectedFunction);
|
List.from(currentFunctions)..addAll(selectedFunction);
|
||||||
} else {
|
} else {
|
||||||
currentSelectedFunctions[event.uniqueCustomId] = List.from(event.functions);
|
currentSelectedFunctions[event.uniqueCustomId] =
|
||||||
|
List.from(event.functions);
|
||||||
}
|
}
|
||||||
|
|
||||||
emit(state.copyWith(selectedFunctions: currentSelectedFunctions));
|
emit(state.copyWith(selectedFunctions: currentSelectedFunctions));
|
||||||
@ -144,11 +162,13 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _onLoadScenes(LoadScenes event, Emitter<RoutineState> emit) async {
|
Future<void> _onLoadScenes(
|
||||||
|
LoadScenes event, Emitter<RoutineState> emit) async {
|
||||||
emit(state.copyWith(isLoading: true, errorMessage: null));
|
emit(state.copyWith(isLoading: true, errorMessage: null));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final scenes = await SceneApi.getScenesByUnitId(event.unitId, event.communityId);
|
final scenes =
|
||||||
|
await SceneApi.getScenesByUnitId(event.unitId, event.communityId);
|
||||||
emit(state.copyWith(
|
emit(state.copyWith(
|
||||||
scenes: scenes,
|
scenes: scenes,
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
@ -163,7 +183,8 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _onLoadAutomation(LoadAutomation event, Emitter<RoutineState> emit) async {
|
Future<void> _onLoadAutomation(
|
||||||
|
LoadAutomation event, Emitter<RoutineState> emit) async {
|
||||||
emit(state.copyWith(isLoading: true, errorMessage: null));
|
emit(state.copyWith(isLoading: true, errorMessage: null));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -191,41 +212,45 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureOr<void> _onSearchRoutines(SearchRoutines event, Emitter<RoutineState> emit) async {
|
FutureOr<void> _onSearchRoutines(
|
||||||
|
SearchRoutines event, Emitter<RoutineState> emit) async {
|
||||||
emit(state.copyWith(isLoading: true, errorMessage: null));
|
emit(state.copyWith(isLoading: true, errorMessage: null));
|
||||||
await Future.delayed(const Duration(seconds: 1));
|
await Future.delayed(const Duration(seconds: 1));
|
||||||
emit(state.copyWith(isLoading: false, errorMessage: null));
|
emit(state.copyWith(isLoading: false, errorMessage: null));
|
||||||
emit(state.copyWith(searchText: event.query));
|
emit(state.copyWith(searchText: event.query));
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureOr<void> _onAddSelectedIcon(AddSelectedIcon event, Emitter<RoutineState> emit) {
|
FutureOr<void> _onAddSelectedIcon(
|
||||||
|
AddSelectedIcon event, Emitter<RoutineState> emit) {
|
||||||
emit(state.copyWith(selectedIcon: event.icon));
|
emit(state.copyWith(selectedIcon: event.icon));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool _isFirstActionDelay(List<Map<String, dynamic>> actions) {
|
// bool _isFirstActionDelay(List<Map<String, dynamic>> actions) {
|
||||||
if (actions.isEmpty) return false;
|
// if (actions.isEmpty) return false;
|
||||||
return actions.first['deviceId'] == 'delay';
|
// return actions.first['deviceId'] == 'delay';
|
||||||
}
|
// }
|
||||||
|
|
||||||
bool _isLastActionDelay(List<Map<String, dynamic>> actions) {
|
bool _isLastActionDelay(List<Map<String, dynamic>> actions) {
|
||||||
if (actions.isEmpty) return false;
|
if (actions.isEmpty) return false;
|
||||||
return actions.last['deviceId'] == 'delay';
|
return actions.last['deviceId'] == 'delay';
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _onCreateScene(CreateSceneEvent event, Emitter<RoutineState> emit) async {
|
Future<void> _onCreateScene(
|
||||||
|
CreateSceneEvent event, Emitter<RoutineState> emit) async {
|
||||||
try {
|
try {
|
||||||
// Check if first action is delay
|
// Check if first action is delay
|
||||||
if (_isFirstActionDelay(state.thenItems)) {
|
// if (_isFirstActionDelay(state.thenItems)) {
|
||||||
emit(state.copyWith(
|
// emit(state.copyWith(
|
||||||
errorMessage: 'Cannot have delay as the first action',
|
// errorMessage: 'Cannot have delay as the first action',
|
||||||
isLoading: false,
|
// isLoading: false,
|
||||||
));
|
// ));
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (_isLastActionDelay(state.thenItems)) {
|
if (_isLastActionDelay(state.thenItems)) {
|
||||||
emit(state.copyWith(
|
emit(state.copyWith(
|
||||||
errorMessage: 'Cannot have delay as the last action',
|
errorMessage:
|
||||||
|
'A delay condition cannot be the only or the last action',
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
));
|
));
|
||||||
return;
|
return;
|
||||||
@ -238,6 +263,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
return functions.map((function) {
|
return functions.map((function) {
|
||||||
if (function.functionCode == 'automation') {
|
if (function.functionCode == 'automation') {
|
||||||
return CreateSceneAction(
|
return CreateSceneAction(
|
||||||
|
actionType: 'automation',
|
||||||
entityId: function.entityId,
|
entityId: function.entityId,
|
||||||
actionExecutor: function.value,
|
actionExecutor: function.value,
|
||||||
executorProperty: null,
|
executorProperty: null,
|
||||||
@ -257,6 +283,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return CreateSceneAction(
|
return CreateSceneAction(
|
||||||
|
actionType: 'scene',
|
||||||
entityId: function.entityId,
|
entityId: function.entityId,
|
||||||
actionExecutor: 'device_issue',
|
actionExecutor: 'device_issue',
|
||||||
executorProperty: CreateSceneExecutorProperty(
|
executorProperty: CreateSceneExecutorProperty(
|
||||||
@ -296,27 +323,33 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _onCreateAutomation(CreateAutomationEvent event, Emitter<RoutineState> emit) async {
|
Future<void> _onCreateAutomation(
|
||||||
|
CreateAutomationEvent event, Emitter<RoutineState> emit) async {
|
||||||
try {
|
try {
|
||||||
if (state.routineName == null || state.routineName!.isEmpty) {
|
if (state.routineName == null || state.routineName!.isEmpty) {
|
||||||
emit(state.copyWith(
|
emit(state.copyWith(
|
||||||
errorMessage: 'Automation name is required',
|
errorMessage: 'Automation name is required',
|
||||||
));
|
));
|
||||||
|
CustomSnackBar.redSnackBar('Automation name is required');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (_isFirstActionDelay(state.thenItems)) {
|
// if (_isFirstActionDelay(state.thenItems)) {
|
||||||
emit(state.copyWith(
|
// emit(state.copyWith(
|
||||||
errorMessage: 'Cannot have delay as the first action',
|
// errorMessage: 'Cannot have delay as the first action',
|
||||||
isLoading: false,
|
// isLoading: false,
|
||||||
));
|
// ));
|
||||||
return;
|
// CustomSnackBar.redSnackBar('Cannot have delay as the first action');
|
||||||
}
|
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
|
||||||
if (_isLastActionDelay(state.thenItems)) {
|
if (_isLastActionDelay(state.thenItems)) {
|
||||||
emit(state.copyWith(
|
emit(state.copyWith(
|
||||||
errorMessage: 'Cannot have delay as the last action',
|
errorMessage:
|
||||||
|
'A delay condition cannot be the only or the last action',
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
));
|
));
|
||||||
|
CustomSnackBar.redSnackBar('Cannot have delay as the last action');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
emit(state.copyWith(isLoading: true, errorMessage: null));
|
emit(state.copyWith(isLoading: true, errorMessage: null));
|
||||||
@ -349,12 +382,25 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final actions = state.thenItems.expand((item) {
|
final actions = state.thenItems.expand((item) {
|
||||||
|
if (item['type'] == 'tap_to_run') {
|
||||||
|
return [
|
||||||
|
AutomationAction(
|
||||||
|
actionType: 'scene',
|
||||||
|
entityId: item['deviceId'],
|
||||||
|
actionExecutor: 'rule_trigger',
|
||||||
|
),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
final functions = state.selectedFunctions[item['uniqueCustomId']] ?? [];
|
final functions = state.selectedFunctions[item['uniqueCustomId']] ?? [];
|
||||||
|
|
||||||
return functions.map((function) {
|
return functions.map((function) {
|
||||||
if (function.functionCode == 'automation') {
|
if (function.functionCode == 'automation') {
|
||||||
return AutomationAction(
|
return AutomationAction(
|
||||||
|
actionType: 'automation',
|
||||||
entityId: function.entityId,
|
entityId: function.entityId,
|
||||||
actionExecutor: function.value,
|
actionExecutor: function.value,
|
||||||
|
executorProperty: null,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -402,26 +448,32 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
isLoading: false,
|
isLoading: false,
|
||||||
errorMessage: result['message'],
|
errorMessage: result['message'],
|
||||||
));
|
));
|
||||||
|
CustomSnackBar.redSnackBar('Something went wrong');
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
emit(state.copyWith(
|
emit(state.copyWith(
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
errorMessage: 'Something went wrong',
|
errorMessage: 'Something went wrong',
|
||||||
));
|
));
|
||||||
|
CustomSnackBar.redSnackBar('Something went wrong');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureOr<void> _onRemoveDragCard(RemoveDragCard event, Emitter<RoutineState> emit) {
|
FutureOr<void> _onRemoveDragCard(
|
||||||
|
RemoveDragCard event, Emitter<RoutineState> emit) {
|
||||||
if (event.isFromThen) {
|
if (event.isFromThen) {
|
||||||
final thenItems = List<Map<String, dynamic>>.from(state.thenItems);
|
final thenItems = List<Map<String, dynamic>>.from(state.thenItems);
|
||||||
final selectedFunctions = Map<String, List<DeviceFunctionData>>.from(state.selectedFunctions);
|
final selectedFunctions =
|
||||||
|
Map<String, List<DeviceFunctionData>>.from(state.selectedFunctions);
|
||||||
|
|
||||||
thenItems.removeAt(event.index);
|
thenItems.removeAt(event.index);
|
||||||
selectedFunctions.remove(event.key);
|
selectedFunctions.remove(event.key);
|
||||||
emit(state.copyWith(thenItems: thenItems, selectedFunctions: selectedFunctions));
|
emit(state.copyWith(
|
||||||
|
thenItems: thenItems, selectedFunctions: selectedFunctions));
|
||||||
} else {
|
} else {
|
||||||
final ifItems = List<Map<String, dynamic>>.from(state.ifItems);
|
final ifItems = List<Map<String, dynamic>>.from(state.ifItems);
|
||||||
final selectedFunctions = Map<String, List<DeviceFunctionData>>.from(state.selectedFunctions);
|
final selectedFunctions =
|
||||||
|
Map<String, List<DeviceFunctionData>>.from(state.selectedFunctions);
|
||||||
|
|
||||||
ifItems.removeAt(event.index);
|
ifItems.removeAt(event.index);
|
||||||
selectedFunctions.remove(event.key);
|
selectedFunctions.remove(event.key);
|
||||||
@ -432,7 +484,8 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
isAutomation: false,
|
isAutomation: false,
|
||||||
isTabToRun: false));
|
isTabToRun: false));
|
||||||
} else {
|
} else {
|
||||||
emit(state.copyWith(ifItems: ifItems, selectedFunctions: selectedFunctions));
|
emit(state.copyWith(
|
||||||
|
ifItems: ifItems, selectedFunctions: selectedFunctions));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -444,18 +497,23 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureOr<void> _onEffectiveTimeEvent(EffectiveTimePeriodEvent event, Emitter<RoutineState> emit) {
|
FutureOr<void> _onEffectiveTimeEvent(
|
||||||
|
EffectiveTimePeriodEvent event, Emitter<RoutineState> emit) {
|
||||||
emit(state.copyWith(effectiveTime: event.effectiveTime));
|
emit(state.copyWith(effectiveTime: event.effectiveTime));
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureOr<void> _onSetRoutineName(SetRoutineName event, Emitter<RoutineState> emit) {
|
FutureOr<void> _onSetRoutineName(
|
||||||
|
SetRoutineName event, Emitter<RoutineState> emit) {
|
||||||
emit(state.copyWith(
|
emit(state.copyWith(
|
||||||
routineName: event.name,
|
routineName: event.name,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
(List<Map<String, dynamic>>, List<Map<String, dynamic>>, Map<String, List<DeviceFunctionData>>)
|
(
|
||||||
_createCardData(
|
List<Map<String, dynamic>>,
|
||||||
|
List<Map<String, dynamic>>,
|
||||||
|
Map<String, List<DeviceFunctionData>>
|
||||||
|
) _createCardData(
|
||||||
List<RoutineAction> actions,
|
List<RoutineAction> actions,
|
||||||
List<RoutineCondition>? conditions,
|
List<RoutineCondition>? conditions,
|
||||||
Map<String, List<DeviceFunctionData>> currentFunctions,
|
Map<String, List<DeviceFunctionData>> currentFunctions,
|
||||||
@ -488,7 +546,8 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
'deviceId': condition.entityId,
|
'deviceId': condition.entityId,
|
||||||
'title': matchingDevice.name ?? condition.entityId,
|
'title': matchingDevice.name ?? condition.entityId,
|
||||||
'productType': condition.entityType,
|
'productType': condition.entityType,
|
||||||
'imagePath': matchingDevice.getDefaultIcon(condition.entityType),
|
'imagePath':
|
||||||
|
matchingDevice.getDefaultIcon(condition.entityType),
|
||||||
};
|
};
|
||||||
|
|
||||||
final functions = matchingDevice.functions;
|
final functions = matchingDevice.functions;
|
||||||
@ -524,8 +583,11 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
final cardData = {
|
final cardData = {
|
||||||
'entityId': action.entityId,
|
'entityId': action.entityId,
|
||||||
'uniqueCustomId': const Uuid().v4(),
|
'uniqueCustomId': const Uuid().v4(),
|
||||||
'deviceId': action.actionExecutor == 'delay' ? 'delay' : action.entityId,
|
'deviceId':
|
||||||
'title': action.actionExecutor == 'delay' ? 'Delay' : (matchingDevice.name ?? 'Device'),
|
action.actionExecutor == 'delay' ? 'delay' : action.entityId,
|
||||||
|
'title': action.actionExecutor == 'delay'
|
||||||
|
? 'Delay'
|
||||||
|
: (matchingDevice.name ?? 'Device'),
|
||||||
'productType': action.productType,
|
'productType': action.productType,
|
||||||
'imagePath': matchingDevice.getDefaultIcon(action.productType),
|
'imagePath': matchingDevice.getDefaultIcon(action.productType),
|
||||||
};
|
};
|
||||||
@ -568,7 +630,8 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
return (thenItems, ifItems, currentFunctions);
|
return (thenItems, ifItems, currentFunctions);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _onGetSceneDetails(GetSceneDetails event, Emitter<RoutineState> emit) async {
|
Future<void> _onGetSceneDetails(
|
||||||
|
GetSceneDetails event, Emitter<RoutineState> emit) async {
|
||||||
try {
|
try {
|
||||||
emit(state.copyWith(
|
emit(state.copyWith(
|
||||||
isLoading: true,
|
isLoading: true,
|
||||||
@ -616,8 +679,10 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
if (!deviceCards.containsKey(deviceId)) {
|
if (!deviceCards.containsKey(deviceId)) {
|
||||||
deviceCards[deviceId] = {
|
deviceCards[deviceId] = {
|
||||||
'entityId': action.entityId,
|
'entityId': action.entityId,
|
||||||
'deviceId': action.actionExecutor == 'delay' ? 'delay' : action.entityId,
|
'deviceId':
|
||||||
'uniqueCustomId': action.type == 'automation' || action.actionExecutor == 'delay'
|
action.actionExecutor == 'delay' ? 'delay' : action.entityId,
|
||||||
|
'uniqueCustomId':
|
||||||
|
action.type == 'automation' || action.actionExecutor == 'delay'
|
||||||
? const Uuid().v4()
|
? const Uuid().v4()
|
||||||
: action.entityId,
|
: action.entityId,
|
||||||
'title': action.actionExecutor == 'delay'
|
'title': action.actionExecutor == 'delay'
|
||||||
@ -653,8 +718,9 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
operationName: 'Automation',
|
operationName: 'Automation',
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
emit(state.copyWith(automationActionExecutor: action.actionExecutor));
|
// emit(state.copyWith(automationActionExecutor: action.actionExecutor));
|
||||||
} else if (action.executorProperty != null && action.actionExecutor != 'delay') {
|
} else if (action.executorProperty != null &&
|
||||||
|
action.actionExecutor != 'delay') {
|
||||||
if (!updatedFunctions.containsKey(uniqueCustomId)) {
|
if (!updatedFunctions.containsKey(uniqueCustomId)) {
|
||||||
updatedFunctions[uniqueCustomId] = [];
|
updatedFunctions[uniqueCustomId] = [];
|
||||||
}
|
}
|
||||||
@ -726,153 +792,8 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _onGetAutomationDetails(
|
FutureOr<void> _onResetRoutineState(
|
||||||
GetAutomationDetails event, Emitter<RoutineState> emit) async {
|
ResetRoutineState event, Emitter<RoutineState> emit) {
|
||||||
try {
|
|
||||||
emit(state.copyWith(
|
|
||||||
isLoading: true,
|
|
||||||
isUpdate: true,
|
|
||||||
isTabToRun: false,
|
|
||||||
automationId: event.automationId,
|
|
||||||
isAutomation: true,
|
|
||||||
ifItems: [],
|
|
||||||
thenItems: [],
|
|
||||||
));
|
|
||||||
|
|
||||||
final automationDetails = await SceneApi.getAutomationDetails(event.automationId);
|
|
||||||
|
|
||||||
final List<Map<String, dynamic>> thenItems;
|
|
||||||
final List<Map<String, dynamic>> ifItems;
|
|
||||||
final Map<String, List<DeviceFunctionData>> updatedFunctions =
|
|
||||||
Map<String, List<DeviceFunctionData>>.from(state.selectedFunctions);
|
|
||||||
|
|
||||||
ifItems = automationDetails.conditions?.map((condition) {
|
|
||||||
late AllDevicesModel? matchingDevice;
|
|
||||||
for (var device in state.devices) {
|
|
||||||
if (device.uuid == condition.entityId) {
|
|
||||||
matchingDevice = device;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
final cardData = {
|
|
||||||
'entityId': condition.entityId,
|
|
||||||
'uniqueCustomId': const Uuid().v4(),
|
|
||||||
'deviceId': condition.expr.statusCode == 'delay' ? 'delay' : condition.entityId,
|
|
||||||
'title': condition.expr.statusCode == 'delay'
|
|
||||||
? 'Delay'
|
|
||||||
: (matchingDevice?.name ?? 'Device'),
|
|
||||||
'productType': condition.productType,
|
|
||||||
'functions': matchingDevice?.functions ?? [],
|
|
||||||
'imagePath': matchingDevice?.getDefaultIcon(condition.productType) ?? '',
|
|
||||||
'device': matchingDevice,
|
|
||||||
};
|
|
||||||
|
|
||||||
final functions = matchingDevice?.functions ?? [];
|
|
||||||
for (var function in functions) {
|
|
||||||
if (function.code == condition.expr.statusCode) {
|
|
||||||
updatedFunctions[cardData['uniqueCustomId'].toString()] = [
|
|
||||||
DeviceFunctionData(
|
|
||||||
entityId: condition.entityId,
|
|
||||||
functionCode: condition.expr.statusCode,
|
|
||||||
value: condition.expr.statusValue,
|
|
||||||
operationName: function.operationName,
|
|
||||||
),
|
|
||||||
];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return cardData;
|
|
||||||
}).toList() ??
|
|
||||||
[];
|
|
||||||
|
|
||||||
// Create then items from actions
|
|
||||||
thenItems = automationDetails.actions.map((action) {
|
|
||||||
final matchingDevice = state.devices.firstWhere(
|
|
||||||
(device) => device.uuid == action.entityId,
|
|
||||||
orElse: () => AllDevicesModel(
|
|
||||||
uuid: action.entityId,
|
|
||||||
name: action.entityId,
|
|
||||||
productType: action.productType,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
final cardData = {
|
|
||||||
'entityId': action.entityId,
|
|
||||||
'uniqueCustomId': const Uuid().v4(),
|
|
||||||
'deviceId': action.actionExecutor == 'delay' ? 'delay' : action.entityId,
|
|
||||||
'title': action.actionExecutor == 'delay' ? 'Delay' : (matchingDevice.name ?? 'Device'),
|
|
||||||
'productType': action.productType,
|
|
||||||
'functions': matchingDevice.functions,
|
|
||||||
'imagePath': matchingDevice.getDefaultIcon(action.productType),
|
|
||||||
'device': matchingDevice,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (action.executorProperty != null && action.actionExecutor != 'delay') {
|
|
||||||
final functions = matchingDevice.functions;
|
|
||||||
final functionCode = action.executorProperty!.functionCode;
|
|
||||||
for (var function in functions) {
|
|
||||||
if (function.code == functionCode) {
|
|
||||||
updatedFunctions[cardData['uniqueCustomId'].toString()] = [
|
|
||||||
DeviceFunctionData(
|
|
||||||
entityId: action.entityId,
|
|
||||||
functionCode: functionCode ?? '',
|
|
||||||
value: action.executorProperty!.functionValue,
|
|
||||||
operationName: function.operationName,
|
|
||||||
),
|
|
||||||
];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (action.actionExecutor == 'delay') {
|
|
||||||
final delayFunction = DelayFunction(
|
|
||||||
deviceId: action.entityId,
|
|
||||||
deviceName: 'Delay',
|
|
||||||
);
|
|
||||||
updatedFunctions[cardData['uniqueCustomId'].toString()] = [
|
|
||||||
DeviceFunctionData(
|
|
||||||
entityId: action.entityId,
|
|
||||||
functionCode: 'delay',
|
|
||||||
value: action.executorProperty?.delaySeconds ?? 0,
|
|
||||||
operationName: delayFunction.operationName,
|
|
||||||
),
|
|
||||||
];
|
|
||||||
} else if (action.actionExecutor == 'rule_disable' ||
|
|
||||||
action.actionExecutor == 'rule_enable') {
|
|
||||||
updatedFunctions[cardData['uniqueCustomId'].toString()] = [
|
|
||||||
DeviceFunctionData(
|
|
||||||
entityId: action.entityId,
|
|
||||||
functionCode: 'automation',
|
|
||||||
value: action.actionExecutor,
|
|
||||||
operationName: action.name ?? 'Automation',
|
|
||||||
),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
return cardData;
|
|
||||||
}).toList();
|
|
||||||
|
|
||||||
emit(state.copyWith(
|
|
||||||
isLoading: false,
|
|
||||||
routineName: automationDetails.name,
|
|
||||||
selectedAutomationOperator: automationDetails.decisionExpr,
|
|
||||||
effectiveTime: automationDetails.effectiveTime,
|
|
||||||
isAutomation: true,
|
|
||||||
thenItems: thenItems,
|
|
||||||
ifItems: ifItems,
|
|
||||||
selectedFunctions: updatedFunctions,
|
|
||||||
automationId: automationDetails.automationId,
|
|
||||||
));
|
|
||||||
} catch (e) {
|
|
||||||
emit(state.copyWith(
|
|
||||||
isLoading: false,
|
|
||||||
errorMessage: 'Failed to load automation details: $e',
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
FutureOr<void> _onResetRoutineState(ResetRoutineState event, Emitter<RoutineState> emit) {
|
|
||||||
emit(state.copyWith(
|
emit(state.copyWith(
|
||||||
ifItems: [],
|
ifItems: [],
|
||||||
thenItems: [],
|
thenItems: [],
|
||||||
@ -900,7 +821,8 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
if (state.isTabToRun) {
|
if (state.isTabToRun) {
|
||||||
SceneApi.deleteScene(unitUuid: spaceId, sceneId: state.sceneId ?? '');
|
SceneApi.deleteScene(unitUuid: spaceId, sceneId: state.sceneId ?? '');
|
||||||
} else {
|
} else {
|
||||||
SceneApi.deleteAutomation(unitUuid: spaceId, automationId: state.automationId ?? '');
|
SceneApi.deleteAutomation(
|
||||||
|
unitUuid: spaceId, automationId: state.automationId ?? '');
|
||||||
}
|
}
|
||||||
|
|
||||||
add(const LoadScenes(spaceId, communityId));
|
add(const LoadScenes(spaceId, communityId));
|
||||||
@ -929,7 +851,8 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
FutureOr<void> _fetchDevices(FetchDevicesInRoutine event, Emitter<RoutineState> emit) async {
|
FutureOr<void> _fetchDevices(
|
||||||
|
FetchDevicesInRoutine event, Emitter<RoutineState> emit) async {
|
||||||
emit(state.copyWith(isLoading: true));
|
emit(state.copyWith(isLoading: true));
|
||||||
try {
|
try {
|
||||||
final devices = await DevicesManagementApi().fetchDevices();
|
final devices = await DevicesManagementApi().fetchDevices();
|
||||||
@ -940,21 +863,23 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureOr<void> _onUpdateScene(UpdateScene event, Emitter<RoutineState> emit) async {
|
FutureOr<void> _onUpdateScene(
|
||||||
|
UpdateScene event, Emitter<RoutineState> emit) async {
|
||||||
try {
|
try {
|
||||||
// Check if first action is delay
|
// Check if first action is delay
|
||||||
if (_isFirstActionDelay(state.thenItems)) {
|
// if (_isFirstActionDelay(state.thenItems)) {
|
||||||
emit(state.copyWith(
|
// emit(state.copyWith(
|
||||||
errorMessage: 'Cannot have delay as the first action',
|
// errorMessage: 'Cannot have delay as the first action',
|
||||||
isLoading: false,
|
// isLoading: false,
|
||||||
));
|
// ));
|
||||||
|
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (_isLastActionDelay(state.thenItems)) {
|
if (_isLastActionDelay(state.thenItems)) {
|
||||||
emit(state.copyWith(
|
emit(state.copyWith(
|
||||||
errorMessage: 'Cannot have delay as the last action',
|
errorMessage:
|
||||||
|
'A delay condition cannot be the only or the last action',
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
));
|
));
|
||||||
return;
|
return;
|
||||||
@ -967,6 +892,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
return functions.map((function) {
|
return functions.map((function) {
|
||||||
if (function.functionCode == 'automation') {
|
if (function.functionCode == 'automation') {
|
||||||
return CreateSceneAction(
|
return CreateSceneAction(
|
||||||
|
actionType: 'automation',
|
||||||
entityId: function.entityId,
|
entityId: function.entityId,
|
||||||
actionExecutor: function.value,
|
actionExecutor: function.value,
|
||||||
executorProperty: null,
|
executorProperty: null,
|
||||||
@ -1006,7 +932,8 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
actions: actions,
|
actions: actions,
|
||||||
);
|
);
|
||||||
|
|
||||||
final result = await SceneApi.updateScene(createSceneModel, state.sceneId ?? '');
|
final result =
|
||||||
|
await SceneApi.updateScene(createSceneModel, state.sceneId ?? '');
|
||||||
if (result['success']) {
|
if (result['success']) {
|
||||||
add(ResetRoutineState());
|
add(ResetRoutineState());
|
||||||
add(const LoadScenes(spaceId, communityId));
|
add(const LoadScenes(spaceId, communityId));
|
||||||
@ -1025,72 +952,125 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureOr<void> _onUpdateAutomation(UpdateAutomation event, Emitter<RoutineState> emit) async {
|
FutureOr<void> _onUpdateAutomation(
|
||||||
if (_isFirstActionDelay(state.thenItems)) {
|
UpdateAutomation event, Emitter<RoutineState> emit) async {
|
||||||
|
try {
|
||||||
|
if (state.routineName == null || state.routineName!.isEmpty) {
|
||||||
emit(state.copyWith(
|
emit(state.copyWith(
|
||||||
errorMessage: 'Cannot have delay as the first action',
|
errorMessage: 'Automation name is required',
|
||||||
|
));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// if (_isFirstActionDelay(state.thenItems)) {
|
||||||
|
// emit(state.copyWith(
|
||||||
|
// errorMessage: 'Cannot have delay as the first action',
|
||||||
|
// isLoading: false,
|
||||||
|
// ));
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
|
||||||
|
if (_isLastActionDelay(state.thenItems)) {
|
||||||
|
emit(state.copyWith(
|
||||||
|
errorMessage: 'Cannot have delay as the last action',
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
));
|
));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
emit(state.copyWith(isLoading: true));
|
emit(state.copyWith(isLoading: true, errorMessage: null));
|
||||||
try {
|
|
||||||
final conditions = state.ifItems
|
|
||||||
.map((item) {
|
|
||||||
final functions = state.selectedFunctions[item['uniqueCustomId']] ?? [];
|
|
||||||
if (functions.isEmpty) return null;
|
|
||||||
|
|
||||||
final function = functions.first;
|
final conditions = state.ifItems.expand((item) {
|
||||||
|
final functions = state.selectedFunctions[item['uniqueCustomId']] ?? [];
|
||||||
|
return functions.map((function) {
|
||||||
return Condition(
|
return Condition(
|
||||||
code: state.ifItems.indexOf(item) + 1,
|
code: state.selectedFunctions[item['uniqueCustomId']]!.indexOf(
|
||||||
|
function,
|
||||||
|
) +
|
||||||
|
1,
|
||||||
entityId: function.entityId,
|
entityId: function.entityId,
|
||||||
entityType: item['productType'],
|
entityType: 'device_report',
|
||||||
expr: ConditionExpr(
|
expr: ConditionExpr(
|
||||||
statusCode: function.functionCode,
|
statusCode: function.functionCode,
|
||||||
statusValue: function.value,
|
|
||||||
comparator: function.condition ?? '==',
|
comparator: function.condition ?? '==',
|
||||||
|
statusValue: function.value,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
})
|
});
|
||||||
.whereType<Condition>()
|
}).toList();
|
||||||
.toList();
|
|
||||||
|
if (conditions.isEmpty) {
|
||||||
|
emit(state.copyWith(
|
||||||
|
isLoading: false,
|
||||||
|
errorMessage: 'At least one condition is required',
|
||||||
|
));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final actions = state.thenItems.expand((item) {
|
||||||
|
if (item['type'] == 'tap_to_run' || item['type'] == 'scene') {
|
||||||
|
return [
|
||||||
|
AutomationAction(
|
||||||
|
actionType: 'scene',
|
||||||
|
entityId: item['deviceId'],
|
||||||
|
actionExecutor: 'rule_trigger',
|
||||||
|
),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
final actions = state.thenItems
|
|
||||||
.map((item) {
|
|
||||||
final functions = state.selectedFunctions[item['uniqueCustomId']] ?? [];
|
final functions = state.selectedFunctions[item['uniqueCustomId']] ?? [];
|
||||||
if (functions.isEmpty) return null;
|
|
||||||
|
|
||||||
final function = functions.first;
|
return functions.map((function) {
|
||||||
|
if (function.functionCode == 'automation') {
|
||||||
|
return AutomationAction(
|
||||||
|
actionType: 'automation',
|
||||||
|
entityId: function.entityId,
|
||||||
|
actionExecutor: function.value,
|
||||||
|
executorProperty: null,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item['deviceId'] == 'delay') {
|
||||||
return AutomationAction(
|
return AutomationAction(
|
||||||
entityId: function.entityId,
|
entityId: function.entityId,
|
||||||
actionExecutor: function.actionExecutor,
|
actionType: 'automation',
|
||||||
|
actionExecutor: 'delay',
|
||||||
|
executorProperty: ExecutorProperty(
|
||||||
|
delaySeconds: int.tryParse(function.value.toString()) ?? 0,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return AutomationAction(
|
||||||
|
entityId: function.entityId,
|
||||||
|
actionExecutor: 'device_issue',
|
||||||
|
actionType: 'automation',
|
||||||
executorProperty: ExecutorProperty(
|
executorProperty: ExecutorProperty(
|
||||||
functionCode: function.functionCode,
|
functionCode: function.functionCode,
|
||||||
functionValue: function.value,
|
functionValue: function.value,
|
||||||
delaySeconds:
|
|
||||||
function.functionCode == 'delay' ? (function.value as num).toInt() : null,
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
})
|
});
|
||||||
.whereType<AutomationAction>()
|
}).toList();
|
||||||
.toList();
|
|
||||||
|
|
||||||
final createAutomationModel = CreateAutomationModel(
|
final createAutomationModel = CreateAutomationModel(
|
||||||
spaceUuid: spaceId,
|
spaceUuid: spaceId,
|
||||||
automationName: state.routineName ?? '',
|
automationName: state.routineName ?? '',
|
||||||
decisionExpr: state.selectedAutomationOperator,
|
decisionExpr: state.selectedAutomationOperator,
|
||||||
effectiveTime: state.effectiveTime ?? EffectiveTime(start: '', end: '', loops: ''),
|
effectiveTime: EffectiveTime(
|
||||||
|
start: state.effectiveTime?.start ?? '00:00',
|
||||||
|
end: state.effectiveTime?.end ?? '23:59',
|
||||||
|
loops: state.effectiveTime?.loops ?? '1111111',
|
||||||
|
),
|
||||||
conditions: conditions,
|
conditions: conditions,
|
||||||
actions: actions,
|
actions: actions,
|
||||||
);
|
);
|
||||||
|
|
||||||
final result =
|
final result = await SceneApi.updateAutomation(
|
||||||
await SceneApi.updateAutomation(createAutomationModel, state.automationId ?? '');
|
createAutomationModel, state.automationId ?? '');
|
||||||
|
|
||||||
if (result['success']) {
|
if (result['success']) {
|
||||||
add(ResetRoutineState());
|
add(ResetRoutineState());
|
||||||
add(const LoadScenes(spaceId, communityId));
|
|
||||||
add(const LoadAutomation(spaceId));
|
add(const LoadAutomation(spaceId));
|
||||||
|
add(const LoadScenes(spaceId, communityId));
|
||||||
} else {
|
} else {
|
||||||
emit(state.copyWith(
|
emit(state.copyWith(
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
@ -1100,13 +1080,198 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
emit(state.copyWith(
|
emit(state.copyWith(
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
errorMessage: 'Failed to update automation: $e',
|
errorMessage: 'Something went wrong',
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureOr<void> _onSetAutomationActionExecutor(
|
Future<void> _onGetAutomationDetails(
|
||||||
SetAutomationActionExecutor event, Emitter<RoutineState> emit) {
|
GetAutomationDetails event, Emitter<RoutineState> emit) async {
|
||||||
emit(state.copyWith(automationActionExecutor: event.automationActionExecutor));
|
try {
|
||||||
|
emit(state.copyWith(
|
||||||
|
isLoading: true,
|
||||||
|
isUpdate: true,
|
||||||
|
isTabToRun: false,
|
||||||
|
automationId: event.automationId,
|
||||||
|
isAutomation: true,
|
||||||
|
ifItems: [],
|
||||||
|
thenItems: [],
|
||||||
|
));
|
||||||
|
|
||||||
|
final automationDetails =
|
||||||
|
await SceneApi.getAutomationDetails(event.automationId);
|
||||||
|
|
||||||
|
final Map<String, Map<String, dynamic>> deviceCards = {};
|
||||||
|
final Map<String, List<DeviceFunctionData>> updatedFunctions =
|
||||||
|
Map<String, List<DeviceFunctionData>>.from(state.selectedFunctions);
|
||||||
|
|
||||||
|
for (RoutineCondition condition in automationDetails.conditions ?? []) {
|
||||||
|
AllDevicesModel? matchingDevice = state.devices.firstWhere(
|
||||||
|
(device) => device.uuid == condition.entityId,
|
||||||
|
orElse: () => AllDevicesModel(
|
||||||
|
uuid: condition.entityId,
|
||||||
|
name: condition.entityId,
|
||||||
|
productType: condition.productType,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
final deviceId = condition.entityId;
|
||||||
|
|
||||||
|
if (!deviceCards.containsKey(deviceId)) {
|
||||||
|
deviceCards[deviceId] = {
|
||||||
|
'entityId': condition.entityId,
|
||||||
|
'deviceId': condition.entityId,
|
||||||
|
'uniqueCustomId': const Uuid().v4(),
|
||||||
|
'title': matchingDevice.name ?? 'Device',
|
||||||
|
'productType': condition.productType,
|
||||||
|
'functions': matchingDevice.functions,
|
||||||
|
'imagePath': matchingDevice.getDefaultIcon(condition.productType),
|
||||||
|
'device': matchingDevice,
|
||||||
|
'type': 'condition',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
final cardData = deviceCards[deviceId]!;
|
||||||
|
final uniqueCustomId = cardData['uniqueCustomId'].toString();
|
||||||
|
|
||||||
|
if (!updatedFunctions.containsKey(uniqueCustomId)) {
|
||||||
|
updatedFunctions[uniqueCustomId] = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
final functions = matchingDevice.functions;
|
||||||
|
for (var function in functions) {
|
||||||
|
if (function.code == condition.expr.statusCode) {
|
||||||
|
updatedFunctions[uniqueCustomId]!.add(
|
||||||
|
DeviceFunctionData(
|
||||||
|
entityId: condition.entityId,
|
||||||
|
functionCode: condition.expr.statusCode,
|
||||||
|
value: condition.expr.statusValue,
|
||||||
|
operationName: function.operationName,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process actions (thenItems)
|
||||||
|
for (var action in automationDetails.actions) {
|
||||||
|
AllDevicesModel? matchingDevice = state.devices.firstWhere(
|
||||||
|
(device) => device.uuid == action.entityId,
|
||||||
|
orElse: () => AllDevicesModel(
|
||||||
|
uuid: action.entityId,
|
||||||
|
name: action.entityId,
|
||||||
|
productType: action.productType,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
final deviceId = action.actionExecutor == 'delay'
|
||||||
|
? '${action.entityId}_delay'
|
||||||
|
: action.entityId;
|
||||||
|
|
||||||
|
if (!deviceCards.containsKey(deviceId)) {
|
||||||
|
deviceCards[deviceId] = {
|
||||||
|
'entityId': action.entityId,
|
||||||
|
'deviceId':
|
||||||
|
action.actionExecutor == 'delay' ? 'delay' : action.entityId,
|
||||||
|
'uniqueCustomId': const Uuid().v4(),
|
||||||
|
'title': action.actionExecutor == 'delay'
|
||||||
|
? 'Delay'
|
||||||
|
: (action.type == 'scene' || action.type == 'automation')
|
||||||
|
? action.name
|
||||||
|
: (matchingDevice.name ?? 'Device'),
|
||||||
|
'productType': action.productType,
|
||||||
|
'functions': matchingDevice.functions,
|
||||||
|
'imagePath': action.actionExecutor == 'delay'
|
||||||
|
? Assets.delay
|
||||||
|
: action.type == 'automation'
|
||||||
|
? Assets.automation
|
||||||
|
: matchingDevice.getDefaultIcon(action.productType),
|
||||||
|
'device': matchingDevice,
|
||||||
|
'type': action.type == 'scene'
|
||||||
|
? 'scene'
|
||||||
|
: action.type == 'automation'
|
||||||
|
? 'automation'
|
||||||
|
: 'action',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
final cardData = deviceCards[deviceId]!;
|
||||||
|
final uniqueCustomId = cardData['uniqueCustomId'].toString();
|
||||||
|
|
||||||
|
if (!updatedFunctions.containsKey(uniqueCustomId)) {
|
||||||
|
updatedFunctions[uniqueCustomId] = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (action.executorProperty != null &&
|
||||||
|
action.actionExecutor != 'delay') {
|
||||||
|
final functions = matchingDevice.functions;
|
||||||
|
final functionCode = action.executorProperty!.functionCode;
|
||||||
|
for (var function in functions) {
|
||||||
|
if (function.code == functionCode) {
|
||||||
|
updatedFunctions[uniqueCustomId]!.add(
|
||||||
|
DeviceFunctionData(
|
||||||
|
entityId: action.entityId,
|
||||||
|
functionCode: functionCode ?? '',
|
||||||
|
value: action.executorProperty!.functionValue,
|
||||||
|
operationName: function.operationName,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (action.actionExecutor == 'delay') {
|
||||||
|
final delayFunction = DelayFunction(
|
||||||
|
deviceId: action.entityId,
|
||||||
|
deviceName: 'Delay',
|
||||||
|
);
|
||||||
|
updatedFunctions[uniqueCustomId]!.add(
|
||||||
|
DeviceFunctionData(
|
||||||
|
entityId: action.entityId,
|
||||||
|
functionCode: 'delay',
|
||||||
|
value: action.executorProperty?.delaySeconds ?? 0,
|
||||||
|
operationName: delayFunction.operationName,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} else if (action.actionExecutor == 'rule_disable' ||
|
||||||
|
action.actionExecutor == 'rule_enable') {
|
||||||
|
updatedFunctions[uniqueCustomId]!.add(
|
||||||
|
DeviceFunctionData(
|
||||||
|
entityId: action.entityId,
|
||||||
|
functionCode: 'automation',
|
||||||
|
value: action.actionExecutor,
|
||||||
|
operationName: action.name ?? 'Automation',
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final ifItems = deviceCards.values
|
||||||
|
.where((card) => card['type'] == 'condition')
|
||||||
|
.toList();
|
||||||
|
final thenItems = deviceCards.values
|
||||||
|
.where((card) =>
|
||||||
|
card['type'] == 'action' ||
|
||||||
|
card['type'] == 'automation' ||
|
||||||
|
card['type'] == 'scene')
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
emit(state.copyWith(
|
||||||
|
isLoading: false,
|
||||||
|
routineName: automationDetails.name,
|
||||||
|
selectedAutomationOperator: automationDetails.decisionExpr,
|
||||||
|
effectiveTime: automationDetails.effectiveTime,
|
||||||
|
isAutomation: true,
|
||||||
|
thenItems: thenItems,
|
||||||
|
ifItems: ifItems,
|
||||||
|
selectedFunctions: updatedFunctions,
|
||||||
|
automationId: automationDetails.automationId,
|
||||||
|
));
|
||||||
|
} catch (e) {
|
||||||
|
emit(state.copyWith(
|
||||||
|
isLoading: false,
|
||||||
|
errorMessage: 'Failed to load automation details: $e',
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -206,3 +206,5 @@ class FetchDevicesInRoutine extends RoutineEvent {}
|
|||||||
class ResetRoutineState extends RoutineEvent {}
|
class ResetRoutineState extends RoutineEvent {}
|
||||||
|
|
||||||
class ClearFunctions extends RoutineEvent {}
|
class ClearFunctions extends RoutineEvent {}
|
||||||
|
|
||||||
|
class ResetErrorMessage extends RoutineEvent {}
|
||||||
|
@ -22,7 +22,7 @@ class RoutineState extends Equatable {
|
|||||||
final String? automationId;
|
final String? automationId;
|
||||||
final bool? isUpdate;
|
final bool? isUpdate;
|
||||||
final List<AllDevicesModel> devices;
|
final List<AllDevicesModel> devices;
|
||||||
final String? automationActionExecutor;
|
// final String? automationActionExecutor;
|
||||||
final bool routineTab;
|
final bool routineTab;
|
||||||
final bool createRoutineView;
|
final bool createRoutineView;
|
||||||
|
|
||||||
@ -48,7 +48,7 @@ class RoutineState extends Equatable {
|
|||||||
this.automationId,
|
this.automationId,
|
||||||
this.isUpdate,
|
this.isUpdate,
|
||||||
this.devices = const [],
|
this.devices = const [],
|
||||||
this.automationActionExecutor,
|
// this.automationActionExecutor,
|
||||||
this.routineTab = false,
|
this.routineTab = false,
|
||||||
this.createRoutineView = false});
|
this.createRoutineView = false});
|
||||||
|
|
||||||
@ -73,7 +73,7 @@ class RoutineState extends Equatable {
|
|||||||
String? automationId,
|
String? automationId,
|
||||||
bool? isUpdate,
|
bool? isUpdate,
|
||||||
List<AllDevicesModel>? devices,
|
List<AllDevicesModel>? devices,
|
||||||
String? automationActionExecutor,
|
// String? automationActionExecutor,
|
||||||
TextEditingController? nameController,
|
TextEditingController? nameController,
|
||||||
bool? routineTab,
|
bool? routineTab,
|
||||||
bool? createRoutineView,
|
bool? createRoutineView,
|
||||||
@ -88,18 +88,21 @@ class RoutineState extends Equatable {
|
|||||||
errorMessage: errorMessage ?? this.errorMessage,
|
errorMessage: errorMessage ?? this.errorMessage,
|
||||||
routineName: routineName ?? this.routineName,
|
routineName: routineName ?? this.routineName,
|
||||||
selectedIcon: selectedIcon ?? this.selectedIcon,
|
selectedIcon: selectedIcon ?? this.selectedIcon,
|
||||||
loadScenesErrorMessage: loadScenesErrorMessage ?? this.loadScenesErrorMessage,
|
loadScenesErrorMessage:
|
||||||
loadAutomationErrorMessage: loadAutomationErrorMessage ?? this.loadAutomationErrorMessage,
|
loadScenesErrorMessage ?? this.loadScenesErrorMessage,
|
||||||
|
loadAutomationErrorMessage:
|
||||||
|
loadAutomationErrorMessage ?? this.loadAutomationErrorMessage,
|
||||||
searchText: searchText ?? this.searchText,
|
searchText: searchText ?? this.searchText,
|
||||||
isTabToRun: isTabToRun ?? this.isTabToRun,
|
isTabToRun: isTabToRun ?? this.isTabToRun,
|
||||||
isAutomation: isAutomation ?? this.isAutomation,
|
isAutomation: isAutomation ?? this.isAutomation,
|
||||||
selectedAutomationOperator: selectedAutomationOperator ?? this.selectedAutomationOperator,
|
selectedAutomationOperator:
|
||||||
|
selectedAutomationOperator ?? this.selectedAutomationOperator,
|
||||||
effectiveTime: effectiveTime ?? this.effectiveTime,
|
effectiveTime: effectiveTime ?? this.effectiveTime,
|
||||||
sceneId: sceneId ?? this.sceneId,
|
sceneId: sceneId ?? this.sceneId,
|
||||||
automationId: automationId ?? this.automationId,
|
automationId: automationId ?? this.automationId,
|
||||||
isUpdate: isUpdate ?? this.isUpdate,
|
isUpdate: isUpdate ?? this.isUpdate,
|
||||||
devices: devices ?? this.devices,
|
devices: devices ?? this.devices,
|
||||||
automationActionExecutor: automationActionExecutor ?? this.automationActionExecutor,
|
// automationActionExecutor: automationActionExecutor ?? this.automationActionExecutor,
|
||||||
routineTab: routineTab ?? this.routineTab,
|
routineTab: routineTab ?? this.routineTab,
|
||||||
createRoutineView: createRoutineView ?? this.createRoutineView);
|
createRoutineView: createRoutineView ?? this.createRoutineView);
|
||||||
}
|
}
|
||||||
@ -126,7 +129,7 @@ class RoutineState extends Equatable {
|
|||||||
automationId,
|
automationId,
|
||||||
isUpdate,
|
isUpdate,
|
||||||
devices,
|
devices,
|
||||||
automationActionExecutor,
|
// automationActionExecutor,
|
||||||
routineTab,
|
routineTab,
|
||||||
createRoutineView
|
createRoutineView
|
||||||
];
|
];
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:flutter_svg/flutter_svg.dart';
|
import 'package:flutter_svg/flutter_svg.dart';
|
||||||
@ -6,6 +8,7 @@ import 'package:syncrow_web/pages/routiens/widgets/dialog_header.dart';
|
|||||||
import 'package:syncrow_web/pages/routiens/widgets/dialog_footer.dart';
|
import 'package:syncrow_web/pages/routiens/widgets/dialog_footer.dart';
|
||||||
import 'package:syncrow_web/utils/color_manager.dart';
|
import 'package:syncrow_web/utils/color_manager.dart';
|
||||||
import 'package:syncrow_web/utils/constants/assets.dart';
|
import 'package:syncrow_web/utils/constants/assets.dart';
|
||||||
|
import 'package:syncrow_web/utils/extension/build_context_x.dart';
|
||||||
|
|
||||||
class SaveRoutineHelper {
|
class SaveRoutineHelper {
|
||||||
static Future<void> showSaveRoutineDialog(BuildContext context) async {
|
static Future<void> showSaveRoutineDialog(BuildContext context) async {
|
||||||
@ -98,18 +101,29 @@ class SaveRoutineHelper {
|
|||||||
final functions =
|
final functions =
|
||||||
state.selectedFunctions[item['uniqueCustomId']] ?? [];
|
state.selectedFunctions[item['uniqueCustomId']] ?? [];
|
||||||
return ListTile(
|
return ListTile(
|
||||||
leading: SvgPicture.asset(
|
leading: item['type'] == 'tap_to_run'
|
||||||
|
? Image.memory(
|
||||||
|
base64Decode(item['icon']),
|
||||||
|
width: 22,
|
||||||
|
height: 22,
|
||||||
|
)
|
||||||
|
: SvgPicture.asset(
|
||||||
item['imagePath'],
|
item['imagePath'],
|
||||||
width: 22,
|
width: 22,
|
||||||
height: 22,
|
height: 22,
|
||||||
),
|
),
|
||||||
title:
|
title: Text(
|
||||||
Text(item['title'], style: const TextStyle(fontSize: 14)),
|
item['title'],
|
||||||
|
style: context.textTheme.bodySmall?.copyWith(
|
||||||
|
fontSize: 14,
|
||||||
|
color: ColorsManager.grayColor,
|
||||||
|
),
|
||||||
|
),
|
||||||
subtitle: Wrap(
|
subtitle: Wrap(
|
||||||
children: functions
|
children: functions
|
||||||
.map((f) => Text(
|
.map((f) => Text(
|
||||||
'${f.operationName}: ${f.value}, ',
|
'${f.operationName}: ${f.value}, ',
|
||||||
style: const TextStyle(
|
style: context.textTheme.bodySmall?.copyWith(
|
||||||
color: ColorsManager.grayColor, fontSize: 8),
|
color: ColorsManager.grayColor, fontSize: 8),
|
||||||
overflow: TextOverflow.ellipsis,
|
overflow: TextOverflow.ellipsis,
|
||||||
maxLines: 3,
|
maxLines: 3,
|
||||||
@ -124,17 +138,17 @@ class SaveRoutineHelper {
|
|||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
if (state.errorMessage != null || state.errorMessage!.isNotEmpty)
|
// if (state.errorMessage != null || state.errorMessage!.isNotEmpty)
|
||||||
Padding(
|
// Padding(
|
||||||
padding: const EdgeInsets.all(8.0),
|
// padding: const EdgeInsets.all(8.0),
|
||||||
child: Text(
|
// child: Text(
|
||||||
state.errorMessage!,
|
// state.errorMessage!,
|
||||||
style: const TextStyle(color: Colors.red),
|
// style: const TextStyle(color: Colors.red),
|
||||||
),
|
// ),
|
||||||
),
|
// ),
|
||||||
DialogFooter(
|
DialogFooter(
|
||||||
onCancel: () => Navigator.pop(context),
|
onCancel: () => Navigator.pop(context),
|
||||||
onConfirm: () {
|
onConfirm: () async {
|
||||||
if (state.isAutomation) {
|
if (state.isAutomation) {
|
||||||
if (state.automationId != null) {
|
if (state.automationId != null) {
|
||||||
context.read<RoutineBloc>().add(const UpdateAutomation());
|
context.read<RoutineBloc>().add(const UpdateAutomation());
|
||||||
@ -148,10 +162,9 @@ class SaveRoutineHelper {
|
|||||||
context.read<RoutineBloc>().add(const CreateSceneEvent());
|
context.read<RoutineBloc>().add(const CreateSceneEvent());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (context.read<RoutineBloc>().state.errorMessage == null ||
|
// if (state.errorMessage == null || state.errorMessage!.isEmpty) {
|
||||||
context.read<RoutineBloc>().state.errorMessage!.isEmpty) {
|
|
||||||
Navigator.pop(context);
|
Navigator.pop(context);
|
||||||
}
|
// }
|
||||||
},
|
},
|
||||||
isConfirmEnabled: true,
|
isConfirmEnabled: true,
|
||||||
),
|
),
|
||||||
|
@ -137,11 +137,13 @@ class ConditionExpr {
|
|||||||
|
|
||||||
class AutomationAction {
|
class AutomationAction {
|
||||||
String entityId;
|
String entityId;
|
||||||
|
String? actionType;
|
||||||
String actionExecutor;
|
String actionExecutor;
|
||||||
ExecutorProperty? executorProperty;
|
ExecutorProperty? executorProperty;
|
||||||
|
|
||||||
AutomationAction({
|
AutomationAction({
|
||||||
required this.entityId,
|
required this.entityId,
|
||||||
|
this.actionType,
|
||||||
required this.actionExecutor,
|
required this.actionExecutor,
|
||||||
this.executorProperty,
|
this.executorProperty,
|
||||||
});
|
});
|
||||||
@ -150,12 +152,15 @@ class AutomationAction {
|
|||||||
return {
|
return {
|
||||||
'entityId': entityId,
|
'entityId': entityId,
|
||||||
'actionExecutor': actionExecutor,
|
'actionExecutor': actionExecutor,
|
||||||
|
if (executorProperty != null)
|
||||||
'executorProperty': executorProperty?.toMap(),
|
'executorProperty': executorProperty?.toMap(),
|
||||||
|
'actionType': actionType
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
factory AutomationAction.fromMap(Map<String, dynamic> map) {
|
factory AutomationAction.fromMap(Map<String, dynamic> map) {
|
||||||
return AutomationAction(
|
return AutomationAction(
|
||||||
|
actionType: map['actionType'],
|
||||||
entityId: map['entityId'] ?? '',
|
entityId: map['entityId'] ?? '',
|
||||||
actionExecutor: map['actionExecutor'] ?? '',
|
actionExecutor: map['actionExecutor'] ?? '',
|
||||||
executorProperty: map['executorProperty'] != null
|
executorProperty: map['executorProperty'] != null
|
||||||
|
@ -95,10 +95,12 @@ class CreateSceneModel {
|
|||||||
|
|
||||||
class CreateSceneAction {
|
class CreateSceneAction {
|
||||||
String entityId;
|
String entityId;
|
||||||
|
String? actionType;
|
||||||
String actionExecutor;
|
String actionExecutor;
|
||||||
CreateSceneExecutorProperty? executorProperty;
|
CreateSceneExecutorProperty? executorProperty;
|
||||||
|
|
||||||
CreateSceneAction({
|
CreateSceneAction({
|
||||||
|
this.actionType,
|
||||||
required this.entityId,
|
required this.entityId,
|
||||||
required this.actionExecutor,
|
required this.actionExecutor,
|
||||||
required this.executorProperty,
|
required this.executorProperty,
|
||||||
@ -110,6 +112,7 @@ class CreateSceneAction {
|
|||||||
CreateSceneExecutorProperty? executorProperty,
|
CreateSceneExecutorProperty? executorProperty,
|
||||||
}) {
|
}) {
|
||||||
return CreateSceneAction(
|
return CreateSceneAction(
|
||||||
|
actionType: actionType ?? this.actionType,
|
||||||
entityId: entityId ?? this.entityId,
|
entityId: entityId ?? this.entityId,
|
||||||
actionExecutor: actionExecutor ?? this.actionExecutor,
|
actionExecutor: actionExecutor ?? this.actionExecutor,
|
||||||
executorProperty: executorProperty ?? this.executorProperty,
|
executorProperty: executorProperty ?? this.executorProperty,
|
||||||
@ -125,6 +128,7 @@ class CreateSceneAction {
|
|||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
return {
|
return {
|
||||||
|
"actionType": actionType,
|
||||||
'entityId': entityId,
|
'entityId': entityId,
|
||||||
'actionExecutor': actionExecutor,
|
'actionExecutor': actionExecutor,
|
||||||
};
|
};
|
||||||
@ -133,6 +137,7 @@ class CreateSceneAction {
|
|||||||
|
|
||||||
factory CreateSceneAction.fromMap(Map<String, dynamic> map) {
|
factory CreateSceneAction.fromMap(Map<String, dynamic> map) {
|
||||||
return CreateSceneAction(
|
return CreateSceneAction(
|
||||||
|
actionType: map['actionType'],
|
||||||
entityId: map['entityId'] ?? '',
|
entityId: map['entityId'] ?? '',
|
||||||
actionExecutor: map['actionExecutor'] ?? '',
|
actionExecutor: map['actionExecutor'] ?? '',
|
||||||
executorProperty:
|
executorProperty:
|
||||||
|
@ -35,6 +35,20 @@ class DraggableCard extends StatelessWidget {
|
|||||||
final deviceFunctions =
|
final deviceFunctions =
|
||||||
state.selectedFunctions[deviceData['uniqueCustomId']] ?? [];
|
state.selectedFunctions[deviceData['uniqueCustomId']] ?? [];
|
||||||
|
|
||||||
|
int index = state.thenItems.indexWhere(
|
||||||
|
(item) => item['uniqueCustomId'] == deviceData['uniqueCustomId']);
|
||||||
|
|
||||||
|
if (index != -1) {
|
||||||
|
return _buildCardContent(context, deviceFunctions, padding: padding);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ifIndex = state.ifItems.indexWhere(
|
||||||
|
(item) => item['uniqueCustomId'] == deviceData['uniqueCustomId']);
|
||||||
|
|
||||||
|
if (ifIndex != -1) {
|
||||||
|
return _buildCardContent(context, deviceFunctions, padding: padding);
|
||||||
|
}
|
||||||
|
|
||||||
return Draggable<Map<String, dynamic>>(
|
return Draggable<Map<String, dynamic>>(
|
||||||
data: deviceData,
|
data: deviceData,
|
||||||
feedback: Transform.rotate(
|
feedback: Transform.rotate(
|
||||||
@ -79,16 +93,12 @@ class DraggableCard extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
padding: const EdgeInsets.all(8),
|
padding: const EdgeInsets.all(8),
|
||||||
child: imagePath.contains('.svg')
|
child: deviceData['type'] == 'tap_to_run'
|
||||||
? SvgPicture.asset(
|
? Image.memory(
|
||||||
imagePath,
|
base64Decode(deviceData['icon']),
|
||||||
)
|
)
|
||||||
: imagePath.contains('.png')
|
: SvgPicture.asset(
|
||||||
? Image.asset(
|
|
||||||
imagePath,
|
imagePath,
|
||||||
)
|
|
||||||
: Image.memory(
|
|
||||||
base64Decode(imagePath),
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
const SizedBox(height: 8),
|
const SizedBox(height: 8),
|
||||||
|
@ -7,98 +7,106 @@ import 'package:syncrow_web/pages/routiens/widgets/dialog_header.dart';
|
|||||||
import 'package:syncrow_web/pages/routiens/widgets/dialog_footer.dart';
|
import 'package:syncrow_web/pages/routiens/widgets/dialog_footer.dart';
|
||||||
import 'package:syncrow_web/utils/constants/assets.dart';
|
import 'package:syncrow_web/utils/constants/assets.dart';
|
||||||
|
|
||||||
class AutomationDialog extends StatelessWidget {
|
class AutomationDialog extends StatefulWidget {
|
||||||
final String automationName;
|
final String automationName;
|
||||||
final String automationId;
|
final String automationId;
|
||||||
final String uniqueCustomId;
|
final String uniqueCustomId;
|
||||||
|
final String? passedAutomationActionExecutor;
|
||||||
|
|
||||||
const AutomationDialog({
|
const AutomationDialog({
|
||||||
super.key,
|
super.key,
|
||||||
required this.automationName,
|
required this.automationName,
|
||||||
required this.automationId,
|
required this.automationId,
|
||||||
required this.uniqueCustomId,
|
required this.uniqueCustomId,
|
||||||
|
this.passedAutomationActionExecutor,
|
||||||
});
|
});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
State<AutomationDialog> createState() => _AutomationDialogState();
|
||||||
return BlocBuilder<RoutineBloc, RoutineState>(
|
}
|
||||||
builder: (context, state) {
|
|
||||||
final isEnabled = state.automationActionExecutor == 'rule_enable';
|
|
||||||
|
|
||||||
|
class _AutomationDialogState extends State<AutomationDialog> {
|
||||||
|
String? selectedAutomationActionExecutor;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
List<DeviceFunctionData>? functions = context
|
||||||
|
.read<RoutineBloc>()
|
||||||
|
.state
|
||||||
|
.selectedFunctions[widget.uniqueCustomId];
|
||||||
|
for (DeviceFunctionData data in functions ?? []) {
|
||||||
|
if (data.entityId == widget.automationId) {
|
||||||
|
selectedAutomationActionExecutor = data.value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
return Dialog(
|
return Dialog(
|
||||||
shape:
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
||||||
RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
|
||||||
child: Container(
|
child: Container(
|
||||||
width: 400,
|
width: 400,
|
||||||
padding: const EdgeInsets.all(16),
|
padding: const EdgeInsets.all(16),
|
||||||
child: Column(
|
child: Column(
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: [
|
children: [
|
||||||
DialogHeader(automationName),
|
DialogHeader(widget.automationName),
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
ListTile(
|
ListTile(
|
||||||
leading:
|
leading: SvgPicture.asset(Assets.acPower, width: 24, height: 24),
|
||||||
SvgPicture.asset(Assets.acPower, width: 24, height: 24),
|
|
||||||
title: const Text('Enable'),
|
title: const Text('Enable'),
|
||||||
trailing: Radio<bool>(
|
trailing: Radio<String?>(
|
||||||
value: true,
|
value: 'rule_enable',
|
||||||
groupValue: isEnabled,
|
groupValue: selectedAutomationActionExecutor,
|
||||||
onChanged: (bool? value) {
|
onChanged: (String? value) {
|
||||||
if (value == true) {
|
setState(() {
|
||||||
context.read<RoutineBloc>().add(
|
selectedAutomationActionExecutor = 'rule_enable';
|
||||||
const SetAutomationActionExecutor(
|
});
|
||||||
automationActionExecutor: 'rule_enable',
|
}),
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
ListTile(
|
ListTile(
|
||||||
leading: SvgPicture.asset(Assets.acPowerOff,
|
leading:
|
||||||
width: 24, height: 24),
|
SvgPicture.asset(Assets.acPowerOff, width: 24, height: 24),
|
||||||
title: const Text('Disable'),
|
title: const Text('Disable'),
|
||||||
trailing: Radio<bool>(
|
trailing: Radio<String?>(
|
||||||
value: false,
|
value: 'rule_disable',
|
||||||
groupValue: isEnabled,
|
groupValue: selectedAutomationActionExecutor,
|
||||||
onChanged: (bool? value) {
|
onChanged: (String? value) {
|
||||||
if (value == false) {
|
setState(() {
|
||||||
context.read<RoutineBloc>().add(
|
selectedAutomationActionExecutor = 'rule_disable';
|
||||||
const SetAutomationActionExecutor(
|
});
|
||||||
automationActionExecutor: 'rule_disable',
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
DialogFooter(
|
DialogFooter(
|
||||||
onConfirm: () {
|
onConfirm: () {
|
||||||
|
if (selectedAutomationActionExecutor != null) {
|
||||||
context.read<RoutineBloc>().add(
|
context.read<RoutineBloc>().add(
|
||||||
AddFunctionToRoutine(
|
AddFunctionToRoutine(
|
||||||
[
|
[
|
||||||
DeviceFunctionData(
|
DeviceFunctionData(
|
||||||
entityId: automationId,
|
entityId: widget.automationId,
|
||||||
functionCode: 'automation',
|
functionCode: 'automation',
|
||||||
value: state.automationActionExecutor,
|
value: selectedAutomationActionExecutor,
|
||||||
operationName: 'Automation',
|
operationName: 'Automation',
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
uniqueCustomId,
|
widget.uniqueCustomId,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
}
|
||||||
Navigator.of(context).pop(true);
|
Navigator.of(context).pop(true);
|
||||||
},
|
},
|
||||||
onCancel: () => Navigator.of(context).pop(false),
|
onCancel: () => Navigator.of(context).pop(),
|
||||||
isConfirmEnabled: true,
|
isConfirmEnabled: selectedAutomationActionExecutor != null,
|
||||||
dialogWidth: 400,
|
dialogWidth: 400,
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,6 +43,13 @@ class _RoutineSearchAndButtonsState extends State<RoutineSearchAndButtons> {
|
|||||||
return Wrap(
|
return Wrap(
|
||||||
runSpacing: 16,
|
runSpacing: 16,
|
||||||
children: [
|
children: [
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.all(8.0),
|
||||||
|
child: Text(
|
||||||
|
state.errorMessage ?? '',
|
||||||
|
style: const TextStyle(color: Colors.red),
|
||||||
|
),
|
||||||
|
),
|
||||||
Row(
|
Row(
|
||||||
crossAxisAlignment: CrossAxisAlignment.end,
|
crossAxisAlignment: CrossAxisAlignment.end,
|
||||||
children: [
|
children: [
|
||||||
@ -214,6 +221,7 @@ class _RoutineSearchAndButtonsState extends State<RoutineSearchAndButtons> {
|
|||||||
}
|
}
|
||||||
// final result =
|
// final result =
|
||||||
// await
|
// await
|
||||||
|
BlocProvider.of<RoutineBloc>(context).add(ResetErrorMessage());
|
||||||
SaveRoutineHelper.showSaveRoutineDialog(context);
|
SaveRoutineHelper.showSaveRoutineDialog(context);
|
||||||
// if (result != null && result) {
|
// if (result != null && result) {
|
||||||
// BlocProvider.of<RoutineBloc>(context).add(
|
// BlocProvider.of<RoutineBloc>(context).add(
|
||||||
@ -341,6 +349,7 @@ class _RoutineSearchAndButtonsState extends State<RoutineSearchAndButtons> {
|
|||||||
}
|
}
|
||||||
// final result =
|
// final result =
|
||||||
// await
|
// await
|
||||||
|
BlocProvider.of<RoutineBloc>(context).add(ResetErrorMessage());
|
||||||
SaveRoutineHelper.showSaveRoutineDialog(context);
|
SaveRoutineHelper.showSaveRoutineDialog(context);
|
||||||
// if (result != null && result) {
|
// if (result != null && result) {
|
||||||
// BlocProvider.of<RoutineBloc>(context).add(
|
// BlocProvider.of<RoutineBloc>(context).add(
|
||||||
|
@ -87,8 +87,11 @@ class ThenContainer extends StatelessWidget {
|
|||||||
...state.thenItems[index],
|
...state.thenItems[index],
|
||||||
'imagePath':
|
'imagePath':
|
||||||
Assets.automation,
|
Assets.automation,
|
||||||
'title': state
|
'title':
|
||||||
.thenItems[index]['name'],
|
state.thenItems[index]
|
||||||
|
['name'] ??
|
||||||
|
state.thenItems[index]
|
||||||
|
['title'],
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@ -149,6 +152,12 @@ class ThenContainer extends StatelessWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (mutableData['type'] == 'automation') {
|
if (mutableData['type'] == 'automation') {
|
||||||
|
int index = state.thenItems.indexWhere(
|
||||||
|
(item) => item['deviceId'] == mutableData['deviceId']);
|
||||||
|
if (index != -1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
final result = await showDialog<bool>(
|
final result = await showDialog<bool>(
|
||||||
context: context,
|
context: context,
|
||||||
builder: (BuildContext context) => AutomationDialog(
|
builder: (BuildContext context) => AutomationDialog(
|
||||||
@ -169,9 +178,14 @@ class ThenContainer extends StatelessWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (mutableData['type'] == 'tap_to_run' && state.isAutomation) {
|
if (mutableData['type'] == 'tap_to_run' && state.isAutomation) {
|
||||||
|
int index = state.thenItems.indexWhere(
|
||||||
|
(item) => item['deviceId'] == mutableData['deviceId']);
|
||||||
|
if (index != -1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
context.read<RoutineBloc>().add(AddToThenContainer({
|
context.read<RoutineBloc>().add(AddToThenContainer({
|
||||||
...mutableData,
|
...mutableData,
|
||||||
'imagePath': Assets.logo,
|
'imagePath': mutableData['imagePath'] ?? Assets.logo,
|
||||||
'title': mutableData['name'],
|
'title': mutableData['name'],
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:syncrow_web/utils/color_manager.dart';
|
||||||
import 'package:syncrow_web/utils/navigation_service.dart';
|
import 'package:syncrow_web/utils/navigation_service.dart';
|
||||||
|
|
||||||
class CustomSnackBar {
|
class CustomSnackBar {
|
||||||
@ -11,6 +12,35 @@ class CustomSnackBar {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static redSnackBar(String message) {
|
||||||
|
final key = NavigationService.snackbarKey;
|
||||||
|
BuildContext? currentContext = key?.currentContext;
|
||||||
|
if (key != null && currentContext != null) {
|
||||||
|
final snackBar = SnackBar(
|
||||||
|
padding: const EdgeInsets.all(16),
|
||||||
|
backgroundColor: ColorsManager.red,
|
||||||
|
content: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
|
||||||
|
const Icon(
|
||||||
|
Icons.check_circle,
|
||||||
|
color: ColorsManager.whiteColors,
|
||||||
|
size: 32,
|
||||||
|
),
|
||||||
|
const SizedBox(
|
||||||
|
width: 8,
|
||||||
|
),
|
||||||
|
Text(
|
||||||
|
message,
|
||||||
|
style: Theme.of(currentContext)
|
||||||
|
.textTheme
|
||||||
|
.bodySmall!
|
||||||
|
.copyWith(fontSize: 14, fontWeight: FontWeight.w500, color: Colors.green),
|
||||||
|
)
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
key.currentState?.showSnackBar(snackBar);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static greenSnackBar(String message) {
|
static greenSnackBar(String message) {
|
||||||
final key = NavigationService.snackbarKey;
|
final key = NavigationService.snackbarKey;
|
||||||
BuildContext? currentContext = key?.currentContext;
|
BuildContext? currentContext = key?.currentContext;
|
||||||
@ -29,8 +59,10 @@ class CustomSnackBar {
|
|||||||
),
|
),
|
||||||
Text(
|
Text(
|
||||||
message,
|
message,
|
||||||
style: Theme.of(currentContext).textTheme.bodySmall!.copyWith(
|
style: Theme.of(currentContext)
|
||||||
fontSize: 14, fontWeight: FontWeight.w500, color: Colors.green),
|
.textTheme
|
||||||
|
.bodySmall!
|
||||||
|
.copyWith(fontSize: 14, fontWeight: FontWeight.w500, color: Colors.green),
|
||||||
)
|
)
|
||||||
]),
|
]),
|
||||||
);
|
);
|
||||||
|
Reference in New Issue
Block a user