import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart'; import 'package:syncrow_app/features/app_layout/model/community_model.dart'; import 'package:syncrow_app/features/app_layout/model/space_model.dart'; import 'package:syncrow_app/features/devices/view/widgets/scene_listview.dart'; import 'package:syncrow_app/features/scene/bloc/create_scene/create_scene_bloc.dart'; import 'package:syncrow_app/features/scene/bloc/scene_bloc/scene_bloc.dart'; import 'package:syncrow_app/features/scene/bloc/scene_bloc/scene_event.dart'; import 'package:syncrow_app/features/scene/bloc/smart_scene/smart_scene_select_dart_bloc.dart'; import 'package:syncrow_app/features/scene/widgets/empty_devices_widget.dart'; import 'package:syncrow_app/features/scene/widgets/scene_view_widget/scene_grid_view.dart'; import 'package:syncrow_app/features/scene/widgets/scene_view_widget/scene_header.dart'; import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart'; import 'package:syncrow_app/utils/context_extension.dart'; import 'package:syncrow_app/utils/resource_manager/color_manager.dart'; class SceneView extends StatelessWidget { final bool pageType; const SceneView({super.key, this.pageType = false}); @override Widget build(BuildContext context) { return BlocProvider( create: (BuildContext context) { if (pageType) { return SceneBloc() ..add(LoadScenes( HomeCubit.getInstance().selectedSpace?.id ?? '', HomeCubit.getInstance().selectedSpace ?? SpaceModel( id: '-1', name: '', community: Community( uuid: '-1', name: '', )), showInDevice: pageType)); } else { return SceneBloc() ..add(LoadScenes( HomeCubit.getInstance().selectedSpace?.id ?? '', HomeCubit.getInstance().selectedSpace ?? SpaceModel( id: '-1', name: '', community: Community( uuid: '-1', name: '', )), showInDevice: pageType)) ..add(LoadAutomation( HomeCubit.getInstance().selectedSpace?.id ?? '', HomeCubit.getInstance().selectedSpace?.community.uuid ?? '')); } }, child: BlocBuilder( builder: (context, state) { if (state is DeleteSceneSuccess) { if (state.success) { BlocProvider.of(context).add(LoadScenes( HomeCubit.getInstance().selectedSpace!.id, HomeCubit.getInstance().selectedSpace!, showInDevice: pageType)); BlocProvider.of(context).add(LoadAutomation( HomeCubit.getInstance().selectedSpace!.id, HomeCubit.getInstance().selectedSpace!.community.uuid)); } } if (state is CreateSceneWithTasks) { if (state.success == true) { BlocProvider.of(context).add(LoadScenes( HomeCubit.getInstance().selectedSpace!.id, HomeCubit.getInstance().selectedSpace!, showInDevice: pageType)); BlocProvider.of(context).add(LoadAutomation( HomeCubit.getInstance().selectedSpace!.id, HomeCubit.getInstance().selectedSpace!.community.uuid)); context .read() .add(const SmartSceneClearEvent()); } } return BlocListener( listener: (context, state) { if (state is SceneTriggerSuccess) { context.showCustomSnackbar( message: 'Scene ${state.sceneName} triggered successfully!'); } }, child: HomeCubit.getInstance().spaces.isEmpty ? const EmptyDevicesWidget() : Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ if (pageType == false) const SceneHeader(), if (pageType == false) const SizedBox(height: 8), BlocBuilder( builder: (context, state) { if (state is SceneLoading) { return const Center( child: CircularProgressIndicator(), ); } if (state is SceneError) { return Center( child: Text(state.message), ); } if (state is SceneLoaded) { final scenes = state.scenes; final automationList = state.automationList; return pageType ? SizedBox( height: context.height * 0.1, child: SceneListview( scenes: scenes, loadingSceneId: state.loadingSceneId, ), ) : Expanded( child: ListView( children: [ Theme( data: ThemeData().copyWith( dividerColor: Colors.transparent), child: ExpansionTile( tilePadding: const EdgeInsets.symmetric( horizontal: 6), initiallyExpanded: true, iconColor: ColorsManager.grayColor, title: const BodyMedium( text: 'Tap to run routines'), children: [ scenes.isNotEmpty ? SceneGrid( scenes: scenes, loadingSceneId: state.loadingSceneId, disablePlayButton: false, loadingStates: state .loadingStates, // Add this line ) : const Center( child: BodyMedium( text: 'No scenes have been added yet', ), ), const SizedBox( height: 10, ), ], ), ), Theme( data: ThemeData().copyWith( dividerColor: Colors.transparent), child: ExpansionTile( initiallyExpanded: true, iconColor: ColorsManager.grayColor, tilePadding: const EdgeInsets.symmetric( horizontal: 6), title: const BodyMedium( text: 'Automation'), children: [ automationList.isNotEmpty ? SceneGrid( scenes: automationList, loadingSceneId: state.loadingSceneId, disablePlayButton: true, loadingStates: state .loadingStates, // Add this line ) : const Center( child: BodyMedium( text: 'No automations have been added yet', ), ), const SizedBox( height: 10, ), ], ), ), const SizedBox( height: 15, ), ], ), ); } return const SizedBox(); }, ), ], ), ); }, ), ); } }