]> git.ktnx.net Git - mobile-ledger.git/commitdiff
move sample text scanning to the template activity
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 21 Feb 2021 10:30:25 +0000 (12:30 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Mon, 1 Mar 2021 06:00:42 +0000 (06:00 +0000)
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

app/src/main/java/net/ktnx/mobileledger/ui/QR.java
app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsAdapter.java
app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsFragment.java
app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplatesActivity.java

index 9735a6fcb9c53efcbec9a3e1ed7b74188a8d1e8f..3250039f2230b21ca258e7e7a4ccd17462b8ee47 100644 (file)
@@ -50,4 +50,8 @@ public class QR {
     public interface QRScanResultReceiver {
         void onQRScanResult(String scanned);
     }
+
+    public interface QRScanTrigger {
+        void triggerQRScan();
+    }
 }
index 9152fdfd691a858f4bdc6cb63123a82f70d8c736..ab0b7b497b6581a05d7c0e49ccfa276dca3efec0 100644 (file)
@@ -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<TemplateDetailsAdapter
 
         }
         private void scanTestQR(View view) {
-            QRScanCapableFragment.triggerQRScan();
+            Context ctx = view.getContext();
+            if (ctx instanceof QR.QRScanTrigger)
+                ((QR.QRScanTrigger) ctx).triggerQRScan();
         }
     }
 
index 5ae4c3c983a242f8dec4d007cd2007cb072dde4f..c023c62cd09756cc151adea7d9deb5a8afc7f825 100644 (file)
@@ -28,6 +28,7 @@ import android.view.ViewGroup;
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
+import androidx.fragment.app.Fragment;
 import androidx.lifecycle.ViewModelProvider;
 import androidx.lifecycle.ViewModelStoreOwner;
 import androidx.navigation.NavController;
@@ -36,10 +37,9 @@ import androidx.recyclerview.widget.LinearLayoutManager;
 
 import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.databinding.TemplateDetailsFragmentBinding;
-import net.ktnx.mobileledger.ui.QRScanCapableFragment;
 import net.ktnx.mobileledger.utils.Logger;
 
-public class TemplateDetailsFragment extends QRScanCapableFragment {
+public class TemplateDetailsFragment extends Fragment {
     static final String ARG_TEMPLATE_ID = "pattern-id";
     private static final String ARG_COLUMN_COUNT = "column-count";
     private TemplateDetailsFragmentBinding b;
@@ -124,12 +124,6 @@ public class TemplateDetailsFragment extends QRScanCapableFragment {
 
         return b.getRoot();
     }
-    @Override
-    protected void onQrScanned(String text) {
-        Logger.debug("PatDet_fr", String.format("Got scanned text '%s'", text));
-        if (text != null)
-            mViewModel.setTestText(text);
-    }
     interface InteractionListener {
         void onDeleteTemplate(@NonNull Long templateId);
     }
index 62a005ffb437950f0de0e330cd9c93c0c1a3a550..b277489e4eccc0b03af7c169ed9a33ae166c81aa 100644 (file)
@@ -20,6 +20,7 @@ package net.ktnx.mobileledger.ui.templates;
 import android.os.Bundle;
 import android.view.MenuItem;
 
+import androidx.activity.result.ActivityResultLauncher;
 import androidx.annotation.NonNull;
 import androidx.appcompat.app.ActionBar;
 import androidx.lifecycle.ViewModelProvider;
@@ -36,6 +37,7 @@ import net.ktnx.mobileledger.dao.TemplateHeaderDAO;
 import net.ktnx.mobileledger.databinding.ActivityTemplatesBinding;
 import net.ktnx.mobileledger.db.DB;
 import net.ktnx.mobileledger.db.TemplateWithAccounts;
+import net.ktnx.mobileledger.ui.QR;
 import net.ktnx.mobileledger.ui.activity.CrashReportingActivity;
 import net.ktnx.mobileledger.utils.Logger;
 
@@ -43,10 +45,11 @@ import java.util.Objects;
 
 public class TemplatesActivity extends CrashReportingActivity
         implements TemplateListFragment.OnTemplateListFragmentInteractionListener,
-        TemplateDetailsFragment.InteractionListener {
+        TemplateDetailsFragment.InteractionListener, QR.QRScanResultReceiver, QR.QRScanTrigger {
     public static final String ARG_ADD_TEMPLATE = "add-template";
     private ActivityTemplatesBinding b;
     private NavController navController;
+    private ActivityResultLauncher<Void> 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