mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-14 09:17:37 +00:00
@ -1,19 +1,20 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:syncrow_web/pages/common/bloc/project_manager.dart';
|
||||
import 'package:syncrow_web/pages/space_tree/bloc/space_tree_bloc.dart';
|
||||
import 'package:syncrow_web/pages/space_tree/bloc/space_tree_event.dart';
|
||||
import 'package:syncrow_web/pages/space_tree/bloc/space_tree_state.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/all_spaces/bloc/space_management_event.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/all_spaces/bloc/space_management_state.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/community_model.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/create_subspace_model.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/product_model.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/space_model.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/all_spaces/bloc/space_management_event.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/all_spaces/bloc/space_management_state.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/subspace_model.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/tag.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/space_model/models/space_template_model.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/space_model/models/tag_body_model.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/space_model/models/tag_update_model.dart';
|
||||
import 'package:syncrow_web/services/product_api.dart';
|
||||
import 'package:syncrow_web/services/space_mana_api.dart';
|
||||
@ -56,10 +57,10 @@ class SpaceManagementBloc
|
||||
UpdateSpaceModelCache event, Emitter<SpaceManagementState> emit) async {
|
||||
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
||||
|
||||
final allSpaceModels = <SpaceTemplateModel>[];
|
||||
List<SpaceTemplateModel> allSpaceModels = [];
|
||||
|
||||
var hasNext = true;
|
||||
var page = 1;
|
||||
bool hasNext = true;
|
||||
int page = 1;
|
||||
|
||||
while (hasNext) {
|
||||
final spaceModels = await _spaceModelApi.listSpaceModels(
|
||||
@ -84,7 +85,7 @@ class SpaceManagementBloc
|
||||
allTags: _cachedTags ?? []));
|
||||
}
|
||||
|
||||
Future<void> _deleteSpaceModelFromCache(DeleteSpaceModelFromCache event,
|
||||
void _deleteSpaceModelFromCache(DeleteSpaceModelFromCache event,
|
||||
Emitter<SpaceManagementState> emit) async {
|
||||
if (_cachedSpaceModels != null) {
|
||||
_cachedSpaceModels = _cachedSpaceModels!
|
||||
@ -120,10 +121,10 @@ class SpaceManagementBloc
|
||||
|
||||
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
||||
|
||||
final allSpaceModels = <SpaceTemplateModel>[];
|
||||
List<SpaceTemplateModel> allSpaceModels = [];
|
||||
|
||||
var hasNext = true;
|
||||
var page = 1;
|
||||
bool hasNext = true;
|
||||
int page = 1;
|
||||
|
||||
while (hasNext) {
|
||||
final spaceModels = await _spaceModelApi.listSpaceModels(
|
||||
@ -158,7 +159,7 @@ class SpaceManagementBloc
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onUpdateCommunity(
|
||||
void _onUpdateCommunity(
|
||||
UpdateCommunityEvent event,
|
||||
Emitter<SpaceManagementState> emit,
|
||||
) async {
|
||||
@ -174,7 +175,7 @@ class SpaceManagementBloc
|
||||
if (previousState is SpaceManagementLoaded) {
|
||||
final updatedCommunities =
|
||||
List<CommunityModel>.from(previousState.communities);
|
||||
for (final community in updatedCommunities) {
|
||||
for (var community in updatedCommunities) {
|
||||
if (community.uuid == event.communityUuid) {
|
||||
community.name = event.name;
|
||||
_spaceTreeBloc.add(OnCommunityAdded(community));
|
||||
@ -183,7 +184,7 @@ class SpaceManagementBloc
|
||||
}
|
||||
}
|
||||
|
||||
final prevSpaceModels = await fetchSpaceModels();
|
||||
var prevSpaceModels = await fetchSpaceModels();
|
||||
|
||||
emit(SpaceManagementLoaded(
|
||||
communities: updatedCommunities,
|
||||
@ -200,14 +201,14 @@ class SpaceManagementBloc
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onloadProducts() async {
|
||||
void _onloadProducts() async {
|
||||
if (_cachedProducts == null) {
|
||||
final products = await _productApi.fetchProducts();
|
||||
_cachedProducts = products;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onFetchProducts(
|
||||
void _onFetchProducts(
|
||||
FetchProductsEvent event,
|
||||
Emitter<SpaceManagementState> emit,
|
||||
) async {
|
||||
@ -222,7 +223,7 @@ class SpaceManagementBloc
|
||||
String communityUuid) async {
|
||||
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
||||
|
||||
return _api.getSpaceHierarchy(communityUuid, projectUuid);
|
||||
return await _api.getSpaceHierarchy(communityUuid, projectUuid);
|
||||
}
|
||||
|
||||
Future<void> _onNewCommunity(
|
||||
@ -237,7 +238,7 @@ class SpaceManagementBloc
|
||||
return;
|
||||
}
|
||||
|
||||
final prevSpaceModels = await fetchSpaceModels();
|
||||
var prevSpaceModels = await fetchSpaceModels();
|
||||
|
||||
emit(BlankState(
|
||||
communities: event.communities,
|
||||
@ -254,15 +255,15 @@ class SpaceManagementBloc
|
||||
try {
|
||||
final previousState = state;
|
||||
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
||||
final spaceBloc = event.context.read<SpaceTreeBloc>();
|
||||
final spaceTreeState = event.context.read<SpaceTreeBloc>().state;
|
||||
var spaceBloc = event.context.read<SpaceTreeBloc>();
|
||||
var spaceTreeState = event.context.read<SpaceTreeBloc>().state;
|
||||
|
||||
final communities =
|
||||
List<CommunityModel> communities =
|
||||
await _waitForCommunityList(spaceBloc, spaceTreeState);
|
||||
await fetchSpaceModels();
|
||||
// await fetchTags();
|
||||
|
||||
final prevSpaceModels = await fetchSpaceModels();
|
||||
var prevSpaceModels = await fetchSpaceModels();
|
||||
|
||||
if (previousState is SpaceManagementLoaded ||
|
||||
previousState is BlankState) {
|
||||
@ -286,17 +287,18 @@ class SpaceManagementBloc
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onLoadCommunityAndSpaces(
|
||||
void _onLoadCommunityAndSpaces(
|
||||
LoadCommunityAndSpacesEvent event,
|
||||
Emitter<SpaceManagementState> emit,
|
||||
) async {
|
||||
final spaceTreeState = event.context.read<SpaceTreeBloc>().state;
|
||||
final spaceBloc = event.context.read<SpaceTreeBloc>();
|
||||
var spaceTreeState = event.context.read<SpaceTreeBloc>().state;
|
||||
var spaceBloc = event.context.read<SpaceTreeBloc>();
|
||||
|
||||
_onloadProducts();
|
||||
await fetchTags();
|
||||
// Wait until `communityList` is loaded
|
||||
final communities = await _waitForCommunityList(spaceBloc, spaceTreeState);
|
||||
List<CommunityModel> communities =
|
||||
await _waitForCommunityList(spaceBloc, spaceTreeState);
|
||||
|
||||
// Fetch space models after communities are available
|
||||
final prevSpaceModels = await fetchSpaceModels();
|
||||
@ -342,7 +344,7 @@ class SpaceManagementBloc
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onCommunityDelete(
|
||||
void _onCommunityDelete(
|
||||
DeleteCommunityEvent event,
|
||||
Emitter<SpaceManagementState> emit,
|
||||
) async {
|
||||
@ -363,7 +365,7 @@ class SpaceManagementBloc
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onCreateCommunity(
|
||||
void _onCreateCommunity(
|
||||
CreateCommunityEvent event,
|
||||
Emitter<SpaceManagementState> emit,
|
||||
) async {
|
||||
@ -373,9 +375,9 @@ class SpaceManagementBloc
|
||||
try {
|
||||
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
||||
await fetchTags();
|
||||
final newCommunity = await _api.createCommunity(
|
||||
CommunityModel? newCommunity = await _api.createCommunity(
|
||||
event.name, event.description, projectUuid);
|
||||
final prevSpaceModels = await fetchSpaceModels();
|
||||
var prevSpaceModels = await fetchSpaceModels();
|
||||
|
||||
if (newCommunity != null) {
|
||||
if (previousState is SpaceManagementLoaded ||
|
||||
@ -403,7 +405,7 @@ class SpaceManagementBloc
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onSelectCommunity(
|
||||
void _onSelectCommunity(
|
||||
SelectCommunityEvent event,
|
||||
Emitter<SpaceManagementState> emit,
|
||||
) async {
|
||||
@ -429,7 +431,7 @@ class SpaceManagementBloc
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _handleCommunitySpaceStateUpdate({
|
||||
void _handleCommunitySpaceStateUpdate({
|
||||
required Emitter<SpaceManagementState> emit,
|
||||
CommunityModel? selectedCommunity,
|
||||
SpaceModel? selectedSpace,
|
||||
@ -462,7 +464,7 @@ class SpaceManagementBloc
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onSaveSpaces(
|
||||
void _onSaveSpaces(
|
||||
SaveSpacesEvent event,
|
||||
Emitter<SpaceManagementState> emit,
|
||||
) async {
|
||||
@ -492,9 +494,9 @@ class SpaceManagementBloc
|
||||
event.spaces.removeWhere(
|
||||
(space) => space.status == SpaceStatus.deleted,
|
||||
);
|
||||
for (final space in event.spaces) {
|
||||
space.status = SpaceStatus.unchanged;
|
||||
}
|
||||
event.spaces.forEach(
|
||||
(space) => space.status = SpaceStatus.unchanged,
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
// emit(SpaceManagementError('Error saving spaces: $e'));
|
||||
@ -513,7 +515,7 @@ class SpaceManagementBloc
|
||||
Emitter<SpaceManagementState> emit,
|
||||
) async {
|
||||
try {
|
||||
final prevSpaceModels = await fetchSpaceModels();
|
||||
var prevSpaceModels = await fetchSpaceModels();
|
||||
|
||||
await fetchTags();
|
||||
|
||||
@ -521,7 +523,7 @@ class SpaceManagementBloc
|
||||
? spaceTreeState.filteredCommunity
|
||||
: spaceTreeState.communityList;
|
||||
|
||||
for (final community in communities) {
|
||||
for (var community in communities) {
|
||||
if (community.uuid == communityUuid) {
|
||||
community.spaces = allSpaces;
|
||||
_spaceTreeBloc.add(InitialEvent());
|
||||
@ -538,7 +540,7 @@ class SpaceManagementBloc
|
||||
}
|
||||
}
|
||||
emit(previousState);
|
||||
} catch (e) {
|
||||
} catch (e, stackTrace) {
|
||||
emit(previousState);
|
||||
// rethrow;
|
||||
}
|
||||
@ -575,36 +577,36 @@ class SpaceManagementBloc
|
||||
space.status == SpaceStatus.deleted &&
|
||||
(space.parent == null || space.parent?.status != SpaceStatus.deleted));
|
||||
|
||||
for (final parent in parentsToDelete) {
|
||||
for (var parent in parentsToDelete) {
|
||||
try {
|
||||
if (parent.uuid != null) {
|
||||
await _api.deleteSpace(communityUuid, parent.uuid!, projectUuid);
|
||||
}
|
||||
} catch (e) {}
|
||||
}
|
||||
orderedSpaces.removeWhere(parentsToDelete.contains);
|
||||
orderedSpaces.removeWhere((space) => parentsToDelete.contains(space));
|
||||
|
||||
for (final space in orderedSpaces) {
|
||||
for (var space in orderedSpaces) {
|
||||
try {
|
||||
if (space.uuid != null && space.uuid!.isNotEmpty) {
|
||||
var tagUpdates = <TagModelUpdate>[];
|
||||
List<TagModelUpdate> tagUpdates = [];
|
||||
|
||||
final matchedSpaces =
|
||||
List<SpaceModel> matchedSpaces =
|
||||
findMatchingSpaces(selectedCommunity.spaces, space.uuid!);
|
||||
|
||||
if (matchedSpaces.isEmpty) continue;
|
||||
|
||||
final prevSpace = matchedSpaces.elementAtOrNull(0);
|
||||
|
||||
final subspaceUpdates = <UpdateSubspaceTemplateModel>[];
|
||||
final prevSubspaces = prevSpace?.subspaces;
|
||||
final newSubspaces = space.subspaces;
|
||||
final List<UpdateSubspaceTemplateModel> subspaceUpdates = [];
|
||||
final List<SubspaceModel>? prevSubspaces = prevSpace?.subspaces;
|
||||
final List<SubspaceModel>? newSubspaces = space.subspaces;
|
||||
|
||||
tagUpdates = processTagUpdates(prevSpace?.tags, space.tags);
|
||||
|
||||
if (prevSubspaces != null || newSubspaces != null) {
|
||||
if (prevSubspaces != null && newSubspaces != null) {
|
||||
for (final prevSubspace in prevSubspaces) {
|
||||
for (var prevSubspace in prevSubspaces) {
|
||||
final existsInNew = newSubspaces
|
||||
.any((subspace) => subspace.uuid == prevSubspace.uuid);
|
||||
if (!existsInNew) {
|
||||
@ -614,7 +616,7 @@ class SpaceManagementBloc
|
||||
}
|
||||
}
|
||||
} else if (prevSubspaces != null && newSubspaces == null) {
|
||||
for (final prevSubspace in prevSubspaces) {
|
||||
for (var prevSubspace in prevSubspaces) {
|
||||
subspaceUpdates.add(UpdateSubspaceTemplateModel(
|
||||
action: custom_action.Action.delete,
|
||||
uuid: prevSubspace.uuid));
|
||||
@ -622,13 +624,13 @@ class SpaceManagementBloc
|
||||
}
|
||||
|
||||
if (newSubspaces != null) {
|
||||
for (final newSubspace in newSubspaces) {
|
||||
for (var newSubspace in newSubspaces) {
|
||||
// Tag without UUID
|
||||
if (newSubspace.uuid == null || newSubspace.uuid!.isEmpty) {
|
||||
final tagUpdates = <TagModelUpdate>[];
|
||||
if ((newSubspace.uuid == null || newSubspace.uuid!.isEmpty)) {
|
||||
final List<TagModelUpdate> tagUpdates = [];
|
||||
|
||||
if (newSubspace.tags != null) {
|
||||
for (final tag in newSubspace.tags!) {
|
||||
for (var tag in newSubspace.tags!) {
|
||||
tagUpdates.add(TagModelUpdate(
|
||||
action: custom_action.Action.add,
|
||||
newTagUuid: tag.uuid == '' ? null : tag.uuid,
|
||||
@ -646,14 +648,14 @@ class SpaceManagementBloc
|
||||
|
||||
if (prevSubspaces != null && newSubspaces != null) {
|
||||
final newSubspaceMap = {
|
||||
for (final subspace in newSubspaces) subspace.uuid: subspace
|
||||
for (var subspace in newSubspaces) subspace.uuid: subspace
|
||||
};
|
||||
|
||||
for (final prevSubspace in prevSubspaces) {
|
||||
for (var prevSubspace in prevSubspaces) {
|
||||
final newSubspace = newSubspaceMap[prevSubspace.uuid];
|
||||
|
||||
if (newSubspace != null) {
|
||||
final tagSubspaceUpdates =
|
||||
final List<TagModelUpdate> tagSubspaceUpdates =
|
||||
processTagUpdates(prevSubspace.tags, newSubspace.tags);
|
||||
subspaceUpdates.add(UpdateSubspaceTemplateModel(
|
||||
action: custom_action.Action.update,
|
||||
@ -667,7 +669,7 @@ class SpaceManagementBloc
|
||||
|
||||
final response = await _api.updateSpace(
|
||||
communityId: communityUuid,
|
||||
spaceId: space.uuid,
|
||||
spaceId: space.uuid!,
|
||||
name: space.name,
|
||||
parentId: space.parent?.uuid,
|
||||
isPrivate: space.isPrivate,
|
||||
@ -681,7 +683,7 @@ class SpaceManagementBloc
|
||||
projectId: projectUuid);
|
||||
} else {
|
||||
// Call create if the space does not have a UUID
|
||||
var tagBodyModels = space.tags != null
|
||||
List<CreateTagBodyModel> tagBodyModels = space.tags != null
|
||||
? space.tags!.map((tag) => tag.toCreateTagBodyModel()).toList()
|
||||
: [];
|
||||
|
||||
@ -729,17 +731,17 @@ class SpaceManagementBloc
|
||||
void visit(SpaceModel space) {
|
||||
if (!result.contains(space)) {
|
||||
result.add(space);
|
||||
for (final child in spaces.where((s) => s.parent == space)) {
|
||||
for (var child in spaces.where((s) => s.parent == space)) {
|
||||
visit(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (final space in topLevelSpaces) {
|
||||
for (var space in topLevelSpaces) {
|
||||
visit(space);
|
||||
}
|
||||
|
||||
for (final space in spaces) {
|
||||
for (var space in spaces) {
|
||||
if (!result.contains(space)) {
|
||||
result.add(space);
|
||||
}
|
||||
@ -747,7 +749,7 @@ class SpaceManagementBloc
|
||||
return result.toList(); // Convert back to a list
|
||||
}
|
||||
|
||||
Future<void> _onLoadSpaceModel(
|
||||
void _onLoadSpaceModel(
|
||||
SpaceModelLoadEvent event, Emitter<SpaceManagementState> emit) async {
|
||||
emit(SpaceManagementLoading());
|
||||
|
||||
@ -758,9 +760,9 @@ class SpaceManagementBloc
|
||||
? spaceTreeState.filteredCommunity
|
||||
: spaceTreeState.communityList;
|
||||
|
||||
final communities = filteredCommunities;
|
||||
List<CommunityModel> communities = filteredCommunities;
|
||||
|
||||
final prevSpaceModels = await fetchSpaceModels();
|
||||
var prevSpaceModels = await fetchSpaceModels();
|
||||
|
||||
emit(SpaceModelLoaded(
|
||||
communities: communities,
|
||||
@ -776,11 +778,11 @@ class SpaceManagementBloc
|
||||
List<Tag>? prevTags,
|
||||
List<Tag>? newTags,
|
||||
) {
|
||||
final tagUpdates = <TagModelUpdate>[];
|
||||
final List<TagModelUpdate> tagUpdates = [];
|
||||
final processedTags = <String?>{};
|
||||
|
||||
if (prevTags == null && newTags != null) {
|
||||
for (final newTag in newTags) {
|
||||
for (var newTag in newTags) {
|
||||
tagUpdates.add(TagModelUpdate(
|
||||
action: custom_action.Action.add,
|
||||
tag: newTag.tag,
|
||||
@ -794,7 +796,7 @@ class SpaceManagementBloc
|
||||
if (newTags != null || prevTags != null) {
|
||||
// Case 1: Tags deleted
|
||||
if (prevTags != null && newTags != null) {
|
||||
for (final prevTag in prevTags) {
|
||||
for (var prevTag in prevTags) {
|
||||
final existsInNew =
|
||||
newTags.any((newTag) => newTag.uuid == prevTag.uuid);
|
||||
if (!existsInNew) {
|
||||
@ -803,7 +805,7 @@ class SpaceManagementBloc
|
||||
}
|
||||
}
|
||||
} else if (prevTags != null && newTags == null) {
|
||||
for (final prevTag in prevTags) {
|
||||
for (var prevTag in prevTags) {
|
||||
tagUpdates.add(TagModelUpdate(
|
||||
action: custom_action.Action.delete, uuid: prevTag.uuid));
|
||||
}
|
||||
@ -813,7 +815,7 @@ class SpaceManagementBloc
|
||||
if (newTags != null) {
|
||||
final prevTagUuids = prevTags?.map((t) => t.uuid).toSet() ?? {};
|
||||
|
||||
for (final newTag in newTags) {
|
||||
for (var newTag in newTags) {
|
||||
// Tag without UUID
|
||||
if ((newTag.uuid == null || !prevTagUuids.contains(newTag.uuid)) &&
|
||||
!processedTags.contains(newTag.tag)) {
|
||||
@ -829,9 +831,9 @@ class SpaceManagementBloc
|
||||
|
||||
// Case 3: Tags updated
|
||||
if (prevTags != null && newTags != null) {
|
||||
final newTagMap = {for (final tag in newTags) tag.uuid: tag};
|
||||
final newTagMap = {for (var tag in newTags) tag.uuid: tag};
|
||||
|
||||
for (final prevTag in prevTags) {
|
||||
for (var prevTag in prevTags) {
|
||||
final newTag = newTagMap[prevTag.uuid];
|
||||
if (newTag != null) {
|
||||
tagUpdates.add(TagModelUpdate(
|
||||
@ -850,9 +852,9 @@ class SpaceManagementBloc
|
||||
|
||||
List<SpaceModel> findMatchingSpaces(
|
||||
List<SpaceModel> spaces, String targetUuid) {
|
||||
final matched = <SpaceModel>[];
|
||||
List<SpaceModel> matched = [];
|
||||
|
||||
for (final space in spaces) {
|
||||
for (var space in spaces) {
|
||||
if (space.uuid == targetUuid) {
|
||||
matched.add(space);
|
||||
}
|
||||
|
@ -166,10 +166,10 @@ class SpaceModelLoadEvent extends SpaceManagementEvent {
|
||||
|
||||
class UpdateSpaceModelCache extends SpaceManagementEvent {
|
||||
final SpaceTemplateModel updatedModel;
|
||||
const UpdateSpaceModelCache(this.updatedModel);
|
||||
UpdateSpaceModelCache(this.updatedModel);
|
||||
}
|
||||
|
||||
class DeleteSpaceModelFromCache extends SpaceManagementEvent {
|
||||
final String deletedUuid;
|
||||
const DeleteSpaceModelFromCache(this.deletedUuid);
|
||||
DeleteSpaceModelFromCache(this.deletedUuid);
|
||||
}
|
||||
|
@ -40,10 +40,7 @@ class BlankState extends SpaceManagementState {
|
||||
final List<Tag> allTags;
|
||||
|
||||
BlankState(
|
||||
{required this.communities,
|
||||
required this.products,
|
||||
this.spaceModels,
|
||||
required this.allTags});
|
||||
{required this.communities, required this.products, this.spaceModels, required this.allTags});
|
||||
}
|
||||
|
||||
class SpaceCreationSuccess extends SpaceManagementState {
|
||||
|
Reference in New Issue
Block a user