diff --git a/lib/main.dart b/lib/main.dart index 20b0311e..f2f640e4 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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'; 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 7b62d0ac..671e446c 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,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 { +class SpaceManagementBloc extends Bloc { final CommunitySpaceManagementApi _api; final ProductApi _productApi; final SpaceModelManagementApi _spaceModelApi; @@ -29,6 +27,7 @@ class SpaceManagementBloc List? _cachedProducts; List? _cachedSpaceModels; final SpaceTreeBloc _spaceTreeBloc; + List? _cachedTags; SpaceManagementBloc( this._api, @@ -55,44 +54,38 @@ class SpaceManagementBloc UpdateSpaceModelCache event, Emitter 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 emit) async { + void _deleteSpaceModelFromCache( + DeleteSpaceModelFromCache event, Emitter 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 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> 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 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.from(previousState.communities); + final updatedCommunities = List.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> _fetchSpacesForCommunity( - String communityUuid) async { + Future> _fetchSpacesForCommunity(String communityUuid) async { final projectUuid = await ProjectManager.getProjectUUID() ?? ''; return await _api.getSpaceHierarchy(communityUuid, projectUuid); @@ -206,7 +211,7 @@ class SpaceManagementBloc Emitter 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 _onBlankState( - BlankStateEvent event, Emitter emit) async { + Future _onBlankState(BlankStateEvent event, Emitter emit) async { try { final previousState = state; final projectUuid = await ProjectManager.getProjectUUID() ?? ''; var spaceBloc = event.context.read(); List 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.from(prevCommunities), - products: _cachedProducts ?? [], - spaceModels: prevSpaceModels, - )); + communities: List.from(prevCommunities), + products: _cachedProducts ?? [], + spaceModels: prevSpaceModels, + allTags: _cachedTags)); return; } @@ -251,8 +256,7 @@ class SpaceManagementBloc List updatedCommunities = await Future.wait( communities.map((community) async { - List spaces = - await _fetchSpacesForCommunity(community.uuid); + List 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 emit, ) async { - _logEvent('LoadCommunityAndSpacesEvent'); - var spaceBloc = event.context.read(); _onloadProducts(); - + await fetchTags(); // Wait until `communityList` is loaded List communities = await _waitForCommunityList(spaceBloc); @@ -296,11 +299,11 @@ class SpaceManagementBloc communities: communities, products: _cachedProducts ?? [], spaceModels: prevSpaceModels, + allTags: _cachedTags ?? [] )); } - Future> _waitForCommunityList( - SpaceTreeBloc spaceBloc) async { + Future> _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.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 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 emit, ) async { var prevSpaceModels = await fetchSpaceModels(); - + await fetchTags(); final communities = List.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 tagUpdates = []; - final prevSpace = - await _api.getSpace(communityUuid, space.uuid!, projectUuid); + final prevSpace = await _api.getSpace(communityUuid, space.uuid!, projectUuid); final List subspaceUpdates = []; final List? prevSubspaces = prevSpace?.subspaces; final List? 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 emit) async { + void _onLoadSpaceModel(SpaceModelLoadEvent event, Emitter emit) async { emit(SpaceManagementLoading()); try { - var prevState = state; + await fetchTags(); final projectUuid = await ProjectManager.getProjectUUID() ?? ''; var spaceBloc = event.context.read(); List communities = spaceBloc.state.communityList; @@ -681,8 +678,7 @@ class SpaceManagementBloc List updatedCommunities = await Future.wait( communities.map((community) async { - List spaces = - await _fetchSpacesForCommunity(community.uuid); + List 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)); } } diff --git a/lib/pages/spaces_management/all_spaces/bloc/space_management_state.dart b/lib/pages/spaces_management/all_spaces/bloc/space_management_state.dart index 674fce4c..3890de81 100644 --- a/lib/pages/spaces_management/all_spaces/bloc/space_management_state.dart +++ b/lib/pages/spaces_management/all_spaces/bloc/space_management_state.dart @@ -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? spaceModels; + List 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 communities; final List products; List? spaceModels; + final List? 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 spaceModels; final List products; final List communities; + final List 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 get props => [communities, products, spaceModels]; + List get props => [communities, products, spaceModels, allTags]; } - diff --git a/lib/pages/spaces_management/all_spaces/view/spaces_management_page.dart b/lib/pages/spaces_management/all_spaces/view/spaces_management_page.dart index 0a847837..8635d4c0 100644 --- a/lib/pages/spaces_management/all_spaces/view/spaces_management_page.dart +++ b/lib/pages/spaces_management/all_spaces/view/spaces_management_page.dart @@ -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 { selectedSpace: null, products: state.products, shouldNavigateToSpaceModelPage: false, + ); } else if (state is SpaceManagementLoaded) { - return LoadedSpaceView( communities: state.communities, selectedCommunity: state.selectedCommunity,