mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-07-16 01:56:19 +00:00
51 lines
1.5 KiB
Dart
51 lines
1.5 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:equatable/equatable.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:syncrow_app/features/scene/bloc/scene_bloc/scene_event.dart';
|
|
import 'package:syncrow_app/features/scene/model/scenes_model.dart';
|
|
import 'package:syncrow_app/services/api/scene_api.dart';
|
|
|
|
part 'scene_state.dart';
|
|
|
|
class SceneBloc extends Bloc<SceneEvent, SceneState> {
|
|
SceneBloc() : super(SceneInitial()) {
|
|
on<LoadScenes>(_onLoadScenes);
|
|
on<SceneTrigger>(_onSceneTrigger);
|
|
}
|
|
|
|
Future<void> _onLoadScenes(LoadScenes event, Emitter<SceneState> emit) async {
|
|
emit(SceneLoading());
|
|
|
|
try {
|
|
if (event.unitId.isNotEmpty) {
|
|
final scenes = await SceneApi.getScenesByUnitId(event.unitId);
|
|
emit(SceneLoaded(scenes));
|
|
} else {
|
|
const SceneError(message: '');
|
|
}
|
|
} catch (e) {
|
|
emit(const SceneError(message: 'Something went wrong'));
|
|
}
|
|
}
|
|
|
|
Future<void> _onSceneTrigger(SceneTrigger event, Emitter<SceneState> emit) async {
|
|
final currentState = state;
|
|
if (currentState is SceneLoaded) {
|
|
emit(SceneLoaded(currentState.scenes, loadingSceneId: event.sceneId));
|
|
|
|
try {
|
|
final success = await SceneApi.triggerScene(event.sceneId);
|
|
if (success) {
|
|
emit(SceneTriggerSuccess(event.name));
|
|
emit(SceneLoaded(currentState.scenes));
|
|
} else {
|
|
emit(const SceneError(message: 'Something went wrong'));
|
|
}
|
|
} catch (e) {
|
|
emit(const SceneError(message: 'Something went wrong'));
|
|
}
|
|
}
|
|
}
|
|
}
|