From 2a016b0b3e56869059fa1831940c0cb96bdf3402 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Wed, 27 Nov 2019 08:30:38 +0200 Subject: [PATCH] new transaction: simulated backend communication is controller via menu (visible only in debug builds) --- .../async/SendTransactionTask.java | 33 ++++++++++++------- .../ui/activity/NewTransactionActivity.java | 16 ++++++++- .../ui/activity/NewTransactionModel.java | 15 +++++++++ app/src/main/res/menu/new_transaction.xml | 9 +++++ app/src/main/res/values-bg/strings.xml | 2 ++ app/src/main/res/values/strings.xml | 2 ++ 6 files changed, 64 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java b/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java index 4c3bc477..b444e332 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java @@ -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 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"); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java index 98d42680..660be197 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java @@ -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 { @Override diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java index 39b9ac8a..1f4cec56 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java @@ -53,6 +53,21 @@ public class NewTransactionModel extends ViewModel { private final MutableLiveData isSubmittable = new MutableLiveData<>(false); private final MutableLiveData focusedItem = new MutableLiveData<>(0); private final MutableLiveData accountCount = new MutableLiveData<>(0); + private final MutableLiveData 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 observer) { + this.simulateSave.observe(owner, observer); + } public int getAccountCount() { return items.size(); } diff --git a/app/src/main/res/menu/new_transaction.xml b/app/src/main/res/menu/new_transaction.xml index 45f11d73..06075080 100644 --- a/app/src/main/res/menu/new_transaction.xml +++ b/app/src/main/res/menu/new_transaction.xml @@ -24,4 +24,13 @@ android:onClick="simulateCrash" android:visible="false" app:showAsAction="never" /> + \ No newline at end of file diff --git a/app/src/main/res/values-bg/strings.xml b/app/src/main/res/values-bg/strings.xml index 4c82beb8..ac1c330d 100644 --- a/app/src/main/res/values-bg/strings.xml +++ b/app/src/main/res/values-bg/strings.xml @@ -114,5 +114,7 @@ ВНИМАНИЕ: Използване на удостоверяване с несигурна схема на достъп 0,00 Запазване… + Симулиране на заявките за съхраняване + Симул. съхр. diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d73df328..2c663bd9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -156,4 +156,6 @@ WARNING: Insecure http used with authentication 0.00 Saving… + Simulate save requests + Simul. save -- 2.39.5