load space models

This commit is contained in:
hannathkadher
2025-01-12 09:25:29 +04:00
parent cfc1b544b7
commit 6591ef1664

View File

@ -52,10 +52,14 @@ class SpaceManagementBloc
break;
}
}
var prevSpaceModels = await fetchSpaceModels(previousState);
emit(SpaceManagementLoaded(
communities: updatedCommunities,
products: previousState.products,
selectedCommunity: previousState.selectedCommunity,
spaceModels: prevSpaceModels,
));
}
} else {
@ -66,6 +70,23 @@ class SpaceManagementBloc
}
}
Future<List<SpaceTemplateModel>> fetchSpaceModels(
SpaceManagementState previousState) async {
List<SpaceTemplateModel> prevSpaceModels = [];
if (previousState is SpaceManagementLoaded || previousState is BlankState) {
prevSpaceModels = List<SpaceTemplateModel>.from(
(previousState as dynamic).spaceModels ?? [],
);
}
if (prevSpaceModels.isEmpty) {
prevSpaceModels = await _spaceModelApi.listSpaceModels(page: 1);
}
return prevSpaceModels;
}
void _onloadProducts() async {
if (_cachedProducts == null) {
final products = await _productApi.fetchProducts();
@ -89,19 +110,24 @@ class SpaceManagementBloc
return await _api.getSpaceHierarchy(communityUuid);
}
void _onNewCommunity(
Future<void> _onNewCommunity(
NewCommunityEvent event,
Emitter<SpaceManagementState> emit,
) {
) async {
try {
final previousState = state;
if (event.communities.isEmpty) {
emit(const SpaceManagementError('No communities provided.'));
return;
}
var prevSpaceModels = await fetchSpaceModels(previousState);
emit(BlankState(
communities: event.communities,
products: _cachedProducts ?? [],
spaceModels: prevSpaceModels,
));
} catch (error) {
emit(SpaceManagementError('Error loading communities: $error'));
@ -112,6 +138,7 @@ class SpaceManagementBloc
BlankStateEvent event, Emitter<SpaceManagementState> emit) async {
try {
final previousState = state;
var prevSpaceModels = await fetchSpaceModels(previousState);
if (previousState is SpaceManagementLoaded ||
previousState is BlankState) {
@ -119,6 +146,7 @@ class SpaceManagementBloc
emit(BlankState(
communities: List<CommunityModel>.from(prevCommunities),
products: _cachedProducts ?? [],
spaceModels: prevSpaceModels,
));
return;
}
@ -139,6 +167,7 @@ class SpaceManagementBloc
}));
emit(BlankState(
spaceModels: prevSpaceModels,
communities: updatedCommunities,
products: _cachedProducts ?? [],
));
@ -217,6 +246,7 @@ class SpaceManagementBloc
try {
CommunityModel? newCommunity =
await _api.createCommunity(event.name, event.description);
var prevSpaceModels = await fetchSpaceModels(previousState);
if (newCommunity != null) {
if (previousState is SpaceManagementLoaded ||
@ -226,6 +256,7 @@ class SpaceManagementBloc
);
final updatedCommunities = prevCommunities..add(newCommunity);
emit(SpaceManagementLoaded(
spaceModels: prevSpaceModels,
communities: updatedCommunities,
products: _cachedProducts ?? [],
selectedCommunity: newCommunity,