mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
removed project cubit from all classes
This commit is contained in:
@ -1,5 +1,8 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:syncrow_web/pages/common/bloc/project_cubit.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:syncrow_web/pages/common/bloc/project_manager.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';
|
||||
@ -15,19 +18,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/strings_manager.dart';
|
||||
import 'package:syncrow_web/utils/constants/temp_const.dart';
|
||||
import 'package:syncrow_web/utils/helpers/shared_preferences_helper.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, this.projectCubit)
|
||||
SpaceManagementBloc(this._api, this._productApi, this._spaceModelApi)
|
||||
: super(SpaceManagementInitial()) {
|
||||
on<LoadCommunityAndSpacesEvent>(_onLoadCommunityAndSpaces);
|
||||
on<UpdateSpacePositionEvent>(_onUpdateSpacePosition);
|
||||
@ -43,17 +46,23 @@ class SpaceManagementBloc
|
||||
on<SpaceModelLoadEvent>(_onLoadSpaceModel);
|
||||
}
|
||||
|
||||
void _logEvent(String eventName) {
|
||||
log('Event Triggered: $eventName');
|
||||
}
|
||||
|
||||
void _onUpdateCommunity(
|
||||
UpdateCommunityEvent event,
|
||||
Emitter<SpaceManagementState> emit,
|
||||
) async {
|
||||
_logEvent('UpdateCommunityEvent');
|
||||
|
||||
final previousState = state;
|
||||
try {
|
||||
final projectUuid = projectCubit.state;
|
||||
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
||||
|
||||
emit(SpaceManagementLoading());
|
||||
final success = await _api.updateCommunity(
|
||||
event.communityUuid, event.name, projectUuid ?? TempConst.projectId);
|
||||
event.communityUuid, event.name, projectUuid);
|
||||
if (success) {
|
||||
if (previousState is SpaceManagementLoaded) {
|
||||
final updatedCommunities =
|
||||
@ -85,7 +94,7 @@ class SpaceManagementBloc
|
||||
Future<List<SpaceTemplateModel>> fetchSpaceModels(
|
||||
SpaceManagementState previousState) async {
|
||||
try {
|
||||
final projectUuid = projectCubit.state;
|
||||
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
||||
|
||||
List<SpaceTemplateModel> allSpaces = [];
|
||||
List<SpaceTemplateModel> prevSpaceModels = [];
|
||||
@ -104,7 +113,7 @@ class SpaceManagementBloc
|
||||
|
||||
while (hasNext) {
|
||||
final spaces = await _spaceModelApi.listSpaceModels(
|
||||
page: page, projectId: projectUuid ?? TempConst.projectId);
|
||||
page: page, projectId: projectUuid);
|
||||
if (spaces.isNotEmpty) {
|
||||
allSpaces.addAll(spaces);
|
||||
page++;
|
||||
@ -113,7 +122,7 @@ class SpaceManagementBloc
|
||||
}
|
||||
}
|
||||
prevSpaceModels = await _spaceModelApi.listSpaceModels(
|
||||
page: 1, projectId: projectUuid ?? TempConst.projectId);
|
||||
page: 1, projectId: projectUuid);
|
||||
}
|
||||
|
||||
return allSpaces;
|
||||
@ -142,10 +151,9 @@ class SpaceManagementBloc
|
||||
|
||||
Future<List<SpaceModel>> _fetchSpacesForCommunity(
|
||||
String communityUuid) async {
|
||||
final projectUuid = projectCubit.state;
|
||||
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
||||
|
||||
return await _api.getSpaceHierarchy(
|
||||
communityUuid, projectUuid ?? TempConst.projectId);
|
||||
return await _api.getSpaceHierarchy(communityUuid, projectUuid);
|
||||
}
|
||||
|
||||
Future<void> _onNewCommunity(
|
||||
@ -176,7 +184,7 @@ class SpaceManagementBloc
|
||||
BlankStateEvent event, Emitter<SpaceManagementState> emit) async {
|
||||
try {
|
||||
final previousState = state;
|
||||
final projectUuid = projectCubit.state;
|
||||
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
||||
|
||||
var prevSpaceModels = await fetchSpaceModels(previousState);
|
||||
|
||||
@ -191,8 +199,7 @@ class SpaceManagementBloc
|
||||
return;
|
||||
}
|
||||
|
||||
final communities =
|
||||
await _api.fetchCommunities(projectUuid ?? TempConst.projectId);
|
||||
final communities = await _api.fetchCommunities(projectUuid);
|
||||
final updatedCommunities =
|
||||
await Future.wait(communities.map((community) async {
|
||||
final spaces = await _fetchSpacesForCommunity(community.uuid);
|
||||
@ -222,14 +229,16 @@ class SpaceManagementBloc
|
||||
LoadCommunityAndSpacesEvent event,
|
||||
Emitter<SpaceManagementState> emit,
|
||||
) async {
|
||||
_logEvent('LoadCommunityAndSpacesEvent');
|
||||
|
||||
var prevState = state;
|
||||
emit(SpaceManagementLoading());
|
||||
try {
|
||||
final projectUuid = projectCubit.state;
|
||||
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
||||
|
||||
_onloadProducts();
|
||||
List<CommunityModel> communities =
|
||||
await _api.fetchCommunities(projectUuid ?? TempConst.projectId);
|
||||
await _api.fetchCommunities(projectUuid);
|
||||
|
||||
List<CommunityModel> updatedCommunities = await Future.wait(
|
||||
communities.map((community) async {
|
||||
@ -263,10 +272,10 @@ class SpaceManagementBloc
|
||||
) async {
|
||||
try {
|
||||
emit(SpaceManagementLoading());
|
||||
final projectUuid = projectCubit.state;
|
||||
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
||||
|
||||
final success = await _api.deleteCommunity(
|
||||
event.communityUuid, projectUuid ?? TempConst.projectId);
|
||||
final success =
|
||||
await _api.deleteCommunity(event.communityUuid, projectUuid);
|
||||
if (success) {
|
||||
add(LoadCommunityAndSpacesEvent());
|
||||
} else {
|
||||
@ -291,9 +300,10 @@ class SpaceManagementBloc
|
||||
emit(SpaceManagementLoading());
|
||||
|
||||
try {
|
||||
final projectUuid = projectCubit.state;
|
||||
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
||||
|
||||
CommunityModel? newCommunity = await _api.createCommunity(
|
||||
event.name, event.description, projectUuid ?? TempConst.projectId);
|
||||
event.name, event.description, projectUuid);
|
||||
var prevSpaceModels = await fetchSpaceModels(previousState);
|
||||
|
||||
if (newCommunity != null) {
|
||||
@ -436,7 +446,7 @@ class SpaceManagementBloc
|
||||
Future<List<SpaceModel>> saveSpacesHierarchically(
|
||||
List<SpaceModel> spaces, String communityUuid) async {
|
||||
final orderedSpaces = flattenHierarchy(spaces);
|
||||
final projectUuid = projectCubit.state;
|
||||
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
||||
|
||||
final parentsToDelete = orderedSpaces.where((space) =>
|
||||
space.status == SpaceStatus.deleted &&
|
||||
@ -445,8 +455,7 @@ class SpaceManagementBloc
|
||||
for (var parent in parentsToDelete) {
|
||||
try {
|
||||
if (parent.uuid != null) {
|
||||
await _api.deleteSpace(
|
||||
communityUuid, parent.uuid!, projectUuid ?? TempConst.projectId);
|
||||
await _api.deleteSpace(communityUuid, parent.uuid!, projectUuid);
|
||||
}
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
@ -459,8 +468,8 @@ class SpaceManagementBloc
|
||||
if (space.uuid != null && space.uuid!.isNotEmpty) {
|
||||
List<TagModelUpdate> tagUpdates = [];
|
||||
|
||||
final prevSpace = await _api.getSpace(
|
||||
communityUuid, space.uuid!, projectUuid ?? TempConst.projectId);
|
||||
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;
|
||||
@ -539,7 +548,7 @@ class SpaceManagementBloc
|
||||
subspaces: subspaceUpdates,
|
||||
tags: tagUpdates,
|
||||
direction: space.incomingConnection?.direction,
|
||||
projectId: projectUuid ?? TempConst.projectId);
|
||||
projectId: projectUuid);
|
||||
} else {
|
||||
// Call create if the space does not have a UUID
|
||||
final List<CreateTagBodyModel> tagBodyModels = space.tags != null
|
||||
@ -568,7 +577,7 @@ class SpaceManagementBloc
|
||||
spaceModelUuid: space.spaceModel?.uuid,
|
||||
tags: tagBodyModels,
|
||||
subspaces: createSubspaceBodyModels,
|
||||
projectId: projectUuid ?? TempConst.projectId);
|
||||
projectId: projectUuid);
|
||||
space.uuid = response?.uuid;
|
||||
}
|
||||
} catch (e) {
|
||||
@ -608,10 +617,10 @@ class SpaceManagementBloc
|
||||
emit(SpaceManagementLoading());
|
||||
try {
|
||||
var prevState = state;
|
||||
final projectUuid = projectCubit.state;
|
||||
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
||||
|
||||
List<CommunityModel> communities =
|
||||
await _api.fetchCommunities(projectUuid ?? TempConst.projectId);
|
||||
await _api.fetchCommunities(projectUuid);
|
||||
|
||||
List<CommunityModel> updatedCommunities = await Future.wait(
|
||||
communities.map((community) async {
|
||||
|
@ -1,6 +1,5 @@
|
||||
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';
|
||||
@ -33,7 +32,6 @@ class SpaceManagementPageState extends State<SpaceManagementPage> {
|
||||
_api,
|
||||
_productApi,
|
||||
_spaceModelApi,
|
||||
context.read<ProjectCubit>(),
|
||||
)..add(LoadCommunityAndSpacesEvent()),
|
||||
),
|
||||
BlocProvider(
|
||||
|
@ -1,6 +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/common/bloc/project_manager.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';
|
||||
@ -88,7 +88,6 @@ class _LoadedSpaceViewState extends State<LoadedSpaceView> {
|
||||
child: BlocProvider(
|
||||
create: (context) => SpaceModelBloc(
|
||||
api: SpaceModelManagementApi(),
|
||||
projectCubit: context.read<ProjectCubit>(),
|
||||
initialSpaceModels: _spaceModels,
|
||||
),
|
||||
child: SpaceModelPage(
|
||||
|
Reference in New Issue
Block a user