mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 07:07:19 +00:00
Removed static space id and community id in the routine
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
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/space_tree/bloc/space_tree_event.dart';
|
||||
@ -8,9 +9,13 @@ import 'package:syncrow_web/services/space_mana_api.dart';
|
||||
import 'package:syncrow_web/utils/constants/temp_const.dart';
|
||||
|
||||
class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
|
||||
String selectedCommunityId = 'aff21a57-2f91-4e5c-b99b-0182c3ab65a9';
|
||||
String selectedSpaceId = '25c96044-fadf-44bb-93c7-3c079e527ce6';
|
||||
// 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()) {
|
||||
on<InitialEvent>(_fetchSpaces);
|
||||
@ -26,14 +31,13 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
|
||||
try {
|
||||
final projectUuid = projectCubit.state;
|
||||
|
||||
List<CommunityModel> communities = await CommunitySpaceManagementApi()
|
||||
.fetchCommunities(projectUuid ?? TempConst.projectId);
|
||||
List<CommunityModel> communities =
|
||||
await CommunitySpaceManagementApi().fetchCommunities(projectUuid ?? TempConst.projectId);
|
||||
|
||||
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 ?? TempConst.projectId);
|
||||
|
||||
return CommunityModel(
|
||||
uuid: community.uuid,
|
||||
@ -48,19 +52,15 @@ 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,17 +92,13 @@ 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);
|
||||
|
||||
@ -133,12 +129,9 @@ 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;
|
||||
@ -158,11 +151,9 @@ 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);
|
||||
}
|
||||
}
|
||||
@ -181,8 +172,7 @@ 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);
|
||||
}
|
||||
@ -195,7 +185,7 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
|
||||
communityAndSpaces[event.communityId] = updatedSelectedSpaces;
|
||||
|
||||
emit(state.copyWith(
|
||||
selectedCommunities: updatedSelectedCommunities,
|
||||
selectedCommunities: updatedSelectedCommunities.toSet().toList(),
|
||||
selectedSpaces: updatedSelectedSpaces,
|
||||
soldCheck: updatedSoldChecks,
|
||||
selectedCommunityAndSpaces: communityAndSpaces));
|
||||
@ -221,18 +211,18 @@ 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();
|
||||
|
||||
emit(state.copyWith(
|
||||
filteredCommunity: filteredCommunity,
|
||||
isSearching: event.searchQuery.isNotEmpty));
|
||||
isSearching: event.searchQuery.isNotEmpty,
|
||||
searchQuery: event.searchQuery));
|
||||
} catch (e) {
|
||||
emit(const SpaceTreeErrorState('Something went wrong'));
|
||||
}
|
||||
@ -241,8 +231,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;
|
||||
}
|
||||
@ -256,15 +246,14 @@ 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;
|
||||
}
|
||||
@ -291,8 +280,7 @@ 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 ?? '');
|
||||
@ -313,4 +301,10 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
|
||||
ids.removeLast();
|
||||
return [];
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() async {
|
||||
textController.dispose();
|
||||
super.close();
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ class SpaceTreeState extends Equatable {
|
||||
final List<String> selectedSpaces;
|
||||
final List<String> soldCheck;
|
||||
final bool isSearching;
|
||||
final String searchQuery;
|
||||
|
||||
const SpaceTreeState(
|
||||
{this.communityList = const [],
|
||||
@ -21,7 +22,8 @@ class SpaceTreeState extends Equatable {
|
||||
this.selectedSpaces = const [],
|
||||
this.soldCheck = const [],
|
||||
this.isSearching = false,
|
||||
this.selectedCommunityAndSpaces = const {}});
|
||||
this.selectedCommunityAndSpaces = const {},
|
||||
this.searchQuery = ''});
|
||||
|
||||
SpaceTreeState copyWith(
|
||||
{List<CommunityModel>? communitiesList,
|
||||
@ -32,7 +34,8 @@ class SpaceTreeState extends Equatable {
|
||||
List<String>? selectedSpaces,
|
||||
List<String>? soldCheck,
|
||||
bool? isSearching,
|
||||
Map<String, List<String>>? selectedCommunityAndSpaces}) {
|
||||
Map<String, List<String>>? selectedCommunityAndSpaces,
|
||||
String? searchQuery}) {
|
||||
return SpaceTreeState(
|
||||
communityList: communitiesList ?? this.communityList,
|
||||
filteredCommunity: filteredCommunity ?? this.filteredCommunity,
|
||||
@ -42,7 +45,8 @@ class SpaceTreeState extends Equatable {
|
||||
selectedSpaces: selectedSpaces ?? this.selectedSpaces,
|
||||
soldCheck: soldCheck ?? this.soldCheck,
|
||||
isSearching: isSearching ?? this.isSearching,
|
||||
selectedCommunityAndSpaces: selectedCommunityAndSpaces ?? this.selectedCommunityAndSpaces);
|
||||
selectedCommunityAndSpaces: selectedCommunityAndSpaces ?? this.selectedCommunityAndSpaces,
|
||||
searchQuery: searchQuery ?? this.searchQuery);
|
||||
}
|
||||
|
||||
@override
|
||||
@ -55,7 +59,8 @@ class SpaceTreeState extends Equatable {
|
||||
selectedSpaces,
|
||||
soldCheck,
|
||||
isSearching,
|
||||
selectedCommunityAndSpaces
|
||||
selectedCommunityAndSpaces,
|
||||
searchQuery
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -39,6 +39,7 @@ class _SpaceTreeViewState extends State<SpaceTreeView> {
|
||||
: Column(
|
||||
children: [
|
||||
CustomSearchBar(
|
||||
searchQuery: state.searchQuery,
|
||||
onSearchChanged: (query) {
|
||||
context.read<SpaceTreeBloc>().add(SearchQueryEvent(query));
|
||||
},
|
||||
|
Reference in New Issue
Block a user