From 7fed91e8112ec2b407892dbf3fcd822134a09fd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maty=C3=A1=C5=A1=20Caras?= Date: Mon, 22 Jan 2024 20:26:10 +0100 Subject: [PATCH] fix: replace platformdialog with adaptive dialog --- CHANGELOG.md | 1 + lib/pw/platformdialog.dart | 31 ----------- lib/views/home.dart | 32 ++++++----- lib/views/recurring_view.dart | 8 +-- lib/views/settings/edit_categories.dart | 17 +++--- lib/views/settings/graph_type.dart | 11 ++-- lib/views/settings/tessdata_list.dart | 16 +++--- lib/views/setup.dart | 74 ++++++++++++++----------- 8 files changed, 85 insertions(+), 105 deletions(-) delete mode 100644 lib/pw/platformdialog.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d1b1ff..3b77505 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ # 1.0.0-alpha+5 - Add tests - Add searching through entries to homepage +- Replace `PlatformDialog` with `AlertDialog.adaptive` # 1.0.0-alpha+4 - Fix OCR downloads # 1.0.0-alpha+3 diff --git a/lib/pw/platformdialog.dart b/lib/pw/platformdialog.dart deleted file mode 100644 index 070d6f6..0000000 --- a/lib/pw/platformdialog.dart +++ /dev/null @@ -1,31 +0,0 @@ -// ignore_for_file: public_member_api_docs - -import 'package:flutter/cupertino.dart'; -import 'package:flutter/material.dart'; -import 'package:prasule/pw/platformwidget.dart'; - -/// A [PlatformWidget] implementation of a dialog -class PlatformDialog extends PlatformWidget { - const PlatformDialog( - {required this.title, super.key, this.content, this.actions = const [],}); - final String title; - final Widget? content; - final List actions; - - @override - AlertDialog createAndroidWidget(BuildContext context) => AlertDialog( - title: Text(title), - content: - (content != null) ? SingleChildScrollView(child: content) : null, - actions: actions, - ); - - @override - CupertinoAlertDialog createIosWidget(BuildContext context) => - CupertinoAlertDialog( - title: Text(title), - content: - (content != null) ? SingleChildScrollView(child: content) : null, - actions: actions, - ); -} diff --git a/lib/views/home.dart b/lib/views/home.dart index 7223ccc..d5a13fc 100644 --- a/lib/views/home.dart +++ b/lib/views/home.dart @@ -21,7 +21,6 @@ import 'package:prasule/api/wallet_manager.dart'; import 'package:prasule/main.dart'; import 'package:prasule/network/tessdata.dart'; import 'package:prasule/pw/platformbutton.dart'; -import 'package:prasule/pw/platformdialog.dart'; import 'package:prasule/pw/platformfield.dart'; import 'package:prasule/pw/platformroute.dart'; import 'package:prasule/util/drawer.dart'; @@ -448,12 +447,15 @@ class _HomeViewState extends State { .onError, icon: Icons.delete, onPressed: (c) { - showDialog( + showAdaptiveDialog( context: context, - builder: (cx) => PlatformDialog( - title: AppLocalizations.of( - context, - ).sureDialog, + builder: (cx) => + AlertDialog.adaptive( + title: Text( + AppLocalizations.of( + context, + ).sureDialog, + ), content: Text( AppLocalizations.of(context) .deleteSure, @@ -595,10 +597,10 @@ class _HomeViewState extends State { final availableLanguages = await TessdataApi.getDownloadedData(); if (availableLanguages.isEmpty) { if (!mounted) return; - await showDialog( + await showAdaptiveDialog( context: context, - builder: (c) => PlatformDialog( - title: AppLocalizations.of(context).missingOcr, + builder: (c) => AlertDialog.adaptive( + title: Text(AppLocalizations.of(context).missingOcr), actions: [ PlatformButton( text: AppLocalizations.of(context).download, @@ -628,10 +630,10 @@ class _HomeViewState extends State { List.filled(availableLanguages.length, false); selectedLanguages[0] = true; - await showDialog( + await showAdaptiveDialog( context: context, builder: (c) => StatefulBuilder( - builder: (ctx, setState) => PlatformDialog( + builder: (ctx, setState) => AlertDialog.adaptive( actions: [ TextButton( onPressed: () async { @@ -652,10 +654,10 @@ class _HomeViewState extends State { logger.i(selected); if (!mounted) return; unawaited( - showDialog( + showAdaptiveDialog( context: context, - builder: (c) => PlatformDialog( - title: AppLocalizations.of(context).ocrLoading, + builder: (c) => AlertDialog.adaptive( + title: Text(AppLocalizations.of(context).ocrLoading), ), barrierDismissible: false, ), @@ -723,7 +725,7 @@ class _HomeViewState extends State { child: const Text("Cancel"), ), ], - title: AppLocalizations.of(context).ocrSelect, + title: Text(AppLocalizations.of(context).ocrSelect), content: Column( children: [ ...List.generate( diff --git a/lib/views/recurring_view.dart b/lib/views/recurring_view.dart index 49bc3e3..ca1797d 100644 --- a/lib/views/recurring_view.dart +++ b/lib/views/recurring_view.dart @@ -11,7 +11,6 @@ import 'package:prasule/api/recurring_entry.dart'; import 'package:prasule/api/wallet.dart'; import 'package:prasule/api/wallet_manager.dart'; import 'package:prasule/pw/platformbutton.dart'; -import 'package:prasule/pw/platformdialog.dart'; import 'package:prasule/pw/platformroute.dart'; import 'package:prasule/util/drawer.dart'; import 'package:prasule/util/text_color.dart'; @@ -215,9 +214,10 @@ class _RecurringEntriesViewState extends State { onPressed: (c) { showDialog( context: context, - builder: (cx) => PlatformDialog( - title: - AppLocalizations.of(context).sureDialog, + builder: (cx) => AlertDialog.adaptive( + title: Text( + AppLocalizations.of(context).sureDialog, + ), content: Text( AppLocalizations.of(context).deleteSure, ), diff --git a/lib/views/settings/edit_categories.dart b/lib/views/settings/edit_categories.dart index 757c87b..22c5a94 100644 --- a/lib/views/settings/edit_categories.dart +++ b/lib/views/settings/edit_categories.dart @@ -12,7 +12,6 @@ import 'package:prasule/api/wallet.dart'; import 'package:prasule/api/wallet_manager.dart'; import 'package:prasule/main.dart'; import 'package:prasule/pw/platformbutton.dart'; -import 'package:prasule/pw/platformdialog.dart'; import 'package:prasule/pw/platformfield.dart'; import 'package:prasule/pw/platformroute.dart'; import 'package:prasule/util/text_color.dart'; @@ -147,9 +146,9 @@ class _EditCategoriesViewState extends State { .getBool("useMaterialYou") ?? false; if (!context.mounted) return; - await showDialog( + await showAdaptiveDialog( context: context, - builder: (c) => PlatformDialog( + builder: (c) => AlertDialog.adaptive( actions: [ PlatformButton( text: AppLocalizations.of(context).done, @@ -158,8 +157,8 @@ class _EditCategoriesViewState extends State { }, ), ], - title: - AppLocalizations.of(context).pickColor, + title: Text( + AppLocalizations.of(context).pickColor), content: Column( children: [ ColorPicker( @@ -215,9 +214,9 @@ class _EditCategoriesViewState extends State { final controller = TextEditingController( text: selectedWallet!.categories[i].name, ); - showDialog( + showAdaptiveDialog( context: context, - builder: (c) => PlatformDialog( + builder: (c) => AlertDialog.adaptive( actions: [ TextButton( onPressed: () async { @@ -243,8 +242,8 @@ class _EditCategoriesViewState extends State { ), ), ], - title: AppLocalizations.of(context) - .setupCategoriesEditingName, + title: Text(AppLocalizations.of(context) + .setupCategoriesEditingName), content: SizedBox( width: 400, child: diff --git a/lib/views/settings/graph_type.dart b/lib/views/settings/graph_type.dart index 2d7c48b..404f055 100644 --- a/lib/views/settings/graph_type.dart +++ b/lib/views/settings/graph_type.dart @@ -2,7 +2,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; -import 'package:prasule/pw/platformdialog.dart'; import 'package:settings_ui/settings_ui.dart'; import 'package:shared_preferences/shared_preferences.dart'; @@ -50,10 +49,10 @@ class _GraphTypeSettingsViewState extends State { ? AppLocalizations.of(context).barChart : AppLocalizations.of(context).lineChart, ), - onPressed: (c) => showDialog( + onPressed: (c) => showAdaptiveDialog( context: c, - builder: (ctx) => PlatformDialog( - title: AppLocalizations.of(context).selectType, + builder: (ctx) => AlertDialog.adaptive( + title: Text(AppLocalizations.of(context).selectType), content: Column( children: [ SizedBox( @@ -110,8 +109,8 @@ class _GraphTypeSettingsViewState extends State { ), onPressed: (c) => showDialog( context: c, - builder: (ctx) => PlatformDialog( - title: AppLocalizations.of(context).selectType, + builder: (ctx) => AlertDialog.adaptive( + title: Text(AppLocalizations.of(context).selectType), content: Column( children: [ SizedBox( diff --git a/lib/views/settings/tessdata_list.dart b/lib/views/settings/tessdata_list.dart index 855202c..99382d3 100644 --- a/lib/views/settings/tessdata_list.dart +++ b/lib/views/settings/tessdata_list.dart @@ -9,7 +9,6 @@ import 'package:flutter_tesseract_ocr/flutter_tesseract_ocr.dart'; import 'package:prasule/main.dart'; import 'package:prasule/network/tessdata.dart'; import 'package:prasule/pw/platformbutton.dart'; -import 'package:prasule/pw/platformdialog.dart'; /// Used to manage downloaded Tessdata for OCR class TessdataListView extends StatefulWidget { @@ -62,10 +61,11 @@ class _TessdataListViewState extends State { final lang = _tessdata[i].keys.first; if (_tessdata[i][lang]!) { // deleting data - await showDialog( + await showAdaptiveDialog( context: context, - builder: (context) => PlatformDialog( - title: AppLocalizations.of(context).sureDialog, + builder: (context) => AlertDialog.adaptive( + title: + Text(AppLocalizations.of(context).sureDialog), content: Text( AppLocalizations.of(context).deleteOcr(lang), ), @@ -98,11 +98,11 @@ class _TessdataListViewState extends State { final progressStream = StreamController(); unawaited( - showDialog( + showAdaptiveDialog( context: context, - builder: (c) => PlatformDialog( - title: AppLocalizations.of(context) - .langDownloadDialog(lang), + builder: (c) => AlertDialog.adaptive( + title: Text(AppLocalizations.of(context) + .langDownloadDialog(lang)), content: StreamBuilder( builder: (context, snapshot) { if (snapshot.connectionState == diff --git a/lib/views/setup.dart b/lib/views/setup.dart index cde8424..f090ad3 100644 --- a/lib/views/setup.dart +++ b/lib/views/setup.dart @@ -1,5 +1,7 @@ // ignore_for_file: inference_failure_on_function_invocation +import 'dart:async'; + import 'package:currency_picker/currency_picker.dart'; import 'package:dynamic_color/dynamic_color.dart'; import 'package:flex_color_picker/flex_color_picker.dart'; @@ -12,7 +14,6 @@ import 'package:prasule/api/category.dart'; import 'package:prasule/api/wallet.dart'; import 'package:prasule/api/wallet_manager.dart'; import 'package:prasule/pw/platformbutton.dart'; -import 'package:prasule/pw/platformdialog.dart'; import 'package:prasule/pw/platformfield.dart'; import 'package:prasule/pw/platformroute.dart'; import 'package:prasule/util/show_message.dart'; @@ -118,11 +119,22 @@ class _SetupViewState extends State { next: Text(AppLocalizations.of(context).next), back: Text(AppLocalizations.of(context).back), done: Text(AppLocalizations.of(context).finish), - onDone: () { + onDone: () async { if (name.isEmpty) { - showMessage( - AppLocalizations.of(context).errorEmptyName, - context, + unawaited( + showMessage( + AppLocalizations.of(context).errorEmptyName, + context, + ), + ); + return; + } + if (await WalletManager.exists(name) && context.mounted) { + unawaited( + showMessage( + AppLocalizations.of(context).walletExists, + context, + ), ); return; } @@ -131,25 +143,19 @@ class _SetupViewState extends State { currency: _selectedCurrency, categories: categories, ); - WalletManager.saveWallet(wallet).then( - (value) { - if (!value) { - showMessage( - AppLocalizations.of(context).walletExists, - context, - ); - return; - } - if (widget.newWallet) { - Navigator.of(context).pop(); - return; - } - Navigator.of(context).pushReplacement( - platformRoute( - (c) => const HomeView(), - ), - ); - }, + await WalletManager.saveWallet(wallet); + + if (widget.newWallet && context.mounted) { + Navigator.of(context).pop(); + return; + } + if (!context.mounted) return; + unawaited( + Navigator.of(context).pushReplacement( + platformRoute( + (c) => const HomeView(), + ), + ), ); }, pages: [ @@ -298,9 +304,9 @@ class _SetupViewState extends State { .getBool("useMaterialYou") ?? false; if (!context.mounted) return; - await showDialog( + await showAdaptiveDialog( context: context, - builder: (c) => PlatformDialog( + builder: (c) => AlertDialog.adaptive( actions: [ PlatformButton( text: AppLocalizations.of(context) @@ -310,8 +316,10 @@ class _SetupViewState extends State { }, ), ], - title: AppLocalizations.of(context) - .pickColor, + title: Text( + AppLocalizations.of(context) + .pickColor, + ), content: Column( children: [ ColorPicker( @@ -363,9 +371,9 @@ class _SetupViewState extends State { final controller = TextEditingController( text: categories[i].name, ); - showDialog( + showAdaptiveDialog( context: context, - builder: (c) => PlatformDialog( + builder: (c) => AlertDialog.adaptive( actions: [ TextButton( onPressed: () { @@ -390,8 +398,10 @@ class _SetupViewState extends State { ), ), ], - title: AppLocalizations.of(context) - .setupCategoriesEditingName, + title: Text( + AppLocalizations.of(context) + .setupCategoriesEditingName, + ), content: SizedBox( width: 400, child: PlatformField(