From 02a5749ba7bb6b1c6ab1d9f26ec55ac8415563b6 Mon Sep 17 00:00:00 2001 From: Imran Remtulla Date: Sat, 17 Sep 2022 00:09:46 -0400 Subject: [PATCH] Removed redundant code --- lib/components/generated_form_modal.dart | 5 +- lib/pages/import_export.dart | 94 +++++++----------------- 2 files changed, 29 insertions(+), 70 deletions(-) diff --git a/lib/components/generated_form_modal.dart b/lib/components/generated_form_modal.dart index 9b15e67..c45ea4b 100644 --- a/lib/components/generated_form_modal.dart +++ b/lib/components/generated_form_modal.dart @@ -4,8 +4,9 @@ import 'package:flutter/services.dart'; class GeneratedFormItem { late String message; late bool required; + late int lines; - GeneratedFormItem(this.message, this.required); + GeneratedFormItem(this.message, this.required, this.lines); } class GeneratedFormModal extends StatefulWidget { @@ -33,6 +34,8 @@ class _GeneratedFormModalState extends State { TextFormField( decoration: InputDecoration(helperText: e.message), controller: controller, + minLines: e.lines <= 1 ? null : e.lines, + maxLines: e.lines <= 1 ? 1 : e.lines, validator: e.required ? (value) { if (value == null || value.isEmpty) { diff --git a/lib/pages/import_export.dart b/lib/pages/import_export.dart index f878dc9..7cf3830 100644 --- a/lib/pages/import_export.dart +++ b/lib/pages/import_export.dart @@ -73,76 +73,32 @@ class _ImportExportPageState extends State { showDialog( context: context, builder: (BuildContext ctx) { - final formKey = GlobalKey(); - final jsonInputController = TextEditingController(); - - return AlertDialog( - scrollable: true, - title: const Text('Import App List'), - content: Column(children: [ - const Text( - 'Copy the contents of the Obtainium export file and paste them into the field below:'), - Form( - key: formKey, - child: TextFormField( - minLines: 7, - maxLines: 7, - decoration: const InputDecoration( - helperText: 'Obtainium export data'), - controller: jsonInputController, - validator: (value) { - if (value == null || value.isEmpty) { - return 'Please enter your Obtainium export data'; - } - bool isJSON = true; - try { - jsonDecode(value); - } catch (e) { - isJSON = false; - } - if (!isJSON) { - return 'Invalid input'; - } - return null; - }, - ), - ) - ]), - actions: [ - TextButton( - onPressed: () { - HapticFeedback.lightImpact(); - Navigator.of(context).pop(); - }, - child: const Text('Cancel')), - TextButton( - onPressed: () { - HapticFeedback.heavyImpact(); - if (formKey.currentState!.validate()) { - appsProvider - .importApps( - jsonInputController.value.text) - .then((value) { - ScaffoldMessenger.of(context) - .showSnackBar( - SnackBar( - content: Text( - '$value App${value == 1 ? '' : 's'} Imported')), - ); - }).catchError((e) { - ScaffoldMessenger.of(context) - .showSnackBar( - SnackBar(content: Text(e.toString())), - ); - }).whenComplete(() { - Navigator.of(context).pop(); - }); - } - }, - child: const Text('Import')), - ], + return GeneratedFormModal( + title: 'Obtainium Import', + items: [ + GeneratedFormItem( + 'Obtainium Export JSON Data', true, 7) + ]); + }).then((values) { + if (values != null) { + try { + jsonDecode(values[0]); + } catch (e) { + throw 'Invalid input'; + } + appsProvider.importApps(values[0]).then((value) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + '$value App${value == 1 ? '' : 's'} Imported')), ); }); + } + }).catchError((e) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text(e.toString())), + ); + }); }, child: const Text('Obtainium Import')), const Divider( @@ -157,7 +113,7 @@ class _ImportExportPageState extends State { return GeneratedFormModal( title: 'Import ${source.name}', items: source.requiredArgs - .map((e) => GeneratedFormItem(e, true)) + .map((e) => GeneratedFormItem(e, true, 1)) .toList()); }).then((values) { if (values != null) {