From 95cedc3a756bd762a5a7492c2c911bd24d39310f Mon Sep 17 00:00:00 2001
From: vjrj <vjrj@comunes.org>
Date: Wed, 2 Aug 2023 22:37:00 +0200
Subject: [PATCH] Import from clipboard

---
 assets/translations/en.json                   | 11 +++-
 assets/translations/es.json                   | 13 ++++-
 lib/ui/screens/fifth_screen.dart              | 57 +++++++++++++++++--
 .../widgets/fifth_screen/import_dialog.dart   |  8 ++-
 4 files changed, 78 insertions(+), 11 deletions(-)

diff --git a/assets/translations/en.json b/assets/translations/en.json
index 0cca1e45..7d390b09 100644
--- a/assets/translations/en.json
+++ b/assets/translations/en.json
@@ -200,5 +200,14 @@
   "share_export_desc": "Your wallet has been exported locally. Would you like to additionally share it with yourself via email/chat/etc. for safekeeping?",
   "share_export_subject": "My Äž1nkgo Wallet",
   "share_export_button": "SHARE",
-  "pay_with_nfc_tooltip": "To receive a payment, simply bring this device close to the other wallet with NFC activated."
+  "pay_with_nfc_tooltip": "To receive a payment, simply bring this device close to the other wallet with NFC activated.",
+  "import_wallet_from_clipboard": "Import Wallet from Clipboard",
+  "import_wallet_from_clipboard_desc": "Please paste a previously exported wallet text to import it",
+  "paste_here": "Paste here your wallet",
+  "paste": "PASTE",
+  "import": "IMPORT",
+  "select_import_method": "Select Import Method",
+  "select_import_method_desc": "Please choose where to import the text from",
+  "file_import": "Import from File",
+  "clipboard_import": "Import from Clipboard"
 }
diff --git a/assets/translations/es.json b/assets/translations/es.json
index 872bd12a..977a729c 100644
--- a/assets/translations/es.json
+++ b/assets/translations/es.json
@@ -7,7 +7,7 @@
   "bottom_nav_second": "Recibir",
   "bottom_nav_trd": "Contactos",
   "bottom_nav_frd": "Saldo",
-  "bottom_nav_fifth": "Información",
+  "bottom_nav_fifth": "Info",
   "send_g1": "Enviar Äž1",
   "g1_amount": "Monto a enviar",
   "g1_amount_hint": "Monto a enviar en {currency}",
@@ -202,5 +202,14 @@
   "share_export_desc": "Tu monedero ha sido exportado localmente. ¿Te gustaría compartirlo adicionalmente contigo mismo vía email/chat/etc. para su resguardo?",
   "share_export_subject": "Mi monedero Äž1nkgo",
   "share_export_button": "COMPARTIR",
-  "pay_with_nfc_tooltip": "Para recibir un pago, simplemente acerca este dispositivo al otro monedero con NFC activado."
+  "pay_with_nfc_tooltip": "Para recibir un pago, simplemente acerca este dispositivo al otro monedero con NFC activado.",
+  "import_wallet_from_clipboard": "Importar monedero desde el portapapeles",
+  "import_wallet_from_clipboard_desc": "Por favor, pega aquí el textod del backup previo de tu monedero",
+  "paste_here": "Pega aquí tu monedero",
+  "paste": "PEGAR",
+  "import": "IMPORTAR",
+  "select_import_method": "Selecciona el método de importación",
+  "select_import_method_desc": "Por favor, selecciona el método de importación de tu monedero",
+  "file_import": "Importar desde archivo",
+  "clipboard_import": "Importar desde portapapeles"
 }
diff --git a/lib/ui/screens/fifth_screen.dart b/lib/ui/screens/fifth_screen.dart
index 5a75b11c..5ccd2aa4 100644
--- a/lib/ui/screens/fifth_screen.dart
+++ b/lib/ui/screens/fifth_screen.dart
@@ -19,6 +19,7 @@ import '../widgets/faq.dart';
 import '../widgets/fifth_screen/export_dialog.dart';
 import '../widgets/fifth_screen/fifth_tutorial.dart';
 import '../widgets/fifth_screen/grid_item.dart';
+import '../widgets/fifth_screen/import_clipboard_dialog.dart';
 import '../widgets/fifth_screen/import_dialog.dart';
 import '../widgets/fifth_screen/link_card.dart';
 import '../widgets/fifth_screen/node_info.dart';
@@ -190,12 +191,7 @@ class _FifthScreenState extends State<FifthScreen> {
                               title: 'import_key',
                               icon: Icons.upload,
                               onTap: () {
-                                showDialog(
-                                  context: context,
-                                  builder: (BuildContext context) {
-                                    return const ImportDialog();
-                                  },
-                                );
+                                _showSelectImportMethodDialog();
                               }),
                         ]),
                     const TextDivider(text: 'faq_title'),
@@ -235,4 +231,53 @@ class _FifthScreenState extends State<FifthScreen> {
                   ]),
             ));
   }
+
+  Future<void> _showSelectImportMethodDialog() async {
+    final String? method = await showDialog<String>(
+      context: context,
+      builder: (BuildContext context) => const SelectImportMethodDialog(),
+    );
+    if (method != null) {
+      if (!mounted) {
+        return;
+      }
+      showDialog(
+        context: context,
+        builder: (BuildContext context) {
+          if (method == 'file') {
+            return const ImportDialog();
+          } else {
+            // if (method == 'clipboard') {
+            return ImportClipboardDialog(onImport: (String wallet) {
+              showDialog(
+                  context: context,
+                  builder: (BuildContext context) {
+                    return ImportDialog(wallet: wallet);
+                  });
+            });
+          }
+        },
+      );
+    }
+  }
+}
+
+class SelectImportMethodDialog extends StatelessWidget {
+  const SelectImportMethodDialog({super.key});
+
+  @override
+  Widget build(BuildContext context) {
+    return AlertDialog(
+      title: Text(tr('select_import_method')),
+      content: Text(tr('select_import_method_desc')),
+      actions: <Widget>[
+        TextButton(
+            child: Text(tr('file_import')),
+            onPressed: () => Navigator.of(context).pop('file')),
+        TextButton(
+            child: Text(tr('clipboard_import')),
+            onPressed: () => Navigator.of(context).pop('clipboard')),
+      ],
+    );
+  }
 }
diff --git a/lib/ui/widgets/fifth_screen/import_dialog.dart b/lib/ui/widgets/fifth_screen/import_dialog.dart
index 408b220e..a0f6e6cf 100644
--- a/lib/ui/widgets/fifth_screen/import_dialog.dart
+++ b/lib/ui/widgets/fifth_screen/import_dialog.dart
@@ -20,7 +20,9 @@ import '../custom_error_widget.dart';
 import 'pattern_util.dart';
 
 class ImportDialog extends StatefulWidget {
-  const ImportDialog({super.key});
+  const ImportDialog({super.key, this.wallet});
+
+  final String? wallet;
 
   @override
   State<ImportDialog> createState() => _ImportDialogState();
@@ -32,7 +34,9 @@ class _ImportDialogState extends State<ImportDialog> {
   @override
   Widget build(BuildContext c) {
     return FutureBuilder<String>(
-        future: kIsWeb ? _importWalletWeb(c) : _importWallet(c),
+        future: widget.wallet == null
+            ? (kIsWeb ? _importWalletWeb(c) : _importWallet(c))
+            : Future<String>.value(widget.wallet),
         builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
           if (snapshot.hasData &&
               snapshot.data != null &&
-- 
GitLab