From 6e90f81760338e0febd178fef09fe48e911862e0 Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Wed, 5 Mar 2025 12:28:00 +0400 Subject: [PATCH] update space bloc on community and space change --- .env.development | 2 +- .../space_tree/bloc/space_tree_bloc.dart | 116 ++++++++++++------ .../space_tree/bloc/space_tree_event.dart | 9 ++ .../bloc/space_management_bloc.dart | 7 +- .../dialog/create_space_model_dialog.dart | 13 ++ .../widgets/space_model_card_widget.dart | 6 + 6 files changed, 114 insertions(+), 39 deletions(-) diff --git a/.env.development b/.env.development index e77609dc..1fd358ec 100644 --- a/.env.development +++ b/.env.development @@ -1,2 +1,2 @@ ENV_NAME=development -BASE_URL=https://syncrow-dev.azurewebsites.net \ No newline at end of file +BASE_URL=http://localhost:4001 \ No newline at end of file diff --git a/lib/pages/space_tree/bloc/space_tree_bloc.dart b/lib/pages/space_tree/bloc/space_tree_bloc.dart index 7820237b..31adaeb1 100644 --- a/lib/pages/space_tree/bloc/space_tree_bloc.dart +++ b/lib/pages/space_tree/bloc/space_tree_bloc.dart @@ -17,6 +17,32 @@ class SpaceTreeBloc extends Bloc { on(_clearAllData); on(_clearCachedData); on(_onCommunityAdded); + on(_onCommunityUpdate); + } + + void _onCommunityUpdate( + OnCommunityUpdated event, + Emitter emit, + ) async { + emit(SpaceTreeLoadingState()); + + try { + final updatedCommunity = event.updatedCommunity; + final updatedCommunities = + List.from(state.communityList); + + final index = updatedCommunities + .indexWhere((community) => community.uuid == updatedCommunity.uuid); + + if (index != -1) { + updatedCommunities[index] = updatedCommunity; + emit(state.copyWith(communitiesList: updatedCommunities)); + } else { + emit(SpaceTreeErrorState('Community not found in the list.')); + } + } catch (e) { + emit(SpaceTreeErrorState('Error updating community: $e')); + } } _fetchSpaces(InitialEvent event, Emitter emit) async { @@ -29,8 +55,8 @@ class SpaceTreeBloc extends Bloc { List updatedCommunities = await Future.wait( communities.map((community) async { - List spaces = - await CommunitySpaceManagementApi().getSpaceHierarchy(community.uuid, projectUuid); + List spaces = await CommunitySpaceManagementApi() + .getSpaceHierarchy(community.uuid, projectUuid); return CommunityModel( uuid: community.uuid, @@ -45,23 +71,27 @@ class SpaceTreeBloc extends Bloc { ); emit(state.copyWith( - communitiesList: updatedCommunities, expandedCommunity: [], expandedSpaces: [])); + communitiesList: updatedCommunities, + expandedCommunity: [], + expandedSpaces: [])); } catch (e) { emit(SpaceTreeErrorState('Error loading communities and spaces: $e')); } } - void _onCommunityAdded(OnCommunityAdded event, Emitter emit) async { - final updatedCommunities = List.from(state.communityList); - updatedCommunities.add(event.newCommunity); + void _onCommunityAdded( + OnCommunityAdded event, Emitter emit) async { + final updatedCommunities = List.from(state.communityList); + updatedCommunities.add(event.newCommunity); - emit(state.copyWith(communitiesList: updatedCommunities)); -} + emit(state.copyWith(communitiesList: updatedCommunities)); + } - - _onCommunityExpanded(OnCommunityExpanded event, Emitter emit) async { + _onCommunityExpanded( + OnCommunityExpanded event, Emitter emit) async { try { - List updatedExpandedCommunityList = List.from(state.expandedCommunities); + List updatedExpandedCommunityList = + List.from(state.expandedCommunities); if (updatedExpandedCommunityList.contains(event.communityId)) { updatedExpandedCommunityList.remove(event.communityId); @@ -93,14 +123,19 @@ class SpaceTreeBloc extends Bloc { } } - _onCommunitySelected(OnCommunitySelected event, Emitter emit) async { + _onCommunitySelected( + OnCommunitySelected event, Emitter emit) async { try { List updatedSelectedCommunities = List.from(state.selectedCommunities.toSet().toList()); - List updatedSelectedSpaces = List.from(state.selectedSpaces.toSet().toList()); - List updatedSoldChecks = List.from(state.soldCheck.toSet().toList()); - Map> communityAndSpaces = Map.from(state.selectedCommunityAndSpaces); - List selectedSpacesInCommunity = communityAndSpaces[event.communityId] ?? []; + List updatedSelectedSpaces = + List.from(state.selectedSpaces.toSet().toList()); + List updatedSoldChecks = + List.from(state.soldCheck.toSet().toList()); + Map> communityAndSpaces = + Map.from(state.selectedCommunityAndSpaces); + List selectedSpacesInCommunity = + communityAndSpaces[event.communityId] ?? []; List childrenIds = _getAllChildIds(event.children); @@ -133,11 +168,15 @@ class SpaceTreeBloc extends Bloc { try { List updatedSelectedCommunities = List.from(state.selectedCommunities.toSet().toList()); - List updatedSelectedSpaces = List.from(state.selectedSpaces.toSet().toList()); - List updatedSoldChecks = List.from(state.soldCheck.toSet().toList()); - Map> communityAndSpaces = Map.from(state.selectedCommunityAndSpaces); + List updatedSelectedSpaces = + List.from(state.selectedSpaces.toSet().toList()); + List updatedSoldChecks = + List.from(state.soldCheck.toSet().toList()); + Map> communityAndSpaces = + Map.from(state.selectedCommunityAndSpaces); - List selectedSpacesInCommunity = communityAndSpaces[event.communityModel.uuid] ?? []; + List selectedSpacesInCommunity = + communityAndSpaces[event.communityModel.uuid] ?? []; List childrenIds = _getAllChildIds(event.children); bool isChildSelected = false; @@ -160,9 +199,11 @@ class SpaceTreeBloc extends Bloc { selectedSpacesInCommunity.addAll(childrenIds); } - List spaces = _getThePathToChild(event.communityModel.uuid, event.spaceId); + List spaces = + _getThePathToChild(event.communityModel.uuid, event.spaceId); for (String space in spaces) { - if (!updatedSelectedSpaces.contains(space) && !updatedSoldChecks.contains(space)) { + if (!updatedSelectedSpaces.contains(space) && + !updatedSoldChecks.contains(space)) { updatedSoldChecks.add(space); } } @@ -185,7 +226,9 @@ class SpaceTreeBloc extends Bloc { updatedSoldChecks.remove(event.spaceId); List parents = - _getThePathToChild(event.communityModel.uuid, event.spaceId).toSet().toList(); + _getThePathToChild(event.communityModel.uuid, event.spaceId) + .toSet() + .toList(); if (updatedSelectedSpaces.isEmpty) { updatedSoldChecks.removeWhere(parents.contains); @@ -193,7 +236,8 @@ class SpaceTreeBloc extends Bloc { } else { // Check if any parent has selected children for (String space in parents) { - if (!_noChildrenSelected(event.communityModel, space, updatedSelectedSpaces, parents)) { + if (!_noChildrenSelected( + event.communityModel, space, updatedSelectedSpaces, parents)) { updatedSoldChecks.remove(space); } } @@ -218,8 +262,8 @@ class SpaceTreeBloc extends Bloc { } } - _noChildrenSelected( - CommunityModel community, String spaceId, List selectedSpaces, List parents) { + _noChildrenSelected(CommunityModel community, String spaceId, + List selectedSpaces, List parents) { if (selectedSpaces.contains(spaceId)) { return true; } @@ -246,10 +290,11 @@ class SpaceTreeBloc extends Bloc { // Filter communities and expand only those that match the query filteredCommunity = communities.where((community) { - final containsQueryInCommunity = - community.name.toLowerCase().contains(event.searchQuery.toLowerCase()); - final containsQueryInSpaces = - community.spaces.any((space) => _containsQuery(space, event.searchQuery.toLowerCase())); + final containsQueryInCommunity = community.name + .toLowerCase() + .contains(event.searchQuery.toLowerCase()); + final containsQueryInSpaces = community.spaces.any( + (space) => _containsQuery(space, event.searchQuery.toLowerCase())); return containsQueryInCommunity || containsQueryInSpaces; }).toList(); @@ -302,8 +347,8 @@ class SpaceTreeBloc extends Bloc { // Helper function to determine if any space or its children match the search query bool _containsQuery(SpaceModel space, String query) { final matchesSpace = space.name.toLowerCase().contains(query); - final matchesChildren = - space.children.any((child) => _containsQuery(child, query)); // Recursive check for children + final matchesChildren = space.children.any((child) => + _containsQuery(child, query)); // Recursive check for children return matchesSpace || matchesChildren; } @@ -326,8 +371,8 @@ class SpaceTreeBloc extends Bloc { return children; } - bool _anySpacesSelectedInCommunity( - CommunityModel community, List selectedSpaces, List partialCheckedList) { + bool _anySpacesSelectedInCommunity(CommunityModel community, + List selectedSpaces, List partialCheckedList) { bool result = false; List ids = _getAllChildIds(community.spaces); for (var id in ids) { @@ -356,7 +401,8 @@ class SpaceTreeBloc extends Bloc { return ids; } - List _getAllParentsIds(SpaceModel child, String spaceId, List listIds) { + List _getAllParentsIds( + SpaceModel child, String spaceId, List listIds) { List ids = listIds; ids.add(child.uuid ?? ''); diff --git a/lib/pages/space_tree/bloc/space_tree_event.dart b/lib/pages/space_tree/bloc/space_tree_event.dart index a22426f7..fdf1240b 100644 --- a/lib/pages/space_tree/bloc/space_tree_event.dart +++ b/lib/pages/space_tree/bloc/space_tree_event.dart @@ -77,6 +77,15 @@ class OnCommunityAdded extends SpaceTreeEvent { List get props => [newCommunity]; } +class OnCommunityUpdated extends SpaceTreeEvent { + final CommunityModel updatedCommunity; + const OnCommunityUpdated(this.updatedCommunity); + + @override + List get props => [updatedCommunity]; +} + + 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 ddc294d7..7b62d0ac 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 @@ -139,7 +139,6 @@ class SpaceManagementBloc UpdateCommunityEvent event, Emitter emit, ) async { - final previousState = state; try { final projectUuid = await ProjectManager.getProjectUUID() ?? ''; @@ -154,6 +153,8 @@ class SpaceManagementBloc for (var community in updatedCommunities) { if (community.uuid == event.communityUuid) { community.name = event.name; + _spaceTreeBloc.add(OnCommunityAdded(community)); + break; } } @@ -456,8 +457,6 @@ class SpaceManagementBloc event.communityUuid, emit, ); - } else { - // add(LoadCommunityAndSpacesEvent()); } } catch (e) { emit(SpaceManagementError('Error saving spaces: $e')); @@ -481,6 +480,8 @@ class SpaceManagementBloc for (var community in communities) { if (community.uuid == communityUuid) { community.spaces = allSpaces; + _spaceTreeBloc.add(OnCommunityUpdated(community)); + emit(SpaceManagementLoaded( communities: communities, products: _cachedProducts ?? [], diff --git a/lib/pages/spaces_management/space_model/widgets/dialog/create_space_model_dialog.dart b/lib/pages/spaces_management/space_model/widgets/dialog/create_space_model_dialog.dart index 9a89c452..69e619b7 100644 --- a/lib/pages/spaces_management/space_model/widgets/dialog/create_space_model_dialog.dart +++ b/lib/pages/spaces_management/space_model/widgets/dialog/create_space_model_dialog.dart @@ -2,6 +2,8 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:syncrow_web/pages/common/buttons/cancel_button.dart'; import 'package:syncrow_web/pages/common/buttons/default_button.dart'; +import 'package:syncrow_web/pages/spaces_management/all_spaces/bloc/space_management_bloc.dart'; +import 'package:syncrow_web/pages/spaces_management/all_spaces/bloc/space_management_event.dart'; import 'package:syncrow_web/pages/spaces_management/all_spaces/model/product_model.dart'; import 'package:syncrow_web/pages/spaces_management/space_model/bloc/create_space_model_bloc.dart'; import 'package:syncrow_web/pages/spaces_management/space_model/bloc/create_space_model_event.dart'; @@ -196,6 +198,12 @@ class CreateSpaceModelDialog extends StatelessWidget { .add(CreateSpaceModel( newSpaceModel: newModel)); + pageContext! + .read< + SpaceManagementBloc>() + .add( + UpdateSpaceModelCache( + newModel)); } Navigator.of(context) .pop(); // Close the dialog @@ -241,6 +249,11 @@ class CreateSpaceModelDialog extends StatelessWidget { spaceModelUuid: newModel.uuid ?? '')); + pageContext! + .read< + SpaceManagementBloc>() + .add(UpdateSpaceModelCache( + newModel)); } Navigator.of(context) .pop(); diff --git a/lib/pages/spaces_management/space_model/widgets/space_model_card_widget.dart b/lib/pages/spaces_management/space_model/widgets/space_model_card_widget.dart index ab65af44..692511cf 100644 --- a/lib/pages/spaces_management/space_model/widgets/space_model_card_widget.dart +++ b/lib/pages/spaces_management/space_model/widgets/space_model_card_widget.dart @@ -4,6 +4,8 @@ import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_svg/svg.dart'; import 'package:syncrow_web/pages/common/buttons/cancel_button.dart'; import 'package:syncrow_web/pages/common/buttons/default_button.dart'; +import 'package:syncrow_web/pages/spaces_management/all_spaces/bloc/space_management_bloc.dart'; +import 'package:syncrow_web/pages/spaces_management/all_spaces/bloc/space_management_event.dart'; import 'package:syncrow_web/pages/spaces_management/space_model/bloc/space_model_bloc.dart'; import 'package:syncrow_web/pages/spaces_management/space_model/bloc/space_model_event.dart'; @@ -182,6 +184,10 @@ class SpaceModelCardWidget extends StatelessWidget { pageContext!.read().add( DeleteSpaceModel(spaceModelUuid: model.uuid ?? ''), ); + + pageContext!.read().add( + DeleteSpaceModelFromCache(model.uuid ?? ''), + ); } }, );