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 4c3bc47..b444e33 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 98d4268..660be19 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 39b9ac8..1f4cec5 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 45f11d7..0607508 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 4c82beb..ac1c330 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 d73df32..2c663bd 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>