diff --git a/lib/pages/access_management/bloc/access_bloc.dart b/lib/pages/access_management/bloc/access_bloc.dart index 4b1c37d6..562bd5b5 100644 --- a/lib/pages/access_management/bloc/access_bloc.dart +++ b/lib/pages/access_management/bloc/access_bloc.dart @@ -8,9 +8,6 @@ import 'package:syncrow_web/pages/common/hour_picker_dialog.dart'; import 'package:syncrow_web/services/access_mang_api.dart'; import 'package:syncrow_web/utils/color_manager.dart'; import 'package:syncrow_web/utils/constants/app_enum.dart'; -import 'package:syncrow_web/utils/constants/strings_manager.dart'; -import 'package:syncrow_web/utils/constants/temp_const.dart'; -import 'package:syncrow_web/utils/helpers/shared_preferences_helper.dart'; import 'package:syncrow_web/utils/snack_bar.dart'; class AccessBloc extends Bloc { diff --git a/lib/pages/device_managment/all_devices/widgets/device_managment_body.dart b/lib/pages/device_managment/all_devices/widgets/device_managment_body.dart index 52b2321c..9297c1fe 100644 --- a/lib/pages/device_managment/all_devices/widgets/device_managment_body.dart +++ b/lib/pages/device_managment/all_devices/widgets/device_managment_body.dart @@ -8,7 +8,6 @@ import 'package:syncrow_web/pages/device_managment/all_devices/models/devices_mo import 'package:syncrow_web/pages/device_managment/all_devices/widgets/device_search_filters.dart'; import 'package:syncrow_web/pages/device_managment/shared/device_batch_control_dialog.dart'; import 'package:syncrow_web/pages/device_managment/shared/device_control_dialog.dart'; -import 'package:syncrow_web/pages/space_tree/bloc/space_tree_bloc.dart'; import 'package:syncrow_web/pages/space_tree/view/space_tree_view.dart'; import 'package:syncrow_web/utils/format_date_time.dart'; import 'package:syncrow_web/utils/helpers/responsice_layout_helper/responsive_layout_helper.dart'; diff --git a/lib/pages/home/bloc/home_bloc.dart b/lib/pages/home/bloc/home_bloc.dart index 25b8b98d..04a02a11 100644 --- a/lib/pages/home/bloc/home_bloc.dart +++ b/lib/pages/home/bloc/home_bloc.dart @@ -83,7 +83,6 @@ class HomeBloc extends Bloc { try { emit(LoadingHome()); policy = await HomeApi().fetchPolicy(); - debugPrint("Fetched policy: $policy"); // Emit a state to trigger the UI update emit(HomeInitial()); } catch (e) { diff --git a/lib/pages/space_tree/bloc/space_tree_bloc.dart b/lib/pages/space_tree/bloc/space_tree_bloc.dart index e7fb4dc1..7820237b 100644 --- a/lib/pages/space_tree/bloc/space_tree_bloc.dart +++ b/lib/pages/space_tree/bloc/space_tree_bloc.dart @@ -16,6 +16,7 @@ class SpaceTreeBloc extends Bloc { on(_onSearch); on(_clearAllData); on(_clearCachedData); + on(_onCommunityAdded); } _fetchSpaces(InitialEvent event, Emitter emit) async { @@ -50,6 +51,14 @@ class SpaceTreeBloc extends Bloc { } } + void _onCommunityAdded(OnCommunityAdded event, Emitter emit) async { + final updatedCommunities = List.from(state.communityList); + updatedCommunities.add(event.newCommunity); + + emit(state.copyWith(communitiesList: updatedCommunities)); +} + + _onCommunityExpanded(OnCommunityExpanded event, Emitter emit) async { try { List updatedExpandedCommunityList = List.from(state.expandedCommunities); diff --git a/lib/pages/space_tree/bloc/space_tree_event.dart b/lib/pages/space_tree/bloc/space_tree_event.dart index e8fa996f..a22426f7 100644 --- a/lib/pages/space_tree/bloc/space_tree_event.dart +++ b/lib/pages/space_tree/bloc/space_tree_event.dart @@ -69,6 +69,14 @@ class SearchQueryEvent extends SpaceTreeEvent { List get props => [searchQuery]; } +class OnCommunityAdded extends SpaceTreeEvent { + final CommunityModel newCommunity; + const OnCommunityAdded(this.newCommunity); + + @override + List get props => [newCommunity]; +} + class ClearAllData extends SpaceTreeEvent {} class ClearCachedData extends SpaceTreeEvent {} diff --git a/lib/pages/spaces_management/all_spaces/bloc/space_management_bloc.dart b/lib/pages/spaces_management/all_spaces/bloc/space_management_bloc.dart index 6dc847eb..cb5de57e 100644 --- a/lib/pages/spaces_management/all_spaces/bloc/space_management_bloc.dart +++ b/lib/pages/spaces_management/all_spaces/bloc/space_management_bloc.dart @@ -1,8 +1,10 @@ import 'dart:developer'; +import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; -import 'package:shared_preferences/shared_preferences.dart'; import 'package:syncrow_web/pages/common/bloc/project_manager.dart'; +import 'package:syncrow_web/pages/space_tree/bloc/space_tree_bloc.dart'; +import 'package:syncrow_web/pages/space_tree/bloc/space_tree_event.dart'; import 'package:syncrow_web/pages/spaces_management/all_spaces/model/community_model.dart'; import 'package:syncrow_web/pages/spaces_management/all_spaces/model/create_subspace_model.dart'; import 'package:syncrow_web/pages/spaces_management/all_spaces/model/product_model.dart'; @@ -17,7 +19,7 @@ import 'package:syncrow_web/pages/spaces_management/space_model/models/tag_updat import 'package:syncrow_web/services/product_api.dart'; import 'package:syncrow_web/services/space_mana_api.dart'; import 'package:syncrow_web/services/space_model_mang_api.dart'; -import 'package:syncrow_web/utils/constants/action_enum.dart'; +import 'package:syncrow_web/utils/constants/action_enum.dart' as custom_action; class SpaceManagementBloc extends Bloc { @@ -30,7 +32,6 @@ class SpaceManagementBloc SpaceManagementBloc(this._api, this._productApi, this._spaceModelApi) : super(SpaceManagementInitial()) { on(_onLoadCommunityAndSpaces); - on(_onUpdateSpacePosition); on(_onCreateCommunity); on(_onSelectCommunity); on(_onCommunityDelete); @@ -182,6 +183,8 @@ class SpaceManagementBloc try { final previousState = state; final projectUuid = await ProjectManager.getProjectUUID() ?? ''; + var spaceBloc = event.context.read(); + List communities = spaceBloc.state.communityList; var prevSpaceModels = await fetchSpaceModels(previousState); @@ -196,25 +199,31 @@ class SpaceManagementBloc return; } - final communities = await _api.fetchCommunities(projectUuid); - final updatedCommunities = - await Future.wait(communities.map((community) async { - final spaces = await _fetchSpacesForCommunity(community.uuid); + if (communities.isEmpty) { + communities = await _api.fetchCommunities(projectUuid); - return CommunityModel( - uuid: community.uuid, - createdAt: community.createdAt, - updatedAt: community.updatedAt, - name: community.name, - description: community.description, - spaces: spaces, - region: community.region, + List updatedCommunities = await Future.wait( + communities.map((community) async { + List spaces = + await _fetchSpacesForCommunity(community.uuid); + return CommunityModel( + uuid: community.uuid, + createdAt: community.createdAt, + updatedAt: community.updatedAt, + name: community.name, + description: community.description, + spaces: spaces, + region: community.region, + ); + }).toList(), ); - })); + + communities = updatedCommunities; + } emit(BlankState( spaceModels: prevSpaceModels, - communities: updatedCommunities, + communities: communities, products: _cachedProducts ?? [], )); } catch (error) { @@ -228,50 +237,49 @@ class SpaceManagementBloc ) async { _logEvent('LoadCommunityAndSpacesEvent'); - // If already loaded, use cached data - if (state is SpaceManagementLoaded) { - emit(state); - return; + var spaceBloc = event.context.read(); + List communities = spaceBloc.state.communityList; + + if (communities.isEmpty) { + _logEvent("community is empty"); + emit(SpaceManagementLoading()); + + try { + final projectUuid = await ProjectManager.getProjectUUID() ?? ''; + _onloadProducts(); + + /* communities = await _api.fetchCommunities(projectUuid); + + List updatedCommunities = await Future.wait( + communities.map((community) async { + List spaces = + await _fetchSpacesForCommunity(community.uuid); + return CommunityModel( + uuid: community.uuid, + createdAt: community.createdAt, + updatedAt: community.updatedAt, + name: community.name, + description: community.description, + spaces: spaces, + region: community.region, + ); + }).toList(), + ); + + communities = updatedCommunities; */ + } catch (e) { + emit(SpaceManagementError('Error loading communities and spaces: $e')); + return; + } } - emit(SpaceManagementLoading()); + final prevSpaceModels = await fetchSpaceModels(state); - try { - final projectUuid = await ProjectManager.getProjectUUID() ?? ''; - - // Load products only once - _onloadProducts(); - - List communities = - await _api.fetchCommunities(projectUuid); - - List updatedCommunities = await Future.wait( - communities.map((community) async { - List spaces = - await _fetchSpacesForCommunity(community.uuid); - return CommunityModel( - uuid: community.uuid, - createdAt: community.createdAt, - updatedAt: community.updatedAt, - name: community.name, - description: community.description, - spaces: spaces, - region: community.region, - ); - }).toList(), - ); - - // Fetch space models only once - final prevSpaceModels = await fetchSpaceModels(state); - - emit(SpaceManagementLoaded( - communities: updatedCommunities, - products: _cachedProducts ?? [], - spaceModels: prevSpaceModels, - )); - } catch (e) { - emit(SpaceManagementError('Error loading communities and spaces: $e')); - } + emit(SpaceManagementLoaded( + communities: communities, + products: _cachedProducts ?? [], + spaceModels: prevSpaceModels, + )); } void _onCommunityDelete( @@ -285,7 +293,7 @@ class SpaceManagementBloc final success = await _api.deleteCommunity(event.communityUuid, projectUuid); if (success) { - add(LoadCommunityAndSpacesEvent()); + // add(LoadCommunityAndSpacesEvent()); } else { emit(const SpaceManagementError('Failed to delete the community.')); } @@ -295,11 +303,6 @@ class SpaceManagementBloc } } - void _onUpdateSpacePosition( - UpdateSpacePositionEvent event, - Emitter emit, - ) {} - void _onCreateCommunity( CreateCommunityEvent event, Emitter emit, @@ -309,6 +312,8 @@ class SpaceManagementBloc try { final projectUuid = await ProjectManager.getProjectUUID() ?? ''; + final BuildContext safeContext = + event.context; // Capture context safely before async calls CommunityModel? newCommunity = await _api.createCommunity( event.name, event.description, projectUuid); @@ -321,6 +326,16 @@ class SpaceManagementBloc (previousState as dynamic).communities, ); final updatedCommunities = prevCommunities..add(newCommunity); + + if (safeContext.mounted) { + print("added"); + final spaceTreeBloc = safeContext.read< + SpaceTreeBloc>(); // ✅ Read bloc only when context is mounted + spaceTreeBloc.add(OnCommunityAdded(newCommunity)); + } else { + print("not mounted"); + } + emit(SpaceManagementLoaded( spaceModels: prevSpaceModels, communities: updatedCommunities, @@ -416,7 +431,7 @@ class SpaceManagementBloc emit, ); } else { - add(LoadCommunityAndSpacesEvent()); + // add(LoadCommunityAndSpacesEvent()); } } catch (e) { emit(SpaceManagementError('Error saving spaces: $e')); @@ -491,13 +506,15 @@ class SpaceManagementBloc .any((subspace) => subspace.uuid == prevSubspace.uuid); if (!existsInNew) { subspaceUpdates.add(UpdateSubspaceTemplateModel( - action: Action.delete, uuid: prevSubspace.uuid)); + action: custom_action.Action.delete, + uuid: prevSubspace.uuid)); } } } else if (prevSubspaces != null && newSubspaces == null) { for (var prevSubspace in prevSubspaces) { subspaceUpdates.add(UpdateSubspaceTemplateModel( - action: Action.delete, uuid: prevSubspace.uuid)); + action: custom_action.Action.delete, + uuid: prevSubspace.uuid)); } } @@ -510,14 +527,14 @@ class SpaceManagementBloc if (newSubspace.tags != null) { for (var tag in newSubspace.tags!) { tagUpdates.add(TagModelUpdate( - action: Action.add, + action: custom_action.Action.add, uuid: tag.uuid == '' ? null : tag.uuid, tag: tag.tag, productUuid: tag.product?.uuid)); } } subspaceUpdates.add(UpdateSubspaceTemplateModel( - action: Action.add, + action: custom_action.Action.add, subspaceName: newSubspace.subspaceName, tags: tagUpdates)); } @@ -536,7 +553,7 @@ class SpaceManagementBloc final List tagSubspaceUpdates = processTagUpdates(prevSubspace.tags, newSubspace.tags); subspaceUpdates.add(UpdateSubspaceTemplateModel( - action: Action.update, + action: custom_action.Action.update, uuid: newSubspace.uuid, subspaceName: newSubspace.subspaceName, tags: tagSubspaceUpdates)); @@ -623,35 +640,42 @@ class SpaceManagementBloc void _onLoadSpaceModel( SpaceModelLoadEvent event, Emitter emit) async { emit(SpaceManagementLoading()); + try { var prevState = state; final projectUuid = await ProjectManager.getProjectUUID() ?? ''; - - List communities = - await _api.fetchCommunities(projectUuid); - - List updatedCommunities = await Future.wait( - communities.map((community) async { - List spaces = - await _fetchSpacesForCommunity(community.uuid); - return CommunityModel( - uuid: community.uuid, - createdAt: community.createdAt, - updatedAt: community.updatedAt, - name: community.name, - description: community.description, - spaces: spaces, // New spaces list - region: community.region, - ); - }).toList(), - ); + var spaceBloc = event.context.read(); + List communities = spaceBloc.state.communityList; var prevSpaceModels = await fetchSpaceModels(prevState); + if (communities.isEmpty) { + communities = await _api.fetchCommunities(projectUuid); + + List updatedCommunities = await Future.wait( + communities.map((community) async { + List spaces = + await _fetchSpacesForCommunity(community.uuid); + return CommunityModel( + uuid: community.uuid, + createdAt: community.createdAt, + updatedAt: community.updatedAt, + name: community.name, + description: community.description, + spaces: spaces, + region: community.region, + ); + }).toList(), + ); + + communities = updatedCommunities; + } + emit(SpaceModelLoaded( - communities: updatedCommunities, - products: _cachedProducts ?? [], - spaceModels: prevSpaceModels)); + communities: communities, + products: _cachedProducts ?? [], + spaceModels: prevSpaceModels, + )); } catch (e) { emit(SpaceManagementError('Error loading communities and spaces: $e')); } @@ -667,7 +691,7 @@ class SpaceManagementBloc if (prevTags == null && newTags != null) { for (var newTag in newTags) { tagUpdates.add(TagModelUpdate( - action: Action.add, + action: custom_action.Action.add, tag: newTag.tag, uuid: newTag.uuid, productUuid: newTag.product?.uuid, @@ -683,14 +707,14 @@ class SpaceManagementBloc final existsInNew = newTags.any((newTag) => newTag.uuid == prevTag.uuid); if (!existsInNew) { - tagUpdates - .add(TagModelUpdate(action: Action.delete, uuid: prevTag.uuid)); + tagUpdates.add(TagModelUpdate( + action: custom_action.Action.delete, uuid: prevTag.uuid)); } } } else if (prevTags != null && newTags == null) { for (var prevTag in prevTags) { - tagUpdates - .add(TagModelUpdate(action: Action.delete, uuid: prevTag.uuid)); + tagUpdates.add(TagModelUpdate( + action: custom_action.Action.delete, uuid: prevTag.uuid)); } } @@ -703,7 +727,7 @@ class SpaceManagementBloc if ((newTag.uuid == null || !prevTagUuids.contains(newTag.uuid)) && !processedTags.contains(newTag.tag)) { tagUpdates.add(TagModelUpdate( - action: Action.add, + action: custom_action.Action.add, tag: newTag.tag, uuid: newTag.uuid == '' ? null : newTag.uuid, productUuid: newTag.product?.uuid)); @@ -720,7 +744,7 @@ class SpaceManagementBloc final newTag = newTagMap[prevTag.uuid]; if (newTag != null) { tagUpdates.add(TagModelUpdate( - action: Action.update, + action: custom_action.Action.update, uuid: newTag.uuid, tag: newTag.tag, )); diff --git a/lib/pages/spaces_management/all_spaces/bloc/space_management_event.dart b/lib/pages/spaces_management/all_spaces/bloc/space_management_event.dart index d25534b4..38e4bfb3 100644 --- a/lib/pages/spaces_management/all_spaces/bloc/space_management_event.dart +++ b/lib/pages/spaces_management/all_spaces/bloc/space_management_event.dart @@ -7,10 +7,16 @@ abstract class SpaceManagementEvent extends Equatable { const SpaceManagementEvent(); @override - List get props => []; + List get props => []; } -class LoadCommunityAndSpacesEvent extends SpaceManagementEvent {} +class LoadCommunityAndSpacesEvent extends SpaceManagementEvent { + final BuildContext context; + + const LoadCommunityAndSpacesEvent(this.context); + @override + List get props => [context]; +} class DeleteCommunityEvent extends SpaceManagementEvent { final String communityUuid; @@ -74,14 +80,12 @@ class UpdateSpacePositionEvent extends SpaceManagementEvent { class CreateCommunityEvent extends SpaceManagementEvent { final String name; final String description; + final BuildContext context; - const CreateCommunityEvent({ - required this.name, - required this.description, - }); + const CreateCommunityEvent(this.name, this.description, this.context); @override - List get props => [name, description]; + List get props => [name, description, context]; } class UpdateCommunityEvent extends SpaceManagementEvent { @@ -141,7 +145,18 @@ class LoadSpaceHierarchyEvent extends SpaceManagementEvent { List get props => [communityId]; } +class BlankStateEvent extends SpaceManagementEvent { + final BuildContext context; -class BlankStateEvent extends SpaceManagementEvent {} + const BlankStateEvent(this.context); + @override + List get props => [context]; +} -class SpaceModelLoadEvent extends SpaceManagementEvent {} +class SpaceModelLoadEvent extends SpaceManagementEvent { + final BuildContext context; + + const SpaceModelLoadEvent(this.context); + @override + List get props => [context]; +} diff --git a/lib/pages/spaces_management/all_spaces/view/spaces_management_page.dart b/lib/pages/spaces_management/all_spaces/view/spaces_management_page.dart index 02000e1a..d5db20eb 100644 --- a/lib/pages/spaces_management/all_spaces/view/spaces_management_page.dart +++ b/lib/pages/spaces_management/all_spaces/view/spaces_management_page.dart @@ -28,14 +28,14 @@ class SpaceManagementPageState extends State { return MultiBlocProvider( providers: [ BlocProvider( - create: (_) => SpaceManagementBloc( + create: (context) => SpaceManagementBloc( _api, _productApi, _spaceModelApi, - )..add(LoadCommunityAndSpacesEvent()), + )..add(LoadCommunityAndSpacesEvent(this.context)), ), BlocProvider( - create: (_) => CenterBodyBloc(), + create: (context) => CenterBodyBloc(), ), ], child: WebScaffold( diff --git a/lib/pages/spaces_management/all_spaces/widgets/blank_community_widget.dart b/lib/pages/spaces_management/all_spaces/widgets/blank_community_widget.dart index 999c27bd..66f1a026 100644 --- a/lib/pages/spaces_management/all_spaces/widgets/blank_community_widget.dart +++ b/lib/pages/spaces_management/all_spaces/widgets/blank_community_widget.dart @@ -73,17 +73,14 @@ class _BlankCommunityWidgetState extends State { context: parentContext, builder: (context) => CreateCommunityDialog( isEditMode: false, - existingCommunityNames: widget.communities.map((community) => community.name).toList(), + existingCommunityNames: + widget.communities.map((community) => community.name).toList(), onCreateCommunity: (String communityName, String description) { parentContext.read().add( - CreateCommunityEvent( - name: communityName, - description: description, - ), + CreateCommunityEvent(communityName, description, context), ); }, ), ); } - } diff --git a/lib/pages/spaces_management/structure_selector/view/center_body_widget.dart b/lib/pages/spaces_management/structure_selector/view/center_body_widget.dart index 45a6aaf7..0f40ddbb 100644 --- a/lib/pages/spaces_management/structure_selector/view/center_body_widget.dart +++ b/lib/pages/spaces_management/structure_selector/view/center_body_widget.dart @@ -15,11 +15,11 @@ class CenterBodyWidget extends StatelessWidget { context.read().add(CommunityStructureSelectedEvent()); } if (state is CommunityStructureState) { - context.read().add(BlankStateEvent()); + context.read().add(BlankStateEvent(context)); } if (state is SpaceModelState) { - context.read().add(SpaceModelLoadEvent()); + context.read().add(SpaceModelLoadEvent(context)); } return Container(