From 1aa277a5209ad399fda1a687cf3de0c5192e6ec5 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Fri, 29 Jan 2021 05:19:50 +0000 Subject: [PATCH] extract QR-scanning machinery from new transaction fragment --- .../java/net/ktnx/mobileledger/ui/QR.java | 53 +++++++++++++++++++ .../mobileledger/ui/QRScanAbleFragment.java | 41 ++++++++++++++ .../NewTransactionFragment.java | 25 ++------- 3 files changed, 97 insertions(+), 22 deletions(-) create mode 100644 app/src/main/java/net/ktnx/mobileledger/ui/QR.java create mode 100644 app/src/main/java/net/ktnx/mobileledger/ui/QRScanAbleFragment.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 index 00000000..9735a6fc --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/ui/QR.java @@ -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 . + */ + +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 registerLauncher(ActivityResultCaller activity, + QRScanResultReceiver resultReceiver) { + return activity.registerForActivityResult(new ActivityResultContract() { + @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 index 00000000..6acb4497 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/ui/QRScanAbleFragment.java @@ -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 . + */ + +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 qrScanTrigger = new MutableLiveData<>(); + protected final ActivityResultLauncher 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); + }); + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionFragment.java index aa44c72f..d27de40a 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionFragment.java @@ -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 scanQrLauncher = - registerForActivityResult(new ActivityResultContract() { - @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.]+)$"); -- 2.39.5