mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
@ -1,9 +1,11 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:syncrow_web/pages/common/bloc/project_manager.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/bloc/automation_scene_trigger_bloc/automation_status_update.dart';
|
||||
import 'package:syncrow_web/pages/routines/bloc/create_routine_bloc/create_routine_bloc.dart';
|
||||
@ -73,7 +75,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
}
|
||||
}
|
||||
|
||||
void _resetErrorMessage(
|
||||
_resetErrorMessage(
|
||||
ResetErrorMessage event,
|
||||
Emitter<RoutineState> emit,
|
||||
) {
|
||||
@ -91,7 +93,7 @@ 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
|
||||
final index = updatedIfItems.indexWhere(
|
||||
int index = updatedIfItems.indexWhere(
|
||||
(map) => map['uniqueCustomId'] == event.item['uniqueCustomId']);
|
||||
// Replace the map if the index is valid
|
||||
if (index != -1) {
|
||||
@ -114,7 +116,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
final currentItems = List<Map<String, dynamic>>.from(state.thenItems);
|
||||
|
||||
// Find the index of the item in teh current itemsList
|
||||
final index = currentItems.indexWhere(
|
||||
int index = currentItems.indexWhere(
|
||||
(map) => map['uniqueCustomId'] == event.item['uniqueCustomId']);
|
||||
// Replace the map if the index is valid
|
||||
if (index != -1) {
|
||||
@ -133,7 +135,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
|
||||
// List<DeviceFunctionData> selectedFunction = List<DeviceFunctionData>.from(event.functions);
|
||||
|
||||
final currentSelectedFunctions =
|
||||
Map<String, List<DeviceFunctionData>> currentSelectedFunctions =
|
||||
Map<String, List<DeviceFunctionData>>.from(state.selectedFunctions);
|
||||
|
||||
// if (currentSelectedFunctions.containsKey(event.uniqueCustomId)) {
|
||||
@ -174,18 +176,18 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
Future<void> _onLoadScenes(
|
||||
LoadScenes event, Emitter<RoutineState> emit) async {
|
||||
emit(state.copyWith(isLoading: true, errorMessage: null));
|
||||
final scenes = <ScenesModel>[];
|
||||
List<ScenesModel> scenes = [];
|
||||
try {
|
||||
final context = NavigationService.navigatorKey.currentContext!;
|
||||
final createRoutineBloc = context.read<CreateRoutineBloc>();
|
||||
BuildContext context = NavigationService.navigatorKey.currentContext!;
|
||||
var createRoutineBloc = context.read<CreateRoutineBloc>();
|
||||
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
||||
if (createRoutineBloc.selectedSpaceId == '' &&
|
||||
createRoutineBloc.selectedCommunityId == '') {
|
||||
final spaceBloc = context.read<SpaceTreeBloc>();
|
||||
for (final communityId in spaceBloc.state.selectedCommunities) {
|
||||
final spacesList =
|
||||
var spaceBloc = context.read<SpaceTreeBloc>();
|
||||
for (var communityId in spaceBloc.state.selectedCommunities) {
|
||||
List<String> spacesList =
|
||||
spaceBloc.state.selectedCommunityAndSpaces[communityId] ?? [];
|
||||
for (final spaceId in spacesList) {
|
||||
for (var spaceId in spacesList) {
|
||||
scenes.addAll(
|
||||
await SceneApi.getScenes(spaceId, communityId, projectUuid));
|
||||
}
|
||||
@ -214,19 +216,19 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
Future<void> _onLoadAutomation(
|
||||
LoadAutomation event, Emitter<RoutineState> emit) async {
|
||||
emit(state.copyWith(isLoading: true, errorMessage: null));
|
||||
final automations = <ScenesModel>[];
|
||||
List<ScenesModel> automations = [];
|
||||
final projectId = await ProjectManager.getProjectUUID() ?? '';
|
||||
|
||||
final context = NavigationService.navigatorKey.currentContext!;
|
||||
final createRoutineBloc = context.read<CreateRoutineBloc>();
|
||||
BuildContext context = NavigationService.navigatorKey.currentContext!;
|
||||
var createRoutineBloc = context.read<CreateRoutineBloc>();
|
||||
try {
|
||||
if (createRoutineBloc.selectedSpaceId == '' &&
|
||||
createRoutineBloc.selectedCommunityId == '') {
|
||||
final spaceBloc = context.read<SpaceTreeBloc>();
|
||||
for (final communityId in spaceBloc.state.selectedCommunities) {
|
||||
final spacesList =
|
||||
var spaceBloc = context.read<SpaceTreeBloc>();
|
||||
for (var communityId in spaceBloc.state.selectedCommunities) {
|
||||
List<String> spacesList =
|
||||
spaceBloc.state.selectedCommunityAndSpaces[communityId] ?? [];
|
||||
for (final spaceId in spacesList) {
|
||||
for (var spaceId in spacesList) {
|
||||
automations.addAll(
|
||||
await SceneApi.getAutomation(spaceId, communityId, projectId));
|
||||
}
|
||||
@ -334,8 +336,8 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
});
|
||||
}).toList();
|
||||
|
||||
final context = NavigationService.navigatorKey.currentContext!;
|
||||
final createRoutineBloc = context.read<CreateRoutineBloc>();
|
||||
BuildContext context = NavigationService.navigatorKey.currentContext!;
|
||||
var createRoutineBloc = context.read<CreateRoutineBloc>();
|
||||
|
||||
final createSceneModel = CreateSceneModel(
|
||||
spaceUuid: createRoutineBloc.selectedSpaceId,
|
||||
@ -359,7 +361,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
}
|
||||
} on APIException catch (e) {
|
||||
final errorData = e.message;
|
||||
final errorMessage = errorData;
|
||||
String errorMessage = errorData;
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: errorMessage,
|
||||
@ -398,7 +400,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
return;
|
||||
}
|
||||
emit(state.copyWith(isLoading: true, errorMessage: null));
|
||||
var i = 0;
|
||||
int i = 0;
|
||||
final conditions = state.ifItems.expand((item) {
|
||||
final functions = state.selectedFunctions[item['uniqueCustomId']] ?? [];
|
||||
return functions.map((function) {
|
||||
@ -466,8 +468,8 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
);
|
||||
});
|
||||
}).toList();
|
||||
final context = NavigationService.navigatorKey.currentContext!;
|
||||
final createRoutineBloc = context.read<CreateRoutineBloc>();
|
||||
BuildContext context = NavigationService.navigatorKey.currentContext!;
|
||||
var createRoutineBloc = context.read<CreateRoutineBloc>();
|
||||
|
||||
final createAutomationModel = CreateAutomationModel(
|
||||
spaceUuid: createRoutineBloc.selectedSpaceId,
|
||||
@ -497,7 +499,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
}
|
||||
} on APIException catch (e) {
|
||||
final errorData = e.message;
|
||||
final errorMessage = errorData;
|
||||
String errorMessage = errorData;
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: errorMessage,
|
||||
@ -703,14 +705,14 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
|
||||
final List<Map<String, dynamic>> thenItems;
|
||||
final List<Map<String, dynamic>> ifItems;
|
||||
final updatedFunctions =
|
||||
final Map<String, List<DeviceFunctionData>> updatedFunctions =
|
||||
Map<String, List<DeviceFunctionData>>.from(state.selectedFunctions);
|
||||
|
||||
final deviceCards = <String, Map<String, dynamic>>{};
|
||||
final Map<String, Map<String, dynamic>> deviceCards = {};
|
||||
|
||||
for (final action in sceneDetails.actions) {
|
||||
for (var action in sceneDetails.actions) {
|
||||
AllDevicesModel? matchingDevice;
|
||||
for (final device in state.devices) {
|
||||
for (var device in state.devices) {
|
||||
if (device.uuid == action.entityId) {
|
||||
matchingDevice = device;
|
||||
break;
|
||||
@ -775,7 +777,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
action.actionExecutor != 'delay') {
|
||||
final functions = matchingDevice?.functions ?? [];
|
||||
final functionCode = action.executorProperty?.functionCode;
|
||||
for (final function in functions) {
|
||||
for (DeviceFunction function in functions) {
|
||||
if (function.code == functionCode) {
|
||||
updatedFunctions[uniqueCustomId]!.add(
|
||||
DeviceFunctionData(
|
||||
@ -869,8 +871,8 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
final projectId = await ProjectManager.getProjectUUID() ?? '';
|
||||
|
||||
emit(state.copyWith(isLoading: true));
|
||||
final context = NavigationService.navigatorKey.currentContext!;
|
||||
final spaceBloc = context.read<SpaceTreeBloc>();
|
||||
BuildContext context = NavigationService.navigatorKey.currentContext!;
|
||||
var spaceBloc = context.read<SpaceTreeBloc>();
|
||||
if (state.isTabToRun) {
|
||||
await SceneApi.deleteScene(
|
||||
unitUuid: spaceBloc.state.selectedSpaces[0],
|
||||
@ -899,7 +901,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
emit(state.copyWith(isLoading: false, createRoutineView: false));
|
||||
} on APIException catch (e) {
|
||||
final errorData = e.message;
|
||||
final errorMessage = errorData;
|
||||
String errorMessage = errorData;
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: errorMessage,
|
||||
@ -927,17 +929,17 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
try {
|
||||
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
||||
final devices = <AllDevicesModel>[];
|
||||
final context = NavigationService.navigatorKey.currentContext!;
|
||||
final createRoutineBloc = context.read<CreateRoutineBloc>();
|
||||
final spaceBloc = context.read<SpaceTreeBloc>();
|
||||
List<AllDevicesModel> devices = [];
|
||||
BuildContext context = NavigationService.navigatorKey.currentContext!;
|
||||
var createRoutineBloc = context.read<CreateRoutineBloc>();
|
||||
var spaceBloc = context.read<SpaceTreeBloc>();
|
||||
|
||||
if (createRoutineBloc.selectedSpaceId == '' &&
|
||||
createRoutineBloc.selectedCommunityId == '') {
|
||||
for (final communityId in spaceBloc.state.selectedCommunities) {
|
||||
final spacesList =
|
||||
for (var communityId in spaceBloc.state.selectedCommunities) {
|
||||
List<String> spacesList =
|
||||
spaceBloc.state.selectedCommunityAndSpaces[communityId] ?? [];
|
||||
for (final spaceId in spacesList) {
|
||||
for (var spaceId in spacesList) {
|
||||
devices.addAll(await DevicesManagementApi()
|
||||
.fetchDevices(communityId, spaceId, projectUuid));
|
||||
}
|
||||
@ -1069,7 +1071,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
return;
|
||||
}
|
||||
emit(state.copyWith(isLoading: true, errorMessage: null));
|
||||
var i = 0;
|
||||
int i = 0;
|
||||
final conditions = state.ifItems.expand((item) {
|
||||
final functions = state.selectedFunctions[item['uniqueCustomId']] ?? [];
|
||||
return functions.map((function) {
|
||||
@ -1140,8 +1142,8 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
});
|
||||
}).toList();
|
||||
|
||||
final context = NavigationService.navigatorKey.currentContext!;
|
||||
final spaceBloc = context.read<CreateRoutineBloc>();
|
||||
BuildContext context = NavigationService.navigatorKey.currentContext!;
|
||||
var spaceBloc = context.read<CreateRoutineBloc>();
|
||||
|
||||
final createAutomationModel = CreateAutomationModel(
|
||||
spaceUuid: spaceBloc.selectedSpaceId,
|
||||
@ -1161,8 +1163,8 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
|
||||
if (result['success']) {
|
||||
add(ResetRoutineState());
|
||||
add(const LoadAutomation());
|
||||
add(const LoadScenes());
|
||||
add(LoadAutomation());
|
||||
add(LoadScenes());
|
||||
} else {
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
@ -1195,14 +1197,14 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
final automationDetails =
|
||||
await SceneApi.getAutomationDetails(event.automationId, projectUuid);
|
||||
|
||||
final deviceIfCards = <String, Map<String, dynamic>>{};
|
||||
final deviceThenCards = <String, Map<String, dynamic>>{};
|
||||
final Map<String, Map<String, dynamic>> deviceIfCards = {};
|
||||
final Map<String, Map<String, dynamic>> deviceThenCards = {};
|
||||
|
||||
final updatedFunctions =
|
||||
final Map<String, List<DeviceFunctionData>> updatedFunctions =
|
||||
Map<String, List<DeviceFunctionData>>.from(state.selectedFunctions);
|
||||
|
||||
for (final condition in automationDetails.conditions ?? []) {
|
||||
final matchingDevice = state.devices.firstWhere(
|
||||
for (RoutineCondition condition in automationDetails.conditions ?? []) {
|
||||
AllDevicesModel? matchingDevice = state.devices.firstWhere(
|
||||
(device) => device.uuid == condition.entityId,
|
||||
orElse: () => AllDevicesModel(
|
||||
uuid: condition.entityId,
|
||||
@ -1239,7 +1241,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
}
|
||||
|
||||
final functions = matchingDevice.functions;
|
||||
for (final function in functions) {
|
||||
for (var function in functions) {
|
||||
if (function.code == condition.expr.statusCode) {
|
||||
updatedFunctions[uniqueCustomId]!.add(
|
||||
DeviceFunctionData(
|
||||
@ -1255,8 +1257,8 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
}
|
||||
|
||||
// Process actions (thenItems)
|
||||
for (final action in automationDetails.actions) {
|
||||
final matchingDevice = state.devices.firstWhere(
|
||||
for (var action in automationDetails.actions) {
|
||||
AllDevicesModel? matchingDevice = state.devices.firstWhere(
|
||||
(device) => device.uuid == action.entityId,
|
||||
orElse: () => AllDevicesModel(
|
||||
uuid: action.entityId,
|
||||
@ -1310,7 +1312,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
action.actionExecutor != 'delay') {
|
||||
final functions = matchingDevice.functions;
|
||||
final functionCode = action.executorProperty!.functionCode;
|
||||
for (final function in functions) {
|
||||
for (var function in functions) {
|
||||
if (function.code == functionCode) {
|
||||
updatedFunctions[uniqueCustomId]!.add(
|
||||
DeviceFunctionData(
|
||||
@ -1401,7 +1403,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
} catch (e) {
|
||||
emit(state.copyWith(
|
||||
loadingSceneId: null,
|
||||
errorMessage: 'Trigger error: $e',
|
||||
errorMessage: 'Trigger error: ${e.toString()}',
|
||||
));
|
||||
}
|
||||
}
|
||||
@ -1446,7 +1448,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
..remove(event.automationId);
|
||||
emit(state.copyWith(
|
||||
loadingAutomationIds: updatedLoadingIds,
|
||||
errorMessage: 'Update error: $e',
|
||||
errorMessage: 'Update error: ${e.toString()}',
|
||||
));
|
||||
}
|
||||
}
|
||||
|
@ -86,8 +86,7 @@ class RemoveDragCard extends RoutineEvent {
|
||||
final int index;
|
||||
final bool isFromThen;
|
||||
final String key;
|
||||
const RemoveDragCard(
|
||||
{required this.index, required this.isFromThen, required this.key});
|
||||
const RemoveDragCard({required this.index, required this.isFromThen, required this.key});
|
||||
@override
|
||||
List<Object> get props => [index, isFromThen, key];
|
||||
}
|
||||
@ -212,6 +211,9 @@ class ClearFunctions extends RoutineEvent {}
|
||||
|
||||
class ResetErrorMessage extends RoutineEvent {}
|
||||
|
||||
|
||||
|
||||
|
||||
class SceneTrigger extends RoutineEvent {
|
||||
final String? sceneId;
|
||||
final String? name;
|
||||
@ -219,7 +221,7 @@ class SceneTrigger extends RoutineEvent {
|
||||
const SceneTrigger({this.sceneId, this.name});
|
||||
|
||||
@override
|
||||
List<Object> get props => [sceneId!, name!];
|
||||
List<Object> get props => [sceneId!,name!];
|
||||
}
|
||||
|
||||
//updateAutomationStatus
|
||||
@ -228,10 +230,7 @@ class UpdateAutomationStatus extends RoutineEvent {
|
||||
final AutomationStatusUpdate automationStatusUpdate;
|
||||
final String communityId;
|
||||
|
||||
const UpdateAutomationStatus(
|
||||
{required this.automationStatusUpdate,
|
||||
required this.automationId,
|
||||
required this.communityId});
|
||||
const UpdateAutomationStatus({required this.automationStatusUpdate, required this.automationId, required this.communityId});
|
||||
|
||||
@override
|
||||
List<Object> get props => [automationStatusUpdate];
|
||||
|
Reference in New Issue
Block a user