mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
354 lines
11 KiB
Dart
354 lines
11 KiB
Dart
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:syncrow_web/pages/spaces_management/model/community_model.dart';
|
|
import 'package:syncrow_web/pages/spaces_management/model/product_model.dart';
|
|
import 'package:syncrow_web/pages/spaces_management/model/space_model.dart';
|
|
import 'package:syncrow_web/pages/spaces_management/bloc/space_management_event.dart';
|
|
import 'package:syncrow_web/pages/spaces_management/bloc/space_management_state.dart';
|
|
import 'package:syncrow_web/services/product_api.dart';
|
|
import 'package:syncrow_web/services/space_mana_api.dart';
|
|
|
|
class SpaceManagementBloc
|
|
extends Bloc<SpaceManagementEvent, SpaceManagementState> {
|
|
final CommunitySpaceManagementApi _api;
|
|
final ProductApi _productApi;
|
|
|
|
List<ProductModel>? _cachedProducts;
|
|
|
|
SpaceManagementBloc(this._api, this._productApi)
|
|
: super(SpaceManagementInitial()) {
|
|
on<LoadCommunityAndSpacesEvent>(_onLoadCommunityAndSpaces);
|
|
on<UpdateSpacePositionEvent>(_onUpdateSpacePosition);
|
|
on<CreateCommunityEvent>(_onCreateCommunity);
|
|
on<SelectCommunityEvent>(_onSelectCommunity);
|
|
on<DeleteCommunityEvent>(_onCommunityDelete);
|
|
on<UpdateCommunityEvent>(_onUpdateCommunity);
|
|
on<SaveSpacesEvent>(_onSaveSpaces);
|
|
on<FetchProductsEvent>(_onFetchProducts);
|
|
on<SelectSpaceEvent>(_onSelectSpace);
|
|
}
|
|
|
|
void _onUpdateCommunity(
|
|
UpdateCommunityEvent event,
|
|
Emitter<SpaceManagementState> emit,
|
|
) async {
|
|
final previousState = state;
|
|
try {
|
|
emit(SpaceManagementLoading());
|
|
final success =
|
|
await _api.updateCommunity(event.communityUuid, event.name);
|
|
if (success) {
|
|
if (previousState is SpaceManagementLoaded) {
|
|
final updatedCommunities =
|
|
List<CommunityModel>.from(previousState.communities);
|
|
for (var community in updatedCommunities) {
|
|
if (community.uuid == event.communityUuid) {
|
|
community.name = event.name;
|
|
break;
|
|
}
|
|
}
|
|
emit(SpaceManagementLoaded(
|
|
communities: updatedCommunities,
|
|
products: previousState.products,
|
|
selectedCommunity: previousState.selectedCommunity,
|
|
));
|
|
}
|
|
} else {
|
|
emit(const SpaceManagementError('Failed to update the community.'));
|
|
}
|
|
} catch (e) {
|
|
emit(SpaceManagementError('Error updating community: $e'));
|
|
}
|
|
}
|
|
|
|
void _onloadProducts() async {
|
|
if (_cachedProducts == null) {
|
|
final products = await _productApi.fetchProducts();
|
|
_cachedProducts = products;
|
|
}
|
|
}
|
|
|
|
void _onFetchProducts(
|
|
FetchProductsEvent event,
|
|
Emitter<SpaceManagementState> emit,
|
|
) async {
|
|
try {
|
|
_onloadProducts();
|
|
} catch (e) {
|
|
emit(SpaceManagementError('Error fetching products: $e'));
|
|
}
|
|
}
|
|
|
|
Future<List<SpaceModel>> _fetchSpacesForCommunity(
|
|
String communityUuid) async {
|
|
return await _api.getSpaceHierarchy(communityUuid);
|
|
}
|
|
|
|
void _onLoadCommunityAndSpaces(
|
|
LoadCommunityAndSpacesEvent event,
|
|
Emitter<SpaceManagementState> emit,
|
|
) async {
|
|
emit(SpaceManagementLoading());
|
|
try {
|
|
_onloadProducts();
|
|
List<CommunityModel> communities = await _api.fetchCommunities();
|
|
|
|
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(),
|
|
);
|
|
|
|
emit(SpaceManagementLoaded(
|
|
communities: updatedCommunities, products: _cachedProducts ?? []));
|
|
} catch (e) {
|
|
emit(SpaceManagementError('Error loading communities and spaces: $e'));
|
|
}
|
|
}
|
|
|
|
void _onCommunityDelete(
|
|
DeleteCommunityEvent event,
|
|
Emitter<SpaceManagementState> emit,
|
|
) async {
|
|
try {
|
|
emit(SpaceManagementLoading());
|
|
|
|
final success = await _api.deleteCommunity(event.communityUuid);
|
|
if (success) {
|
|
add(LoadCommunityAndSpacesEvent());
|
|
} else {
|
|
emit(const SpaceManagementError('Failed to delete the community.'));
|
|
}
|
|
} catch (e) {
|
|
// Handle unexpected errors
|
|
emit(SpaceManagementError('Error saving spaces: $e'));
|
|
}
|
|
}
|
|
|
|
void _onUpdateSpacePosition(
|
|
UpdateSpacePositionEvent event,
|
|
Emitter<SpaceManagementState> emit,
|
|
) {}
|
|
|
|
void _onCreateCommunity(
|
|
CreateCommunityEvent event,
|
|
Emitter<SpaceManagementState> emit,
|
|
) async {
|
|
final previousState = state;
|
|
emit(SpaceManagementLoading());
|
|
|
|
try {
|
|
CommunityModel? newCommunity =
|
|
await _api.createCommunity(event.name, event.description);
|
|
|
|
if (newCommunity != null) {
|
|
if (previousState is SpaceManagementLoaded) {
|
|
final updatedCommunities =
|
|
List<CommunityModel>.from(previousState.communities)
|
|
..add(newCommunity);
|
|
emit(SpaceManagementLoaded(
|
|
communities: updatedCommunities,
|
|
products: _cachedProducts ?? [],
|
|
selectedCommunity: null,
|
|
selectedSpace: null));
|
|
}
|
|
} else {
|
|
emit(const SpaceManagementError('Error creating community'));
|
|
}
|
|
} catch (e) {
|
|
emit(SpaceManagementError('Error creating community: $e'));
|
|
}
|
|
}
|
|
|
|
void _onSelectCommunity(
|
|
SelectCommunityEvent event,
|
|
Emitter<SpaceManagementState> emit,
|
|
) async {
|
|
_handleCommunitySpaceStateUpdate(
|
|
emit: emit,
|
|
selectedCommunity: event.selectedCommunity,
|
|
selectedSpace: null,
|
|
);
|
|
}
|
|
|
|
void _onSelectSpace(
|
|
SelectSpaceEvent event,
|
|
Emitter<SpaceManagementState> emit,
|
|
) {
|
|
_handleCommunitySpaceStateUpdate(
|
|
emit: emit,
|
|
selectedCommunity: event.selectedCommunity,
|
|
selectedSpace: event.selectedSpace,
|
|
);
|
|
}
|
|
|
|
void _handleCommunitySpaceStateUpdate({
|
|
required Emitter<SpaceManagementState> emit,
|
|
CommunityModel? selectedCommunity,
|
|
SpaceModel? selectedSpace,
|
|
}) {
|
|
final previousState = state;
|
|
emit(SpaceManagementLoading());
|
|
|
|
try {
|
|
if (previousState is SpaceManagementLoaded) {
|
|
final communities = List<CommunityModel>.from(
|
|
(previousState as dynamic).communities,
|
|
);
|
|
emit(SpaceManagementLoaded(
|
|
communities: communities,
|
|
products: _cachedProducts ?? [],
|
|
selectedCommunity: selectedCommunity,
|
|
selectedSpace: selectedSpace,
|
|
));
|
|
}
|
|
} catch (e) {
|
|
emit(SpaceManagementError('Error updating state: $e'));
|
|
}
|
|
}
|
|
|
|
void _onSaveSpaces(
|
|
SaveSpacesEvent event,
|
|
Emitter<SpaceManagementState> emit,
|
|
) async {
|
|
final previousState = state;
|
|
emit(SpaceManagementLoading());
|
|
|
|
try {
|
|
final updatedSpaces =
|
|
await saveSpacesHierarchically(event.spaces, event.communityUuid);
|
|
|
|
final allSpaces = await _fetchSpacesForCommunity(event.communityUuid);
|
|
|
|
emit(SpaceCreationSuccess(spaces: updatedSpaces));
|
|
|
|
if (previousState is SpaceManagementLoaded) {
|
|
_updateLoadedState(
|
|
previousState,
|
|
allSpaces,
|
|
event.communityUuid,
|
|
emit,
|
|
);
|
|
} else {
|
|
add(LoadCommunityAndSpacesEvent());
|
|
}
|
|
} catch (e) {
|
|
emit(SpaceManagementError('Error saving spaces: $e'));
|
|
|
|
if (previousState is SpaceManagementLoaded) {
|
|
emit(previousState);
|
|
}
|
|
}
|
|
}
|
|
|
|
void _updateLoadedState(
|
|
SpaceManagementLoaded previousState,
|
|
List<SpaceModel> allSpaces,
|
|
String communityUuid,
|
|
Emitter<SpaceManagementState> emit,
|
|
) {
|
|
final communities = List<CommunityModel>.from(previousState.communities);
|
|
|
|
for (var community in communities) {
|
|
if (community.uuid == communityUuid) {
|
|
community.spaces = allSpaces;
|
|
emit(SpaceManagementLoaded(
|
|
communities: communities,
|
|
products: _cachedProducts ?? [],
|
|
selectedCommunity: community,
|
|
selectedSpace: null,
|
|
));
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<List<SpaceModel>> saveSpacesHierarchically(
|
|
List<SpaceModel> spaces, String communityUuid) async {
|
|
final orderedSpaces = flattenHierarchy(spaces);
|
|
|
|
final parentsToDelete = orderedSpaces.where((space) =>
|
|
space.status == SpaceStatus.deleted &&
|
|
(space.parent == null || space.parent?.status != SpaceStatus.deleted));
|
|
|
|
for (var parent in parentsToDelete) {
|
|
try {
|
|
if (parent.uuid != null) {
|
|
await _api.deleteSpace(communityUuid, parent.uuid!);
|
|
}
|
|
} catch (e) {
|
|
print(
|
|
'Error deleting space ${parent.name} (UUID: ${parent.uuid}, Community UUID: $communityUuid): $e');
|
|
rethrow; // Decide whether to stop execution or continue
|
|
}
|
|
}
|
|
|
|
for (var space in orderedSpaces) {
|
|
try {
|
|
if (space.uuid != null && space.uuid!.isNotEmpty) {
|
|
final response = await _api.updateSpace(
|
|
communityId: communityUuid,
|
|
spaceId: space.uuid!,
|
|
name: space.name,
|
|
parentId: space.parent?.uuid,
|
|
isPrivate: space.isPrivate,
|
|
position: space.position,
|
|
icon: space.icon,
|
|
direction: space.incomingConnection?.direction,
|
|
products: space.selectedProducts);
|
|
} else {
|
|
// Call create if the space does not have a UUID
|
|
final response = await _api.createSpace(
|
|
communityId: communityUuid,
|
|
name: space.name,
|
|
parentId: space.parent?.uuid,
|
|
isPrivate: space.isPrivate,
|
|
position: space.position,
|
|
icon: space.icon,
|
|
direction: space.incomingConnection?.direction,
|
|
products: space.selectedProducts);
|
|
space.uuid = response?.uuid;
|
|
}
|
|
} catch (e) {
|
|
print('Error creating space ${space.name}: $e');
|
|
rethrow; // Stop further execution on failure
|
|
}
|
|
}
|
|
return spaces;
|
|
}
|
|
|
|
List<SpaceModel> flattenHierarchy(List<SpaceModel> spaces) {
|
|
final result = <SpaceModel>{};
|
|
final topLevelSpaces = spaces.where((space) => space.parent == null);
|
|
|
|
void visit(SpaceModel space) {
|
|
if (!result.contains(space)) {
|
|
result.add(space);
|
|
for (var child in spaces.where((s) => s.parent == space)) {
|
|
visit(child);
|
|
}
|
|
}
|
|
}
|
|
|
|
for (var space in topLevelSpaces) {
|
|
visit(space);
|
|
}
|
|
|
|
for (var space in spaces) {
|
|
if (!result.contains(space)) {
|
|
result.add(space);
|
|
}
|
|
}
|
|
return result.toList(); // Convert back to a list
|
|
}
|
|
}
|