From 77a9aa2f19e878f791db64c7bd8621b084eb6eeb Mon Sep 17 00:00:00 2001 From: Abdullah Alassaf Date: Wed, 26 Mar 2025 04:29:33 +0300 Subject: [PATCH] Added include spaces to the communites api --- .../space_tree/bloc/space_tree_bloc.dart | 117 +++++++----------- lib/services/space_mana_api.dart | 44 +++---- 2 files changed, 63 insertions(+), 98 deletions(-) diff --git a/lib/pages/space_tree/bloc/space_tree_bloc.dart b/lib/pages/space_tree/bloc/space_tree_bloc.dart index 31adaeb1..6315ee0f 100644 --- a/lib/pages/space_tree/bloc/space_tree_bloc.dart +++ b/lib/pages/space_tree/bloc/space_tree_bloc.dart @@ -28,11 +28,10 @@ class SpaceTreeBloc extends Bloc { try { final updatedCommunity = event.updatedCommunity; - final updatedCommunities = - List.from(state.communityList); + 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; @@ -51,47 +50,41 @@ class SpaceTreeBloc extends Bloc { final projectUuid = await ProjectManager.getProjectUUID() ?? ''; List communities = - await CommunitySpaceManagementApi().fetchCommunities(projectUuid); + await CommunitySpaceManagementApi().fetchCommunities(projectUuid, includeSpaces: true); - List updatedCommunities = await Future.wait( - communities.map((community) async { - List spaces = await CommunitySpaceManagementApi() - .getSpaceHierarchy(community.uuid, projectUuid); + // List updatedCommunities = await Future.wait( + // communities.map((community) async { + // List spaces = + // await CommunitySpaceManagementApi().getSpaceHierarchy(community.uuid, projectUuid); - return CommunityModel( - uuid: community.uuid, - createdAt: community.createdAt, - updatedAt: community.updatedAt, - name: community.name, - description: community.description, - spaces: spaces, - region: community.region, - ); - }).toList(), - ); + // return CommunityModel( + // uuid: community.uuid, + // createdAt: community.createdAt, + // updatedAt: community.updatedAt, + // name: community.name, + // description: community.description, + // spaces: spaces, + // region: community.region, + // ); + // }).toList(), + // ); - emit(state.copyWith( - communitiesList: updatedCommunities, - expandedCommunity: [], - expandedSpaces: [])); + emit(state.copyWith(communitiesList: communities, expandedCommunity: [], expandedSpaces: [])); } catch (e) { emit(SpaceTreeErrorState('Error loading communities and spaces: $e')); } } - 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); @@ -123,19 +116,14 @@ 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); @@ -168,15 +156,11 @@ 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; @@ -199,11 +183,9 @@ 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); } } @@ -226,9 +208,7 @@ 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); @@ -236,8 +216,7 @@ 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); } } @@ -262,8 +241,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; } @@ -290,11 +269,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(); @@ -347,8 +325,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; } @@ -371,8 +349,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) { @@ -401,8 +379,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 ?? ''); diff --git a/lib/services/space_mana_api.dart b/lib/services/space_mana_api.dart index f18a8da8..629f775c 100644 --- a/lib/services/space_mana_api.dart +++ b/lib/services/space_mana_api.dart @@ -3,27 +3,24 @@ import 'package:syncrow_web/pages/spaces_management/all_spaces/model/community_m import 'package:syncrow_web/pages/spaces_management/all_spaces/model/create_subspace_model.dart'; import 'package:syncrow_web/pages/spaces_management/all_spaces/model/space_model.dart'; import 'package:syncrow_web/pages/spaces_management/all_spaces/model/space_response_model.dart'; -import 'package:syncrow_web/pages/spaces_management/space_model/models/create_space_template_body_model.dart'; import 'package:syncrow_web/pages/spaces_management/space_model/models/space_template_model.dart'; import 'package:syncrow_web/pages/spaces_management/space_model/models/tag_body_model.dart'; import 'package:syncrow_web/pages/spaces_management/space_model/models/tag_update_model.dart'; import 'package:syncrow_web/services/api/http_service.dart'; import 'package:syncrow_web/utils/constants/api_const.dart'; -import 'package:syncrow_web/utils/constants/temp_const.dart'; class CommunitySpaceManagementApi { // Community Management APIs Future> fetchCommunities(String projectId, - {int page = 1}) async { + {int page = 1, bool includeSpaces = false}) async { try { List allCommunities = []; bool hasNext = true; while (hasNext) { await HTTPService().get( - path: ApiEndpoints.getCommunityList - .replaceAll('{projectId}', projectId), - queryParameters: {'page': page}, + path: ApiEndpoints.getCommunityList.replaceAll('{projectId}', projectId), + queryParameters: {'page': page, 'includeSpaces': includeSpaces}, expectedResponseModel: (json) { try { List jsonData = json['data'] ?? []; @@ -52,8 +49,7 @@ class CommunitySpaceManagementApi { Future getCommunityById(String communityId) async { try { final response = await HTTPService().get( - path: ApiEndpoints.getCommunityById - .replaceAll('{communityId}', communityId), + path: ApiEndpoints.getCommunityById.replaceAll('{communityId}', communityId), expectedResponseModel: (json) { return CommunityModel.fromJson(json['data']); }, @@ -65,8 +61,7 @@ class CommunitySpaceManagementApi { } } - Future createCommunity( - String name, String description, String projectId) async { + Future createCommunity(String name, String description, String projectId) async { try { final response = await HTTPService().post( path: ApiEndpoints.createCommunity.replaceAll('{projectId}', projectId), @@ -85,8 +80,7 @@ class CommunitySpaceManagementApi { } } - Future updateCommunity( - String communityId, String name, String projectId) async { + Future updateCommunity(String communityId, String name, String projectId) async { try { final response = await HTTPService().put( path: ApiEndpoints.updateCommunity @@ -123,8 +117,7 @@ class CommunitySpaceManagementApi { } } - Future fetchSpaces( - String communityId, String projectId) async { + Future fetchSpaces(String communityId, String projectId) async { try { final response = await HTTPService().get( path: ApiEndpoints.listSpaces @@ -150,8 +143,7 @@ class CommunitySpaceManagementApi { } } - Future getSpace( - String communityId, String spaceId, String projectId) async { + Future getSpace(String communityId, String spaceId, String projectId) async { try { final response = await HTTPService().get( path: ApiEndpoints.getSpace @@ -262,8 +254,7 @@ class CommunitySpaceManagementApi { } } - Future deleteSpace( - String communityId, String spaceId, String projectId) async { + Future deleteSpace(String communityId, String spaceId, String projectId) async { try { final response = await HTTPService().delete( path: ApiEndpoints.deleteSpace @@ -281,17 +272,15 @@ class CommunitySpaceManagementApi { } } - Future> getSpaceHierarchy( - String communityId, String projectId) async { + Future> getSpaceHierarchy(String communityId, String projectId) async { try { final response = await HTTPService().get( path: ApiEndpoints.getSpaceHierarchy .replaceAll('{communityId}', communityId) .replaceAll('{projectId}', projectId), expectedResponseModel: (json) { - final spaceModels = (json['data'] as List) - .map((spaceJson) => SpaceModel.fromJson(spaceJson)) - .toList(); + final spaceModels = + (json['data'] as List).map((spaceJson) => SpaceModel.fromJson(spaceJson)).toList(); return spaceModels; }, @@ -302,17 +291,16 @@ class CommunitySpaceManagementApi { return []; } } - Future> getSpaceOnlyWithDevices( - {String? communityId, String? projectId}) async { + + Future> getSpaceOnlyWithDevices({String? communityId, String? projectId}) async { try { final response = await HTTPService().get( path: ApiEndpoints.spaceOnlyWithDevices .replaceAll('{communityId}', communityId!) .replaceAll('{projectId}', projectId!), expectedResponseModel: (json) { - final spaceModels = (json['data'] as List) - .map((spaceJson) => SpaceModel.fromJson(spaceJson)) - .toList(); + final spaceModels = + (json['data'] as List).map((spaceJson) => SpaceModel.fromJson(spaceJson)).toList(); return spaceModels; }, );