Skip to content

Commit

Permalink
Reduce redundant code about confirm dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
lomirus committed Dec 12, 2023
1 parent 2472585 commit 8f684d7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 75 deletions.
25 changes: 25 additions & 0 deletions lib/libs/misc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,31 @@ Future<void> showErrorDialog(BuildContext context, String title) async {
);
}

Future<bool> showConfirmDialog(BuildContext context, String title) async {
final res = await showDialog<bool>(
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]}");
}
Expand Down
51 changes: 11 additions & 40 deletions lib/pages/construct/formative_editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1988,27 +1988,11 @@ class _FormativeEditorState extends State<FormativeEditor> with TickerProviderSt
),
direction: DismissDirection.endToStart,
confirmDismiss: (direction) async {
final res = await showDialog<bool>(
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) {
Expand Down Expand Up @@ -2122,27 +2106,14 @@ class _FormativeEditorState extends State<FormativeEditor> 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,
),
Expand Down
59 changes: 24 additions & 35 deletions lib/pages/search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -217,47 +218,35 @@ class _SearchPageState extends State<SearchPage> {
),
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<LexiconModel>().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<LexiconModel>().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"),
),
],
),
);
}),
}
},
),
],
),
),
Expand Down

0 comments on commit 8f684d7

Please sign in to comment.