|
| 1 | +import "package:flutter/material.dart"; |
| 2 | +import "package:gitdone/core/settings_handler.dart"; |
| 3 | +import "package:gitdone/core/task_handler.dart"; |
| 4 | +import "package:gitdone/ui/_widgets/confirm_dialog.dart"; |
| 5 | + |
| 6 | +/// Shows a confirmation dialog for marking a task as done or open. |
| 7 | +/// |
| 8 | +/// If the "don't ask again" setting is enabled, it will execute [onConfirm] immediately. |
| 9 | +Future<void> showMarkTaskConfirmationDialog({ |
| 10 | + required final BuildContext context, |
| 11 | + required final String currentTaskState, |
| 12 | + required final VoidCallback onConfirm, |
| 13 | +}) async { |
| 14 | + final _DialogTexts texts = currentTaskState == IssueState.open.value |
| 15 | + ? ( |
| 16 | + title: "Mark as done", |
| 17 | + description: |
| 18 | + "Are you sure you want to mark this task as done? This will close the GitHub issue.", |
| 19 | + confirmText: "Mark as done", |
| 20 | + ) |
| 21 | + : ( |
| 22 | + title: "Reopen task", |
| 23 | + description: "Are you sure you want to reopen this task?", |
| 24 | + confirmText: "Reopen", |
| 25 | + ); |
| 26 | + |
| 27 | + if (!SettingsHandler().showMarkTaskStateConfirmation()) { |
| 28 | + onConfirm(); |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + bool doNotAskAgain = false; |
| 33 | + await showDialog( |
| 34 | + context: context, |
| 35 | + builder: (final context) => StatefulBuilder( |
| 36 | + builder: (final context, final setState) => ConfirmDialog( |
| 37 | + title: Text(texts.title), |
| 38 | + content: _buildDialogContent( |
| 39 | + description: texts.description, |
| 40 | + doNotAskAgain: doNotAskAgain, |
| 41 | + onChanged: (final value) => setState(() => doNotAskAgain = value), |
| 42 | + ), |
| 43 | + confirmText: texts.confirmText, |
| 44 | + onConfirm: () async { |
| 45 | + if (doNotAskAgain) { |
| 46 | + await SettingsHandler().setShowMarkTaskStateConfirmation( |
| 47 | + value: false, |
| 48 | + ); |
| 49 | + } |
| 50 | + onConfirm(); |
| 51 | + }, |
| 52 | + cancelText: "Cancel", |
| 53 | + ), |
| 54 | + ), |
| 55 | + ); |
| 56 | +} |
| 57 | + |
| 58 | +Widget _buildDialogContent({ |
| 59 | + required final String description, |
| 60 | + required final bool doNotAskAgain, |
| 61 | + required final ValueChanged<bool> onChanged, |
| 62 | +}) => Column( |
| 63 | + mainAxisSize: MainAxisSize.min, |
| 64 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 65 | + children: [ |
| 66 | + Text(description), |
| 67 | + Row( |
| 68 | + children: [ |
| 69 | + Checkbox( |
| 70 | + value: doNotAskAgain, |
| 71 | + onChanged: (final value) => onChanged(value ?? false), |
| 72 | + ), |
| 73 | + const Text("Don't ask me again"), |
| 74 | + ], |
| 75 | + ), |
| 76 | + ], |
| 77 | +); |
| 78 | + |
| 79 | +typedef _DialogTexts = ({String title, String description, String confirmText}); |
0 commit comments