From 8f684d73ac21e63376614c40f432dff02bb296ce Mon Sep 17 00:00:00 2001 From: Mirus Date: Tue, 12 Dec 2023 14:31:55 +0800 Subject: [PATCH] Reduce redundant code about confirm dialog --- lib/libs/misc.dart | 25 ++++++++++ lib/pages/construct/formative_editor.dart | 51 +++++--------------- lib/pages/search.dart | 59 +++++++++-------------- 3 files changed, 60 insertions(+), 75 deletions(-) diff --git a/lib/libs/misc.dart b/lib/libs/misc.dart index 22e8351..9e57e4d 100644 --- a/lib/libs/misc.dart +++ b/lib/libs/misc.dart @@ -44,6 +44,31 @@ Future showErrorDialog(BuildContext context, String title) async { ); } +Future showConfirmDialog(BuildContext context, String title) async { + final res = await showDialog( + context: context, + barrierDismissible: false, + builder: (dialogContext) => AlertDialog( + title: const Text("Confirm"), + content: Text(title), + actions: [ + TextButton( + onPressed: () => Navigator.pop(dialogContext, false), + child: const Text("Cancel"), + ), + TextButton( + onPressed: () => Navigator.pop(dialogContext, true), + child: const Text("Ok"), + ) + ], + ), + ); + if (res == null) { + return false; + } + return res; +} + String escapeRegExp(String text) { return text.replaceAllMapped(RegExp(r"[-[\]{}()*+?.,\\^$|#\s]"), (m) => "\\${m[0]}"); } diff --git a/lib/pages/construct/formative_editor.dart b/lib/pages/construct/formative_editor.dart index 436a836..5cbbb42 100644 --- a/lib/pages/construct/formative_editor.dart +++ b/lib/pages/construct/formative_editor.dart @@ -1988,27 +1988,11 @@ class _FormativeEditorState extends State with TickerProviderSt ), direction: DismissDirection.endToStart, confirmDismiss: (direction) async { - final res = await showDialog( - context: context, - builder: (dialogContext) => AlertDialog( - title: const Text("Confirm"), - content: const Text("Are you sure to delete this affix?"), - actions: [ - TextButton( - onPressed: () => Navigator.pop(dialogContext, false), - child: const Text("Cancel"), - ), - TextButton( - onPressed: () => Navigator.pop(dialogContext, true), - child: const Text("Ok"), - ) - ], - ), + final isConfirmed = await showConfirmDialog( + context, + "Are you sure to delete this affix?", ); - if (res == null) { - return false; - } - return res; + return isConfirmed; }, onDismissed: (direction) { widget.updateFormative((f) { @@ -2122,27 +2106,14 @@ class _FormativeEditorState extends State with TickerProviderSt leading: const Icon(Icons.delete), title: const Text("Delete"), subtitle: const Text("Delete this formative"), - onTap: () { - showDialog( - context: context, - builder: (BuildContext dialogContext) => AlertDialog( - title: const Text("Confirm"), - content: const Text("Are you sure to remove this formative?"), - actions: [ - TextButton( - onPressed: () => Navigator.pop(dialogContext), - child: const Text("Cancel"), - ), - TextButton( - onPressed: () async { - Navigator.pop(dialogContext); - widget.removeFormative(); - }, - child: const Text("Ok"), - ), - ], - ), + onTap: () async { + final isConfirmed = await showConfirmDialog( + context, + "Are you sure to remove this formative?", ); + if (isConfirmed) { + widget.removeFormative(); + } }, tileColor: Theme.of(context).colorScheme.errorContainer, ), diff --git a/lib/pages/search.dart b/lib/pages/search.dart index 9dc78f4..f684465 100644 --- a/lib/pages/search.dart +++ b/lib/pages/search.dart @@ -2,11 +2,12 @@ import 'dart:convert'; import 'package:file_picker/file_picker.dart'; import 'package:flutter/material.dart'; -import 'package:enthrirhs/pages/root.dart'; import 'package:provider/provider.dart'; -import 'package:enthrirhs/utils/store.dart'; -import '../utils/types.dart'; +import 'package:enthrirhs/libs/misc.dart'; +import 'package:enthrirhs/pages/root.dart'; +import 'package:enthrirhs/utils/store.dart'; +import 'package:enthrirhs/utils/types.dart'; class LexiconActionButton extends StatelessWidget { final IconData icon; @@ -217,47 +218,35 @@ class _SearchPageState extends State { ), const SizedBox(height: 8), LexiconActionButton( - icon: Icons.delete_forever, - label: "Clear the lexicon", - onPressed: () { + icon: Icons.delete_forever, + label: "Clear the lexicon", + onPressed: () async { + final isConfirmed = await showConfirmDialog( + context, + "Are you sure to clear all the roots and affixes from your device?", + ); + + if (isConfirmed) { + if (!context.mounted) return; + final count = await context.read().database.clearRoots(); + setState(() => rootCount -= count); + if (!context.mounted) return; showDialog( context: context, - builder: (BuildContext dialogContext) => AlertDialog( - title: const Text("Warning"), - content: const Text( - "Are you sure to clear all the roots and affixes from your device?"), + builder: (BuildContext context) => AlertDialog( + title: const Text("Success"), + content: Text("$count roots have been cleared."), actions: [ TextButton( - onPressed: () => Navigator.pop(dialogContext), - child: const Text("Cancel"), - ), - TextButton( - onPressed: () async { - Navigator.pop(dialogContext); - final count = - await context.read().database.clearRoots(); - setState(() => rootCount -= count); - if (!context.mounted) return; - showDialog( - context: context, - builder: (BuildContext context) => AlertDialog( - title: const Text("Success"), - content: Text("$count roots have been cleared."), - actions: [ - TextButton( - onPressed: () => Navigator.pop(context), - child: const Text("Ok"), - ), - ], - ), - ); - }, + onPressed: () => Navigator.pop(context), child: const Text("Ok"), ), ], ), ); - }), + } + }, + ), ], ), ),