]> git.ktnx.net Git - mobile-ledger.git/commitdiff
new transaction: simulated backend communication is controller via menu (visible...
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Wed, 27 Nov 2019 06:30:38 +0000 (08:30 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Wed, 27 Nov 2019 06:30:38 +0000 (08:30 +0200)
app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java
app/src/main/res/menu/new_transaction.xml
app/src/main/res/values-bg/strings.xml
app/src/main/res/values/strings.xml

index 4c3bc477b8ddfeb43c82bd5e9268cb6f0105b232..b444e332e3b00580bb1f1b979bdbbb390d009cfe 100644 (file)
@@ -28,6 +28,7 @@ import net.ktnx.mobileledger.model.LedgerTransaction;
 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
 import net.ktnx.mobileledger.model.MobileLedgerProfile;
 import net.ktnx.mobileledger.utils.Globals;
+import net.ktnx.mobileledger.utils.Logger;
 import net.ktnx.mobileledger.utils.NetworkUtil;
 import net.ktnx.mobileledger.utils.UrlEncodedFormData;
 
@@ -54,24 +55,32 @@ public class SendTransactionTask extends AsyncTask<LedgerTransaction, Void, Void
     private String session;
     private LedgerTransaction ltr;
     private MobileLedgerProfile mProfile;
+    private boolean simulate = false;
 
+    public SendTransactionTask(TaskCallback callback, MobileLedgerProfile profile,
+                               boolean simulate) {
+        taskCallback = callback;
+        mProfile = profile;
+        this.simulate = simulate;
+    }
     public SendTransactionTask(TaskCallback callback, MobileLedgerProfile profile) {
         taskCallback = callback;
         mProfile = profile;
+        simulate = false;
     }
     private boolean sendOK() throws IOException {
-//        if (BuildConfig.DEBUG) {
-//            try {
-//                Thread.sleep(1500);
-//                if (Math.random() > 0.3)
-//                    throw new RuntimeException("Simulated test exception");
-//            }
-//            catch (InterruptedException ex) {
-//                Logger.debug("network", ex.toString());
-//            }
-//
-//            return true;
-//        }
+        if (simulate) {
+            try {
+                Thread.sleep(1500);
+                if (Math.random() > 0.3)
+                    throw new RuntimeException("Simulated test exception");
+            }
+            catch (InterruptedException ex) {
+                Logger.debug("network", ex.toString());
+            }
+
+            return true;
+        }
 
         HttpURLConnection http = NetworkUtil.prepareConnection(mProfile, "add");
         http.setRequestMethod("PUT");
index 98d4268063ce0f0903e5e51f25cac71307630701..660be19773891fa62c3cd85341719fef2e6752ff 100644 (file)
@@ -24,6 +24,7 @@ import android.view.Menu;
 import android.view.MenuItem;
 
 import androidx.appcompat.widget.Toolbar;
+import androidx.lifecycle.ViewModelProviders;
 import androidx.navigation.NavController;
 import androidx.navigation.Navigation;
 
@@ -48,6 +49,7 @@ import static net.ktnx.mobileledger.utils.Logger.debug;
 public class NewTransactionActivity extends ProfileThemedActivity implements TaskCallback,
         NewTransactionFragment.OnNewTransactionFragmentInteractionListener {
     private NavController navController;
+    private NewTransactionModel model;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -62,6 +64,9 @@ public class NewTransactionActivity extends ProfileThemedActivity implements Tas
 
         Objects.requireNonNull(getSupportActionBar())
                .setDisplayHomeAsUpEnabled(true);
+
+        model = ViewModelProviders.of(this)
+                                  .get(NewTransactionModel.class);
     }
     @Override
     protected void initProfile() {
@@ -99,7 +104,8 @@ public class NewTransactionActivity extends ProfileThemedActivity implements Tas
         navController.navigate(R.id.action_newTransactionFragment_to_newTransactionSavingFragment);
         try {
 
-            SendTransactionTask saver = new SendTransactionTask(this, mProfile);
+            SendTransactionTask saver =
+                    new SendTransactionTask(this, mProfile, model.getSimulateSave());
             saver.execute(tr);
         }
         catch (Exception e) {
@@ -121,8 +127,13 @@ public class NewTransactionActivity extends ProfileThemedActivity implements Tas
         if (BuildConfig.DEBUG) {
             menu.findItem(R.id.action_simulate_crash)
                 .setVisible(true);
+            menu.findItem(R.id.action_simulate_save)
+                .setVisible(true);
         }
 
+        model.observeSimulateSave(this, state -> menu.findItem(R.id.action_simulate_save)
+                                                     .setChecked(state));
+
         return true;
     }
 
@@ -141,6 +152,9 @@ public class NewTransactionActivity extends ProfileThemedActivity implements Tas
         else
             navController.navigate(R.id.action_newTransactionSavingFragment_Success, b);
     }
+    public void toggleSimulateSave(MenuItem item) {
+        model.toggleSimulateSave();
+    }
 
     private class AsyncCrasher extends AsyncTask<Void, Void, Void> {
         @Override
index 39b9ac8ad60f09dd3b44eae84995870f9158ccf1..1f4cec56ab462ee6d903ab42254a5e57e20b7531 100644 (file)
@@ -53,6 +53,21 @@ public class NewTransactionModel extends ViewModel {
     private final MutableLiveData<Boolean> isSubmittable = new MutableLiveData<>(false);
     private final MutableLiveData<Integer> focusedItem = new MutableLiveData<>(0);
     private final MutableLiveData<Integer> accountCount = new MutableLiveData<>(0);
+    private final MutableLiveData<Boolean> simulateSave = new MutableLiveData<>(false);
+    public boolean getSimulateSave() {
+        return simulateSave.getValue();
+    }
+    public void setSimulateSave(boolean simulateSave) {
+        this.simulateSave.setValue(simulateSave);
+    }
+    public void toggleSimulateSave() {
+        simulateSave.setValue(!simulateSave.getValue());
+    }
+    public void observeSimulateSave(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
+                                    @NonNull
+                                            androidx.lifecycle.Observer<? super Boolean> observer) {
+        this.simulateSave.observe(owner, observer);
+    }
     public int getAccountCount() {
         return items.size();
     }
index 45f11d7334088d756f56d8df9ec6800b3a5c4bed..060750803728d9bb0905680803133d37eb07819c 100644 (file)
         android:onClick="simulateCrash"
         android:visible="false"
         app:showAsAction="never" />
+    <item
+        android:id="@+id/action_simulate_save"
+        android:checkable="true"
+        android:checked="false"
+        android:onClick="toggleSimulateSave"
+        android:title="@string/simulate_save_label"
+        android:titleCondensed="@string/simulate_save_condensed_label"
+        android:visible="false"
+        app:showAsAction="never" />
 </menu>
\ No newline at end of file
index 4c82beb8f301bbbee7b6ca07d5ba917fc96ded5b..ac1c330d678a24faf70ac1e4db22fe1f9260bcab 100644 (file)
     <string name="insecure_scheme_with_auth">ВНИМАНИЕ: Използване на удостоверяване с несигурна схема на достъп</string>
     <string name="zero_amount">0,00</string>
     <string name="new_transaction_saving">Запазване…</string>
+    <string name="simulate_save_label">Симулиране на заявките за съхраняване</string>
+    <string name="simulate_save_condensed_label">Симул. съхр.</string>
 
 </resources>
index d73df328fa13216d048e6187c51f7bb4990ad3da..2c663bd947d1d45a0bd044a90c95408e2c30023a 100644 (file)
     <string name="insecure_scheme_with_auth">WARNING: Insecure http used with authentication</string>
     <string name="zero_amount">0.00</string>
     <string name="new_transaction_saving">Saving…</string>
+    <string name="simulate_save_label">Simulate save requests</string>
+    <string name="simulate_save_condensed_label">Simul. save</string>
 </resources>