]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java
upgrade a couple of library versions
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / NewTransactionActivity.java
index 98d4268063ce0f0903e5e51f25cac71307630701..7499641741c4a09c19725fbcfac141bc67587492 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2019 Damyan Ivanov.
+ * Copyright © 2020 Damyan Ivanov.
  * This file is part of MoLe.
  * MoLe is free software: you can distribute it and/or modify it
  * under the term of the GNU General Public License as published by
 
 package net.ktnx.mobileledger.ui.activity;
 
-import android.os.AsyncTask;
 import android.os.Bundle;
 import android.util.TypedValue;
 import android.view.Menu;
 import android.view.MenuItem;
+import android.view.View;
 
 import androidx.appcompat.widget.Toolbar;
+import androidx.lifecycle.ViewModelProvider;
 import androidx.navigation.NavController;
-import androidx.navigation.Navigation;
+import androidx.navigation.fragment.NavHostFragment;
 
 import net.ktnx.mobileledger.BuildConfig;
 import net.ktnx.mobileledger.R;
@@ -38,16 +39,10 @@ import java.util.Objects;
 
 import static net.ktnx.mobileledger.utils.Logger.debug;
 
-/*
- * TODO: nicer progress while transaction is submitted
- * TODO: reports
- * TODO: get rid of the custom session/cookie and auth code?
- *         (the last problem with the POST was the missing content-length header)
- *  */
-
 public class NewTransactionActivity extends ProfileThemedActivity implements TaskCallback,
         NewTransactionFragment.OnNewTransactionFragmentInteractionListener {
     private NavController navController;
+    private NewTransactionModel model;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -55,13 +50,17 @@ public class NewTransactionActivity extends ProfileThemedActivity implements Tas
         setContentView(R.layout.activity_new_transaction);
         Toolbar toolbar = findViewById(R.id.toolbar);
         setSupportActionBar(toolbar);
-        Data.profile.observe(this,
+        Data.observeProfile(this,
                 mobileLedgerProfile -> toolbar.setSubtitle(mobileLedgerProfile.getName()));
 
-        navController = Navigation.findNavController(this, R.id.new_transaction_nav);
+        NavHostFragment navHostFragment = (NavHostFragment) Objects.requireNonNull(
+                getSupportFragmentManager().findFragmentById(R.id.new_transaction_nav));
+        navController = navHostFragment.getNavController();
 
         Objects.requireNonNull(getSupportActionBar())
                .setDisplayHomeAsUpEnabled(true);
+
+        model = new ViewModelProvider(this).get(NewTransactionModel.class);
     }
     @Override
     protected void initProfile() {
@@ -89,17 +88,12 @@ public class NewTransactionActivity extends ProfileThemedActivity implements Tas
         }
         return super.onOptionsItemSelected(item);
     }
-
-    @Override
-    protected void onStart() {
-        super.onStart();
-        // FIXME if (tvDescription.getText().toString().isEmpty()) tvDescription.requestFocus();
-    }
     public void onTransactionSave(LedgerTransaction tr) {
         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 +115,16 @@ 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);
+            findViewById(R.id.simulationLabel).setVisibility(state ? View.VISIBLE : View.GONE);
+        });
+
         return true;
     }
 
@@ -141,12 +143,8 @@ public class NewTransactionActivity extends ProfileThemedActivity implements Tas
         else
             navController.navigate(R.id.action_newTransactionSavingFragment_Success, b);
     }
-
-    private class AsyncCrasher extends AsyncTask<Void, Void, Void> {
-        @Override
-        protected Void doInBackground(Void... voids) {
-            throw new RuntimeException("Simulated crash");
-        }
+    public void toggleSimulateSave(MenuItem item) {
+        model.toggleSimulateSave();
     }
 
 }