From 086190fcb88b75bf3df3eeeb6357f15ed1127f4f Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sun, 21 Feb 2021 12:30:25 +0200 Subject: [PATCH] move sample text scanning to the template activity the initial idea was to have an abstract fragment class that could handle the details about launching the QR scanner and accept the result. however, the right place to do this is the activity, because only an activity can receive the scan result, and launch the QR app. Before, this was worked around by a LiveData thing that the activity observed, and which the "Scan" buttons triggered. This has a nasty side effect that the LiveData is triggered whenever the activity is re-instantiated --- .../java/net/ktnx/mobileledger/ui/QR.java | 4 ++++ .../ui/templates/TemplateDetailsAdapter.java | 7 +++++-- .../ui/templates/TemplateDetailsFragment.java | 10 ++-------- .../ui/templates/TemplatesActivity.java | 19 ++++++++++++++++++- 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/QR.java b/app/src/main/java/net/ktnx/mobileledger/ui/QR.java index 9735a6fc..3250039f 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/QR.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/QR.java @@ -50,4 +50,8 @@ public class QR { public interface QRScanResultReceiver { void onQRScanResult(String scanned); } + + public interface QRScanTrigger { + void triggerQRScan(); + } } diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsAdapter.java b/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsAdapter.java index 9152fdfd..ab0b7b49 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsAdapter.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsAdapter.java @@ -18,6 +18,7 @@ package net.ktnx.mobileledger.ui.templates; import android.annotation.SuppressLint; +import android.content.Context; import android.content.res.Resources; import android.text.Editable; import android.text.TextWatcher; @@ -41,7 +42,7 @@ import net.ktnx.mobileledger.databinding.TemplateDetailsHeaderBinding; import net.ktnx.mobileledger.db.AccountAutocompleteAdapter; import net.ktnx.mobileledger.model.Data; import net.ktnx.mobileledger.model.TemplateDetailsItem; -import net.ktnx.mobileledger.ui.QRScanCapableFragment; +import net.ktnx.mobileledger.ui.QR; import net.ktnx.mobileledger.ui.TemplateDetailSourceSelectorFragment; import net.ktnx.mobileledger.utils.Logger; import net.ktnx.mobileledger.utils.Misc; @@ -617,7 +618,9 @@ class TemplateDetailsAdapter extends RecyclerView.Adapter qrScanLauncher; // @Override // public boolean onCreateOptionsMenu(Menu menu) { // super.onCreateOptionsMenu(menu); @@ -86,6 +89,8 @@ public class TemplatesActivity extends CrashReportingActivity b.fabAdd.setOnClickListener(v -> onEditTemplate(null)); b.fabSave.setOnClickListener(v -> onSaveTemplate()); + + qrScanLauncher = QR.registerLauncher(this, this); } @Override public boolean onOptionsItemSelected(MenuItem item) { @@ -153,4 +158,16 @@ public class TemplatesActivity extends CrashReportingActivity }); }); } + @Override + public void onQRScanResult(String scanned) { + Logger.debug("PatDet_fr", String.format("Got scanned text '%s'", scanned)); + TemplateDetailsViewModel model = new ViewModelProvider( + navController.getViewModelStoreOwner(R.id.template_list_navigation)).get( + TemplateDetailsViewModel.class); + model.setTestText(scanned); + } + @Override + public void triggerQRScan() { + qrScanLauncher.launch(null); + } } \ No newline at end of file -- 2.39.5