mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 07:07:19 +00:00
cached space models
This commit is contained in:
@ -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<ProductModel>? _cachedProducts;
|
||||
List<SpaceTemplateModel>? _cachedSpaceModels;
|
||||
|
||||
SpaceManagementBloc(this._api, this._productApi, this._spaceModelApi)
|
||||
: super(SpaceManagementInitial()) {
|
||||
@ -48,6 +50,38 @@ class SpaceManagementBloc
|
||||
log('Event Triggered: $eventName');
|
||||
}
|
||||
|
||||
Future<List<SpaceTemplateModel>> fetchSpaceModels(
|
||||
SpaceManagementState previousState) async {
|
||||
try {
|
||||
if (_cachedSpaceModels != null) {
|
||||
return _cachedSpaceModels!;
|
||||
}
|
||||
|
||||
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
||||
|
||||
List<SpaceTemplateModel> 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<SpaceManagementState> emit,
|
||||
@ -89,46 +123,7 @@ class SpaceManagementBloc
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<SpaceTemplateModel>> fetchSpaceModels(
|
||||
SpaceManagementState previousState) async {
|
||||
try {
|
||||
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
||||
|
||||
List<SpaceTemplateModel> allSpaces = [];
|
||||
List<SpaceTemplateModel> prevSpaceModels = [];
|
||||
|
||||
if (previousState is SpaceManagementLoaded ||
|
||||
previousState is BlankState) {
|
||||
prevSpaceModels = List<SpaceTemplateModel>.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<SpaceTreeBloc>();
|
||||
List<CommunityModel> communities = spaceBloc.state.communityList;
|
||||
List<CommunityModel> communities = await _waitForCommunityList(spaceBloc);
|
||||
|
||||
var prevSpaceModels = await fetchSpaceModels(previousState);
|
||||
|
||||
@ -238,43 +233,13 @@ class SpaceManagementBloc
|
||||
_logEvent('LoadCommunityAndSpacesEvent');
|
||||
|
||||
var spaceBloc = event.context.read<SpaceTreeBloc>();
|
||||
List<CommunityModel> 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<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;
|
||||
}
|
||||
}
|
||||
// Wait until `communityList` is loaded
|
||||
List<CommunityModel> 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<List<CommunityModel>> _waitForCommunityList(
|
||||
SpaceTreeBloc spaceBloc) async {
|
||||
// Check if communityList is already populated
|
||||
if (spaceBloc.state.communityList.isNotEmpty) {
|
||||
return spaceBloc.state.communityList;
|
||||
}
|
||||
|
||||
final completer = Completer<List<CommunityModel>>();
|
||||
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<SpaceManagementState> emit,
|
||||
|
@ -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<CreateSpaceModelEvent, CreateSpaceModelState> {
|
||||
|
Reference in New Issue
Block a user