feat: Deleting games associated with matches deletes them
This commit is contained in:
@@ -159,7 +159,7 @@ class _ChooseGameViewState extends State<ChooseGameView> {
|
||||
adaptivePageRoute(
|
||||
builder: (context) => CreateGameView(
|
||||
gameToEdit: game,
|
||||
canDelete: canDeleteGame(game),
|
||||
matchCount: getMatchCount(game),
|
||||
onGameChanged: () {
|
||||
widget.onGamesUpdated?.call();
|
||||
},
|
||||
@@ -224,11 +224,10 @@ class _ChooseGameViewState extends State<ChooseGameView> {
|
||||
gameCounts = await db.gameDao.getGameUsage();
|
||||
}
|
||||
|
||||
// A game can only be deleted if there are no matches using it
|
||||
bool canDeleteGame(Game game) {
|
||||
final count = gameCounts
|
||||
// Returns the number of matches that use the given [game].
|
||||
int getMatchCount(Game game) {
|
||||
return gameCounts
|
||||
.firstWhere((gc) => gc.$1.id == game.id, orElse: () => (game, 0))
|
||||
.$2;
|
||||
return count == 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ class CreateGameView extends StatefulWidget {
|
||||
super.key,
|
||||
required this.onGameChanged,
|
||||
this.gameToEdit,
|
||||
this.canDelete = false,
|
||||
this.matchCount = 0,
|
||||
});
|
||||
|
||||
/// Callback to invoke when the game is created or edited
|
||||
@@ -34,7 +34,7 @@ class CreateGameView extends StatefulWidget {
|
||||
/// An optional game to prefill the fields
|
||||
final Game? gameToEdit;
|
||||
|
||||
final bool canDelete;
|
||||
final int matchCount;
|
||||
|
||||
@override
|
||||
State<CreateGameView> createState() => _CreateGameViewState();
|
||||
@@ -139,45 +139,59 @@ class _CreateGameViewState extends State<CreateGameView> {
|
||||
if (isEditMode())
|
||||
IconButton(
|
||||
icon: const Icon(Icons.delete),
|
||||
onPressed: widget.canDelete
|
||||
? () async {
|
||||
showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => CustomAlertDialog(
|
||||
title: loc.delete_game,
|
||||
content: Text(loc.this_cannot_be_undone),
|
||||
actions: [
|
||||
CustomDialogAction(
|
||||
onPressed: () =>
|
||||
Navigator.of(context).pop(false),
|
||||
text: loc.cancel,
|
||||
),
|
||||
CustomDialogAction(
|
||||
onPressed: () =>
|
||||
Navigator.of(context).pop(true),
|
||||
text: loc.delete,
|
||||
),
|
||||
],
|
||||
),
|
||||
).then((confirmed) async {
|
||||
if (confirmed == true && context.mounted) {
|
||||
bool success = await db.gameDao.deleteGame(
|
||||
gameId: widget.gameToEdit!.id,
|
||||
);
|
||||
if (!context.mounted) return;
|
||||
if (success) {
|
||||
widget.onGameChanged.call();
|
||||
Navigator.of(
|
||||
context,
|
||||
).pop((game: widget.gameToEdit, delete: true));
|
||||
} else {
|
||||
if (!mounted) return;
|
||||
showSnackbar(message: loc.error_deleting_game);
|
||||
}
|
||||
}
|
||||
});
|
||||
onPressed: () async {
|
||||
if (!context.mounted) return;
|
||||
|
||||
// Build the dialog content based on match count
|
||||
final String dialogContent = widget.matchCount > 0
|
||||
? loc.delete_game_with_matches_warning(widget.matchCount)
|
||||
: loc.this_cannot_be_undone;
|
||||
|
||||
showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => CustomAlertDialog(
|
||||
title: loc.delete_game,
|
||||
content: Text(dialogContent),
|
||||
actions: [
|
||||
CustomDialogAction(
|
||||
isDestructive: true,
|
||||
onPressed: () => Navigator.of(context).pop(true),
|
||||
text: loc.delete,
|
||||
),
|
||||
CustomDialogAction(
|
||||
onPressed: () => Navigator.of(context).pop(false),
|
||||
buttonType: ButtonType.secondary,
|
||||
text: loc.cancel,
|
||||
),
|
||||
],
|
||||
),
|
||||
).then((confirmed) async {
|
||||
if (confirmed == true && context.mounted) {
|
||||
// Delete assocaited matches
|
||||
if (widget.matchCount > 0) {
|
||||
await db.matchDao.deleteMatchesByGame(
|
||||
gameId: widget.gameToEdit!.id,
|
||||
);
|
||||
}
|
||||
: null,
|
||||
|
||||
// Delete the targetted game
|
||||
bool success = await db.gameDao.deleteGame(
|
||||
gameId: widget.gameToEdit!.id,
|
||||
);
|
||||
|
||||
if (!context.mounted) return;
|
||||
if (success) {
|
||||
widget.onGameChanged.call();
|
||||
Navigator.of(
|
||||
context,
|
||||
).pop((game: widget.gameToEdit, delete: true));
|
||||
} else {
|
||||
if (!mounted) return;
|
||||
showSnackbar(message: loc.error_deleting_game);
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
@@ -14,6 +14,7 @@ class AnimatedDialogButton extends StatefulWidget {
|
||||
required this.onPressed,
|
||||
this.buttonConstraints,
|
||||
this.buttonType = ButtonType.primary,
|
||||
this.isDescructive = false,
|
||||
});
|
||||
|
||||
final String buttonText;
|
||||
@@ -24,6 +25,8 @@ class AnimatedDialogButton extends StatefulWidget {
|
||||
|
||||
final ButtonType buttonType;
|
||||
|
||||
final bool isDescructive;
|
||||
|
||||
@override
|
||||
State<AnimatedDialogButton> createState() => _AnimatedDialogButtonState();
|
||||
}
|
||||
@@ -33,28 +36,8 @@ class _AnimatedDialogButtonState extends State<AnimatedDialogButton> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final textStyling = TextStyle(
|
||||
color: widget.buttonType == ButtonType.primary
|
||||
? Colors.black
|
||||
: Colors.white,
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
);
|
||||
|
||||
final buttonDecoration = widget.buttonType == ButtonType.primary
|
||||
// Primary
|
||||
? BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
)
|
||||
: widget.buttonType == ButtonType.secondary
|
||||
// Secondary
|
||||
? BoxDecoration(
|
||||
border: BoxBorder.all(color: Colors.white, width: 2),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
)
|
||||
// Tertiary
|
||||
: const BoxDecoration();
|
||||
final textStyling = _getTextStyling();
|
||||
final buttonDecoration = _getButtonDecoration();
|
||||
|
||||
return GestureDetector(
|
||||
onTapDown: (_) => setState(() => _isPressed = true),
|
||||
@@ -84,4 +67,42 @@ class _AnimatedDialogButtonState extends State<AnimatedDialogButton> {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
TextStyle _getTextStyling() {
|
||||
late Color textColor;
|
||||
if (widget.buttonType == ButtonType.primary) {
|
||||
textColor = widget.isDescructive ? Colors.white : Colors.black;
|
||||
} else if (widget.buttonType == ButtonType.secondary) {
|
||||
textColor = widget.isDescructive ? Colors.red : Colors.white;
|
||||
} else {
|
||||
textColor = widget.isDescructive ? Colors.red : Colors.white;
|
||||
}
|
||||
|
||||
return TextStyle(
|
||||
color: textColor,
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
);
|
||||
}
|
||||
|
||||
BoxDecoration _getButtonDecoration() {
|
||||
if (widget.buttonType == ButtonType.primary) {
|
||||
// Primary
|
||||
return BoxDecoration(
|
||||
color: widget.isDescructive ? Colors.red : Colors.white,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
);
|
||||
} else if (widget.buttonType == ButtonType.secondary) {
|
||||
// Secondary
|
||||
return BoxDecoration(
|
||||
border: BoxBorder.all(
|
||||
color: widget.isDescructive ? Colors.red : Colors.white,
|
||||
width: 2,
|
||||
),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
);
|
||||
}
|
||||
// Tertiary
|
||||
return const BoxDecoration();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ class CustomDialogAction extends StatelessWidget {
|
||||
required this.onPressed,
|
||||
required this.text,
|
||||
this.buttonType = ButtonType.primary,
|
||||
this.isDestructive = false,
|
||||
});
|
||||
|
||||
final String text;
|
||||
@@ -20,12 +21,15 @@ class CustomDialogAction extends StatelessWidget {
|
||||
|
||||
final VoidCallback onPressed;
|
||||
|
||||
final bool isDestructive;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AnimatedDialogButton(
|
||||
onPressed: onPressed,
|
||||
buttonText: text,
|
||||
buttonType: buttonType,
|
||||
isDescructive: isDestructive,
|
||||
buttonConstraints: const BoxConstraints(minWidth: 300),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user