|
|
|
@ -4,6 +4,7 @@ import 'package:bloc/bloc.dart';
|
|
|
|
|
import 'package:equatable/equatable.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:syncrow_web/pages/common/bloc/project_cubit.dart';
|
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
|
import 'package:syncrow_web/pages/device_managment/all_devices/models/devices_model.dart';
|
|
|
|
|
import 'package:syncrow_web/pages/routines/models/create_scene_and_autoamtion/create_automation_model.dart';
|
|
|
|
|
import 'package:syncrow_web/pages/routines/models/create_scene_and_autoamtion/create_scene_model.dart';
|
|
|
|
@ -11,18 +12,20 @@ import 'package:syncrow_web/pages/routines/models/delay/delay_fucntions.dart';
|
|
|
|
|
import 'package:syncrow_web/pages/routines/models/device_functions.dart';
|
|
|
|
|
import 'package:syncrow_web/pages/routines/models/routine_details_model.dart';
|
|
|
|
|
import 'package:syncrow_web/pages/routines/models/routine_model.dart';
|
|
|
|
|
import 'package:syncrow_web/pages/space_tree/bloc/space_tree_bloc.dart';
|
|
|
|
|
import 'package:syncrow_web/services/devices_mang_api.dart';
|
|
|
|
|
import 'package:syncrow_web/services/routines_api.dart';
|
|
|
|
|
import 'package:syncrow_web/utils/constants/assets.dart';
|
|
|
|
|
import 'package:syncrow_web/utils/constants/temp_const.dart';
|
|
|
|
|
import 'package:syncrow_web/utils/navigation_service.dart';
|
|
|
|
|
import 'package:syncrow_web/utils/snack_bar.dart';
|
|
|
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
|
|
|
|
|
|
part 'routine_event.dart';
|
|
|
|
|
part 'routine_state.dart';
|
|
|
|
|
|
|
|
|
|
String spaceId = '25c96044-fadf-44bb-93c7-3c079e527ce6';
|
|
|
|
|
String communityId = 'aff21a57-2f91-4e5c-b99b-0182c3ab65a9';
|
|
|
|
|
// String spaceId = '25c96044-fadf-44bb-93c7-3c079e527ce6';
|
|
|
|
|
// String communityId = 'aff21a57-2f91-4e5c-b99b-0182c3ab65a9';
|
|
|
|
|
|
|
|
|
|
class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
final ProjectCubit projectCubit;
|
|
|
|
@ -58,12 +61,11 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
TriggerSwitchTabsEvent event,
|
|
|
|
|
Emitter<RoutineState> emit,
|
|
|
|
|
) {
|
|
|
|
|
emit(state.copyWith(
|
|
|
|
|
routineTab: event.isRoutineTab, createRoutineView: false));
|
|
|
|
|
emit(state.copyWith(routineTab: event.isRoutineTab, createRoutineView: false));
|
|
|
|
|
add(ResetRoutineState());
|
|
|
|
|
if (event.isRoutineTab) {
|
|
|
|
|
add(LoadScenes(spaceId, communityId));
|
|
|
|
|
add(LoadAutomation(spaceId));
|
|
|
|
|
add(const LoadScenes());
|
|
|
|
|
add(const LoadAutomation());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -85,8 +87,8 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
final updatedIfItems = List<Map<String, dynamic>>.from(state.ifItems);
|
|
|
|
|
|
|
|
|
|
// Find the index of the item in teh current itemsList
|
|
|
|
|
int index = updatedIfItems.indexWhere(
|
|
|
|
|
(map) => map['uniqueCustomId'] == event.item['uniqueCustomId']);
|
|
|
|
|
int index =
|
|
|
|
|
updatedIfItems.indexWhere((map) => map['uniqueCustomId'] == event.item['uniqueCustomId']);
|
|
|
|
|
// Replace the map if the index is valid
|
|
|
|
|
if (index != -1) {
|
|
|
|
|
updatedIfItems[index] = event.item;
|
|
|
|
@ -95,21 +97,18 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.isTabToRun) {
|
|
|
|
|
emit(state.copyWith(
|
|
|
|
|
ifItems: updatedIfItems, isTabToRun: true, isAutomation: false));
|
|
|
|
|
emit(state.copyWith(ifItems: updatedIfItems, isTabToRun: true, isAutomation: false));
|
|
|
|
|
} 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);
|
|
|
|
|
|
|
|
|
|
// Find the index of the item in teh current itemsList
|
|
|
|
|
int index = currentItems.indexWhere(
|
|
|
|
|
(map) => map['uniqueCustomId'] == event.item['uniqueCustomId']);
|
|
|
|
|
int index =
|
|
|
|
|
currentItems.indexWhere((map) => map['uniqueCustomId'] == event.item['uniqueCustomId']);
|
|
|
|
|
// Replace the map if the index is valid
|
|
|
|
|
if (index != -1) {
|
|
|
|
|
currentItems[index] = event.item;
|
|
|
|
@ -120,26 +119,22 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
emit(state.copyWith(thenItems: currentItems));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _onAddFunctionsToRoutine(
|
|
|
|
|
AddFunctionToRoutine event, Emitter<RoutineState> emit) {
|
|
|
|
|
void _onAddFunctionsToRoutine(AddFunctionToRoutine event, Emitter<RoutineState> emit) {
|
|
|
|
|
try {
|
|
|
|
|
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>>.from(state.selectedFunctions);
|
|
|
|
|
if (currentSelectedFunctions.containsKey(event.uniqueCustomId)) {
|
|
|
|
|
List<DeviceFunctionData> currentFunctions =
|
|
|
|
|
List<DeviceFunctionData>.from(
|
|
|
|
|
currentSelectedFunctions[event.uniqueCustomId] ?? []);
|
|
|
|
|
List<DeviceFunctionData>.from(currentSelectedFunctions[event.uniqueCustomId] ?? []);
|
|
|
|
|
|
|
|
|
|
List<String> functionCode = [];
|
|
|
|
|
for (int i = 0; i < selectedFunction.length; i++) {
|
|
|
|
|
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];
|
|
|
|
|
if (!functionCode.contains(currentFunctions[j].functionCode)) {
|
|
|
|
|
functionCode.add(currentFunctions[j].functionCode);
|
|
|
|
@ -149,15 +144,13 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)..addAll(selectedFunction);
|
|
|
|
|
currentSelectedFunctions[event.uniqueCustomId] = List.from(currentFunctions)
|
|
|
|
|
..addAll(selectedFunction);
|
|
|
|
|
} else {
|
|
|
|
|
currentSelectedFunctions[event.uniqueCustomId] =
|
|
|
|
|
List.from(event.functions);
|
|
|
|
|
currentSelectedFunctions[event.uniqueCustomId] = List.from(event.functions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
emit(state.copyWith(selectedFunctions: currentSelectedFunctions));
|
|
|
|
@ -166,21 +159,21 @@ 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));
|
|
|
|
|
|
|
|
|
|
List<ScenesModel> scenes = [];
|
|
|
|
|
try {
|
|
|
|
|
final projectUuid = projectCubit.state;
|
|
|
|
|
|
|
|
|
|
spaceId = event.spaceId;
|
|
|
|
|
communityId = event.communityId;
|
|
|
|
|
|
|
|
|
|
List<ScenesModel> scenes = [];
|
|
|
|
|
|
|
|
|
|
if (communityId.isNotEmpty && spaceId.isNotEmpty) {
|
|
|
|
|
scenes = await SceneApi.getScenes(event.spaceId, event.communityId, projectUuid ?? TempConst.projectId);
|
|
|
|
|
BuildContext context = NavigationService.navigatorKey.currentContext!;
|
|
|
|
|
var spaceBloc = context.read<SpaceTreeBloc>();
|
|
|
|
|
for (var communityId in spaceBloc.state.selectedCommunities) {
|
|
|
|
|
List<String> spacesList = spaceBloc.state.selectedCommunityAndSpaces[communityId] ?? [];
|
|
|
|
|
for (var spaceId in spacesList) {
|
|
|
|
|
scenes.addAll(
|
|
|
|
|
await SceneApi.getScenes(spaceId, communityId, projectUuid ?? TempConst.projectId));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
emit(state.copyWith(
|
|
|
|
|
scenes: scenes,
|
|
|
|
|
isLoading: false,
|
|
|
|
@ -191,19 +184,22 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
loadScenesErrorMessage: 'Failed to load scenes',
|
|
|
|
|
errorMessage: '',
|
|
|
|
|
loadAutomationErrorMessage: '',
|
|
|
|
|
scenes: []));
|
|
|
|
|
scenes: scenes));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
List<ScenesModel> automations = [];
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
spaceId = event.spaceId;
|
|
|
|
|
List<ScenesModel> automations = [];
|
|
|
|
|
if (spaceId.isNotEmpty) {
|
|
|
|
|
automations = await SceneApi.getAutomation(event.spaceId);
|
|
|
|
|
BuildContext context = NavigationService.navigatorKey.currentContext!;
|
|
|
|
|
var spaceBloc = context.read<SpaceTreeBloc>();
|
|
|
|
|
for (var communityId in spaceBloc.state.selectedCommunities) {
|
|
|
|
|
List<String> spacesList = spaceBloc.state.selectedCommunityAndSpaces[communityId] ?? [];
|
|
|
|
|
for (var spaceId in spacesList) {
|
|
|
|
|
automations.addAll(await SceneApi.getAutomation(spaceId));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
emit(state.copyWith(
|
|
|
|
|
automations: automations,
|
|
|
|
@ -215,20 +211,18 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
loadAutomationErrorMessage: 'Failed to load automations',
|
|
|
|
|
errorMessage: '',
|
|
|
|
|
loadScenesErrorMessage: '',
|
|
|
|
|
automations: []));
|
|
|
|
|
automations: automations));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
await Future.delayed(const Duration(seconds: 1));
|
|
|
|
|
emit(state.copyWith(isLoading: false, errorMessage: null));
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -242,8 +236,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
return actions.last['deviceId'] == 'delay';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> _onCreateScene(
|
|
|
|
|
CreateSceneEvent event, Emitter<RoutineState> emit) async {
|
|
|
|
|
Future<void> _onCreateScene(CreateSceneEvent event, Emitter<RoutineState> emit) async {
|
|
|
|
|
try {
|
|
|
|
|
// Check if first action is delay
|
|
|
|
|
// if (_isFirstActionDelay(state.thenItems)) {
|
|
|
|
@ -256,8 +249,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
|
|
|
|
|
if (_isLastActionDelay(state.thenItems)) {
|
|
|
|
|
emit(state.copyWith(
|
|
|
|
|
errorMessage:
|
|
|
|
|
'A delay condition cannot be the only or the last action',
|
|
|
|
|
errorMessage: 'A delay condition cannot be the only or the last action',
|
|
|
|
|
isLoading: false,
|
|
|
|
|
));
|
|
|
|
|
return;
|
|
|
|
@ -302,8 +294,11 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
});
|
|
|
|
|
}).toList();
|
|
|
|
|
|
|
|
|
|
BuildContext context = NavigationService.navigatorKey.currentContext!;
|
|
|
|
|
var spaceBloc = context.read<SpaceTreeBloc>();
|
|
|
|
|
|
|
|
|
|
final createSceneModel = CreateSceneModel(
|
|
|
|
|
spaceUuid: spaceId,
|
|
|
|
|
spaceUuid: spaceBloc.state.selectedSpaces[0],
|
|
|
|
|
iconId: state.selectedIcon ?? '',
|
|
|
|
|
showInDevice: true,
|
|
|
|
|
sceneName: state.routineName ?? '',
|
|
|
|
@ -314,8 +309,8 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
final result = await SceneApi.createScene(createSceneModel);
|
|
|
|
|
if (result['success']) {
|
|
|
|
|
add(ResetRoutineState());
|
|
|
|
|
add(LoadScenes(spaceId, communityId));
|
|
|
|
|
add(LoadAutomation(spaceId));
|
|
|
|
|
add(const LoadScenes());
|
|
|
|
|
add(const LoadAutomation());
|
|
|
|
|
} else {
|
|
|
|
|
emit(state.copyWith(
|
|
|
|
|
isLoading: false,
|
|
|
|
@ -330,8 +325,7 @@ 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 {
|
|
|
|
|
if (state.routineName == null || state.routineName!.isEmpty) {
|
|
|
|
|
emit(state.copyWith(
|
|
|
|
@ -352,8 +346,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
|
|
|
|
|
if (_isLastActionDelay(state.thenItems)) {
|
|
|
|
|
emit(state.copyWith(
|
|
|
|
|
errorMessage:
|
|
|
|
|
'A delay condition cannot be the only or the last action',
|
|
|
|
|
errorMessage: 'A delay condition cannot be the only or the last action',
|
|
|
|
|
isLoading: false,
|
|
|
|
|
));
|
|
|
|
|
CustomSnackBar.redSnackBar('Cannot have delay as the last action');
|
|
|
|
@ -428,9 +421,11 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}).toList();
|
|
|
|
|
BuildContext context = NavigationService.navigatorKey.currentContext!;
|
|
|
|
|
var spaceBloc = context.read<SpaceTreeBloc>();
|
|
|
|
|
|
|
|
|
|
final createAutomationModel = CreateAutomationModel(
|
|
|
|
|
spaceUuid: spaceId,
|
|
|
|
|
spaceUuid: spaceBloc.state.selectedSpaces[0],
|
|
|
|
|
automationName: state.routineName ?? '',
|
|
|
|
|
decisionExpr: state.selectedAutomationOperator,
|
|
|
|
|
effectiveTime: EffectiveTime(
|
|
|
|
@ -445,8 +440,8 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
final result = await SceneApi.createAutomation(createAutomationModel);
|
|
|
|
|
if (result['success']) {
|
|
|
|
|
add(ResetRoutineState());
|
|
|
|
|
add(LoadAutomation(spaceId));
|
|
|
|
|
add(LoadScenes(spaceId, communityId));
|
|
|
|
|
add(const LoadAutomation());
|
|
|
|
|
add(const LoadScenes());
|
|
|
|
|
} else {
|
|
|
|
|
emit(state.copyWith(
|
|
|
|
|
isLoading: false,
|
|
|
|
@ -463,21 +458,17 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FutureOr<void> _onRemoveDragCard(
|
|
|
|
|
RemoveDragCard event, Emitter<RoutineState> emit) {
|
|
|
|
|
FutureOr<void> _onRemoveDragCard(RemoveDragCard event, Emitter<RoutineState> emit) {
|
|
|
|
|
if (event.isFromThen) {
|
|
|
|
|
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);
|
|
|
|
|
selectedFunctions.remove(event.key);
|
|
|
|
|
emit(state.copyWith(
|
|
|
|
|
thenItems: thenItems, selectedFunctions: selectedFunctions));
|
|
|
|
|
emit(state.copyWith(thenItems: thenItems, selectedFunctions: selectedFunctions));
|
|
|
|
|
} else {
|
|
|
|
|
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);
|
|
|
|
|
selectedFunctions.remove(event.key);
|
|
|
|
@ -488,8 +479,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
isAutomation: false,
|
|
|
|
|
isTabToRun: false));
|
|
|
|
|
} else {
|
|
|
|
|
emit(state.copyWith(
|
|
|
|
|
ifItems: ifItems, selectedFunctions: selectedFunctions));
|
|
|
|
|
emit(state.copyWith(ifItems: ifItems, selectedFunctions: selectedFunctions));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -501,23 +491,18 @@ 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));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FutureOr<void> _onSetRoutineName(
|
|
|
|
|
SetRoutineName event, Emitter<RoutineState> emit) {
|
|
|
|
|
FutureOr<void> _onSetRoutineName(SetRoutineName event, Emitter<RoutineState> emit) {
|
|
|
|
|
emit(state.copyWith(
|
|
|
|
|
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<RoutineCondition>? conditions,
|
|
|
|
|
Map<String, List<DeviceFunctionData>> currentFunctions,
|
|
|
|
@ -550,8 +535,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
'deviceId': condition.entityId,
|
|
|
|
|
'title': matchingDevice.name ?? condition.entityId,
|
|
|
|
|
'productType': condition.entityType,
|
|
|
|
|
'imagePath':
|
|
|
|
|
matchingDevice.getDefaultIcon(condition.entityType),
|
|
|
|
|
'imagePath': matchingDevice.getDefaultIcon(condition.entityType),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
final functions = matchingDevice.functions;
|
|
|
|
@ -587,11 +571,8 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
final cardData = {
|
|
|
|
|
'entityId': action.entityId,
|
|
|
|
|
'uniqueCustomId': const Uuid().v4(),
|
|
|
|
|
'deviceId':
|
|
|
|
|
action.actionExecutor == 'delay' ? 'delay' : action.entityId,
|
|
|
|
|
'title': action.actionExecutor == 'delay'
|
|
|
|
|
? 'Delay'
|
|
|
|
|
: (matchingDevice.name ?? 'Device'),
|
|
|
|
|
'deviceId': action.actionExecutor == 'delay' ? 'delay' : action.entityId,
|
|
|
|
|
'title': action.actionExecutor == 'delay' ? 'Delay' : (matchingDevice.name ?? 'Device'),
|
|
|
|
|
'productType': action.productType,
|
|
|
|
|
'imagePath': matchingDevice.getDefaultIcon(action.productType),
|
|
|
|
|
};
|
|
|
|
@ -634,8 +615,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
return (thenItems, ifItems, currentFunctions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> _onGetSceneDetails(
|
|
|
|
|
GetSceneDetails event, Emitter<RoutineState> emit) async {
|
|
|
|
|
Future<void> _onGetSceneDetails(GetSceneDetails event, Emitter<RoutineState> emit) async {
|
|
|
|
|
try {
|
|
|
|
|
emit(state.copyWith(
|
|
|
|
|
isLoading: true,
|
|
|
|
@ -683,12 +663,10 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
if (!deviceCards.containsKey(deviceId)) {
|
|
|
|
|
deviceCards[deviceId] = {
|
|
|
|
|
'entityId': action.entityId,
|
|
|
|
|
'deviceId':
|
|
|
|
|
action.actionExecutor == 'delay' ? 'delay' : action.entityId,
|
|
|
|
|
'uniqueCustomId':
|
|
|
|
|
action.type == 'automation' || action.actionExecutor == 'delay'
|
|
|
|
|
? const Uuid().v4()
|
|
|
|
|
: action.entityId,
|
|
|
|
|
'deviceId': action.actionExecutor == 'delay' ? 'delay' : action.entityId,
|
|
|
|
|
'uniqueCustomId': action.type == 'automation' || action.actionExecutor == 'delay'
|
|
|
|
|
? const Uuid().v4()
|
|
|
|
|
: action.entityId,
|
|
|
|
|
'title': action.actionExecutor == 'delay'
|
|
|
|
|
? 'Delay'
|
|
|
|
|
: action.type == 'automation'
|
|
|
|
@ -723,8 +701,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
// 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)) {
|
|
|
|
|
updatedFunctions[uniqueCustomId] = [];
|
|
|
|
|
}
|
|
|
|
@ -796,8 +773,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FutureOr<void> _onResetRoutineState(
|
|
|
|
|
ResetRoutineState event, Emitter<RoutineState> emit) {
|
|
|
|
|
FutureOr<void> _onResetRoutineState(ResetRoutineState event, Emitter<RoutineState> emit) {
|
|
|
|
|
emit(state.copyWith(
|
|
|
|
|
ifItems: [],
|
|
|
|
|
thenItems: [],
|
|
|
|
@ -821,18 +797,21 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
createRoutineView: false));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FutureOr<void> _deleteScene(DeleteScene event, Emitter<RoutineState> emit) {
|
|
|
|
|
FutureOr<void> _deleteScene(DeleteScene event, Emitter<RoutineState> emit) async {
|
|
|
|
|
try {
|
|
|
|
|
emit(state.copyWith(isLoading: true));
|
|
|
|
|
BuildContext context = NavigationService.navigatorKey.currentContext!;
|
|
|
|
|
var spaceBloc = context.read<SpaceTreeBloc>();
|
|
|
|
|
if (state.isTabToRun) {
|
|
|
|
|
SceneApi.deleteScene(unitUuid: spaceId, sceneId: state.sceneId ?? '');
|
|
|
|
|
await SceneApi.deleteScene(
|
|
|
|
|
unitUuid: spaceBloc.state.selectedSpaces[0], sceneId: state.sceneId ?? '');
|
|
|
|
|
} else {
|
|
|
|
|
SceneApi.deleteAutomation(
|
|
|
|
|
unitUuid: spaceId, automationId: state.automationId ?? '');
|
|
|
|
|
await SceneApi.deleteAutomation(
|
|
|
|
|
unitUuid: spaceBloc.state.selectedSpaces[0], automationId: state.automationId ?? '');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
add(LoadScenes(spaceId, communityId));
|
|
|
|
|
add(LoadAutomation(spaceId));
|
|
|
|
|
add(const LoadScenes());
|
|
|
|
|
add(const LoadAutomation());
|
|
|
|
|
add(ResetRoutineState());
|
|
|
|
|
emit(state.copyWith(isLoading: false, createRoutineView: false));
|
|
|
|
|
} catch (e) {
|
|
|
|
@ -857,13 +836,21 @@ 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));
|
|
|
|
|
try {
|
|
|
|
|
final projectUuid = projectCubit.state;
|
|
|
|
|
final devices = await DevicesManagementApi()
|
|
|
|
|
.fetchDevices('', '', projectUuid ?? TempConst.projectId);
|
|
|
|
|
List<AllDevicesModel> devices = [];
|
|
|
|
|
|
|
|
|
|
BuildContext context = NavigationService.navigatorKey.currentContext!;
|
|
|
|
|
var spaceBloc = context.read<SpaceTreeBloc>();
|
|
|
|
|
for (var communityId in spaceBloc.state.selectedCommunities) {
|
|
|
|
|
List<String> spacesList = spaceBloc.state.selectedCommunityAndSpaces[communityId] ?? [];
|
|
|
|
|
for (var spaceId in spacesList) {
|
|
|
|
|
devices.addAll(await DevicesManagementApi()
|
|
|
|
|
.fetchDevices(communityId, spaceId, projectUuid ?? TempConst.projectId));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
emit(state.copyWith(isLoading: false, devices: devices));
|
|
|
|
|
} catch (e) {
|
|
|
|
@ -871,8 +858,7 @@ 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 {
|
|
|
|
|
// Check if first action is delay
|
|
|
|
|
// if (_isFirstActionDelay(state.thenItems)) {
|
|
|
|
@ -886,8 +872,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
|
|
|
|
|
if (_isLastActionDelay(state.thenItems)) {
|
|
|
|
|
emit(state.copyWith(
|
|
|
|
|
errorMessage:
|
|
|
|
|
'A delay condition cannot be the only or the last action',
|
|
|
|
|
errorMessage: 'A delay condition cannot be the only or the last action',
|
|
|
|
|
isLoading: false,
|
|
|
|
|
));
|
|
|
|
|
return;
|
|
|
|
@ -940,12 +925,11 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
actions: actions,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
final result =
|
|
|
|
|
await SceneApi.updateScene(createSceneModel, state.sceneId ?? '');
|
|
|
|
|
final result = await SceneApi.updateScene(createSceneModel, state.sceneId ?? '');
|
|
|
|
|
if (result['success']) {
|
|
|
|
|
add(ResetRoutineState());
|
|
|
|
|
add(LoadScenes(spaceId, communityId));
|
|
|
|
|
add(LoadAutomation(spaceId));
|
|
|
|
|
add(const LoadScenes());
|
|
|
|
|
add(const LoadAutomation());
|
|
|
|
|
} else {
|
|
|
|
|
emit(state.copyWith(
|
|
|
|
|
isLoading: false,
|
|
|
|
@ -960,8 +944,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FutureOr<void> _onUpdateAutomation(
|
|
|
|
|
UpdateAutomation event, Emitter<RoutineState> emit) async {
|
|
|
|
|
FutureOr<void> _onUpdateAutomation(UpdateAutomation event, Emitter<RoutineState> emit) async {
|
|
|
|
|
try {
|
|
|
|
|
if (state.routineName == null || state.routineName!.isEmpty) {
|
|
|
|
|
emit(state.copyWith(
|
|
|
|
@ -1056,8 +1039,11 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
});
|
|
|
|
|
}).toList();
|
|
|
|
|
|
|
|
|
|
BuildContext context = NavigationService.navigatorKey.currentContext!;
|
|
|
|
|
var spaceBloc = context.read<SpaceTreeBloc>();
|
|
|
|
|
|
|
|
|
|
final createAutomationModel = CreateAutomationModel(
|
|
|
|
|
spaceUuid: spaceId,
|
|
|
|
|
spaceUuid: spaceBloc.state.selectedSpaces[0],
|
|
|
|
|
automationName: state.routineName ?? '',
|
|
|
|
|
decisionExpr: state.selectedAutomationOperator,
|
|
|
|
|
effectiveTime: EffectiveTime(
|
|
|
|
@ -1069,13 +1055,13 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
actions: actions,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
final result = await SceneApi.updateAutomation(
|
|
|
|
|
createAutomationModel, state.automationId ?? '');
|
|
|
|
|
final result =
|
|
|
|
|
await SceneApi.updateAutomation(createAutomationModel, state.automationId ?? '');
|
|
|
|
|
|
|
|
|
|
if (result['success']) {
|
|
|
|
|
add(ResetRoutineState());
|
|
|
|
|
add(LoadAutomation(spaceId));
|
|
|
|
|
add(LoadScenes(spaceId, communityId));
|
|
|
|
|
add(LoadAutomation());
|
|
|
|
|
add(LoadScenes());
|
|
|
|
|
} else {
|
|
|
|
|
emit(state.copyWith(
|
|
|
|
|
isLoading: false,
|
|
|
|
@ -1103,8 +1089,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
thenItems: [],
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
final automationDetails =
|
|
|
|
|
await SceneApi.getAutomationDetails(event.automationId);
|
|
|
|
|
final automationDetails = await SceneApi.getAutomationDetails(event.automationId);
|
|
|
|
|
|
|
|
|
|
final Map<String, Map<String, dynamic>> deviceIfCards = {};
|
|
|
|
|
final Map<String, Map<String, dynamic>> deviceThenCards = {};
|
|
|
|
@ -1172,15 +1157,13 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
final deviceId = action.actionExecutor == 'delay'
|
|
|
|
|
? '${action.entityId}_delay'
|
|
|
|
|
: action.entityId;
|
|
|
|
|
final deviceId =
|
|
|
|
|
action.actionExecutor == 'delay' ? '${action.entityId}_delay' : action.entityId;
|
|
|
|
|
|
|
|
|
|
if (!deviceThenCards.containsKey(deviceId)) {
|
|
|
|
|
deviceThenCards[deviceId] = {
|
|
|
|
|
'entityId': action.entityId,
|
|
|
|
|
'deviceId':
|
|
|
|
|
action.actionExecutor == 'delay' ? 'delay' : action.entityId,
|
|
|
|
|
'deviceId': action.actionExecutor == 'delay' ? 'delay' : action.entityId,
|
|
|
|
|
'uniqueCustomId': const Uuid().v4(),
|
|
|
|
|
'title': action.actionExecutor == 'delay'
|
|
|
|
|
? 'Delay'
|
|
|
|
@ -1211,8 +1194,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
updatedFunctions[uniqueCustomId] = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (action.executorProperty != null &&
|
|
|
|
|
action.actionExecutor != 'delay') {
|
|
|
|
|
if (action.executorProperty != null && action.actionExecutor != 'delay') {
|
|
|
|
|
final functions = matchingDevice.functions;
|
|
|
|
|
final functionCode = action.executorProperty!.functionCode;
|
|
|
|
|
for (var function in functions) {
|
|
|
|
@ -1254,14 +1236,10 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final ifItems = deviceIfCards.values
|
|
|
|
|
.where((card) => card['type'] == 'condition')
|
|
|
|
|
.toList();
|
|
|
|
|
final ifItems = deviceIfCards.values.where((card) => card['type'] == 'condition').toList();
|
|
|
|
|
final thenItems = deviceThenCards.values
|
|
|
|
|
.where((card) =>
|
|
|
|
|
card['type'] == 'action' ||
|
|
|
|
|
card['type'] == 'automation' ||
|
|
|
|
|
card['type'] == 'scene')
|
|
|
|
|
card['type'] == 'action' || card['type'] == 'automation' || card['type'] == 'scene')
|
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
|
|
emit(state.copyWith(
|
|
|
|
|