]> git.ktnx.net Git - mobile-ledger.git/commitdiff
extract QR-scanning machinery from new transaction fragment
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 29 Jan 2021 05:19:50 +0000 (05:19 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 29 Jan 2021 11:27:04 +0000 (11:27 +0000)
app/src/main/java/net/ktnx/mobileledger/ui/QR.java [new file with mode: 0644]
app/src/main/java/net/ktnx/mobileledger/ui/QRScanAbleFragment.java [new file with mode: 0644]
app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionFragment.java

diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/QR.java b/app/src/main/java/net/ktnx/mobileledger/ui/QR.java
new file mode 100644 (file)
index 0000000..9735a6f
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * Copyright © 2021 Damyan Ivanov.
+ * This file is part of MoLe.
+ * MoLe is free software: you can distribute it and/or modify it
+ * under the term of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your opinion), any later version.
+ *
+ * MoLe is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License terms for details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package net.ktnx.mobileledger.ui;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.Intent;
+
+import androidx.activity.result.ActivityResultCaller;
+import androidx.activity.result.ActivityResultLauncher;
+import androidx.activity.result.contract.ActivityResultContract;
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+
+public class QR {
+    private static final String SCAN_APP_NAME = "com.google.zxing.client.android.SCAN";
+    public static ActivityResultLauncher<Void> registerLauncher(ActivityResultCaller activity,
+                                                                QRScanResultReceiver resultReceiver) {
+        return activity.registerForActivityResult(new ActivityResultContract<Void, String>() {
+            @NonNull
+            @Override
+            public Intent createIntent(@NonNull Context context, Void input) {
+                final Intent intent = new Intent(SCAN_APP_NAME);
+                intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
+                return intent;
+            }
+            @Override
+            public String parseResult(int resultCode, @Nullable Intent intent) {
+                if (resultCode == Activity.RESULT_CANCELED || intent == null)
+                    return null;
+                return intent.getStringExtra("SCAN_RESULT");
+            }
+        }, resultReceiver::onQRScanResult);
+    }
+    public interface QRScanResultReceiver {
+        void onQRScanResult(String scanned);
+    }
+}
diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/QRScanAbleFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/QRScanAbleFragment.java
new file mode 100644 (file)
index 0000000..6acb449
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright © 2021 Damyan Ivanov.
+ * This file is part of MoLe.
+ * MoLe is free software: you can distribute it and/or modify it
+ * under the term of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your opinion), any later version.
+ *
+ * MoLe is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License terms for details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package net.ktnx.mobileledger.ui;
+
+import android.content.Context;
+
+import androidx.activity.result.ActivityResultLauncher;
+import androidx.annotation.NonNull;
+import androidx.fragment.app.Fragment;
+import androidx.lifecycle.MutableLiveData;
+
+public abstract class QRScanAbleFragment extends Fragment {
+    private static final MutableLiveData<Integer> qrScanTrigger = new MutableLiveData<>();
+    protected final ActivityResultLauncher<Void> scanQrLauncher = QR.registerLauncher(this, this::onQrScanned);
+    public static void triggerQRScan() {
+        qrScanTrigger.setValue(1);
+    }
+    protected abstract void onQrScanned(String text);
+    @Override
+    public void onAttach(@NonNull Context context) {
+        super.onAttach(context);
+        qrScanTrigger.observe(this, ignored -> {
+            scanQrLauncher.launch(null);
+        });
+    }
+}
index aa44c72ff8f2d3f9674904cf7125e0566170a0b8..d27de40a80503ed2e1b7c20fdb730ba383c5a9f8 100644 (file)
@@ -17,9 +17,7 @@
 
 package net.ktnx.mobileledger.ui.new_transaction;
 
-import android.app.Activity;
 import android.content.Context;
-import android.content.Intent;
 import android.content.res.Resources;
 import android.os.Bundle;
 import android.renderscript.RSInvalidStateException;
@@ -31,8 +29,6 @@ import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ProgressBar;
 
-import androidx.activity.result.ActivityResultLauncher;
-import androidx.activity.result.contract.ActivityResultContract;
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
 import androidx.appcompat.app.AlertDialog;
@@ -51,6 +47,7 @@ import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.LedgerTransaction;
 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
 import net.ktnx.mobileledger.model.MobileLedgerProfile;
+import net.ktnx.mobileledger.ui.QRScanAbleFragment;
 import net.ktnx.mobileledger.utils.Logger;
 import net.ktnx.mobileledger.utils.Misc;
 import net.ktnx.mobileledger.utils.SimpleDate;
@@ -69,25 +66,9 @@ import java.util.regex.Pattern;
 
 // TODO: offer to undo account remove-on-swipe
 
-public class NewTransactionFragment extends Fragment {
+public class NewTransactionFragment extends QRScanAbleFragment {
     private NewTransactionItemsAdapter listAdapter;
     private NewTransactionModel viewModel;
-    final ActivityResultLauncher<Void> scanQrLauncher =
-            registerForActivityResult(new ActivityResultContract<Void, String>() {
-                @NonNull
-                @Override
-                public Intent createIntent(@NonNull Context context, Void input) {
-                    final Intent intent = new Intent("com.google.zxing.client.android.SCAN");
-                    intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
-                    return intent;
-                }
-                @Override
-                public String parseResult(int resultCode, @Nullable Intent intent) {
-                    if (resultCode == Activity.RESULT_CANCELED)
-                        return null;
-                    return intent.getStringExtra("SCAN_RESULT");
-                }
-            }, this::onQrScanned);
     private FloatingActionButton fab;
     private OnNewTransactionFragmentInteractionListener mListener;
     private MobileLedgerProfile mProfile;
@@ -95,7 +76,7 @@ public class NewTransactionFragment extends Fragment {
         // Required empty public constructor
         setHasOptionsMenu(true);
     }
-    private void onQrScanned(String text) {
+    protected void onQrScanned(String text) {
         Logger.debug("qr", String.format("Got QR scan result [%s]", text));
         Pattern p =
                 Pattern.compile("^(\\d+)\\*(\\d+)\\*(\\d+)-(\\d+)-(\\d+)\\*([:\\d]+)\\*([\\d.]+)$");