reduced no of calls on load

This commit is contained in:
hannathkadher
2025-02-28 19:37:50 +04:00
parent b84513c09d
commit 7607e5f80d

View File

@ -18,9 +18,6 @@ import 'package:syncrow_web/services/product_api.dart';
import 'package:syncrow_web/services/space_mana_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/services/space_model_mang_api.dart';
import 'package:syncrow_web/utils/constants/action_enum.dart'; import 'package:syncrow_web/utils/constants/action_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';
class SpaceManagementBloc class SpaceManagementBloc
extends Bloc<SpaceManagementEvent, SpaceManagementState> { extends Bloc<SpaceManagementEvent, SpaceManagementState> {
@ -231,12 +228,20 @@ class SpaceManagementBloc
) async { ) async {
_logEvent('LoadCommunityAndSpacesEvent'); _logEvent('LoadCommunityAndSpacesEvent');
var prevState = state; // If already loaded, use cached data
if (state is SpaceManagementLoaded) {
emit(state);
return;
}
emit(SpaceManagementLoading()); emit(SpaceManagementLoading());
try { try {
final projectUuid = await ProjectManager.getProjectUUID() ?? ''; final projectUuid = await ProjectManager.getProjectUUID() ?? '';
// Load products only once
_onloadProducts(); _onloadProducts();
List<CommunityModel> communities = List<CommunityModel> communities =
await _api.fetchCommunities(projectUuid); await _api.fetchCommunities(projectUuid);
@ -250,17 +255,20 @@ class SpaceManagementBloc
updatedAt: community.updatedAt, updatedAt: community.updatedAt,
name: community.name, name: community.name,
description: community.description, description: community.description,
spaces: spaces, // New spaces list spaces: spaces,
region: community.region, region: community.region,
); );
}).toList(), }).toList(),
); );
final prevSpaceModels = await fetchSpaceModels(prevState); // Fetch space models only once
final prevSpaceModels = await fetchSpaceModels(state);
emit(SpaceManagementLoaded( emit(SpaceManagementLoaded(
communities: updatedCommunities, communities: updatedCommunities,
products: _cachedProducts ?? [], products: _cachedProducts ?? [],
spaceModels: prevSpaceModels)); spaceModels: prevSpaceModels,
));
} catch (e) { } catch (e) {
emit(SpaceManagementError('Error loading communities and spaces: $e')); emit(SpaceManagementError('Error loading communities and spaces: $e'));
} }