mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 07:07:19 +00:00
routines and automation Toggle
This commit is contained in:
@ -1,124 +0,0 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:syncrow_web/pages/auth/model/project_model.dart';
|
||||
import 'package:syncrow_web/pages/routines/bloc/automation_scene_trigger_bloc/automation_scene_trigger_event.dart';
|
||||
import 'package:syncrow_web/pages/routines/bloc/automation_scene_trigger_bloc/automation_scene_trigger_status.dart';
|
||||
import 'package:syncrow_web/pages/routines/models/routine_model.dart';
|
||||
import 'package:syncrow_web/services/routines_api.dart';
|
||||
|
||||
class AutomationSceneTriggerBloc extends Bloc<AutomationSceneTriggerEvent, AutomationSceneTriggerStatus> {
|
||||
AutomationSceneTriggerBloc() : super(AutomationSceneInitial()) {
|
||||
// on<LoadScenes>(_onLoadScenes);
|
||||
// on<LoadAutomation>(_onLoadAutomation);
|
||||
on<SceneTrigger>(_onSceneTrigger);
|
||||
on<UpdateAutomationStatus>(_onUpdateAutomationStatus);
|
||||
}
|
||||
|
||||
List<ScenesModel> scenes = [];
|
||||
List<ScenesModel> automationList = [];
|
||||
|
||||
// Future<void> _onLoadScenes(LoadScenes event, Emitter<AutomationSceneTriggerStatus> emit) async {
|
||||
// emit(SceneLoading());
|
||||
|
||||
// try {
|
||||
// Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
// if (event.unitId.isNotEmpty) {
|
||||
// scenes = await SceneApi.getScenesByUnitId(event.unitId,
|
||||
// event.unit.community.uuid, project?.uuid ?? TempConst.projectIdDev,
|
||||
// showInDevice: event.showInDevice);
|
||||
// emit(SceneLoaded(scenes, automationList));
|
||||
// } else {
|
||||
// emit(const SceneError(message: 'Unit ID is empty'));
|
||||
// }
|
||||
// } catch (e) {
|
||||
// emit(const SceneError(message: 'Something went wrong'));
|
||||
// }
|
||||
// }
|
||||
|
||||
// Future<void> _onLoadAutomation(
|
||||
// LoadAutomation event, Emitter<AutomationSceneTriggerStatus> emit) async {
|
||||
// emit(SceneLoading());
|
||||
|
||||
// try {
|
||||
// Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
// if (event.unitId.isNotEmpty) {
|
||||
// automationList = await SceneApi.getAutomationByUnitId(
|
||||
// event.unitId, event.communityId, project?.uuid ?? '');
|
||||
// emit(SceneLoaded(scenes, automationList));
|
||||
// } else {
|
||||
// emit(const SceneError(message: 'Unit ID is empty'));
|
||||
// }
|
||||
// } catch (e) {
|
||||
// emit(const SceneError(message: 'Something went wrong'));
|
||||
// }
|
||||
// }
|
||||
|
||||
Future<void> _onSceneTrigger(
|
||||
SceneTrigger event, Emitter<AutomationSceneTriggerStatus> emit) async {
|
||||
final currentState = state;
|
||||
if (currentState is AutomationSceneLoaded) {
|
||||
emit(AutomationSceneLoaded(
|
||||
currentState.scenes,
|
||||
currentState.automationList,
|
||||
loadingSceneId: event.sceneId,
|
||||
));
|
||||
|
||||
try {
|
||||
final success = await SceneApi.triggerScene(event.sceneId);
|
||||
if (success) {
|
||||
emit(SceneTriggerSuccess(event.name));
|
||||
emit(AutomationSceneLoaded(currentState.scenes, currentState.automationList));
|
||||
} else {
|
||||
emit(const AutomationSceneError(message: 'Something went wrong'));
|
||||
}
|
||||
} catch (e) {
|
||||
emit(const AutomationSceneError(message: 'Something went wrong'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onUpdateAutomationStatus(
|
||||
UpdateAutomationStatus event, Emitter<AutomationSceneTriggerStatus> emit) async {
|
||||
final currentState = state;
|
||||
if (currentState is AutomationSceneLoaded) {
|
||||
final newLoadingStates =
|
||||
Map<String, bool>.from(currentState.loadingStates)
|
||||
..[event.automationId] = true;
|
||||
|
||||
emit(AutomationSceneLoaded(
|
||||
currentState.scenes,
|
||||
currentState.automationList,
|
||||
loadingStates: newLoadingStates,
|
||||
));
|
||||
|
||||
try {
|
||||
Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
final success = await SceneApi.updateAutomationStatus(
|
||||
event.automationId,
|
||||
event.automationStatusUpdate,
|
||||
project?.uuid ?? '');
|
||||
if (success) {
|
||||
automationList = await SceneApi.getAutomationByUnitId(
|
||||
event.automationStatusUpdate.spaceUuid,
|
||||
event.communityId,
|
||||
project?.uuid ?? '');
|
||||
newLoadingStates[event.automationId] = false;
|
||||
emit(AutomationSceneLoaded(
|
||||
currentState.scenes,
|
||||
automationList,
|
||||
loadingStates: newLoadingStates,
|
||||
));
|
||||
} else {
|
||||
emit(const AutomationSceneError(message: 'Something went wrong'));
|
||||
}
|
||||
} catch (e) {
|
||||
emit(const AutomationSceneError(message: 'Something went wrong'));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:syncrow_web/pages/routines/bloc/automation_scene_trigger_bloc/automation_status_update.dart';
|
||||
import 'package:syncrow_web/pages/routines/bloc/routine_bloc/routine_bloc.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/space_model.dart';
|
||||
|
||||
abstract class AutomationSceneTriggerEvent extends Equatable {
|
||||
const AutomationSceneTriggerEvent();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
class AutomationSceneScenes extends AutomationSceneTriggerEvent {
|
||||
final String unitId;
|
||||
final bool showInDevice;
|
||||
final SpaceModel unit;
|
||||
|
||||
const AutomationSceneScenes(this.unitId, this.unit, {this.showInDevice = false});
|
||||
|
||||
@override
|
||||
List<Object> get props => [unitId, showInDevice];
|
||||
}
|
||||
|
||||
class AutomationSceneAutomation extends AutomationSceneTriggerEvent {
|
||||
final String unitId;
|
||||
final String communityId;
|
||||
|
||||
|
||||
const AutomationSceneAutomation(this.unitId, this.communityId);
|
||||
|
||||
@override
|
||||
List<Object> get props => [unitId, communityId];
|
||||
}
|
||||
|
||||
class SceneTrigger extends AutomationSceneTriggerEvent {
|
||||
final String sceneId;
|
||||
final String name;
|
||||
|
||||
const SceneTrigger(this.sceneId, this.name);
|
||||
|
||||
@override
|
||||
List<Object> get props => [sceneId];
|
||||
}
|
||||
|
||||
//updateAutomationStatus
|
||||
class UpdateAutomationStatus extends AutomationSceneTriggerEvent {
|
||||
final String automationId;
|
||||
final AutomationStatusUpdate automationStatusUpdate;
|
||||
final String communityId;
|
||||
|
||||
const UpdateAutomationStatus({required this.automationStatusUpdate, required this.automationId, required this.communityId});
|
||||
|
||||
@override
|
||||
List<Object> get props => [automationStatusUpdate];
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:syncrow_web/pages/routines/models/routine_model.dart';
|
||||
|
||||
abstract class AutomationSceneTriggerStatus extends Equatable {
|
||||
const AutomationSceneTriggerStatus();
|
||||
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
class AutomationSceneInitial extends AutomationSceneTriggerStatus {}
|
||||
|
||||
class AutomationSceneLoading extends AutomationSceneTriggerStatus {}
|
||||
|
||||
class AutomationSceneLoaded extends AutomationSceneTriggerStatus {
|
||||
final List<ScenesModel> scenes;
|
||||
final List<ScenesModel> automationList;
|
||||
final String? loadingSceneId;
|
||||
final Map<String, bool> loadingStates;
|
||||
|
||||
const AutomationSceneLoaded(this.scenes, this.automationList,
|
||||
{this.loadingSceneId, this.loadingStates = const {}});
|
||||
|
||||
@override
|
||||
List<Object?> get props =>
|
||||
[scenes, loadingSceneId, automationList, loadingStates];
|
||||
}
|
||||
|
||||
class AutomationSceneError extends AutomationSceneTriggerStatus {
|
||||
final String message;
|
||||
|
||||
const AutomationSceneError({required this.message});
|
||||
|
||||
@override
|
||||
List<Object> get props => [message];
|
||||
}
|
||||
|
||||
class SceneTriggerSuccess extends AutomationSceneTriggerStatus {
|
||||
final String sceneName;
|
||||
|
||||
const SceneTriggerSuccess(this.sceneName);
|
||||
|
||||
@override
|
||||
List<Object> get props => [sceneName];
|
||||
}
|
||||
|
||||
class UpdateAutomationStatusLoading extends AutomationSceneTriggerStatus {
|
||||
const UpdateAutomationStatusLoading();
|
||||
}
|
@ -5,6 +5,7 @@ import 'package:flutter/material.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/models/create_scene_and_autoamtion/create_automation_model.dart';
|
||||
import 'package:syncrow_web/pages/routines/models/create_scene_and_autoamtion/create_scene_model.dart';
|
||||
import 'package:syncrow_web/pages/routines/models/delay/delay_fucntions.dart';
|
||||
@ -51,6 +52,8 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
on<TriggerSwitchTabsEvent>(_triggerSwitchTabsEvent);
|
||||
on<CreateNewRoutineViewEvent>(_createNewRoutineViewEvent);
|
||||
on<ResetErrorMessage>(_resetErrorMessage);
|
||||
on<SceneTrigger>(_onSceneTrigger);
|
||||
on<UpdateAutomationStatus>(_onUpdateAutomationStatus);
|
||||
}
|
||||
|
||||
FutureOr<void> _triggerSwitchTabsEvent(
|
||||
@ -1269,4 +1272,79 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onSceneTrigger(
|
||||
SceneTrigger event, Emitter<RoutineState> emit) async {
|
||||
emit(state.copyWith(loadingSceneId: event.sceneId));
|
||||
|
||||
try {
|
||||
final success = await SceneApi.triggerScene(event.sceneId!);
|
||||
|
||||
if (success) {
|
||||
emit(state.copyWith(
|
||||
loadingSceneId: null,
|
||||
// Add success state if needed
|
||||
));
|
||||
// Optional: Add delay to show success feedback
|
||||
await Future.delayed(const Duration(milliseconds: 500));
|
||||
} else {
|
||||
emit(state.copyWith(
|
||||
loadingSceneId: null,
|
||||
errorMessage: 'Trigger failed',
|
||||
));
|
||||
}
|
||||
} catch (e) {
|
||||
emit(state.copyWith(
|
||||
loadingSceneId: null,
|
||||
errorMessage: 'Trigger error: ${e.toString()}',
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Future<void> _onUpdateAutomationStatus(
|
||||
UpdateAutomationStatus event, Emitter<RoutineState> emit) async {
|
||||
// Create a new set safely
|
||||
final currentLoadingIds = state.loadingAutomationIds;
|
||||
final newLoadingIds = {...currentLoadingIds!}..add(event.automationId);
|
||||
|
||||
emit(state.copyWith(loadingAutomationIds: newLoadingIds));
|
||||
|
||||
try {
|
||||
final projectId = await ProjectManager.getProjectUUID() ?? '';
|
||||
final success = await SceneApi.updateAutomationStatus(
|
||||
event.automationId, event.automationStatusUpdate, projectId);
|
||||
|
||||
if (success) {
|
||||
final updatedAutomations = await SceneApi.getAutomationByUnitId(
|
||||
event.automationStatusUpdate.spaceUuid,
|
||||
event.communityId,
|
||||
projectId);
|
||||
|
||||
// Remove from loading set safely
|
||||
final updatedLoadingIds = {...state.loadingAutomationIds!}
|
||||
..remove(event.automationId);
|
||||
|
||||
emit(state.copyWith(
|
||||
automations: updatedAutomations,
|
||||
loadingAutomationIds: updatedLoadingIds,
|
||||
));
|
||||
} else {
|
||||
final updatedLoadingIds = {...state.loadingAutomationIds!}
|
||||
..remove(event.automationId);
|
||||
emit(state.copyWith(
|
||||
loadingAutomationIds: updatedLoadingIds,
|
||||
errorMessage: 'Update failed',
|
||||
));
|
||||
}
|
||||
} catch (e) {
|
||||
final updatedLoadingIds = {...state.loadingAutomationIds!}
|
||||
..remove(event.automationId);
|
||||
emit(state.copyWith(
|
||||
loadingAutomationIds: updatedLoadingIds,
|
||||
errorMessage: 'Update error: ${e.toString()}',
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -210,3 +210,28 @@ class ResetRoutineState extends RoutineEvent {}
|
||||
class ClearFunctions extends RoutineEvent {}
|
||||
|
||||
class ResetErrorMessage extends RoutineEvent {}
|
||||
|
||||
|
||||
|
||||
|
||||
class SceneTrigger extends RoutineEvent {
|
||||
final String? sceneId;
|
||||
final String? name;
|
||||
|
||||
const SceneTrigger({this.sceneId, this.name});
|
||||
|
||||
@override
|
||||
List<Object> get props => [sceneId!,name!];
|
||||
}
|
||||
|
||||
//updateAutomationStatus
|
||||
class UpdateAutomationStatus extends RoutineEvent {
|
||||
final String automationId;
|
||||
final AutomationStatusUpdate automationStatusUpdate;
|
||||
final String communityId;
|
||||
|
||||
const UpdateAutomationStatus({required this.automationStatusUpdate, required this.automationId, required this.communityId});
|
||||
|
||||
@override
|
||||
List<Object> get props => [automationStatusUpdate];
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
part of 'routine_bloc.dart';
|
||||
|
||||
class RoutineState extends Equatable {
|
||||
final String? loadingSceneId;
|
||||
final List<Map<String, dynamic>> ifItems;
|
||||
final List<Map<String, dynamic>> thenItems;
|
||||
final List<Map<String, String>> availableCards;
|
||||
@ -25,6 +26,7 @@ class RoutineState extends Equatable {
|
||||
// final String? automationActionExecutor;
|
||||
final bool routineTab;
|
||||
final bool createRoutineView;
|
||||
final Set<String>? loadingAutomationIds; // Track loading automations
|
||||
|
||||
const RoutineState(
|
||||
{this.ifItems = const [],
|
||||
@ -47,12 +49,16 @@ class RoutineState extends Equatable {
|
||||
this.sceneId,
|
||||
this.automationId,
|
||||
this.isUpdate,
|
||||
this.loadingAutomationIds = const <String>{}, // Initialize with empty set
|
||||
this.loadingSceneId,
|
||||
this.devices = const [],
|
||||
// this.automationActionExecutor,
|
||||
this.routineTab = false,
|
||||
this.createRoutineView = false});
|
||||
|
||||
RoutineState copyWith({
|
||||
String? loadingSceneId,
|
||||
Set<String>? loadingAutomationIds,
|
||||
List<Map<String, dynamic>>? ifItems,
|
||||
List<Map<String, dynamic>>? thenItems,
|
||||
List<ScenesModel>? scenes,
|
||||
@ -79,6 +85,8 @@ class RoutineState extends Equatable {
|
||||
bool? createRoutineView,
|
||||
}) {
|
||||
return RoutineState(
|
||||
loadingSceneId: loadingSceneId,
|
||||
loadingAutomationIds: loadingAutomationIds ?? this.loadingAutomationIds,
|
||||
ifItems: ifItems ?? this.ifItems,
|
||||
thenItems: thenItems ?? this.thenItems,
|
||||
scenes: scenes ?? this.scenes,
|
||||
@ -109,6 +117,7 @@ class RoutineState extends Equatable {
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
loadingAutomationIds,
|
||||
ifItems,
|
||||
thenItems,
|
||||
scenes,
|
||||
@ -134,3 +143,38 @@ class RoutineState extends Equatable {
|
||||
createRoutineView
|
||||
];
|
||||
}
|
||||
|
||||
class SceneInitial extends RoutineState {}
|
||||
|
||||
class SceneLoading extends RoutineState {}
|
||||
|
||||
class SceneLoaded extends RoutineState {
|
||||
final List<ScenesModel>? scenesOrAutomation;
|
||||
const SceneLoaded({this.scenesOrAutomation});
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
scenesOrAutomation,
|
||||
];
|
||||
}
|
||||
|
||||
class SceneError extends RoutineState {
|
||||
final String message;
|
||||
|
||||
const SceneError({required this.message});
|
||||
|
||||
@override
|
||||
List<Object> get props => [message];
|
||||
}
|
||||
|
||||
class SceneTriggerSuccess extends RoutineState {
|
||||
final String sceneName;
|
||||
|
||||
const SceneTriggerSuccess(this.sceneName);
|
||||
|
||||
@override
|
||||
List<Object> get props => [sceneName];
|
||||
}
|
||||
|
||||
class UpdateAutomationStatusLoading extends RoutineState {
|
||||
const UpdateAutomationStatusLoading();
|
||||
}
|
||||
|
Reference in New Issue
Block a user