deselect selected spaces when selecting a new space in analytics side bar.

This commit is contained in:
Faris Armoush
2025-05-06 15:53:43 +03:00
parent f8f58a24b8
commit 55a6974bdc
4 changed files with 74 additions and 29 deletions

View File

@ -1,4 +1,7 @@
import 'package:flutter/material.dart'; 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'; import 'package:syncrow_web/pages/space_tree/view/space_tree_view.dart';
class AnalyticsCommunitiesSidebar extends StatelessWidget { class AnalyticsCommunitiesSidebar extends StatelessWidget {
@ -10,7 +13,9 @@ class AnalyticsCommunitiesSidebar extends StatelessWidget {
child: SpaceTreeView( child: SpaceTreeView(
title: const Text('Communities'), title: const Text('Communities'),
shouldDisableDeselectingChildrenOfSelectedParent: true, shouldDisableDeselectingChildrenOfSelectedParent: true,
onSelect: () {}, onSelect: () {
context.read<SpaceTreeBloc>().add(const SpaceTreeClearSelectionEvent());
},
isSide: false, isSide: false,
), ),
); );

View File

@ -23,6 +23,7 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
on<OnCommunityUpdated>(_onCommunityUpdate); on<OnCommunityUpdated>(_onCommunityUpdate);
on<PaginationEvent>(_fetchPaginationSpaces); on<PaginationEvent>(_fetchPaginationSpaces);
on<DebouncedSearchEvent>(_onDebouncedSearch); on<DebouncedSearchEvent>(_onDebouncedSearch);
on<SpaceTreeClearSelectionEvent>(_onSpaceTreeClearSelectionEvent);
} }
Timer _timer = Timer(const Duration(microseconds: 0), () {}); Timer _timer = Timer(const Duration(microseconds: 0), () {});
@ -36,8 +37,8 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
final updatedCommunity = event.updatedCommunity; final updatedCommunity = event.updatedCommunity;
final updatedCommunities = List<CommunityModel>.from(state.communityList); final updatedCommunities = List<CommunityModel>.from(state.communityList);
final index = final index = updatedCommunities
updatedCommunities.indexWhere((community) => community.uuid == updatedCommunity.uuid); .indexWhere((community) => community.uuid == updatedCommunity.uuid);
if (index != -1) { if (index != -1) {
updatedCommunities[index] = updatedCommunity; updatedCommunities[index] = updatedCommunity;
@ -93,8 +94,11 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
if (paginationModel.hasNext && state.searchQuery.isEmpty) { if (paginationModel.hasNext && state.searchQuery.isEmpty) {
final projectUuid = await ProjectManager.getProjectUUID() ?? ''; final projectUuid = await ProjectManager.getProjectUUID() ?? '';
paginationModel = await CommunitySpaceManagementApi().fetchCommunitiesAndSpaces( paginationModel = await CommunitySpaceManagementApi()
projectId: projectUuid, page: paginationModel.pageNum, search: state.searchQuery); .fetchCommunitiesAndSpaces(
projectId: projectUuid,
page: paginationModel.pageNum,
search: state.searchQuery);
communities.addAll(paginationModel.communities); communities.addAll(paginationModel.communities);
} }
@ -107,16 +111,19 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
paginationIsLoading: false)); paginationIsLoading: false));
} }
void _onCommunityAdded(OnCommunityAdded event, Emitter<SpaceTreeState> emit) async { void _onCommunityAdded(
OnCommunityAdded event, Emitter<SpaceTreeState> emit) async {
final updatedCommunities = List<CommunityModel>.from(state.communityList); final updatedCommunities = List<CommunityModel>.from(state.communityList);
updatedCommunities.add(event.newCommunity); updatedCommunities.add(event.newCommunity);
emit(state.copyWith(communitiesList: updatedCommunities)); emit(state.copyWith(communitiesList: updatedCommunities));
} }
_onCommunityExpanded(OnCommunityExpanded event, Emitter<SpaceTreeState> emit) async { _onCommunityExpanded(
OnCommunityExpanded event, Emitter<SpaceTreeState> emit) async {
try { try {
List<String> updatedExpandedCommunityList = List.from(state.expandedCommunities); List<String> updatedExpandedCommunityList =
List.from(state.expandedCommunities);
if (updatedExpandedCommunityList.contains(event.communityId)) { if (updatedExpandedCommunityList.contains(event.communityId)) {
updatedExpandedCommunityList.remove(event.communityId); updatedExpandedCommunityList.remove(event.communityId);
@ -148,14 +155,18 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
} }
} }
_onCommunitySelected(OnCommunitySelected event, Emitter<SpaceTreeState> emit) async { _onCommunitySelected(
OnCommunitySelected event, Emitter<SpaceTreeState> emit) async {
try { try {
List<String> updatedSelectedCommunities = List<String> updatedSelectedCommunities =
List.from(state.selectedCommunities.toSet().toList()); List.from(state.selectedCommunities.toSet().toList());
List<String> updatedSelectedSpaces = List.from(state.selectedSpaces.toSet().toList()); List<String> updatedSelectedSpaces =
List.from(state.selectedSpaces.toSet().toList());
List<String> updatedSoldChecks = List.from(state.soldCheck.toSet().toList()); List<String> updatedSoldChecks = List.from(state.soldCheck.toSet().toList());
Map<String, List<String>> communityAndSpaces = Map.from(state.selectedCommunityAndSpaces); Map<String, List<String>> communityAndSpaces =
List<String> selectedSpacesInCommunity = communityAndSpaces[event.communityId] ?? []; Map.from(state.selectedCommunityAndSpaces);
List<String> selectedSpacesInCommunity =
communityAndSpaces[event.communityId] ?? [];
List<String> childrenIds = _getAllChildIds(event.children); List<String> childrenIds = _getAllChildIds(event.children);
@ -188,11 +199,14 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
try { try {
List<String> updatedSelectedCommunities = List<String> updatedSelectedCommunities =
List.from(state.selectedCommunities.toSet().toList()); List.from(state.selectedCommunities.toSet().toList());
List<String> updatedSelectedSpaces = List.from(state.selectedSpaces.toSet().toList()); List<String> updatedSelectedSpaces =
List.from(state.selectedSpaces.toSet().toList());
List<String> updatedSoldChecks = List.from(state.soldCheck.toSet().toList()); List<String> updatedSoldChecks = List.from(state.soldCheck.toSet().toList());
Map<String, List<String>> communityAndSpaces = Map.from(state.selectedCommunityAndSpaces); Map<String, List<String>> communityAndSpaces =
Map.from(state.selectedCommunityAndSpaces);
List<String> selectedSpacesInCommunity = communityAndSpaces[event.communityModel.uuid] ?? []; List<String> selectedSpacesInCommunity =
communityAndSpaces[event.communityModel.uuid] ?? [];
List<String> childrenIds = _getAllChildIds(event.children); List<String> childrenIds = _getAllChildIds(event.children);
bool isChildSelected = false; bool isChildSelected = false;
@ -215,9 +229,11 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
selectedSpacesInCommunity.addAll(childrenIds); selectedSpacesInCommunity.addAll(childrenIds);
} }
List<String> spaces = _getThePathToChild(event.communityModel.uuid, event.spaceId); List<String> spaces =
_getThePathToChild(event.communityModel.uuid, event.spaceId);
for (String space in spaces) { for (String space in spaces) {
if (!updatedSelectedSpaces.contains(space) && !updatedSoldChecks.contains(space)) { if (!updatedSelectedSpaces.contains(space) &&
!updatedSoldChecks.contains(space)) {
updatedSoldChecks.add(space); updatedSoldChecks.add(space);
} }
} }
@ -240,7 +256,9 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
updatedSoldChecks.remove(event.spaceId); updatedSoldChecks.remove(event.spaceId);
List<String> parents = List<String> parents =
_getThePathToChild(event.communityModel.uuid, event.spaceId).toSet().toList(); _getThePathToChild(event.communityModel.uuid, event.spaceId)
.toSet()
.toList();
if (updatedSelectedSpaces.isEmpty) { if (updatedSelectedSpaces.isEmpty) {
updatedSoldChecks.removeWhere(parents.contains); updatedSoldChecks.removeWhere(parents.contains);
@ -248,7 +266,8 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
} else { } else {
// Check if any parent has selected children // Check if any parent has selected children
for (String space in parents) { for (String space in parents) {
if (!_noChildrenSelected(event.communityModel, space, updatedSelectedSpaces, parents)) { if (!_noChildrenSelected(
event.communityModel, space, updatedSelectedSpaces, parents)) {
updatedSoldChecks.remove(space); updatedSoldChecks.remove(space);
} }
} }
@ -273,8 +292,8 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
} }
} }
_noChildrenSelected( _noChildrenSelected(CommunityModel community, String spaceId,
CommunityModel community, String spaceId, List<String> selectedSpaces, List<String> parents) { List<String> selectedSpaces, List<String> parents) {
if (selectedSpaces.contains(spaceId)) { if (selectedSpaces.contains(spaceId)) {
return true; return true;
} }
@ -300,7 +319,8 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
if (_timer.isActive) { if (_timer.isActive) {
_timer.cancel(); // clear timer _timer.cancel(); // clear timer
} }
_timer = Timer(duration, () async => add(DebouncedSearchEvent(event.searchQuery))); _timer =
Timer(duration, () async => add(DebouncedSearchEvent(event.searchQuery)));
// List<CommunityModel> communities = List.from(state.communityList); // List<CommunityModel> communities = List.from(state.communityList);
// List<CommunityModel> filteredCommunity = []; // List<CommunityModel> filteredCommunity = [];
@ -324,7 +344,8 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
} }
} }
_onDebouncedSearch(DebouncedSearchEvent event, Emitter<SpaceTreeState> emit) async { _onDebouncedSearch(
DebouncedSearchEvent event, Emitter<SpaceTreeState> emit) async {
emit(state.copyWith( emit(state.copyWith(
isSearching: true, isSearching: true,
)); ));
@ -333,7 +354,8 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
final projectUuid = await ProjectManager.getProjectUUID() ?? ''; final projectUuid = await ProjectManager.getProjectUUID() ?? '';
paginationModel = await CommunitySpaceManagementApi() paginationModel = await CommunitySpaceManagementApi()
.fetchCommunitiesAndSpaces(projectId: projectUuid, page: 1, search: event.searchQuery); .fetchCommunitiesAndSpaces(
projectId: projectUuid, page: 1, search: event.searchQuery);
} catch (_) {} } catch (_) {}
emit(state.copyWith( emit(state.copyWith(
@ -405,8 +427,8 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
return children; return children;
} }
bool _anySpacesSelectedInCommunity( bool _anySpacesSelectedInCommunity(CommunityModel community,
CommunityModel community, List<String> selectedSpaces, List<String> partialCheckedList) { List<String> selectedSpaces, List<String> partialCheckedList) {
bool result = false; bool result = false;
List<String> ids = _getAllChildIds(community.spaces); List<String> ids = _getAllChildIds(community.spaces);
for (var id in ids) { for (var id in ids) {
@ -435,7 +457,8 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
return ids; return ids;
} }
List<String> _getAllParentsIds(SpaceModel child, String spaceId, List<String> listIds) { List<String> _getAllParentsIds(
SpaceModel child, String spaceId, List<String> listIds) {
List<String> ids = listIds; List<String> ids = listIds;
ids.add(child.uuid ?? ''); ids.add(child.uuid ?? '');
@ -457,6 +480,19 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
return []; return [];
} }
void _onSpaceTreeClearSelectionEvent(
SpaceTreeClearSelectionEvent event,
Emitter<SpaceTreeState> emit,
) async {
emit(
state.copyWith(
selectedCommunities: [],
selectedCommunityAndSpaces: {},
selectedSpaces: [],
),
);
}
@override @override
Future<void> close() async { Future<void> close() async {
_timer.cancel(); _timer.cancel();

View File

@ -108,3 +108,7 @@ class OnCommunityUpdated extends SpaceTreeEvent {
class ClearAllData extends SpaceTreeEvent {} class ClearAllData extends SpaceTreeEvent {}
class ClearCachedData extends SpaceTreeEvent {} class ClearCachedData extends SpaceTreeEvent {}
class SpaceTreeClearSelectionEvent extends SpaceTreeEvent {
const SpaceTreeClearSelectionEvent();
}

View File

@ -170,13 +170,13 @@ class _SpaceTreeViewState extends State<SpaceTreeView> {
communities[index].uuid, communities[index].uuid,
), ),
onItemSelected: () { onItemSelected: () {
widget.onSelect();
context.read<SpaceTreeBloc>().add( context.read<SpaceTreeBloc>().add(
OnCommunitySelected( OnCommunitySelected(
communities[index].uuid, communities[index].uuid,
communities[index].spaces, communities[index].spaces,
), ),
); );
widget.onSelect();
}, },
children: communities[index].spaces.map( children: communities[index].spaces.map(
(space) { (space) {
@ -195,6 +195,7 @@ class _SpaceTreeViewState extends State<SpaceTreeView> {
isParentSelected) { isParentSelected) {
return; return;
} }
widget.onSelect();
context.read<SpaceTreeBloc>().add( context.read<SpaceTreeBloc>().add(
OnSpaceSelected( OnSpaceSelected(
communities[index], communities[index],
@ -202,7 +203,6 @@ class _SpaceTreeViewState extends State<SpaceTreeView> {
space.children, space.children,
), ),
); );
widget.onSelect();
}, },
onExpansionChanged: () => onExpansionChanged: () =>
context.read<SpaceTreeBloc>().add( context.read<SpaceTreeBloc>().add(