mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
218 lines
6.7 KiB
Dart
218 lines
6.7 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:bloc/bloc.dart';
|
|
import 'package:equatable/equatable.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:syncrow_web/pages/routiens/models/create_scene/create_scene_model.dart';
|
|
import 'package:syncrow_web/pages/routiens/models/device_functions.dart';
|
|
import 'package:syncrow_web/pages/routiens/models/routine_model.dart';
|
|
import 'package:syncrow_web/services/routines_api.dart';
|
|
|
|
part 'routine_event.dart';
|
|
part 'routine_state.dart';
|
|
|
|
const spaceId = '25c96044-fadf-44bb-93c7-3c079e527ce6';
|
|
|
|
class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
|
bool isAutomation = false;
|
|
bool isTabToRun = false;
|
|
|
|
RoutineBloc() : super(const RoutineState()) {
|
|
on<AddToIfContainer>(_onAddToIfContainer);
|
|
on<AddToThenContainer>(_onAddToThenContainer);
|
|
on<LoadScenes>(_onLoadScenes);
|
|
on<LoadAutomation>(_onLoadAutomation);
|
|
on<AddFunctionToRoutine>(_onAddFunctionsToRoutine);
|
|
on<SearchRoutines>(_onSearchRoutines);
|
|
on<AddSelectedIcon>(_onAddSelectedIcon);
|
|
on<CreateSceneEvent>(_onCreateScene);
|
|
on<RemoveDragCard>(_onRemoveDragCard);
|
|
// on<RemoveFunction>(_onRemoveFunction);
|
|
// on<ClearFunctions>(_onClearFunctions);
|
|
}
|
|
|
|
void _onAddToIfContainer(AddToIfContainer event, Emitter<RoutineState> emit) {
|
|
final updatedIfItems = List<Map<String, dynamic>>.from(state.ifItems)
|
|
..add(event.item);
|
|
if (event.isTabToRun) {
|
|
isTabToRun = true;
|
|
isAutomation = false;
|
|
} else {
|
|
isTabToRun = false;
|
|
isAutomation = true;
|
|
}
|
|
emit(state.copyWith(ifItems: updatedIfItems));
|
|
}
|
|
|
|
void _onAddToThenContainer(
|
|
AddToThenContainer event, Emitter<RoutineState> emit) {
|
|
final updatedThenItems = List<Map<String, dynamic>>.from(state.thenItems)
|
|
..add(event.item);
|
|
emit(state.copyWith(thenItems: updatedThenItems));
|
|
}
|
|
|
|
void _onAddFunctionsToRoutine(
|
|
AddFunctionToRoutine event, Emitter<RoutineState> emit) {
|
|
try {
|
|
if (event.functions.isEmpty) return;
|
|
|
|
final currentSelectedFunctions =
|
|
Map<String, List<DeviceFunctionData>>.from(state.selectedFunctions);
|
|
|
|
if (currentSelectedFunctions.containsKey(event.uniqueCustomId)) {
|
|
currentSelectedFunctions[event.uniqueCustomId] =
|
|
List.from(currentSelectedFunctions[event.uniqueCustomId]!)
|
|
..addAll(event.functions);
|
|
} else {
|
|
currentSelectedFunctions[event.uniqueCustomId] =
|
|
List.from(event.functions);
|
|
}
|
|
|
|
emit(state.copyWith(selectedFunctions: currentSelectedFunctions));
|
|
} catch (e) {
|
|
debugPrint('Error adding functions: $e');
|
|
}
|
|
}
|
|
|
|
Future<void> _onLoadScenes(
|
|
LoadScenes event, Emitter<RoutineState> emit) async {
|
|
emit(state.copyWith(isLoading: true, errorMessage: null));
|
|
|
|
try {
|
|
final scenes = await SceneApi.getScenesByUnitId(event.unitId);
|
|
emit(state.copyWith(
|
|
scenes: scenes,
|
|
isLoading: false,
|
|
));
|
|
} catch (e) {
|
|
emit(state.copyWith(
|
|
isLoading: false,
|
|
loadScenesErrorMessage: 'Failed to load scenes',
|
|
errorMessage: '',
|
|
loadAutomationErrorMessage: '',
|
|
));
|
|
}
|
|
}
|
|
|
|
Future<void> _onLoadAutomation(
|
|
LoadAutomation event, Emitter<RoutineState> emit) async {
|
|
emit(state.copyWith(isLoading: true, errorMessage: null));
|
|
|
|
try {
|
|
final automations = await SceneApi.getAutomationByUnitId(event.unitId);
|
|
emit(state.copyWith(
|
|
automations: automations,
|
|
isLoading: false,
|
|
));
|
|
} catch (e) {
|
|
emit(state.copyWith(
|
|
isLoading: false,
|
|
loadAutomationErrorMessage: 'Failed to load automations',
|
|
errorMessage: '',
|
|
loadScenesErrorMessage: '',
|
|
));
|
|
}
|
|
}
|
|
|
|
FutureOr<void> _onSearchRoutines(
|
|
SearchRoutines event, Emitter<RoutineState> emit) {
|
|
emit(state.copyWith(routineName: event.query));
|
|
}
|
|
|
|
FutureOr<void> _onAddSelectedIcon(
|
|
AddSelectedIcon event, Emitter<RoutineState> emit) {
|
|
emit(state.copyWith(selectedIcon: event.icon));
|
|
}
|
|
|
|
bool _isFirstActionDelay(List<Map<String, dynamic>> actions) {
|
|
if (actions.isEmpty) return false;
|
|
return actions.first['deviceId'] == 'delay';
|
|
}
|
|
|
|
Future<void> _onCreateScene(
|
|
CreateSceneEvent event, Emitter<RoutineState> emit) async {
|
|
try {
|
|
// Check if first action is delay
|
|
if (_isFirstActionDelay(state.thenItems)) {
|
|
emit(state.copyWith(
|
|
errorMessage: 'Cannot have delay as the first action',
|
|
isLoading: false,
|
|
));
|
|
return;
|
|
}
|
|
|
|
emit(state.copyWith(isLoading: true));
|
|
|
|
final actions = state.thenItems
|
|
.map((item) {
|
|
final functions =
|
|
state.selectedFunctions[item['uniqueCustomId']] ?? [];
|
|
if (functions.isEmpty) return null;
|
|
|
|
final function = functions.first;
|
|
if (item['deviceId'] == 'delay') {
|
|
return CreateSceneAction(
|
|
entityId: function.entityId,
|
|
actionExecutor: 'delay',
|
|
executorProperty: CreateSceneExecutorProperty(
|
|
functionCode: '',
|
|
functionValue: '',
|
|
delaySeconds: function.value,
|
|
),
|
|
);
|
|
}
|
|
|
|
return CreateSceneAction(
|
|
entityId: function.entityId,
|
|
actionExecutor: 'device_issue',
|
|
executorProperty: CreateSceneExecutorProperty(
|
|
functionCode: function.functionCode.toString(),
|
|
functionValue: function.value,
|
|
delaySeconds: 0,
|
|
),
|
|
);
|
|
})
|
|
.whereType<CreateSceneAction>()
|
|
.toList();
|
|
|
|
final createSceneModel = CreateSceneModel(
|
|
spaceUuid: spaceId,
|
|
iconId: state.selectedIcon ?? '',
|
|
showInDevice: true,
|
|
sceneName: state.routineName ?? '',
|
|
decisionExpr: 'and',
|
|
actions: actions,
|
|
);
|
|
|
|
final result = await SceneApi.createScene(createSceneModel);
|
|
if (result['success']) {
|
|
emit(const RoutineState());
|
|
} else {
|
|
emit(state.copyWith(
|
|
isLoading: false,
|
|
errorMessage: result['message'],
|
|
));
|
|
}
|
|
} catch (e) {
|
|
emit(state.copyWith(
|
|
isLoading: false,
|
|
errorMessage: e.toString(),
|
|
));
|
|
}
|
|
}
|
|
|
|
FutureOr<void> _onRemoveDragCard(
|
|
RemoveDragCard event, Emitter<RoutineState> emit) {
|
|
if (event.isFromThen) {
|
|
/// remove element from thenItems at specific index
|
|
final thenItems = List<Map<String, dynamic>>.from(state.thenItems);
|
|
thenItems.removeAt(event.index);
|
|
emit(state.copyWith(thenItems: thenItems));
|
|
} else {
|
|
final ifItems = List<Map<String, dynamic>>.from(state.ifItems);
|
|
ifItems.removeAt(event.index);
|
|
emit(state.copyWith(ifItems: ifItems));
|
|
}
|
|
}
|
|
}
|