From c7c8e9a519aeb1004d014f05612b7a47f0fce06b Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Tue, 4 Mar 2025 11:40:06 +0400 Subject: [PATCH] cached space models --- .../bloc/space_management_bloc.dart | 135 ++++++++---------- .../bloc/create_space_model_bloc.dart | 3 - 2 files changed, 60 insertions(+), 78 deletions(-) diff --git a/lib/pages/spaces_management/all_spaces/bloc/space_management_bloc.dart b/lib/pages/spaces_management/all_spaces/bloc/space_management_bloc.dart index cb5de57e..84dc2442 100644 --- a/lib/pages/spaces_management/all_spaces/bloc/space_management_bloc.dart +++ b/lib/pages/spaces_management/all_spaces/bloc/space_management_bloc.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:developer'; import 'package:flutter/material.dart'; @@ -28,6 +29,7 @@ class SpaceManagementBloc final SpaceModelManagementApi _spaceModelApi; List? _cachedProducts; + List? _cachedSpaceModels; SpaceManagementBloc(this._api, this._productApi, this._spaceModelApi) : super(SpaceManagementInitial()) { @@ -48,6 +50,38 @@ class SpaceManagementBloc log('Event Triggered: $eventName'); } + Future> fetchSpaceModels( + SpaceManagementState previousState) async { + try { + if (_cachedSpaceModels != null) { + return _cachedSpaceModels!; + } + + final projectUuid = await ProjectManager.getProjectUUID() ?? ''; + + List allSpaceModels = []; + + bool hasNext = true; + int page = 1; + + while (hasNext) { + final spaceModels = await _spaceModelApi.listSpaceModels( + page: page, projectId: projectUuid); + if (spaceModels.isNotEmpty) { + allSpaceModels.addAll(spaceModels); + page++; + } else { + hasNext = false; + } + } + + _cachedSpaceModels = allSpaceModels; + return allSpaceModels; + } catch (e) { + return []; + } + } + void _onUpdateCommunity( UpdateCommunityEvent event, Emitter emit, @@ -89,46 +123,7 @@ class SpaceManagementBloc } } - Future> fetchSpaceModels( - SpaceManagementState previousState) async { - try { - final projectUuid = await ProjectManager.getProjectUUID() ?? ''; - - List allSpaces = []; - List prevSpaceModels = []; - - if (previousState is SpaceManagementLoaded || - previousState is BlankState) { - prevSpaceModels = List.from( - (previousState as dynamic).spaceModels ?? [], - ); - allSpaces.addAll(prevSpaceModels); - } - - if (prevSpaceModels.isEmpty) { - bool hasNext = true; - int page = 1; - - while (hasNext) { - final spaces = await _spaceModelApi.listSpaceModels( - page: page, projectId: projectUuid); - if (spaces.isNotEmpty) { - allSpaces.addAll(spaces); - page++; - } else { - hasNext = false; - } - } - prevSpaceModels = await _spaceModelApi.listSpaceModels( - page: 1, projectId: projectUuid); - } - - return allSpaces; - } catch (e) { - return []; - } - } - + void _onloadProducts() async { if (_cachedProducts == null) { final products = await _productApi.fetchProducts(); @@ -184,7 +179,7 @@ class SpaceManagementBloc final previousState = state; final projectUuid = await ProjectManager.getProjectUUID() ?? ''; var spaceBloc = event.context.read(); - List communities = spaceBloc.state.communityList; + List communities = await _waitForCommunityList(spaceBloc); var prevSpaceModels = await fetchSpaceModels(previousState); @@ -238,43 +233,13 @@ class SpaceManagementBloc _logEvent('LoadCommunityAndSpacesEvent'); var spaceBloc = event.context.read(); - List communities = spaceBloc.state.communityList; + _onloadProducts(); - if (communities.isEmpty) { - _logEvent("community is empty"); - emit(SpaceManagementLoading()); - - try { - final projectUuid = await ProjectManager.getProjectUUID() ?? ''; - _onloadProducts(); - - /* communities = await _api.fetchCommunities(projectUuid); - - List updatedCommunities = await Future.wait( - communities.map((community) async { - List 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; - } - } + // Wait until `communityList` is loaded + List communities = await _waitForCommunityList(spaceBloc); + // Fetch space models after communities are available final prevSpaceModels = await fetchSpaceModels(state); - emit(SpaceManagementLoaded( communities: communities, products: _cachedProducts ?? [], @@ -282,6 +247,26 @@ class SpaceManagementBloc )); } + Future> _waitForCommunityList( + SpaceTreeBloc spaceBloc) async { + // Check if communityList is already populated + if (spaceBloc.state.communityList.isNotEmpty) { + return spaceBloc.state.communityList; + } + + final completer = Completer>(); + final subscription = spaceBloc.stream.listen((state) { + if (state.communityList.isNotEmpty) { + completer.complete(state.communityList); + } + }); + + // Return the list once available, then cancel the listener + final communities = await completer.future; + await subscription.cancel(); + return communities; + } + void _onCommunityDelete( DeleteCommunityEvent event, Emitter emit, diff --git a/lib/pages/spaces_management/space_model/bloc/create_space_model_bloc.dart b/lib/pages/spaces_management/space_model/bloc/create_space_model_bloc.dart index 7c68e15c..8237c172 100644 --- a/lib/pages/spaces_management/space_model/bloc/create_space_model_bloc.dart +++ b/lib/pages/spaces_management/space_model/bloc/create_space_model_bloc.dart @@ -9,9 +9,6 @@ import 'package:syncrow_web/pages/spaces_management/space_model/models/tag_model import 'package:syncrow_web/pages/spaces_management/space_model/models/tag_update_model.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/strings_manager.dart'; -import 'package:syncrow_web/utils/constants/temp_const.dart'; -import 'package:syncrow_web/utils/helpers/shared_preferences_helper.dart'; class CreateSpaceModelBloc extends Bloc {