mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 07:07:19 +00:00
Merge branch 'dev1' into link_space_model_spaces
This commit is contained in:
@ -1,23 +1,24 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:syncrow_web/pages/common/bloc/project_cubit.dart';
|
||||
import 'package:syncrow_web/pages/common/bloc/project_manager.dart';
|
||||
import 'package:syncrow_web/pages/space_tree/bloc/space_tree_event.dart';
|
||||
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<SpaceTreeEvent, SpaceTreeState> {
|
||||
// String selectedCommunityId = 'aff21a57-2f91-4e5c-b99b-0182c3ab65a9';
|
||||
// String selectedSpaceId = '25c96044-fadf-44bb-93c7-3c079e527ce6';
|
||||
final ProjectCubit projectCubit;
|
||||
final TextEditingController textController = TextEditingController();
|
||||
|
||||
// String selectedCommunityId = 'aff21a57-2f91-4e5c-b99b-0182c3ab65a9';
|
||||
// String selectedSpaceId = '25c96044-fadf-44bb-93c7-3c079e527ce6';
|
||||
|
||||
SpaceTreeBloc(this.projectCubit) : super(const SpaceTreeState()) {
|
||||
SpaceTreeBloc() : super(const SpaceTreeState()) {
|
||||
on<InitialEvent>(_fetchSpaces);
|
||||
on<OnCommunityExpanded>(_onCommunityExpanded);
|
||||
on<OnSpaceExpanded>(_onSpaceExpanded);
|
||||
@ -29,15 +30,15 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
|
||||
_fetchSpaces(InitialEvent event, Emitter<SpaceTreeState> emit) async {
|
||||
emit(SpaceTreeLoadingState());
|
||||
try {
|
||||
final projectUuid = projectCubit.state;
|
||||
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
||||
|
||||
List<CommunityModel> communities =
|
||||
await CommunitySpaceManagementApi().fetchCommunities(projectUuid ?? TempConst.projectId);
|
||||
await CommunitySpaceManagementApi().fetchCommunities(projectUuid);
|
||||
|
||||
List<CommunityModel> updatedCommunities = await Future.wait(
|
||||
communities.map((community) async {
|
||||
List<SpaceModel> spaces = await CommunitySpaceManagementApi()
|
||||
.getSpaceHierarchy(community.uuid, projectUuid ?? TempConst.projectId);
|
||||
.getSpaceHierarchy(community.uuid, projectUuid);
|
||||
|
||||
return CommunityModel(
|
||||
uuid: community.uuid,
|
||||
@ -52,15 +53,19 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
|
||||
);
|
||||
|
||||
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<SpaceTreeState> emit) async {
|
||||
_onCommunityExpanded(
|
||||
OnCommunityExpanded event, Emitter<SpaceTreeState> emit) async {
|
||||
try {
|
||||
List<String> updatedExpandedCommunityList = List.from(state.expandedCommunities);
|
||||
List<String> updatedExpandedCommunityList =
|
||||
List.from(state.expandedCommunities);
|
||||
|
||||
if (updatedExpandedCommunityList.contains(event.communityId)) {
|
||||
updatedExpandedCommunityList.remove(event.communityId);
|
||||
@ -92,13 +97,17 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
|
||||
}
|
||||
}
|
||||
|
||||
_onCommunitySelected(OnCommunitySelected event, Emitter<SpaceTreeState> emit) async {
|
||||
_onCommunitySelected(
|
||||
OnCommunitySelected event, Emitter<SpaceTreeState> emit) async {
|
||||
try {
|
||||
List<String> updatedSelectedCommunities =
|
||||
List.from(state.selectedCommunities.toSet().toList());
|
||||
List<String> updatedSelectedSpaces = List.from(state.selectedSpaces.toSet().toList());
|
||||
List<String> updatedSoldChecks = List.from(state.soldCheck.toSet().toList());
|
||||
Map<String, List<String>> communityAndSpaces = Map.from(state.selectedCommunityAndSpaces);
|
||||
List<String> updatedSelectedSpaces =
|
||||
List.from(state.selectedSpaces.toSet().toList());
|
||||
List<String> updatedSoldChecks =
|
||||
List.from(state.soldCheck.toSet().toList());
|
||||
Map<String, List<String>> communityAndSpaces =
|
||||
Map.from(state.selectedCommunityAndSpaces);
|
||||
|
||||
List<String> childrenIds = _getAllChildIds(event.children);
|
||||
|
||||
@ -129,9 +138,12 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
|
||||
try {
|
||||
List<String> updatedSelectedCommunities =
|
||||
List.from(state.selectedCommunities.toSet().toList());
|
||||
List<String> updatedSelectedSpaces = List.from(state.selectedSpaces.toSet().toList());
|
||||
List<String> updatedSoldChecks = List.from(state.soldCheck.toSet().toList());
|
||||
Map<String, List<String>> communityAndSpaces = Map.from(state.selectedCommunityAndSpaces);
|
||||
List<String> updatedSelectedSpaces =
|
||||
List.from(state.selectedSpaces.toSet().toList());
|
||||
List<String> updatedSoldChecks =
|
||||
List.from(state.soldCheck.toSet().toList());
|
||||
Map<String, List<String>> communityAndSpaces =
|
||||
Map.from(state.selectedCommunityAndSpaces);
|
||||
|
||||
List<String> childrenIds = _getAllChildIds(event.children);
|
||||
bool isChildSelected = false;
|
||||
@ -151,9 +163,11 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
|
||||
updatedSelectedSpaces.addAll(childrenIds);
|
||||
}
|
||||
|
||||
List<String> spaces = _getThePathToChild(event.communityId, event.spaceId);
|
||||
List<String> spaces =
|
||||
_getThePathToChild(event.communityId, event.spaceId);
|
||||
for (String space in spaces) {
|
||||
if (!updatedSelectedSpaces.contains(space) && !updatedSoldChecks.contains(space)) {
|
||||
if (!updatedSelectedSpaces.contains(space) &&
|
||||
!updatedSoldChecks.contains(space)) {
|
||||
updatedSoldChecks.add(space);
|
||||
}
|
||||
}
|
||||
@ -172,7 +186,8 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
|
||||
}
|
||||
updatedSoldChecks.remove(event.spaceId);
|
||||
|
||||
List<String> parents = _getThePathToChild(event.communityId, event.spaceId);
|
||||
List<String> parents =
|
||||
_getThePathToChild(event.communityId, event.spaceId);
|
||||
if (!_parentSelected(parents, updatedSelectedSpaces)) {
|
||||
updatedSoldChecks.removeWhere(parents.contains);
|
||||
}
|
||||
@ -211,10 +226,11 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
|
||||
|
||||
// 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();
|
||||
@ -231,8 +247,8 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
|
||||
// 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;
|
||||
}
|
||||
@ -246,14 +262,15 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
|
||||
return ids;
|
||||
}
|
||||
|
||||
bool _anySpacesSelectedInCommunity(
|
||||
String communityId, List<String> selectedSpaces, List<String> partialCheckedList) {
|
||||
bool _anySpacesSelectedInCommunity(String communityId,
|
||||
List<String> selectedSpaces, List<String> partialCheckedList) {
|
||||
bool result = false;
|
||||
for (var community in state.communityList) {
|
||||
if (community.uuid == communityId) {
|
||||
List<String> ids = _getAllChildIds(community.spaces);
|
||||
for (var id in ids) {
|
||||
result = selectedSpaces.contains(id) || partialCheckedList.contains(id);
|
||||
result =
|
||||
selectedSpaces.contains(id) || partialCheckedList.contains(id);
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
@ -280,7 +297,8 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
|
||||
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;
|
||||
|
||||
ids.add(child.uuid ?? '');
|
||||
|
Reference in New Issue
Block a user