diff --git a/lib/pages/space_tree/bloc/space_tree_bloc.dart b/lib/pages/space_tree/bloc/space_tree_bloc.dart index 3325a721..6c3cee82 100644 --- a/lib/pages/space_tree/bloc/space_tree_bloc.dart +++ b/lib/pages/space_tree/bloc/space_tree_bloc.dart @@ -1,4 +1,3 @@ -import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:syncrow_web/pages/common/bloc/project_manager.dart'; import 'package:syncrow_web/pages/space_tree/bloc/space_tree_event.dart'; @@ -6,18 +5,8 @@ import 'package:syncrow_web/pages/space_tree/bloc/space_tree_state.dart'; import 'package:syncrow_web/pages/spaces_management/all_spaces/model/community_model.dart'; import 'package:syncrow_web/pages/spaces_management/all_spaces/model/space_model.dart'; import 'package:syncrow_web/services/space_mana_api.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'; class SpaceTreeBloc extends Bloc { - // String selectedCommunityId = 'aff21a57-2f91-4e5c-b99b-0182c3ab65a9'; - // String selectedSpaceId = '25c96044-fadf-44bb-93c7-3c079e527ce6'; - final TextEditingController textController = TextEditingController(); - - // String selectedCommunityId = 'aff21a57-2f91-4e5c-b99b-0182c3ab65a9'; - // String selectedSpaceId = '25c96044-fadf-44bb-93c7-3c079e527ce6'; - SpaceTreeBloc() : super(const SpaceTreeState()) { on(_fetchSpaces); on(_onCommunityExpanded); @@ -37,8 +26,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, @@ -53,19 +42,15 @@ 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')); } } - _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); @@ -97,17 +82,13 @@ 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 updatedSelectedSpaces = List.from(state.selectedSpaces.toSet().toList()); + List updatedSoldChecks = List.from(state.soldCheck.toSet().toList()); + Map> communityAndSpaces = Map.from(state.selectedCommunityAndSpaces); List childrenIds = _getAllChildIds(event.children); @@ -138,12 +119,9 @@ 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 childrenIds = _getAllChildIds(event.children); bool isChildSelected = false; @@ -158,16 +136,14 @@ class SpaceTreeBloc extends Bloc { !updatedSoldChecks.contains(event.spaceId)) { // First click: Select the space and all its children updatedSelectedSpaces.add(event.spaceId); - updatedSelectedCommunities.add(event.communityId); + updatedSelectedCommunities.add(event.communityModel.uuid); if (childrenIds.isNotEmpty) { updatedSelectedSpaces.addAll(childrenIds); } - List spaces = - _getThePathToChild(event.communityId, 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); } } @@ -187,17 +163,27 @@ class SpaceTreeBloc extends Bloc { updatedSoldChecks.remove(event.spaceId); List parents = - _getThePathToChild(event.communityId, event.spaceId); - if (!_parentSelected(parents, updatedSelectedSpaces)) { + _getThePathToChild(event.communityModel.uuid, event.spaceId).toSet().toList(); + + if (updatedSelectedSpaces.isEmpty) { updatedSoldChecks.removeWhere(parents.contains); - } - if (!_anySpacesSelectedInCommunity( - event.communityId, updatedSelectedSpaces, updatedSoldChecks)) { - updatedSelectedCommunities.remove(event.communityId); + updatedSelectedCommunities.remove(event.communityModel.uuid); + } else { + // Check if any parent has selected children + for (String space in parents) { + if (!_noChildrenSelected(event.communityModel, space, updatedSelectedSpaces, parents)) { + updatedSoldChecks.remove(space); + } + } + + if (!_anySpacesSelectedInCommunity( + event.communityModel, updatedSelectedSpaces, updatedSoldChecks)) { + updatedSelectedCommunities.remove(event.communityModel.uuid); + } } } - communityAndSpaces[event.communityId] = updatedSelectedSpaces; + communityAndSpaces[event.communityModel.uuid] = updatedSelectedSpaces; emit(state.copyWith( selectedCommunities: updatedSelectedCommunities.toSet().toList(), @@ -210,12 +196,24 @@ class SpaceTreeBloc extends Bloc { } } - _parentSelected(List parents, List selectedSpaces) { - for (String space in parents) { - if (selectedSpaces.contains(space)) { - return true; + _noChildrenSelected( + CommunityModel community, String spaceId, List selectedSpaces, List parents) { + if (selectedSpaces.contains(spaceId)) { + return true; + } + + List children = _getAllChildSpaces(community.spaces); + for (var child in children) { + if (spaceId == child.uuid) { + List ids = _getAllChildIds(child.children); + for (var id in ids) { + if (selectedSpaces.contains(id)) { + return true; + } + } } } + return false; } @@ -226,11 +224,10 @@ 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(); @@ -247,8 +244,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; } @@ -259,22 +256,26 @@ class SpaceTreeBloc extends Bloc { ids.add(child.uuid!); ids.addAll(_getAllChildIds(child.children)); } - return ids; + return ids.toSet().toList(); } - bool _anySpacesSelectedInCommunity(String communityId, - List selectedSpaces, List partialCheckedList) { + List _getAllChildSpaces(List spaces) { + List children = []; + for (var child in spaces) { + children.add(child); + children.addAll(_getAllChildSpaces(child.children)); + } + return children; + } + + bool _anySpacesSelectedInCommunity( + CommunityModel community, List selectedSpaces, List partialCheckedList) { bool result = false; - for (var community in state.communityList) { - if (community.uuid == communityId) { - List ids = _getAllChildIds(community.spaces); - for (var id in ids) { - result = - selectedSpaces.contains(id) || partialCheckedList.contains(id); - if (result) { - return result; - } - } + List ids = _getAllChildIds(community.spaces); + for (var id in ids) { + result = selectedSpaces.contains(id) || partialCheckedList.contains(id); + if (result) { + return result; } } return result; @@ -297,8 +298,7 @@ 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 ?? ''); @@ -322,7 +322,6 @@ class SpaceTreeBloc extends Bloc { @override Future close() async { - textController.dispose(); super.close(); } } diff --git a/lib/pages/space_tree/bloc/space_tree_event.dart b/lib/pages/space_tree/bloc/space_tree_event.dart index 7b1b550c..5a44d7fb 100644 --- a/lib/pages/space_tree/bloc/space_tree_event.dart +++ b/lib/pages/space_tree/bloc/space_tree_event.dart @@ -1,4 +1,5 @@ import 'package:equatable/equatable.dart'; +import 'package:syncrow_web/pages/spaces_management/all_spaces/model/community_model.dart'; import 'package:syncrow_web/pages/spaces_management/all_spaces/model/space_model.dart'; class SpaceTreeEvent extends Equatable { @@ -49,14 +50,14 @@ class OnSpaceExpanded extends SpaceTreeEvent { } class OnSpaceSelected extends SpaceTreeEvent { - final String communityId; final String spaceId; final List children; + final CommunityModel communityModel; - const OnSpaceSelected(this.communityId, this.spaceId, this.children); + const OnSpaceSelected(this.communityModel, this.spaceId, this.children); @override - List get props => [communityId, spaceId, children]; + List get props => [communityModel, spaceId, children]; } class SearchQueryEvent extends SpaceTreeEvent { diff --git a/lib/pages/space_tree/view/space_tree_view.dart b/lib/pages/space_tree/view/space_tree_view.dart index da038f7a..5d52a4d3 100644 --- a/lib/pages/space_tree/view/space_tree_view.dart +++ b/lib/pages/space_tree/view/space_tree_view.dart @@ -100,8 +100,8 @@ class _SpaceTreeViewState extends State { state.expandedSpaces.contains(space.uuid), onItemSelected: () { context.read().add( - OnSpaceSelected(community.uuid, - space.uuid ?? '', space.children)); + OnSpaceSelected(community, space.uuid ?? '', + space.children)); widget.onSelect(); }, onExpansionChanged: () { @@ -114,7 +114,7 @@ class _SpaceTreeViewState extends State { state.soldCheck.contains(space.uuid), isSoldCheck: state.soldCheck.contains(space.uuid), children: _buildNestedSpaces( - context, state, space, community.uuid), + context, state, space, community), ); }).toList(), ), @@ -197,7 +197,7 @@ class _SpaceTreeViewState extends State { } List _buildNestedSpaces( - BuildContext context, SpaceTreeState state, SpaceModel space, String communityId) { + BuildContext context, SpaceTreeState state, SpaceModel space, CommunityModel community) { return space.children.map((child) { return CustomExpansionTileSpaceTree( isSelected: @@ -208,13 +208,13 @@ class _SpaceTreeViewState extends State { onItemSelected: () { context .read() - .add(OnSpaceSelected(communityId, child.uuid ?? '', child.children)); + .add(OnSpaceSelected(community, child.uuid ?? '', child.children)); widget.onSelect(); }, onExpansionChanged: () { - context.read().add(OnSpaceExpanded(communityId, child.uuid ?? '')); + context.read().add(OnSpaceExpanded(community.uuid, child.uuid ?? '')); }, - children: _buildNestedSpaces(context, state, child, communityId), + children: _buildNestedSpaces(context, state, child, community), ); }).toList(); }