fixed delete community

This commit is contained in:
hannathkadher
2024-11-21 13:02:48 +04:00
parent 8a7f9ab2dc
commit 288360f1af
5 changed files with 64 additions and 28 deletions

View File

@ -19,6 +19,7 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
on<CreateCommunityEvent>(_onCreateCommunity); on<CreateCommunityEvent>(_onCreateCommunity);
on<SaveSpacesEvent>(_onSaveSpaces); on<SaveSpacesEvent>(_onSaveSpaces);
on<FetchProductsEvent>(_onFetchProducts); on<FetchProductsEvent>(_onFetchProducts);
on<DeleteCommunityEvent>(_onCommunityDelete);
} }
void _onFetchProducts( void _onFetchProducts(
@ -74,11 +75,29 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
} }
} }
void _onCommunityDelete(
DeleteCommunityEvent event,
Emitter<SpaceManagementState> emit,
) async {
try {
emit(SpaceManagementLoading());
final success = await _api.deleteCommunity(event.communityUuid);
if (success) {
add(LoadCommunityAndSpacesEvent());
} else {
emit(const SpaceManagementError('Failed to delete the community.'));
}
} catch (e) {
// Handle unexpected errors
emit(SpaceManagementError('Error saving spaces: $e'));
}
}
void _onUpdateSpacePosition( void _onUpdateSpacePosition(
UpdateSpacePositionEvent event, UpdateSpacePositionEvent event,
Emitter<SpaceManagementState> emit, Emitter<SpaceManagementState> emit,
) { ) {
// Handle space position update logic
} }
void _onCreateCommunity( void _onCreateCommunity(
@ -110,7 +129,6 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
} }
} }
void _onSaveSpaces( void _onSaveSpaces(
SaveSpacesEvent event, SaveSpacesEvent event,
Emitter<SpaceManagementState> emit, Emitter<SpaceManagementState> emit,
@ -147,8 +165,7 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
position: space.position, position: space.position,
icon: space.icon, icon: space.icon,
direction: space.incomingConnection?.direction, direction: space.incomingConnection?.direction,
products: space.selectedProducts products: space.selectedProducts);
);
} else { } else {
// Call create if the space does not have a UUID // Call create if the space does not have a UUID
final response = await _api.createSpace( final response = await _api.createSpace(
@ -159,9 +176,7 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
position: space.position, position: space.position,
icon: space.icon, icon: space.icon,
direction: space.incomingConnection?.direction, direction: space.incomingConnection?.direction,
products: space.selectedProducts products: space.selectedProducts);
);
space.uuid = response?.uuid; space.uuid = response?.uuid;
} }
} catch (e) { } catch (e) {

View File

@ -11,6 +11,17 @@ abstract class SpaceManagementEvent extends Equatable {
class LoadCommunityAndSpacesEvent extends SpaceManagementEvent {} class LoadCommunityAndSpacesEvent extends SpaceManagementEvent {}
class DeleteCommunityEvent extends SpaceManagementEvent {
final String communityUuid;
const DeleteCommunityEvent({
required this.communityUuid,
});
@override
List<Object> get props => [communityUuid];
}
class CreateSpaceEvent extends SpaceManagementEvent { class CreateSpaceEvent extends SpaceManagementEvent {
final String name; final String name;
final String icon; final String icon;

View File

@ -56,8 +56,10 @@ class SpaceManagementPageState extends State<SpaceManagementPage> {
); );
if (selectedIndex != -1) { if (selectedIndex != -1) {
selectedCommunity = state.communities[selectedIndex]; selectedCommunity = state.communities[selectedIndex];
} else {
selectedCommunity = null;
selectedSpace = null;
} }
return LoadedSpaceView( return LoadedSpaceView(
communities: state.communities, communities: state.communities,
selectedCommunity: selectedCommunity, selectedCommunity: selectedCommunity,

View File

@ -119,7 +119,6 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
return SpaceContainerWidget( return SpaceContainerWidget(
index: index, index: index,
onDoubleTap: () { onDoubleTap: () {
print(spaces[index].toString());
_showEditSpaceDialog(spaces[index]); _showEditSpaceDialog(spaces[index]);
}, },
icon: spaces[index].icon ?? '', icon: spaces[index].icon ?? '',
@ -209,12 +208,12 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
onPressed: () { onPressed: () {
showDeleteConfirmationDialog(context, () { showDeleteConfirmationDialog(context, () {
// Handle the delete action here // Handle the delete action here
print("Delete confirmed");
Navigator.of(context).pop(); // Close the dialog after confirming Navigator.of(context).pop(); // Close the dialog after confirming
_onDelete();
}, widget.selectedSpace != null); }, widget.selectedSpace != null);
}, },
icon: SvgPicture.asset( icon: SvgPicture.asset(
Assets.acFanAuto, // Path to your SVG asset Assets.delete, // Path to your SVG asset
width: 18, // Adjust width as needed width: 18, // Adjust width as needed
height: 18, // Adjust height as needed height: 18, // Adjust height as needed
), ),
@ -426,4 +425,13 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
communityUuid: communityUuid, communityUuid: communityUuid,
)); ));
} }
void _onDelete() {
if (widget.selectedCommunity != null && widget.selectedCommunity?.uuid != null) {
context.read<SpaceManagementBloc>().add(DeleteCommunityEvent(
communityUuid: widget.selectedCommunity!.uuid,
));
}
}
} }

View File

@ -235,5 +235,5 @@ class Assets {
static const String garageDoor = 'assets/icons/garage_opener.svg'; static const String garageDoor = 'assets/icons/garage_opener.svg';
static const String doorSensor = 'assets/icons/door_sensor.svg'; static const String doorSensor = 'assets/icons/door_sensor.svg';
static const String delete = 'assets/icons/delete_svg'; static const String delete = 'assets/icons/delete.svg';
} }