added update in space tree bloc

This commit is contained in:
hannathkadher
2025-03-02 14:40:34 +04:00
parent 7607e5f80d
commit 692b05a600
10 changed files with 172 additions and 124 deletions

View File

@ -8,9 +8,6 @@ import 'package:syncrow_web/pages/common/hour_picker_dialog.dart';
import 'package:syncrow_web/services/access_mang_api.dart';
import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/constants/app_enum.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';
import 'package:syncrow_web/utils/snack_bar.dart';
class AccessBloc extends Bloc<AccessEvent, AccessState> {

View File

@ -8,7 +8,6 @@ import 'package:syncrow_web/pages/device_managment/all_devices/models/devices_mo
import 'package:syncrow_web/pages/device_managment/all_devices/widgets/device_search_filters.dart';
import 'package:syncrow_web/pages/device_managment/shared/device_batch_control_dialog.dart';
import 'package:syncrow_web/pages/device_managment/shared/device_control_dialog.dart';
import 'package:syncrow_web/pages/space_tree/bloc/space_tree_bloc.dart';
import 'package:syncrow_web/pages/space_tree/view/space_tree_view.dart';
import 'package:syncrow_web/utils/format_date_time.dart';
import 'package:syncrow_web/utils/helpers/responsice_layout_helper/responsive_layout_helper.dart';

View File

@ -83,7 +83,6 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
try {
emit(LoadingHome());
policy = await HomeApi().fetchPolicy();
debugPrint("Fetched policy: $policy");
// Emit a state to trigger the UI update
emit(HomeInitial());
} catch (e) {

View File

@ -16,6 +16,7 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
on<SearchQueryEvent>(_onSearch);
on<ClearAllData>(_clearAllData);
on<ClearCachedData>(_clearCachedData);
on<OnCommunityAdded>(_onCommunityAdded);
}
_fetchSpaces(InitialEvent event, Emitter<SpaceTreeState> emit) async {
@ -50,6 +51,14 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
}
}
void _onCommunityAdded(OnCommunityAdded event, Emitter<SpaceTreeState> emit) async {
final updatedCommunities = List<CommunityModel>.from(state.communityList);
updatedCommunities.add(event.newCommunity);
emit(state.copyWith(communitiesList: updatedCommunities));
}
_onCommunityExpanded(OnCommunityExpanded event, Emitter<SpaceTreeState> emit) async {
try {
List<String> updatedExpandedCommunityList = List.from(state.expandedCommunities);

View File

@ -69,6 +69,14 @@ class SearchQueryEvent extends SpaceTreeEvent {
List<Object> get props => [searchQuery];
}
class OnCommunityAdded extends SpaceTreeEvent {
final CommunityModel newCommunity;
const OnCommunityAdded(this.newCommunity);
@override
List<Object> get props => [newCommunity];
}
class ClearAllData extends SpaceTreeEvent {}
class ClearCachedData extends SpaceTreeEvent {}

View File

@ -1,8 +1,10 @@
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:syncrow_web/pages/common/bloc/project_manager.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/spaces_management/all_spaces/model/community_model.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/create_subspace_model.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/product_model.dart';
@ -17,7 +19,7 @@ import 'package:syncrow_web/pages/spaces_management/space_model/models/tag_updat
import 'package:syncrow_web/services/product_api.dart';
import 'package:syncrow_web/services/space_mana_api.dart';
import 'package:syncrow_web/services/space_model_mang_api.dart';
import 'package:syncrow_web/utils/constants/action_enum.dart';
import 'package:syncrow_web/utils/constants/action_enum.dart' as custom_action;
class SpaceManagementBloc
extends Bloc<SpaceManagementEvent, SpaceManagementState> {
@ -30,7 +32,6 @@ class SpaceManagementBloc
SpaceManagementBloc(this._api, this._productApi, this._spaceModelApi)
: super(SpaceManagementInitial()) {
on<LoadCommunityAndSpacesEvent>(_onLoadCommunityAndSpaces);
on<UpdateSpacePositionEvent>(_onUpdateSpacePosition);
on<CreateCommunityEvent>(_onCreateCommunity);
on<SelectCommunityEvent>(_onSelectCommunity);
on<DeleteCommunityEvent>(_onCommunityDelete);
@ -182,6 +183,8 @@ class SpaceManagementBloc
try {
final previousState = state;
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
var spaceBloc = event.context.read<SpaceTreeBloc>();
List<CommunityModel> communities = spaceBloc.state.communityList;
var prevSpaceModels = await fetchSpaceModels(previousState);
@ -196,25 +199,31 @@ class SpaceManagementBloc
return;
}
final communities = await _api.fetchCommunities(projectUuid);
final updatedCommunities =
await Future.wait(communities.map((community) async {
final spaces = await _fetchSpacesForCommunity(community.uuid);
if (communities.isEmpty) {
communities = await _api.fetchCommunities(projectUuid);
return CommunityModel(
uuid: community.uuid,
createdAt: community.createdAt,
updatedAt: community.updatedAt,
name: community.name,
description: community.description,
spaces: spaces,
region: community.region,
List<CommunityModel> updatedCommunities = await Future.wait(
communities.map((community) async {
List<SpaceModel> spaces =
await _fetchSpacesForCommunity(community.uuid);
return CommunityModel(
uuid: community.uuid,
createdAt: community.createdAt,
updatedAt: community.updatedAt,
name: community.name,
description: community.description,
spaces: spaces,
region: community.region,
);
}).toList(),
);
}));
communities = updatedCommunities;
}
emit(BlankState(
spaceModels: prevSpaceModels,
communities: updatedCommunities,
communities: communities,
products: _cachedProducts ?? [],
));
} catch (error) {
@ -228,50 +237,49 @@ class SpaceManagementBloc
) async {
_logEvent('LoadCommunityAndSpacesEvent');
// If already loaded, use cached data
if (state is SpaceManagementLoaded) {
emit(state);
return;
var spaceBloc = event.context.read<SpaceTreeBloc>();
List<CommunityModel> communities = spaceBloc.state.communityList;
if (communities.isEmpty) {
_logEvent("community is empty");
emit(SpaceManagementLoading());
try {
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
_onloadProducts();
/* communities = await _api.fetchCommunities(projectUuid);
List<CommunityModel> updatedCommunities = await Future.wait(
communities.map((community) async {
List<SpaceModel> spaces =
await _fetchSpacesForCommunity(community.uuid);
return CommunityModel(
uuid: community.uuid,
createdAt: community.createdAt,
updatedAt: community.updatedAt,
name: community.name,
description: community.description,
spaces: spaces,
region: community.region,
);
}).toList(),
);
communities = updatedCommunities; */
} catch (e) {
emit(SpaceManagementError('Error loading communities and spaces: $e'));
return;
}
}
emit(SpaceManagementLoading());
final prevSpaceModels = await fetchSpaceModels(state);
try {
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
// Load products only once
_onloadProducts();
List<CommunityModel> communities =
await _api.fetchCommunities(projectUuid);
List<CommunityModel> updatedCommunities = await Future.wait(
communities.map((community) async {
List<SpaceModel> spaces =
await _fetchSpacesForCommunity(community.uuid);
return CommunityModel(
uuid: community.uuid,
createdAt: community.createdAt,
updatedAt: community.updatedAt,
name: community.name,
description: community.description,
spaces: spaces,
region: community.region,
);
}).toList(),
);
// Fetch space models only once
final prevSpaceModels = await fetchSpaceModels(state);
emit(SpaceManagementLoaded(
communities: updatedCommunities,
products: _cachedProducts ?? [],
spaceModels: prevSpaceModels,
));
} catch (e) {
emit(SpaceManagementError('Error loading communities and spaces: $e'));
}
emit(SpaceManagementLoaded(
communities: communities,
products: _cachedProducts ?? [],
spaceModels: prevSpaceModels,
));
}
void _onCommunityDelete(
@ -285,7 +293,7 @@ class SpaceManagementBloc
final success =
await _api.deleteCommunity(event.communityUuid, projectUuid);
if (success) {
add(LoadCommunityAndSpacesEvent());
// add(LoadCommunityAndSpacesEvent());
} else {
emit(const SpaceManagementError('Failed to delete the community.'));
}
@ -295,11 +303,6 @@ class SpaceManagementBloc
}
}
void _onUpdateSpacePosition(
UpdateSpacePositionEvent event,
Emitter<SpaceManagementState> emit,
) {}
void _onCreateCommunity(
CreateCommunityEvent event,
Emitter<SpaceManagementState> emit,
@ -309,6 +312,8 @@ class SpaceManagementBloc
try {
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
final BuildContext safeContext =
event.context; // Capture context safely before async calls
CommunityModel? newCommunity = await _api.createCommunity(
event.name, event.description, projectUuid);
@ -321,6 +326,16 @@ class SpaceManagementBloc
(previousState as dynamic).communities,
);
final updatedCommunities = prevCommunities..add(newCommunity);
if (safeContext.mounted) {
print("added");
final spaceTreeBloc = safeContext.read<
SpaceTreeBloc>(); // ✅ Read bloc only when context is mounted
spaceTreeBloc.add(OnCommunityAdded(newCommunity));
} else {
print("not mounted");
}
emit(SpaceManagementLoaded(
spaceModels: prevSpaceModels,
communities: updatedCommunities,
@ -416,7 +431,7 @@ class SpaceManagementBloc
emit,
);
} else {
add(LoadCommunityAndSpacesEvent());
// add(LoadCommunityAndSpacesEvent());
}
} catch (e) {
emit(SpaceManagementError('Error saving spaces: $e'));
@ -491,13 +506,15 @@ class SpaceManagementBloc
.any((subspace) => subspace.uuid == prevSubspace.uuid);
if (!existsInNew) {
subspaceUpdates.add(UpdateSubspaceTemplateModel(
action: Action.delete, uuid: prevSubspace.uuid));
action: custom_action.Action.delete,
uuid: prevSubspace.uuid));
}
}
} else if (prevSubspaces != null && newSubspaces == null) {
for (var prevSubspace in prevSubspaces) {
subspaceUpdates.add(UpdateSubspaceTemplateModel(
action: Action.delete, uuid: prevSubspace.uuid));
action: custom_action.Action.delete,
uuid: prevSubspace.uuid));
}
}
@ -510,14 +527,14 @@ class SpaceManagementBloc
if (newSubspace.tags != null) {
for (var tag in newSubspace.tags!) {
tagUpdates.add(TagModelUpdate(
action: Action.add,
action: custom_action.Action.add,
uuid: tag.uuid == '' ? null : tag.uuid,
tag: tag.tag,
productUuid: tag.product?.uuid));
}
}
subspaceUpdates.add(UpdateSubspaceTemplateModel(
action: Action.add,
action: custom_action.Action.add,
subspaceName: newSubspace.subspaceName,
tags: tagUpdates));
}
@ -536,7 +553,7 @@ class SpaceManagementBloc
final List<TagModelUpdate> tagSubspaceUpdates =
processTagUpdates(prevSubspace.tags, newSubspace.tags);
subspaceUpdates.add(UpdateSubspaceTemplateModel(
action: Action.update,
action: custom_action.Action.update,
uuid: newSubspace.uuid,
subspaceName: newSubspace.subspaceName,
tags: tagSubspaceUpdates));
@ -623,35 +640,42 @@ class SpaceManagementBloc
void _onLoadSpaceModel(
SpaceModelLoadEvent event, Emitter<SpaceManagementState> emit) async {
emit(SpaceManagementLoading());
try {
var prevState = state;
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
List<CommunityModel> communities =
await _api.fetchCommunities(projectUuid);
List<CommunityModel> updatedCommunities = await Future.wait(
communities.map((community) async {
List<SpaceModel> spaces =
await _fetchSpacesForCommunity(community.uuid);
return CommunityModel(
uuid: community.uuid,
createdAt: community.createdAt,
updatedAt: community.updatedAt,
name: community.name,
description: community.description,
spaces: spaces, // New spaces list
region: community.region,
);
}).toList(),
);
var spaceBloc = event.context.read<SpaceTreeBloc>();
List<CommunityModel> communities = spaceBloc.state.communityList;
var prevSpaceModels = await fetchSpaceModels(prevState);
if (communities.isEmpty) {
communities = await _api.fetchCommunities(projectUuid);
List<CommunityModel> updatedCommunities = await Future.wait(
communities.map((community) async {
List<SpaceModel> spaces =
await _fetchSpacesForCommunity(community.uuid);
return CommunityModel(
uuid: community.uuid,
createdAt: community.createdAt,
updatedAt: community.updatedAt,
name: community.name,
description: community.description,
spaces: spaces,
region: community.region,
);
}).toList(),
);
communities = updatedCommunities;
}
emit(SpaceModelLoaded(
communities: updatedCommunities,
products: _cachedProducts ?? [],
spaceModels: prevSpaceModels));
communities: communities,
products: _cachedProducts ?? [],
spaceModels: prevSpaceModels,
));
} catch (e) {
emit(SpaceManagementError('Error loading communities and spaces: $e'));
}
@ -667,7 +691,7 @@ class SpaceManagementBloc
if (prevTags == null && newTags != null) {
for (var newTag in newTags) {
tagUpdates.add(TagModelUpdate(
action: Action.add,
action: custom_action.Action.add,
tag: newTag.tag,
uuid: newTag.uuid,
productUuid: newTag.product?.uuid,
@ -683,14 +707,14 @@ class SpaceManagementBloc
final existsInNew =
newTags.any((newTag) => newTag.uuid == prevTag.uuid);
if (!existsInNew) {
tagUpdates
.add(TagModelUpdate(action: Action.delete, uuid: prevTag.uuid));
tagUpdates.add(TagModelUpdate(
action: custom_action.Action.delete, uuid: prevTag.uuid));
}
}
} else if (prevTags != null && newTags == null) {
for (var prevTag in prevTags) {
tagUpdates
.add(TagModelUpdate(action: Action.delete, uuid: prevTag.uuid));
tagUpdates.add(TagModelUpdate(
action: custom_action.Action.delete, uuid: prevTag.uuid));
}
}
@ -703,7 +727,7 @@ class SpaceManagementBloc
if ((newTag.uuid == null || !prevTagUuids.contains(newTag.uuid)) &&
!processedTags.contains(newTag.tag)) {
tagUpdates.add(TagModelUpdate(
action: Action.add,
action: custom_action.Action.add,
tag: newTag.tag,
uuid: newTag.uuid == '' ? null : newTag.uuid,
productUuid: newTag.product?.uuid));
@ -720,7 +744,7 @@ class SpaceManagementBloc
final newTag = newTagMap[prevTag.uuid];
if (newTag != null) {
tagUpdates.add(TagModelUpdate(
action: Action.update,
action: custom_action.Action.update,
uuid: newTag.uuid,
tag: newTag.tag,
));

View File

@ -7,10 +7,16 @@ abstract class SpaceManagementEvent extends Equatable {
const SpaceManagementEvent();
@override
List<Object> get props => [];
List<Object?> get props => [];
}
class LoadCommunityAndSpacesEvent extends SpaceManagementEvent {}
class LoadCommunityAndSpacesEvent extends SpaceManagementEvent {
final BuildContext context;
const LoadCommunityAndSpacesEvent(this.context);
@override
List<Object?> get props => [context];
}
class DeleteCommunityEvent extends SpaceManagementEvent {
final String communityUuid;
@ -74,14 +80,12 @@ class UpdateSpacePositionEvent extends SpaceManagementEvent {
class CreateCommunityEvent extends SpaceManagementEvent {
final String name;
final String description;
final BuildContext context;
const CreateCommunityEvent({
required this.name,
required this.description,
});
const CreateCommunityEvent(this.name, this.description, this.context);
@override
List<Object> get props => [name, description];
List<Object?> get props => [name, description, context];
}
class UpdateCommunityEvent extends SpaceManagementEvent {
@ -141,7 +145,18 @@ class LoadSpaceHierarchyEvent extends SpaceManagementEvent {
List<Object> get props => [communityId];
}
class BlankStateEvent extends SpaceManagementEvent {
final BuildContext context;
class BlankStateEvent extends SpaceManagementEvent {}
const BlankStateEvent(this.context);
@override
List<Object?> get props => [context];
}
class SpaceModelLoadEvent extends SpaceManagementEvent {}
class SpaceModelLoadEvent extends SpaceManagementEvent {
final BuildContext context;
const SpaceModelLoadEvent(this.context);
@override
List<Object?> get props => [context];
}

View File

@ -28,14 +28,14 @@ class SpaceManagementPageState extends State<SpaceManagementPage> {
return MultiBlocProvider(
providers: [
BlocProvider(
create: (_) => SpaceManagementBloc(
create: (context) => SpaceManagementBloc(
_api,
_productApi,
_spaceModelApi,
)..add(LoadCommunityAndSpacesEvent()),
)..add(LoadCommunityAndSpacesEvent(this.context)),
),
BlocProvider(
create: (_) => CenterBodyBloc(),
create: (context) => CenterBodyBloc(),
),
],
child: WebScaffold(

View File

@ -73,17 +73,14 @@ class _BlankCommunityWidgetState extends State<BlankCommunityWidget> {
context: parentContext,
builder: (context) => CreateCommunityDialog(
isEditMode: false,
existingCommunityNames: widget.communities.map((community) => community.name).toList(),
existingCommunityNames:
widget.communities.map((community) => community.name).toList(),
onCreateCommunity: (String communityName, String description) {
parentContext.read<SpaceManagementBloc>().add(
CreateCommunityEvent(
name: communityName,
description: description,
),
CreateCommunityEvent(communityName, description, context),
);
},
),
);
}
}

View File

@ -15,11 +15,11 @@ class CenterBodyWidget extends StatelessWidget {
context.read<CenterBodyBloc>().add(CommunityStructureSelectedEvent());
}
if (state is CommunityStructureState) {
context.read<SpaceManagementBloc>().add(BlankStateEvent());
context.read<SpaceManagementBloc>().add(BlankStateEvent(context));
}
if (state is SpaceModelState) {
context.read<SpaceManagementBloc>().add(SpaceModelLoadEvent());
context.read<SpaceManagementBloc>().add(SpaceModelLoadEvent(context));
}
return Container(