mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 07:07:19 +00:00
added update in space tree bloc
This commit is contained in:
@ -8,9 +8,6 @@ import 'package:syncrow_web/pages/common/hour_picker_dialog.dart';
|
|||||||
import 'package:syncrow_web/services/access_mang_api.dart';
|
import 'package:syncrow_web/services/access_mang_api.dart';
|
||||||
import 'package:syncrow_web/utils/color_manager.dart';
|
import 'package:syncrow_web/utils/color_manager.dart';
|
||||||
import 'package:syncrow_web/utils/constants/app_enum.dart';
|
import 'package:syncrow_web/utils/constants/app_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';
|
|
||||||
import 'package:syncrow_web/utils/snack_bar.dart';
|
import 'package:syncrow_web/utils/snack_bar.dart';
|
||||||
|
|
||||||
class AccessBloc extends Bloc<AccessEvent, AccessState> {
|
class AccessBloc extends Bloc<AccessEvent, AccessState> {
|
||||||
|
@ -8,7 +8,6 @@ import 'package:syncrow_web/pages/device_managment/all_devices/models/devices_mo
|
|||||||
import 'package:syncrow_web/pages/device_managment/all_devices/widgets/device_search_filters.dart';
|
import 'package:syncrow_web/pages/device_managment/all_devices/widgets/device_search_filters.dart';
|
||||||
import 'package:syncrow_web/pages/device_managment/shared/device_batch_control_dialog.dart';
|
import 'package:syncrow_web/pages/device_managment/shared/device_batch_control_dialog.dart';
|
||||||
import 'package:syncrow_web/pages/device_managment/shared/device_control_dialog.dart';
|
import 'package:syncrow_web/pages/device_managment/shared/device_control_dialog.dart';
|
||||||
import 'package:syncrow_web/pages/space_tree/bloc/space_tree_bloc.dart';
|
|
||||||
import 'package:syncrow_web/pages/space_tree/view/space_tree_view.dart';
|
import 'package:syncrow_web/pages/space_tree/view/space_tree_view.dart';
|
||||||
import 'package:syncrow_web/utils/format_date_time.dart';
|
import 'package:syncrow_web/utils/format_date_time.dart';
|
||||||
import 'package:syncrow_web/utils/helpers/responsice_layout_helper/responsive_layout_helper.dart';
|
import 'package:syncrow_web/utils/helpers/responsice_layout_helper/responsive_layout_helper.dart';
|
||||||
|
@ -83,7 +83,6 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
|||||||
try {
|
try {
|
||||||
emit(LoadingHome());
|
emit(LoadingHome());
|
||||||
policy = await HomeApi().fetchPolicy();
|
policy = await HomeApi().fetchPolicy();
|
||||||
debugPrint("Fetched policy: $policy");
|
|
||||||
// Emit a state to trigger the UI update
|
// Emit a state to trigger the UI update
|
||||||
emit(HomeInitial());
|
emit(HomeInitial());
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -16,6 +16,7 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
|
|||||||
on<SearchQueryEvent>(_onSearch);
|
on<SearchQueryEvent>(_onSearch);
|
||||||
on<ClearAllData>(_clearAllData);
|
on<ClearAllData>(_clearAllData);
|
||||||
on<ClearCachedData>(_clearCachedData);
|
on<ClearCachedData>(_clearCachedData);
|
||||||
|
on<OnCommunityAdded>(_onCommunityAdded);
|
||||||
}
|
}
|
||||||
|
|
||||||
_fetchSpaces(InitialEvent event, Emitter<SpaceTreeState> emit) async {
|
_fetchSpaces(InitialEvent event, Emitter<SpaceTreeState> emit) async {
|
||||||
@ -50,6 +51,14 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _onCommunityAdded(OnCommunityAdded event, Emitter<SpaceTreeState> emit) async {
|
||||||
|
final updatedCommunities = List<CommunityModel>.from(state.communityList);
|
||||||
|
updatedCommunities.add(event.newCommunity);
|
||||||
|
|
||||||
|
emit(state.copyWith(communitiesList: updatedCommunities));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
_onCommunityExpanded(OnCommunityExpanded event, Emitter<SpaceTreeState> emit) async {
|
_onCommunityExpanded(OnCommunityExpanded event, Emitter<SpaceTreeState> emit) async {
|
||||||
try {
|
try {
|
||||||
List<String> updatedExpandedCommunityList = List.from(state.expandedCommunities);
|
List<String> updatedExpandedCommunityList = List.from(state.expandedCommunities);
|
||||||
|
@ -69,6 +69,14 @@ class SearchQueryEvent extends SpaceTreeEvent {
|
|||||||
List<Object> get props => [searchQuery];
|
List<Object> get props => [searchQuery];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class OnCommunityAdded extends SpaceTreeEvent {
|
||||||
|
final CommunityModel newCommunity;
|
||||||
|
const OnCommunityAdded(this.newCommunity);
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object> get props => [newCommunity];
|
||||||
|
}
|
||||||
|
|
||||||
class ClearAllData extends SpaceTreeEvent {}
|
class ClearAllData extends SpaceTreeEvent {}
|
||||||
|
|
||||||
class ClearCachedData extends SpaceTreeEvent {}
|
class ClearCachedData extends SpaceTreeEvent {}
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
import 'dart:developer';
|
import 'dart:developer';
|
||||||
|
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
|
||||||
import 'package:syncrow_web/pages/common/bloc/project_manager.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/spaces_management/all_spaces/model/community_model.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/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/product_model.dart';
|
||||||
@ -17,7 +19,7 @@ import 'package:syncrow_web/pages/spaces_management/space_model/models/tag_updat
|
|||||||
import 'package:syncrow_web/services/product_api.dart';
|
import 'package:syncrow_web/services/product_api.dart';
|
||||||
import 'package:syncrow_web/services/space_mana_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/services/space_model_mang_api.dart';
|
||||||
import 'package:syncrow_web/utils/constants/action_enum.dart';
|
import 'package:syncrow_web/utils/constants/action_enum.dart' as custom_action;
|
||||||
|
|
||||||
class SpaceManagementBloc
|
class SpaceManagementBloc
|
||||||
extends Bloc<SpaceManagementEvent, SpaceManagementState> {
|
extends Bloc<SpaceManagementEvent, SpaceManagementState> {
|
||||||
@ -30,7 +32,6 @@ class SpaceManagementBloc
|
|||||||
SpaceManagementBloc(this._api, this._productApi, this._spaceModelApi)
|
SpaceManagementBloc(this._api, this._productApi, this._spaceModelApi)
|
||||||
: super(SpaceManagementInitial()) {
|
: super(SpaceManagementInitial()) {
|
||||||
on<LoadCommunityAndSpacesEvent>(_onLoadCommunityAndSpaces);
|
on<LoadCommunityAndSpacesEvent>(_onLoadCommunityAndSpaces);
|
||||||
on<UpdateSpacePositionEvent>(_onUpdateSpacePosition);
|
|
||||||
on<CreateCommunityEvent>(_onCreateCommunity);
|
on<CreateCommunityEvent>(_onCreateCommunity);
|
||||||
on<SelectCommunityEvent>(_onSelectCommunity);
|
on<SelectCommunityEvent>(_onSelectCommunity);
|
||||||
on<DeleteCommunityEvent>(_onCommunityDelete);
|
on<DeleteCommunityEvent>(_onCommunityDelete);
|
||||||
@ -182,6 +183,8 @@ class SpaceManagementBloc
|
|||||||
try {
|
try {
|
||||||
final previousState = state;
|
final previousState = state;
|
||||||
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
||||||
|
var spaceBloc = event.context.read<SpaceTreeBloc>();
|
||||||
|
List<CommunityModel> communities = spaceBloc.state.communityList;
|
||||||
|
|
||||||
var prevSpaceModels = await fetchSpaceModels(previousState);
|
var prevSpaceModels = await fetchSpaceModels(previousState);
|
||||||
|
|
||||||
@ -196,25 +199,31 @@ class SpaceManagementBloc
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final communities = await _api.fetchCommunities(projectUuid);
|
if (communities.isEmpty) {
|
||||||
final updatedCommunities =
|
communities = await _api.fetchCommunities(projectUuid);
|
||||||
await Future.wait(communities.map((community) async {
|
|
||||||
final spaces = await _fetchSpacesForCommunity(community.uuid);
|
|
||||||
|
|
||||||
return CommunityModel(
|
List<CommunityModel> updatedCommunities = await Future.wait(
|
||||||
uuid: community.uuid,
|
communities.map((community) async {
|
||||||
createdAt: community.createdAt,
|
List<SpaceModel> spaces =
|
||||||
updatedAt: community.updatedAt,
|
await _fetchSpacesForCommunity(community.uuid);
|
||||||
name: community.name,
|
return CommunityModel(
|
||||||
description: community.description,
|
uuid: community.uuid,
|
||||||
spaces: spaces,
|
createdAt: community.createdAt,
|
||||||
region: community.region,
|
updatedAt: community.updatedAt,
|
||||||
|
name: community.name,
|
||||||
|
description: community.description,
|
||||||
|
spaces: spaces,
|
||||||
|
region: community.region,
|
||||||
|
);
|
||||||
|
}).toList(),
|
||||||
);
|
);
|
||||||
}));
|
|
||||||
|
communities = updatedCommunities;
|
||||||
|
}
|
||||||
|
|
||||||
emit(BlankState(
|
emit(BlankState(
|
||||||
spaceModels: prevSpaceModels,
|
spaceModels: prevSpaceModels,
|
||||||
communities: updatedCommunities,
|
communities: communities,
|
||||||
products: _cachedProducts ?? [],
|
products: _cachedProducts ?? [],
|
||||||
));
|
));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -228,50 +237,49 @@ class SpaceManagementBloc
|
|||||||
) async {
|
) async {
|
||||||
_logEvent('LoadCommunityAndSpacesEvent');
|
_logEvent('LoadCommunityAndSpacesEvent');
|
||||||
|
|
||||||
// If already loaded, use cached data
|
var spaceBloc = event.context.read<SpaceTreeBloc>();
|
||||||
if (state is SpaceManagementLoaded) {
|
List<CommunityModel> communities = spaceBloc.state.communityList;
|
||||||
emit(state);
|
|
||||||
return;
|
if (communities.isEmpty) {
|
||||||
|
_logEvent("community is empty");
|
||||||
|
emit(SpaceManagementLoading());
|
||||||
|
|
||||||
|
try {
|
||||||
|
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
||||||
|
_onloadProducts();
|
||||||
|
|
||||||
|
/* communities = await _api.fetchCommunities(projectUuid);
|
||||||
|
|
||||||
|
List<CommunityModel> updatedCommunities = await Future.wait(
|
||||||
|
communities.map((community) async {
|
||||||
|
List<SpaceModel> spaces =
|
||||||
|
await _fetchSpacesForCommunity(community.uuid);
|
||||||
|
return CommunityModel(
|
||||||
|
uuid: community.uuid,
|
||||||
|
createdAt: community.createdAt,
|
||||||
|
updatedAt: community.updatedAt,
|
||||||
|
name: community.name,
|
||||||
|
description: community.description,
|
||||||
|
spaces: spaces,
|
||||||
|
region: community.region,
|
||||||
|
);
|
||||||
|
}).toList(),
|
||||||
|
);
|
||||||
|
|
||||||
|
communities = updatedCommunities; */
|
||||||
|
} catch (e) {
|
||||||
|
emit(SpaceManagementError('Error loading communities and spaces: $e'));
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
emit(SpaceManagementLoading());
|
final prevSpaceModels = await fetchSpaceModels(state);
|
||||||
|
|
||||||
try {
|
emit(SpaceManagementLoaded(
|
||||||
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
communities: communities,
|
||||||
|
products: _cachedProducts ?? [],
|
||||||
// Load products only once
|
spaceModels: prevSpaceModels,
|
||||||
_onloadProducts();
|
));
|
||||||
|
|
||||||
List<CommunityModel> communities =
|
|
||||||
await _api.fetchCommunities(projectUuid);
|
|
||||||
|
|
||||||
List<CommunityModel> updatedCommunities = await Future.wait(
|
|
||||||
communities.map((community) async {
|
|
||||||
List<SpaceModel> spaces =
|
|
||||||
await _fetchSpacesForCommunity(community.uuid);
|
|
||||||
return CommunityModel(
|
|
||||||
uuid: community.uuid,
|
|
||||||
createdAt: community.createdAt,
|
|
||||||
updatedAt: community.updatedAt,
|
|
||||||
name: community.name,
|
|
||||||
description: community.description,
|
|
||||||
spaces: spaces,
|
|
||||||
region: community.region,
|
|
||||||
);
|
|
||||||
}).toList(),
|
|
||||||
);
|
|
||||||
|
|
||||||
// Fetch space models only once
|
|
||||||
final prevSpaceModels = await fetchSpaceModels(state);
|
|
||||||
|
|
||||||
emit(SpaceManagementLoaded(
|
|
||||||
communities: updatedCommunities,
|
|
||||||
products: _cachedProducts ?? [],
|
|
||||||
spaceModels: prevSpaceModels,
|
|
||||||
));
|
|
||||||
} catch (e) {
|
|
||||||
emit(SpaceManagementError('Error loading communities and spaces: $e'));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onCommunityDelete(
|
void _onCommunityDelete(
|
||||||
@ -285,7 +293,7 @@ class SpaceManagementBloc
|
|||||||
final success =
|
final success =
|
||||||
await _api.deleteCommunity(event.communityUuid, projectUuid);
|
await _api.deleteCommunity(event.communityUuid, projectUuid);
|
||||||
if (success) {
|
if (success) {
|
||||||
add(LoadCommunityAndSpacesEvent());
|
// add(LoadCommunityAndSpacesEvent());
|
||||||
} else {
|
} else {
|
||||||
emit(const SpaceManagementError('Failed to delete the community.'));
|
emit(const SpaceManagementError('Failed to delete the community.'));
|
||||||
}
|
}
|
||||||
@ -295,11 +303,6 @@ class SpaceManagementBloc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onUpdateSpacePosition(
|
|
||||||
UpdateSpacePositionEvent event,
|
|
||||||
Emitter<SpaceManagementState> emit,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
void _onCreateCommunity(
|
void _onCreateCommunity(
|
||||||
CreateCommunityEvent event,
|
CreateCommunityEvent event,
|
||||||
Emitter<SpaceManagementState> emit,
|
Emitter<SpaceManagementState> emit,
|
||||||
@ -309,6 +312,8 @@ class SpaceManagementBloc
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
||||||
|
final BuildContext safeContext =
|
||||||
|
event.context; // Capture context safely before async calls
|
||||||
|
|
||||||
CommunityModel? newCommunity = await _api.createCommunity(
|
CommunityModel? newCommunity = await _api.createCommunity(
|
||||||
event.name, event.description, projectUuid);
|
event.name, event.description, projectUuid);
|
||||||
@ -321,6 +326,16 @@ class SpaceManagementBloc
|
|||||||
(previousState as dynamic).communities,
|
(previousState as dynamic).communities,
|
||||||
);
|
);
|
||||||
final updatedCommunities = prevCommunities..add(newCommunity);
|
final updatedCommunities = prevCommunities..add(newCommunity);
|
||||||
|
|
||||||
|
if (safeContext.mounted) {
|
||||||
|
print("added");
|
||||||
|
final spaceTreeBloc = safeContext.read<
|
||||||
|
SpaceTreeBloc>(); // ✅ Read bloc only when context is mounted
|
||||||
|
spaceTreeBloc.add(OnCommunityAdded(newCommunity));
|
||||||
|
} else {
|
||||||
|
print("not mounted");
|
||||||
|
}
|
||||||
|
|
||||||
emit(SpaceManagementLoaded(
|
emit(SpaceManagementLoaded(
|
||||||
spaceModels: prevSpaceModels,
|
spaceModels: prevSpaceModels,
|
||||||
communities: updatedCommunities,
|
communities: updatedCommunities,
|
||||||
@ -416,7 +431,7 @@ class SpaceManagementBloc
|
|||||||
emit,
|
emit,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
add(LoadCommunityAndSpacesEvent());
|
// add(LoadCommunityAndSpacesEvent());
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
emit(SpaceManagementError('Error saving spaces: $e'));
|
emit(SpaceManagementError('Error saving spaces: $e'));
|
||||||
@ -491,13 +506,15 @@ class SpaceManagementBloc
|
|||||||
.any((subspace) => subspace.uuid == prevSubspace.uuid);
|
.any((subspace) => subspace.uuid == prevSubspace.uuid);
|
||||||
if (!existsInNew) {
|
if (!existsInNew) {
|
||||||
subspaceUpdates.add(UpdateSubspaceTemplateModel(
|
subspaceUpdates.add(UpdateSubspaceTemplateModel(
|
||||||
action: Action.delete, uuid: prevSubspace.uuid));
|
action: custom_action.Action.delete,
|
||||||
|
uuid: prevSubspace.uuid));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (prevSubspaces != null && newSubspaces == null) {
|
} else if (prevSubspaces != null && newSubspaces == null) {
|
||||||
for (var prevSubspace in prevSubspaces) {
|
for (var prevSubspace in prevSubspaces) {
|
||||||
subspaceUpdates.add(UpdateSubspaceTemplateModel(
|
subspaceUpdates.add(UpdateSubspaceTemplateModel(
|
||||||
action: Action.delete, uuid: prevSubspace.uuid));
|
action: custom_action.Action.delete,
|
||||||
|
uuid: prevSubspace.uuid));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -510,14 +527,14 @@ class SpaceManagementBloc
|
|||||||
if (newSubspace.tags != null) {
|
if (newSubspace.tags != null) {
|
||||||
for (var tag in newSubspace.tags!) {
|
for (var tag in newSubspace.tags!) {
|
||||||
tagUpdates.add(TagModelUpdate(
|
tagUpdates.add(TagModelUpdate(
|
||||||
action: Action.add,
|
action: custom_action.Action.add,
|
||||||
uuid: tag.uuid == '' ? null : tag.uuid,
|
uuid: tag.uuid == '' ? null : tag.uuid,
|
||||||
tag: tag.tag,
|
tag: tag.tag,
|
||||||
productUuid: tag.product?.uuid));
|
productUuid: tag.product?.uuid));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
subspaceUpdates.add(UpdateSubspaceTemplateModel(
|
subspaceUpdates.add(UpdateSubspaceTemplateModel(
|
||||||
action: Action.add,
|
action: custom_action.Action.add,
|
||||||
subspaceName: newSubspace.subspaceName,
|
subspaceName: newSubspace.subspaceName,
|
||||||
tags: tagUpdates));
|
tags: tagUpdates));
|
||||||
}
|
}
|
||||||
@ -536,7 +553,7 @@ class SpaceManagementBloc
|
|||||||
final List<TagModelUpdate> tagSubspaceUpdates =
|
final List<TagModelUpdate> tagSubspaceUpdates =
|
||||||
processTagUpdates(prevSubspace.tags, newSubspace.tags);
|
processTagUpdates(prevSubspace.tags, newSubspace.tags);
|
||||||
subspaceUpdates.add(UpdateSubspaceTemplateModel(
|
subspaceUpdates.add(UpdateSubspaceTemplateModel(
|
||||||
action: Action.update,
|
action: custom_action.Action.update,
|
||||||
uuid: newSubspace.uuid,
|
uuid: newSubspace.uuid,
|
||||||
subspaceName: newSubspace.subspaceName,
|
subspaceName: newSubspace.subspaceName,
|
||||||
tags: tagSubspaceUpdates));
|
tags: tagSubspaceUpdates));
|
||||||
@ -623,35 +640,42 @@ class SpaceManagementBloc
|
|||||||
void _onLoadSpaceModel(
|
void _onLoadSpaceModel(
|
||||||
SpaceModelLoadEvent event, Emitter<SpaceManagementState> emit) async {
|
SpaceModelLoadEvent event, Emitter<SpaceManagementState> emit) async {
|
||||||
emit(SpaceManagementLoading());
|
emit(SpaceManagementLoading());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var prevState = state;
|
var prevState = state;
|
||||||
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
final projectUuid = await ProjectManager.getProjectUUID() ?? '';
|
||||||
|
var spaceBloc = event.context.read<SpaceTreeBloc>();
|
||||||
List<CommunityModel> communities =
|
List<CommunityModel> communities = spaceBloc.state.communityList;
|
||||||
await _api.fetchCommunities(projectUuid);
|
|
||||||
|
|
||||||
List<CommunityModel> updatedCommunities = await Future.wait(
|
|
||||||
communities.map((community) async {
|
|
||||||
List<SpaceModel> spaces =
|
|
||||||
await _fetchSpacesForCommunity(community.uuid);
|
|
||||||
return CommunityModel(
|
|
||||||
uuid: community.uuid,
|
|
||||||
createdAt: community.createdAt,
|
|
||||||
updatedAt: community.updatedAt,
|
|
||||||
name: community.name,
|
|
||||||
description: community.description,
|
|
||||||
spaces: spaces, // New spaces list
|
|
||||||
region: community.region,
|
|
||||||
);
|
|
||||||
}).toList(),
|
|
||||||
);
|
|
||||||
|
|
||||||
var prevSpaceModels = await fetchSpaceModels(prevState);
|
var prevSpaceModels = await fetchSpaceModels(prevState);
|
||||||
|
|
||||||
|
if (communities.isEmpty) {
|
||||||
|
communities = await _api.fetchCommunities(projectUuid);
|
||||||
|
|
||||||
|
List<CommunityModel> updatedCommunities = await Future.wait(
|
||||||
|
communities.map((community) async {
|
||||||
|
List<SpaceModel> spaces =
|
||||||
|
await _fetchSpacesForCommunity(community.uuid);
|
||||||
|
return CommunityModel(
|
||||||
|
uuid: community.uuid,
|
||||||
|
createdAt: community.createdAt,
|
||||||
|
updatedAt: community.updatedAt,
|
||||||
|
name: community.name,
|
||||||
|
description: community.description,
|
||||||
|
spaces: spaces,
|
||||||
|
region: community.region,
|
||||||
|
);
|
||||||
|
}).toList(),
|
||||||
|
);
|
||||||
|
|
||||||
|
communities = updatedCommunities;
|
||||||
|
}
|
||||||
|
|
||||||
emit(SpaceModelLoaded(
|
emit(SpaceModelLoaded(
|
||||||
communities: updatedCommunities,
|
communities: communities,
|
||||||
products: _cachedProducts ?? [],
|
products: _cachedProducts ?? [],
|
||||||
spaceModels: prevSpaceModels));
|
spaceModels: prevSpaceModels,
|
||||||
|
));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
emit(SpaceManagementError('Error loading communities and spaces: $e'));
|
emit(SpaceManagementError('Error loading communities and spaces: $e'));
|
||||||
}
|
}
|
||||||
@ -667,7 +691,7 @@ class SpaceManagementBloc
|
|||||||
if (prevTags == null && newTags != null) {
|
if (prevTags == null && newTags != null) {
|
||||||
for (var newTag in newTags) {
|
for (var newTag in newTags) {
|
||||||
tagUpdates.add(TagModelUpdate(
|
tagUpdates.add(TagModelUpdate(
|
||||||
action: Action.add,
|
action: custom_action.Action.add,
|
||||||
tag: newTag.tag,
|
tag: newTag.tag,
|
||||||
uuid: newTag.uuid,
|
uuid: newTag.uuid,
|
||||||
productUuid: newTag.product?.uuid,
|
productUuid: newTag.product?.uuid,
|
||||||
@ -683,14 +707,14 @@ class SpaceManagementBloc
|
|||||||
final existsInNew =
|
final existsInNew =
|
||||||
newTags.any((newTag) => newTag.uuid == prevTag.uuid);
|
newTags.any((newTag) => newTag.uuid == prevTag.uuid);
|
||||||
if (!existsInNew) {
|
if (!existsInNew) {
|
||||||
tagUpdates
|
tagUpdates.add(TagModelUpdate(
|
||||||
.add(TagModelUpdate(action: Action.delete, uuid: prevTag.uuid));
|
action: custom_action.Action.delete, uuid: prevTag.uuid));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (prevTags != null && newTags == null) {
|
} else if (prevTags != null && newTags == null) {
|
||||||
for (var prevTag in prevTags) {
|
for (var prevTag in prevTags) {
|
||||||
tagUpdates
|
tagUpdates.add(TagModelUpdate(
|
||||||
.add(TagModelUpdate(action: Action.delete, uuid: prevTag.uuid));
|
action: custom_action.Action.delete, uuid: prevTag.uuid));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -703,7 +727,7 @@ class SpaceManagementBloc
|
|||||||
if ((newTag.uuid == null || !prevTagUuids.contains(newTag.uuid)) &&
|
if ((newTag.uuid == null || !prevTagUuids.contains(newTag.uuid)) &&
|
||||||
!processedTags.contains(newTag.tag)) {
|
!processedTags.contains(newTag.tag)) {
|
||||||
tagUpdates.add(TagModelUpdate(
|
tagUpdates.add(TagModelUpdate(
|
||||||
action: Action.add,
|
action: custom_action.Action.add,
|
||||||
tag: newTag.tag,
|
tag: newTag.tag,
|
||||||
uuid: newTag.uuid == '' ? null : newTag.uuid,
|
uuid: newTag.uuid == '' ? null : newTag.uuid,
|
||||||
productUuid: newTag.product?.uuid));
|
productUuid: newTag.product?.uuid));
|
||||||
@ -720,7 +744,7 @@ class SpaceManagementBloc
|
|||||||
final newTag = newTagMap[prevTag.uuid];
|
final newTag = newTagMap[prevTag.uuid];
|
||||||
if (newTag != null) {
|
if (newTag != null) {
|
||||||
tagUpdates.add(TagModelUpdate(
|
tagUpdates.add(TagModelUpdate(
|
||||||
action: Action.update,
|
action: custom_action.Action.update,
|
||||||
uuid: newTag.uuid,
|
uuid: newTag.uuid,
|
||||||
tag: newTag.tag,
|
tag: newTag.tag,
|
||||||
));
|
));
|
||||||
|
@ -7,10 +7,16 @@ abstract class SpaceManagementEvent extends Equatable {
|
|||||||
const SpaceManagementEvent();
|
const SpaceManagementEvent();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<Object> get props => [];
|
List<Object?> get props => [];
|
||||||
}
|
}
|
||||||
|
|
||||||
class LoadCommunityAndSpacesEvent extends SpaceManagementEvent {}
|
class LoadCommunityAndSpacesEvent extends SpaceManagementEvent {
|
||||||
|
final BuildContext context;
|
||||||
|
|
||||||
|
const LoadCommunityAndSpacesEvent(this.context);
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [context];
|
||||||
|
}
|
||||||
|
|
||||||
class DeleteCommunityEvent extends SpaceManagementEvent {
|
class DeleteCommunityEvent extends SpaceManagementEvent {
|
||||||
final String communityUuid;
|
final String communityUuid;
|
||||||
@ -74,14 +80,12 @@ class UpdateSpacePositionEvent extends SpaceManagementEvent {
|
|||||||
class CreateCommunityEvent extends SpaceManagementEvent {
|
class CreateCommunityEvent extends SpaceManagementEvent {
|
||||||
final String name;
|
final String name;
|
||||||
final String description;
|
final String description;
|
||||||
|
final BuildContext context;
|
||||||
|
|
||||||
const CreateCommunityEvent({
|
const CreateCommunityEvent(this.name, this.description, this.context);
|
||||||
required this.name,
|
|
||||||
required this.description,
|
|
||||||
});
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<Object> get props => [name, description];
|
List<Object?> get props => [name, description, context];
|
||||||
}
|
}
|
||||||
|
|
||||||
class UpdateCommunityEvent extends SpaceManagementEvent {
|
class UpdateCommunityEvent extends SpaceManagementEvent {
|
||||||
@ -141,7 +145,18 @@ class LoadSpaceHierarchyEvent extends SpaceManagementEvent {
|
|||||||
List<Object> get props => [communityId];
|
List<Object> get props => [communityId];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class BlankStateEvent extends SpaceManagementEvent {
|
||||||
|
final BuildContext context;
|
||||||
|
|
||||||
class BlankStateEvent extends SpaceManagementEvent {}
|
const BlankStateEvent(this.context);
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [context];
|
||||||
|
}
|
||||||
|
|
||||||
class SpaceModelLoadEvent extends SpaceManagementEvent {}
|
class SpaceModelLoadEvent extends SpaceManagementEvent {
|
||||||
|
final BuildContext context;
|
||||||
|
|
||||||
|
const SpaceModelLoadEvent(this.context);
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [context];
|
||||||
|
}
|
||||||
|
@ -28,14 +28,14 @@ class SpaceManagementPageState extends State<SpaceManagementPage> {
|
|||||||
return MultiBlocProvider(
|
return MultiBlocProvider(
|
||||||
providers: [
|
providers: [
|
||||||
BlocProvider(
|
BlocProvider(
|
||||||
create: (_) => SpaceManagementBloc(
|
create: (context) => SpaceManagementBloc(
|
||||||
_api,
|
_api,
|
||||||
_productApi,
|
_productApi,
|
||||||
_spaceModelApi,
|
_spaceModelApi,
|
||||||
)..add(LoadCommunityAndSpacesEvent()),
|
)..add(LoadCommunityAndSpacesEvent(this.context)),
|
||||||
),
|
),
|
||||||
BlocProvider(
|
BlocProvider(
|
||||||
create: (_) => CenterBodyBloc(),
|
create: (context) => CenterBodyBloc(),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
child: WebScaffold(
|
child: WebScaffold(
|
||||||
|
@ -73,17 +73,14 @@ class _BlankCommunityWidgetState extends State<BlankCommunityWidget> {
|
|||||||
context: parentContext,
|
context: parentContext,
|
||||||
builder: (context) => CreateCommunityDialog(
|
builder: (context) => CreateCommunityDialog(
|
||||||
isEditMode: false,
|
isEditMode: false,
|
||||||
existingCommunityNames: widget.communities.map((community) => community.name).toList(),
|
existingCommunityNames:
|
||||||
|
widget.communities.map((community) => community.name).toList(),
|
||||||
onCreateCommunity: (String communityName, String description) {
|
onCreateCommunity: (String communityName, String description) {
|
||||||
parentContext.read<SpaceManagementBloc>().add(
|
parentContext.read<SpaceManagementBloc>().add(
|
||||||
CreateCommunityEvent(
|
CreateCommunityEvent(communityName, description, context),
|
||||||
name: communityName,
|
|
||||||
description: description,
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -15,11 +15,11 @@ class CenterBodyWidget extends StatelessWidget {
|
|||||||
context.read<CenterBodyBloc>().add(CommunityStructureSelectedEvent());
|
context.read<CenterBodyBloc>().add(CommunityStructureSelectedEvent());
|
||||||
}
|
}
|
||||||
if (state is CommunityStructureState) {
|
if (state is CommunityStructureState) {
|
||||||
context.read<SpaceManagementBloc>().add(BlankStateEvent());
|
context.read<SpaceManagementBloc>().add(BlankStateEvent(context));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (state is SpaceModelState) {
|
if (state is SpaceModelState) {
|
||||||
context.read<SpaceManagementBloc>().add(SpaceModelLoadEvent());
|
context.read<SpaceManagementBloc>().add(SpaceModelLoadEvent(context));
|
||||||
}
|
}
|
||||||
|
|
||||||
return Container(
|
return Container(
|
||||||
|
Reference in New Issue
Block a user