Added ProjectCubit as a dependency in blocs for managing project-level state.

This commit is contained in:
hannathkadher
2025-02-15 00:36:20 +04:00
parent da445e11aa
commit e1609309cf
25 changed files with 509 additions and 268 deletions

View File

@ -1,4 +1,5 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/common/bloc/project_cubit.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';
@ -14,16 +15,19 @@ import 'package:syncrow_web/services/product_api.dart';
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';
import 'package:syncrow_web/utils/constants/temp_const.dart';
class SpaceManagementBloc
extends Bloc<SpaceManagementEvent, SpaceManagementState> {
final CommunitySpaceManagementApi _api;
final ProductApi _productApi;
final SpaceModelManagementApi _spaceModelApi;
final ProjectCubit projectCubit;
List<ProductModel>? _cachedProducts;
SpaceManagementBloc(this._api, this._productApi, this._spaceModelApi)
SpaceManagementBloc(
this._api, this._productApi, this._spaceModelApi, this.projectCubit)
: super(SpaceManagementInitial()) {
on<LoadCommunityAndSpacesEvent>(_onLoadCommunityAndSpaces);
on<UpdateSpacePositionEvent>(_onUpdateSpacePosition);
@ -45,9 +49,11 @@ class SpaceManagementBloc
) async {
final previousState = state;
try {
final projectUuid = projectCubit.state;
emit(SpaceManagementLoading());
final success =
await _api.updateCommunity(event.communityUuid, event.name);
final success = await _api.updateCommunity(
event.communityUuid, event.name, projectUuid ?? TempConst.projectId);
if (success) {
if (previousState is SpaceManagementLoaded) {
final updatedCommunities =
@ -79,6 +85,8 @@ class SpaceManagementBloc
Future<List<SpaceTemplateModel>> fetchSpaceModels(
SpaceManagementState previousState) async {
try {
final projectUuid = projectCubit.state;
List<SpaceTemplateModel> allSpaces = [];
List<SpaceTemplateModel> prevSpaceModels = [];
@ -95,7 +103,8 @@ class SpaceManagementBloc
int page = 1;
while (hasNext) {
final spaces = await _spaceModelApi.listSpaceModels(page: page);
final spaces = await _spaceModelApi.listSpaceModels(
page: page, projectId: projectUuid ?? TempConst.projectId);
if (spaces.isNotEmpty) {
allSpaces.addAll(spaces);
page++;
@ -103,7 +112,8 @@ class SpaceManagementBloc
hasNext = false;
}
}
prevSpaceModels = await _spaceModelApi.listSpaceModels(page: 1);
prevSpaceModels = await _spaceModelApi.listSpaceModels(
page: 1, projectId: projectUuid ?? TempConst.projectId);
}
return allSpaces;
@ -132,7 +142,10 @@ class SpaceManagementBloc
Future<List<SpaceModel>> _fetchSpacesForCommunity(
String communityUuid) async {
return await _api.getSpaceHierarchy(communityUuid);
final projectUuid = projectCubit.state;
return await _api.getSpaceHierarchy(
communityUuid, projectUuid ?? TempConst.projectId);
}
Future<void> _onNewCommunity(
@ -163,6 +176,8 @@ class SpaceManagementBloc
BlankStateEvent event, Emitter<SpaceManagementState> emit) async {
try {
final previousState = state;
final projectUuid = projectCubit.state;
var prevSpaceModels = await fetchSpaceModels(previousState);
if (previousState is SpaceManagementLoaded ||
@ -176,7 +191,8 @@ class SpaceManagementBloc
return;
}
final communities = await _api.fetchCommunities();
final communities =
await _api.fetchCommunities(projectUuid ?? TempConst.projectId);
final updatedCommunities =
await Future.wait(communities.map((community) async {
final spaces = await _fetchSpacesForCommunity(community.uuid);
@ -209,8 +225,11 @@ class SpaceManagementBloc
var prevState = state;
emit(SpaceManagementLoading());
try {
final projectUuid = projectCubit.state;
_onloadProducts();
List<CommunityModel> communities = await _api.fetchCommunities();
List<CommunityModel> communities =
await _api.fetchCommunities(projectUuid ?? TempConst.projectId);
List<CommunityModel> updatedCommunities = await Future.wait(
communities.map((community) async {
@ -244,8 +263,10 @@ class SpaceManagementBloc
) async {
try {
emit(SpaceManagementLoading());
final projectUuid = projectCubit.state;
final success = await _api.deleteCommunity(event.communityUuid);
final success = await _api.deleteCommunity(
event.communityUuid, projectUuid ?? TempConst.projectId);
if (success) {
add(LoadCommunityAndSpacesEvent());
} else {
@ -270,8 +291,9 @@ class SpaceManagementBloc
emit(SpaceManagementLoading());
try {
CommunityModel? newCommunity =
await _api.createCommunity(event.name, event.description);
final projectUuid = projectCubit.state;
CommunityModel? newCommunity = await _api.createCommunity(
event.name, event.description, projectUuid ?? TempConst.projectId);
var prevSpaceModels = await fetchSpaceModels(previousState);
if (newCommunity != null) {
@ -414,6 +436,7 @@ class SpaceManagementBloc
Future<List<SpaceModel>> saveSpacesHierarchically(
List<SpaceModel> spaces, String communityUuid) async {
final orderedSpaces = flattenHierarchy(spaces);
final projectUuid = projectCubit.state;
final parentsToDelete = orderedSpaces.where((space) =>
space.status == SpaceStatus.deleted &&
@ -422,7 +445,8 @@ class SpaceManagementBloc
for (var parent in parentsToDelete) {
try {
if (parent.uuid != null) {
await _api.deleteSpace(communityUuid, parent.uuid!);
await _api.deleteSpace(
communityUuid, parent.uuid!, projectUuid ?? TempConst.projectId);
}
} catch (e) {
rethrow;
@ -435,7 +459,8 @@ class SpaceManagementBloc
if (space.uuid != null && space.uuid!.isNotEmpty) {
List<TagModelUpdate> tagUpdates = [];
final prevSpace = await _api.getSpace(communityUuid, space.uuid!);
final prevSpace = await _api.getSpace(
communityUuid, space.uuid!, projectUuid ?? TempConst.projectId);
final List<UpdateSubspaceTemplateModel> subspaceUpdates = [];
final List<SubspaceModel>? prevSubspaces = prevSpace?.subspaces;
final List<SubspaceModel>? newSubspaces = space.subspaces;
@ -504,17 +529,17 @@ class SpaceManagementBloc
}
final response = await _api.updateSpace(
communityId: communityUuid,
spaceId: space.uuid!,
name: space.name,
parentId: space.parent?.uuid,
isPrivate: space.isPrivate,
position: space.position,
icon: space.icon,
subspaces: subspaceUpdates,
tags: tagUpdates,
direction: space.incomingConnection?.direction,
);
communityId: communityUuid,
spaceId: space.uuid!,
name: space.name,
parentId: space.parent?.uuid,
isPrivate: space.isPrivate,
position: space.position,
icon: space.icon,
subspaces: subspaceUpdates,
tags: tagUpdates,
direction: space.incomingConnection?.direction,
projectId: projectUuid ?? TempConst.projectId);
} else {
// Call create if the space does not have a UUID
final List<CreateTagBodyModel> tagBodyModels = space.tags != null
@ -533,17 +558,17 @@ class SpaceManagementBloc
[];
final response = await _api.createSpace(
communityId: communityUuid,
name: space.name,
parentId: space.parent?.uuid,
isPrivate: space.isPrivate,
position: space.position,
icon: space.icon,
direction: space.incomingConnection?.direction,
spaceModelUuid: space.spaceModel?.uuid,
tags: tagBodyModels,
subspaces: createSubspaceBodyModels,
);
communityId: communityUuid,
name: space.name,
parentId: space.parent?.uuid,
isPrivate: space.isPrivate,
position: space.position,
icon: space.icon,
direction: space.incomingConnection?.direction,
spaceModelUuid: space.spaceModel?.uuid,
tags: tagBodyModels,
subspaces: createSubspaceBodyModels,
projectId: projectUuid ?? TempConst.projectId);
space.uuid = response?.uuid;
}
} catch (e) {
@ -583,8 +608,10 @@ class SpaceManagementBloc
emit(SpaceManagementLoading());
try {
var prevState = state;
final projectUuid = projectCubit.state;
List<CommunityModel> communities = await _api.fetchCommunities();
List<CommunityModel> communities =
await _api.fetchCommunities(projectUuid ?? TempConst.projectId);
List<CommunityModel> updatedCommunities = await Future.wait(
communities.map((community) async {

View File

@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/common/bloc/project_cubit.dart';
import 'package:syncrow_web/pages/device_managment/shared/navigate_home_grid_view.dart';
import 'package:syncrow_web/pages/spaces_management/structure_selector/bloc/center_body_bloc.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/bloc/space_management_bloc.dart';
@ -28,8 +29,12 @@ class SpaceManagementPageState extends State<SpaceManagementPage> {
return MultiBlocProvider(
providers: [
BlocProvider(
create: (_) => SpaceManagementBloc(_api, _productApi, _spaceModelApi)
..add(LoadCommunityAndSpacesEvent()),
create: (_) => SpaceManagementBloc(
_api,
_productApi,
_spaceModelApi,
context.read<ProjectCubit>(),
)..add(LoadCommunityAndSpacesEvent()),
),
BlocProvider(
create: (_) => CenterBodyBloc(),

View File

@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/common/bloc/project_cubit.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';
@ -87,6 +88,7 @@ class _LoadedSpaceViewState extends State<LoadedSpaceView> {
child: BlocProvider(
create: (context) => SpaceModelBloc(
api: SpaceModelManagementApi(),
projectCubit: context.read<ProjectCubit>(),
initialSpaceModels: _spaceModels,
),
child: SpaceModelPage(