From b0b039875a409d5b5a45112b1589bcb6d8e8c87d Mon Sep 17 00:00:00 2001 From: Mathis Kirchner Date: Mon, 9 Mar 2026 15:29:03 +0100 Subject: [PATCH] add callback & implement deleteObsoleteMatchGroupRelations func --- .../group_view/create_group_view.dart | 67 +++++++------------ 1 file changed, 23 insertions(+), 44 deletions(-) diff --git a/lib/presentation/views/main_menu/group_view/create_group_view.dart b/lib/presentation/views/main_menu/group_view/create_group_view.dart index 97a49e4..bb77057 100644 --- a/lib/presentation/views/main_menu/group_view/create_group_view.dart +++ b/lib/presentation/views/main_menu/group_view/create_group_view.dart @@ -12,11 +12,13 @@ import 'package:tallee/presentation/widgets/player_selection.dart'; import 'package:tallee/presentation/widgets/text_input/text_input_field.dart'; class CreateGroupView extends StatefulWidget { - const CreateGroupView({super.key, this.groupToEdit}); + const CreateGroupView({super.key, this.groupToEdit, this.onMembersChanged}); /// The group to edit, if any final Group? groupToEdit; + final VoidCallback? onMembersChanged; + @override State createState() => _CreateGroupViewState(); } @@ -70,49 +72,6 @@ class _CreateGroupViewState extends State { title: Text( widget.groupToEdit == null ? loc.create_new_group : loc.edit_group, ), - actions: widget.groupToEdit == null - ? [] - : [ - IconButton( - icon: const Icon(Icons.delete), - onPressed: () async { - if (widget.groupToEdit != null) { - showDialog( - context: context, - builder: (context) => AlertDialog( - title: Text(loc.delete_group), - content: Text(loc.this_cannot_be_undone), - actions: [ - TextButton( - onPressed: () => - Navigator.of(context).pop(false), - child: Text(loc.cancel), - ), - TextButton( - onPressed: () => - Navigator.of(context).pop(true), - child: Text(loc.delete), - ), - ], - ), - ).then((confirmed) async { - if (confirmed == true && context.mounted) { - bool success = await db.groupDao.deleteGroup( - groupId: widget.groupToEdit!.id, - ); - if (!context.mounted) return; - if (success) { - Navigator.pop(context); - } else { - if (!mounted) return; - showSnackbar(message: loc.error_deleting_group); - } - } - }); - } - }, - ), - ], ), body: SafeArea( child: Column( @@ -222,6 +181,8 @@ class _CreateGroupViewState extends State { groupId: widget.groupToEdit!.id, newPlayers: selectedPlayers, ); + await deleteObsoleteMatchGroupRelations(); + widget.onMembersChanged?.call(); } final success = successfullNameChange && successfullMemberChange; @@ -229,6 +190,24 @@ class _CreateGroupViewState extends State { return (success, updatedGroup); } + Future deleteObsoleteMatchGroupRelations() async { + final matches = await db.matchDao.getAllMatches(); + final groupMatches = matches + .where((match) => match.group?.id == widget.groupToEdit!.id) + .toList(); + + final selectedPlayerIds = selectedPlayers.map((p) => p.id).toSet(); + final relationshipsToDelete = groupMatches.where((match) { + return !match.players.any( + (player) => selectedPlayerIds.contains(player.id), + ); + }).toList(); + + for (var match in relationshipsToDelete) { + await db.matchDao.deleteMatchGroup(matchId: match.id); + } + } + /// Displays a snackbar with the given message and optional action. /// /// [message] The message to display in the snackbar.