added tags

This commit is contained in:
hannathkadher
2025-03-05 17:12:02 +04:00
parent a7995bb2b8
commit c9427b35be
4 changed files with 108 additions and 113 deletions

View File

@ -4,7 +4,6 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:go_router/go_router.dart';
import 'package:syncrow_web/firebase_options_dev.dart';
import 'package:syncrow_web/firebase_options_prod.dart';
import 'package:syncrow_web/pages/auth/bloc/auth_bloc.dart';
import 'package:syncrow_web/pages/home/bloc/home_bloc.dart';

View File

@ -1,5 +1,4 @@
import 'dart:async';
import 'dart:developer';
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';
@ -20,8 +19,7 @@ import 'package:syncrow_web/services/space_mana_api.dart';
import 'package:syncrow_web/services/space_model_mang_api.dart';
import 'package:syncrow_web/utils/constants/action_enum.dart' as custom_action;
class SpaceManagementBloc
extends Bloc<SpaceManagementEvent, SpaceManagementState> {
class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementState> {
final CommunitySpaceManagementApi _api;
final ProductApi _productApi;
final SpaceModelManagementApi _spaceModelApi;
@ -29,6 +27,7 @@ class SpaceManagementBloc
List<ProductModel>? _cachedProducts;
List<SpaceTemplateModel>? _cachedSpaceModels;
final SpaceTreeBloc _spaceTreeBloc;
List<Tag>? _cachedTags;
SpaceManagementBloc(
this._api,
@ -55,44 +54,38 @@ class SpaceManagementBloc
UpdateSpaceModelCache event, Emitter<SpaceManagementState> emit) async {
if (_cachedSpaceModels != null) {
_cachedSpaceModels = _cachedSpaceModels!.map((model) {
return model.uuid == event.updatedModel.uuid
? event.updatedModel
: model;
return model.uuid == event.updatedModel.uuid ? event.updatedModel : model;
}).toList();
} else {
_cachedSpaceModels = await fetchSpaceModels();
}
await fetchSpaceModels();
emit(SpaceModelLoaded(
communities: state is SpaceManagementLoaded
? (state as SpaceManagementLoaded).communities
: [],
products: _cachedProducts ?? [],
spaceModels: List.from(_cachedSpaceModels ?? []),
));
communities:
state is SpaceManagementLoaded ? (state as SpaceManagementLoaded).communities : [],
products: _cachedProducts ?? [],
spaceModels: List.from(_cachedSpaceModels ?? []),
allTags: _cachedTags ?? []));
}
void _deleteSpaceModelFromCache(DeleteSpaceModelFromCache event,
Emitter<SpaceManagementState> emit) async {
void _deleteSpaceModelFromCache(
DeleteSpaceModelFromCache event, Emitter<SpaceManagementState> emit) async {
if (_cachedSpaceModels != null) {
_cachedSpaceModels = _cachedSpaceModels!
.where((model) => model.uuid != event.deletedUuid)
.toList();
_cachedSpaceModels =
_cachedSpaceModels!.where((model) => model.uuid != event.deletedUuid).toList();
} else {
_cachedSpaceModels = await fetchSpaceModels();
}
await fetchSpaceModels();
emit(SpaceModelLoaded(
communities: state is SpaceManagementLoaded
? (state as SpaceManagementLoaded).communities
: [],
products: _cachedProducts ?? [],
spaceModels: List.from(_cachedSpaceModels ?? []),
));
}
void _logEvent(String eventName) {
log('Event Triggered: $eventName');
communities:
state is SpaceManagementLoaded ? (state as SpaceManagementLoaded).communities : [],
products: _cachedProducts ?? [],
spaceModels: List.from(_cachedSpaceModels ?? []),
allTags: _cachedTags ?? []));
}
void updateCachedSpaceModels(List<SpaceTemplateModel> updatedModels) {
@ -117,8 +110,8 @@ class SpaceManagementBloc
int page = 1;
while (hasNext) {
final spaceModels = await _spaceModelApi.listSpaceModels(
page: page, projectId: projectUuid);
final spaceModels =
await _spaceModelApi.listSpaceModels(page: page, projectId: projectUuid);
if (spaceModels.isNotEmpty) {
allSpaceModels.addAll(spaceModels);
page++;
@ -135,6 +128,20 @@ class SpaceManagementBloc
}
}
Future<List<Tag>> fetchTags() async {
try {
if (_cachedTags != null) {
return _cachedTags!;
}
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
final allTags = await _spaceModelApi.listTags(projectId: projectUuid);
_cachedTags = allTags;
return allTags;
} catch (e) {
return [];
}
}
void _onUpdateCommunity(
UpdateCommunityEvent event,
Emitter<SpaceManagementState> emit,
@ -142,14 +149,13 @@ class SpaceManagementBloc
final previousState = state;
try {
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
await fetchTags();
emit(SpaceManagementLoading());
final success = await _api.updateCommunity(
event.communityUuid, event.name, projectUuid);
final success = await _api.updateCommunity(event.communityUuid, event.name, projectUuid);
if (success) {
if (previousState is SpaceManagementLoaded) {
final updatedCommunities =
List<CommunityModel>.from(previousState.communities);
final updatedCommunities = List<CommunityModel>.from(previousState.communities);
for (var community in updatedCommunities) {
if (community.uuid == event.communityUuid) {
community.name = event.name;
@ -162,11 +168,11 @@ class SpaceManagementBloc
var prevSpaceModels = await fetchSpaceModels();
emit(SpaceManagementLoaded(
communities: updatedCommunities,
products: previousState.products,
selectedCommunity: previousState.selectedCommunity,
spaceModels: prevSpaceModels,
));
communities: updatedCommunities,
products: previousState.products,
selectedCommunity: previousState.selectedCommunity,
spaceModels: prevSpaceModels,
allTags: _cachedTags ?? []));
}
} else {
emit(const SpaceManagementError('Failed to update the community.'));
@ -194,8 +200,7 @@ class SpaceManagementBloc
}
}
Future<List<SpaceModel>> _fetchSpacesForCommunity(
String communityUuid) async {
Future<List<SpaceModel>> _fetchSpacesForCommunity(String communityUuid) async {
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
return await _api.getSpaceHierarchy(communityUuid, projectUuid);
@ -206,7 +211,7 @@ class SpaceManagementBloc
Emitter<SpaceManagementState> emit,
) async {
try {
final previousState = state;
await fetchTags();
if (event.communities.isEmpty) {
emit(const SpaceManagementError('No communities provided.'));
@ -216,33 +221,33 @@ class SpaceManagementBloc
var prevSpaceModels = await fetchSpaceModels();
emit(BlankState(
communities: event.communities,
products: _cachedProducts ?? [],
spaceModels: prevSpaceModels,
));
communities: event.communities,
products: _cachedProducts ?? [],
spaceModels: prevSpaceModels,
allTags: _cachedTags ?? []));
} catch (error) {
emit(SpaceManagementError('Error loading communities: $error'));
}
}
Future<void> _onBlankState(
BlankStateEvent event, Emitter<SpaceManagementState> emit) async {
Future<void> _onBlankState(BlankStateEvent event, Emitter<SpaceManagementState> emit) async {
try {
final previousState = state;
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
var spaceBloc = event.context.read<SpaceTreeBloc>();
List<CommunityModel> communities = await _waitForCommunityList(spaceBloc);
await fetchSpaceModels();
await fetchTags();
var prevSpaceModels = await fetchSpaceModels();
if (previousState is SpaceManagementLoaded ||
previousState is BlankState) {
if (previousState is SpaceManagementLoaded || previousState is BlankState) {
final prevCommunities = (previousState as dynamic).communities ?? [];
emit(BlankState(
communities: List<CommunityModel>.from(prevCommunities),
products: _cachedProducts ?? [],
spaceModels: prevSpaceModels,
));
communities: List<CommunityModel>.from(prevCommunities),
products: _cachedProducts ?? [],
spaceModels: prevSpaceModels,
allTags: _cachedTags));
return;
}
@ -251,8 +256,7 @@ class SpaceManagementBloc
List<CommunityModel> updatedCommunities = await Future.wait(
communities.map((community) async {
List<SpaceModel> spaces =
await _fetchSpacesForCommunity(community.uuid);
List<SpaceModel> spaces = await _fetchSpacesForCommunity(community.uuid);
return CommunityModel(
uuid: community.uuid,
createdAt: community.createdAt,
@ -272,6 +276,7 @@ class SpaceManagementBloc
spaceModels: prevSpaceModels,
communities: communities,
products: _cachedProducts ?? [],
allTags: _cachedTags ?? [],
));
} catch (error) {
emit(SpaceManagementError('Error loading communities: $error'));
@ -282,11 +287,9 @@ class SpaceManagementBloc
LoadCommunityAndSpacesEvent event,
Emitter<SpaceManagementState> emit,
) async {
_logEvent('LoadCommunityAndSpacesEvent');
var spaceBloc = event.context.read<SpaceTreeBloc>();
_onloadProducts();
await fetchTags();
// Wait until `communityList` is loaded
List<CommunityModel> communities = await _waitForCommunityList(spaceBloc);
@ -296,11 +299,11 @@ class SpaceManagementBloc
communities: communities,
products: _cachedProducts ?? [],
spaceModels: prevSpaceModels,
allTags: _cachedTags ?? []
));
}
Future<List<CommunityModel>> _waitForCommunityList(
SpaceTreeBloc spaceBloc) async {
Future<List<CommunityModel>> _waitForCommunityList(SpaceTreeBloc spaceBloc) async {
// Check if communityList is already populated
if (spaceBloc.state.communityList.isNotEmpty) {
return spaceBloc.state.communityList;
@ -327,8 +330,7 @@ class SpaceManagementBloc
emit(SpaceManagementLoading());
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
final success =
await _api.deleteCommunity(event.communityUuid, projectUuid);
final success = await _api.deleteCommunity(event.communityUuid, projectUuid);
if (success) {
// add(LoadCommunityAndSpacesEvent());
} else {
@ -349,14 +351,13 @@ class SpaceManagementBloc
try {
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
CommunityModel? newCommunity = await _api.createCommunity(
event.name, event.description, projectUuid);
await fetchTags();
CommunityModel? newCommunity =
await _api.createCommunity(event.name, event.description, projectUuid);
var prevSpaceModels = await fetchSpaceModels();
if (newCommunity != null) {
if (previousState is SpaceManagementLoaded ||
previousState is BlankState) {
if (previousState is SpaceManagementLoaded || previousState is BlankState) {
final prevCommunities = List<CommunityModel>.from(
(previousState as dynamic).communities,
);
@ -368,7 +369,9 @@ class SpaceManagementBloc
communities: updatedCommunities,
products: _cachedProducts ?? [],
selectedCommunity: newCommunity,
selectedSpace: null));
selectedSpace: null,
allTags: _cachedTags ?? [],
));
}
} else {
emit(const SpaceManagementError('Error creating community'));
@ -408,11 +411,12 @@ class SpaceManagementBloc
required Emitter<SpaceManagementState> emit,
CommunityModel? selectedCommunity,
SpaceModel? selectedSpace,
}) {
}) async {
final previousState = state;
emit(SpaceManagementLoading());
try {
await fetchTags();
if (previousState is SpaceManagementLoaded ||
previousState is BlankState ||
previousState is SpaceModelLoaded) {
@ -428,7 +432,8 @@ class SpaceManagementBloc
products: _cachedProducts ?? [],
selectedCommunity: selectedCommunity,
selectedSpace: selectedSpace,
spaceModels: spaceModels));
spaceModels: spaceModels,
allTags: _cachedTags ?? []));
}
} catch (e) {
emit(SpaceManagementError('Error updating state: $e'));
@ -443,8 +448,7 @@ class SpaceManagementBloc
emit(SpaceManagementLoading());
try {
final updatedSpaces =
await saveSpacesHierarchically(event.spaces, event.communityUuid);
final updatedSpaces = await saveSpacesHierarchically(event.spaces, event.communityUuid);
final allSpaces = await _fetchSpacesForCommunity(event.communityUuid);
@ -474,7 +478,7 @@ class SpaceManagementBloc
Emitter<SpaceManagementState> emit,
) async {
var prevSpaceModels = await fetchSpaceModels();
await fetchTags();
final communities = List<CommunityModel>.from(previousState.communities);
for (var community in communities) {
@ -487,7 +491,8 @@ class SpaceManagementBloc
products: _cachedProducts ?? [],
selectedCommunity: community,
selectedSpace: null,
spaceModels: prevSpaceModels));
spaceModels: prevSpaceModels,
allTags: _cachedTags ??[]));
return;
}
}
@ -518,8 +523,7 @@ class SpaceManagementBloc
if (space.uuid != null && space.uuid!.isNotEmpty) {
List<TagModelUpdate> tagUpdates = [];
final prevSpace =
await _api.getSpace(communityUuid, space.uuid!, projectUuid);
final prevSpace = await _api.getSpace(communityUuid, space.uuid!, projectUuid);
final List<UpdateSubspaceTemplateModel> subspaceUpdates = [];
final List<SubspaceModel>? prevSubspaces = prevSpace?.subspaces;
final List<SubspaceModel>? newSubspaces = space.subspaces;
@ -529,19 +533,17 @@ class SpaceManagementBloc
if (prevSubspaces != null || newSubspaces != null) {
if (prevSubspaces != null && newSubspaces != null) {
for (var prevSubspace in prevSubspaces) {
final existsInNew = newSubspaces
.any((subspace) => subspace.uuid == prevSubspace.uuid);
final existsInNew =
newSubspaces.any((subspace) => subspace.uuid == prevSubspace.uuid);
if (!existsInNew) {
subspaceUpdates.add(UpdateSubspaceTemplateModel(
action: custom_action.Action.delete,
uuid: prevSubspace.uuid));
action: custom_action.Action.delete, uuid: prevSubspace.uuid));
}
}
} else if (prevSubspaces != null && newSubspaces == null) {
for (var prevSubspace in prevSubspaces) {
subspaceUpdates.add(UpdateSubspaceTemplateModel(
action: custom_action.Action.delete,
uuid: prevSubspace.uuid));
action: custom_action.Action.delete, uuid: prevSubspace.uuid));
}
}
@ -569,9 +571,7 @@ class SpaceManagementBloc
}
if (prevSubspaces != null && newSubspaces != null) {
final newSubspaceMap = {
for (var subspace in newSubspaces) subspace.uuid: subspace
};
final newSubspaceMap = {for (var subspace in newSubspaces) subspace.uuid: subspace};
for (var prevSubspace in prevSubspaces) {
final newSubspace = newSubspaceMap[prevSubspace.uuid];
@ -608,10 +608,8 @@ class SpaceManagementBloc
: [];
final createSubspaceBodyModels = space.subspaces?.map((subspace) {
final tagBodyModels = subspace.tags
?.map((tag) => tag.toCreateTagBodyModel())
.toList() ??
[];
final tagBodyModels =
subspace.tags?.map((tag) => tag.toCreateTagBodyModel()).toList() ?? [];
return CreateSubspaceModel()
..subspaceName = subspace.subspaceName
..tags = tagBodyModels;
@ -664,12 +662,11 @@ class SpaceManagementBloc
return result.toList(); // Convert back to a list
}
void _onLoadSpaceModel(
SpaceModelLoadEvent event, Emitter<SpaceManagementState> emit) async {
void _onLoadSpaceModel(SpaceModelLoadEvent event, Emitter<SpaceManagementState> emit) async {
emit(SpaceManagementLoading());
try {
var prevState = state;
await fetchTags();
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
var spaceBloc = event.context.read<SpaceTreeBloc>();
List<CommunityModel> communities = spaceBloc.state.communityList;
@ -681,8 +678,7 @@ class SpaceManagementBloc
List<CommunityModel> updatedCommunities = await Future.wait(
communities.map((community) async {
List<SpaceModel> spaces =
await _fetchSpacesForCommunity(community.uuid);
List<SpaceModel> spaces = await _fetchSpacesForCommunity(community.uuid);
return CommunityModel(
uuid: community.uuid,
createdAt: community.createdAt,
@ -702,6 +698,7 @@ class SpaceManagementBloc
communities: communities,
products: _cachedProducts ?? [],
spaceModels: prevSpaceModels,
allTags: _cachedTags ?? []
));
} catch (e) {
emit(SpaceManagementError('Error loading communities and spaces: $e'));
@ -731,17 +728,14 @@ class SpaceManagementBloc
// Case 1: Tags deleted
if (prevTags != null && newTags != null) {
for (var prevTag in prevTags) {
final existsInNew =
newTags.any((newTag) => newTag.uuid == prevTag.uuid);
final existsInNew = newTags.any((newTag) => newTag.uuid == prevTag.uuid);
if (!existsInNew) {
tagUpdates.add(TagModelUpdate(
action: custom_action.Action.delete, uuid: prevTag.uuid));
tagUpdates.add(TagModelUpdate(action: custom_action.Action.delete, uuid: prevTag.uuid));
}
}
} else if (prevTags != null && newTags == null) {
for (var prevTag in prevTags) {
tagUpdates.add(TagModelUpdate(
action: custom_action.Action.delete, uuid: prevTag.uuid));
tagUpdates.add(TagModelUpdate(action: custom_action.Action.delete, uuid: prevTag.uuid));
}
}

View File

@ -2,6 +2,7 @@ import 'package:equatable/equatable.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/community_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/model/tag.dart';
import 'package:syncrow_web/pages/spaces_management/space_model/models/space_template_model.dart';
abstract class SpaceManagementState extends Equatable {
@ -21,22 +22,25 @@ class SpaceManagementLoaded extends SpaceManagementState {
CommunityModel? selectedCommunity;
SpaceModel? selectedSpace;
List<SpaceTemplateModel>? spaceModels;
List<Tag> allTags;
SpaceManagementLoaded(
{required this.communities,
required this.products,
this.selectedCommunity,
this.selectedSpace,
this.spaceModels});
this.spaceModels,
required this.allTags});
}
class BlankState extends SpaceManagementState {
final List<CommunityModel> communities;
final List<ProductModel> products;
List<SpaceTemplateModel>? spaceModels;
final List<Tag>? allTags;
BlankState(
{required this.communities, required this.products, this.spaceModels});
{required this.communities, required this.products, this.spaceModels, required this.allTags});
}
class SpaceCreationSuccess extends SpaceManagementState {
@ -61,14 +65,14 @@ class SpaceModelLoaded extends SpaceManagementState {
List<SpaceTemplateModel> spaceModels;
final List<ProductModel> products;
final List<CommunityModel> communities;
final List<Tag> allTags;
SpaceModelLoaded({
required this.communities,
required this.products,
required this.spaceModels,
});
SpaceModelLoaded(
{required this.communities,
required this.products,
required this.spaceModels,
required this.allTags});
@override
List<Object> get props => [communities, products, spaceModels];
List<Object> get props => [communities, products, spaceModels, allTags];
}

View File

@ -1,5 +1,3 @@
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/device_managment/shared/navigate_home_grid_view.dart';
@ -62,9 +60,9 @@ class SpaceManagementPageState extends State<SpaceManagementPage> {
selectedSpace: null,
products: state.products,
shouldNavigateToSpaceModelPage: false,
);
} else if (state is SpaceManagementLoaded) {
return LoadedSpaceView(
communities: state.communities,
selectedCommunity: state.selectedCommunity,