diff --git a/lib/pages/analytics/modules/analytics/widgets/analytics_communities_sidebar.dart b/lib/pages/analytics/modules/analytics/widgets/analytics_communities_sidebar.dart index c6d1b7af..d8a3cbd7 100644 --- a/lib/pages/analytics/modules/analytics/widgets/analytics_communities_sidebar.dart +++ b/lib/pages/analytics/modules/analytics/widgets/analytics_communities_sidebar.dart @@ -1,4 +1,7 @@ import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.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/space_tree/view/space_tree_view.dart'; class AnalyticsCommunitiesSidebar extends StatelessWidget { @@ -10,7 +13,9 @@ class AnalyticsCommunitiesSidebar extends StatelessWidget { child: SpaceTreeView( title: const Text('Communities'), shouldDisableDeselectingChildrenOfSelectedParent: true, - onSelect: () {}, + onSelect: () { + context.read().add(const SpaceTreeClearSelectionEvent()); + }, isSide: false, ), ); diff --git a/lib/pages/space_tree/bloc/space_tree_bloc.dart b/lib/pages/space_tree/bloc/space_tree_bloc.dart index ab20b430..a3a29004 100644 --- a/lib/pages/space_tree/bloc/space_tree_bloc.dart +++ b/lib/pages/space_tree/bloc/space_tree_bloc.dart @@ -23,6 +23,7 @@ class SpaceTreeBloc extends Bloc { on(_onCommunityUpdate); on(_fetchPaginationSpaces); on(_onDebouncedSearch); + on(_onSpaceTreeClearSelectionEvent); } Timer _timer = Timer(const Duration(microseconds: 0), () {}); @@ -36,8 +37,8 @@ class SpaceTreeBloc extends Bloc { final updatedCommunity = event.updatedCommunity; final updatedCommunities = List.from(state.communityList); - final index = - updatedCommunities.indexWhere((community) => community.uuid == updatedCommunity.uuid); + final index = updatedCommunities + .indexWhere((community) => community.uuid == updatedCommunity.uuid); if (index != -1) { updatedCommunities[index] = updatedCommunity; @@ -93,8 +94,11 @@ class SpaceTreeBloc extends Bloc { if (paginationModel.hasNext && state.searchQuery.isEmpty) { final projectUuid = await ProjectManager.getProjectUUID() ?? ''; - paginationModel = await CommunitySpaceManagementApi().fetchCommunitiesAndSpaces( - projectId: projectUuid, page: paginationModel.pageNum, search: state.searchQuery); + paginationModel = await CommunitySpaceManagementApi() + .fetchCommunitiesAndSpaces( + projectId: projectUuid, + page: paginationModel.pageNum, + search: state.searchQuery); communities.addAll(paginationModel.communities); } @@ -107,16 +111,19 @@ class SpaceTreeBloc extends Bloc { paginationIsLoading: false)); } - void _onCommunityAdded(OnCommunityAdded event, Emitter emit) async { + 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 { + _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); @@ -148,14 +155,18 @@ 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 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] ?? []; + Map> communityAndSpaces = + Map.from(state.selectedCommunityAndSpaces); + List selectedSpacesInCommunity = + communityAndSpaces[event.communityId] ?? []; List childrenIds = _getAllChildIds(event.children); @@ -188,11 +199,14 @@ class SpaceTreeBloc extends Bloc { try { List updatedSelectedCommunities = List.from(state.selectedCommunities.toSet().toList()); - List updatedSelectedSpaces = List.from(state.selectedSpaces.toSet().toList()); + List updatedSelectedSpaces = + List.from(state.selectedSpaces.toSet().toList()); List updatedSoldChecks = List.from(state.soldCheck.toSet().toList()); - Map> communityAndSpaces = Map.from(state.selectedCommunityAndSpaces); + 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; @@ -215,9 +229,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); } } @@ -240,7 +256,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); @@ -248,7 +266,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); } } @@ -273,8 +292,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; } @@ -300,7 +319,8 @@ class SpaceTreeBloc extends Bloc { if (_timer.isActive) { _timer.cancel(); // clear timer } - _timer = Timer(duration, () async => add(DebouncedSearchEvent(event.searchQuery))); + _timer = + Timer(duration, () async => add(DebouncedSearchEvent(event.searchQuery))); // List communities = List.from(state.communityList); // List filteredCommunity = []; @@ -324,7 +344,8 @@ class SpaceTreeBloc extends Bloc { } } - _onDebouncedSearch(DebouncedSearchEvent event, Emitter emit) async { + _onDebouncedSearch( + DebouncedSearchEvent event, Emitter emit) async { emit(state.copyWith( isSearching: true, )); @@ -333,7 +354,8 @@ class SpaceTreeBloc extends Bloc { final projectUuid = await ProjectManager.getProjectUUID() ?? ''; paginationModel = await CommunitySpaceManagementApi() - .fetchCommunitiesAndSpaces(projectId: projectUuid, page: 1, search: event.searchQuery); + .fetchCommunitiesAndSpaces( + projectId: projectUuid, page: 1, search: event.searchQuery); } catch (_) {} emit(state.copyWith( @@ -405,8 +427,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) { @@ -435,7 +457,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 ?? ''); @@ -457,6 +480,19 @@ class SpaceTreeBloc extends Bloc { return []; } + void _onSpaceTreeClearSelectionEvent( + SpaceTreeClearSelectionEvent event, + Emitter emit, + ) async { + emit( + state.copyWith( + selectedCommunities: [], + selectedCommunityAndSpaces: {}, + selectedSpaces: [], + ), + ); + } + @override Future close() async { _timer.cancel(); diff --git a/lib/pages/space_tree/bloc/space_tree_event.dart b/lib/pages/space_tree/bloc/space_tree_event.dart index 22254ce0..9c2342fc 100644 --- a/lib/pages/space_tree/bloc/space_tree_event.dart +++ b/lib/pages/space_tree/bloc/space_tree_event.dart @@ -108,3 +108,7 @@ class OnCommunityUpdated extends SpaceTreeEvent { class ClearAllData extends SpaceTreeEvent {} class ClearCachedData extends SpaceTreeEvent {} + +class SpaceTreeClearSelectionEvent extends SpaceTreeEvent { + const SpaceTreeClearSelectionEvent(); +} diff --git a/lib/pages/space_tree/view/space_tree_view.dart b/lib/pages/space_tree/view/space_tree_view.dart index 85087d92..fadcdc0c 100644 --- a/lib/pages/space_tree/view/space_tree_view.dart +++ b/lib/pages/space_tree/view/space_tree_view.dart @@ -170,13 +170,13 @@ class _SpaceTreeViewState extends State { communities[index].uuid, ), onItemSelected: () { + widget.onSelect(); context.read().add( OnCommunitySelected( communities[index].uuid, communities[index].spaces, ), ); - widget.onSelect(); }, children: communities[index].spaces.map( (space) { @@ -195,6 +195,7 @@ class _SpaceTreeViewState extends State { isParentSelected) { return; } + widget.onSelect(); context.read().add( OnSpaceSelected( communities[index], @@ -202,7 +203,6 @@ class _SpaceTreeViewState extends State { space.children, ), ); - widget.onSelect(); }, onExpansionChanged: () => context.read().add(