]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionFragment.java
new transaction: hide currency/commodity selector by default; add menu item for showing
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / NewTransactionFragment.java
index 2775716bb9d4073fd36fa520ab7c59c668d2071b..55b0e6106c91ac3d0b22337df5def7f8072522d3 100644 (file)
 
 package net.ktnx.mobileledger.ui.activity;
 
-import android.app.Activity;
 import android.content.Context;
 import android.os.Bundle;
 import android.renderscript.RSInvalidStateException;
 import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.MenuInflater;
+import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
 import androidx.fragment.app.Fragment;
-import androidx.lifecycle.ViewModelProviders;
-import androidx.recyclerview.widget.ItemTouchHelper;
+import androidx.fragment.app.FragmentActivity;
+import androidx.lifecycle.ViewModelProvider;
 import androidx.recyclerview.widget.LinearLayoutManager;
 import androidx.recyclerview.widget.RecyclerView;
 
@@ -43,6 +43,8 @@ import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.LedgerTransaction;
 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
 import net.ktnx.mobileledger.model.MobileLedgerProfile;
+import net.ktnx.mobileledger.utils.Logger;
+import net.ktnx.mobileledger.utils.Misc;
 
 import org.jetbrains.annotations.NotNull;
 
@@ -54,6 +56,9 @@ import java.util.Date;
  * {@link OnNewTransactionFragmentInteractionListener} interface
  * to handle interaction events.
  */
+
+// TODO: offer to undo account remove-on-swipe
+
 public class NewTransactionFragment extends Fragment {
     private NewTransactionItemsAdapter listAdapter;
     private NewTransactionModel viewModel;
@@ -74,6 +79,14 @@ public class NewTransactionFragment extends Fragment {
                 listAdapter.reset();
                 return true;
             });
+        final MenuItem toggleCurrencyItem = menu.findItem(R.id.toggle_currency);
+        toggleCurrencyItem.setOnMenuItemClickListener(item -> {
+            viewModel.toggleCurrencyVisible();
+            return true;
+        });
+        final FragmentActivity activity = getActivity();
+        if (activity != null)
+            viewModel.showCurrency.observe(activity, toggleCurrencyItem::setChecked);
     }
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
@@ -85,63 +98,24 @@ public class NewTransactionFragment extends Fragment {
     @Override
     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
-        Activity activity = getActivity();
+        FragmentActivity activity = getActivity();
         if (activity == null)
             throw new RSInvalidStateException(
                     "getActivity() returned null within onActivityCreated()");
 
         list = activity.findViewById(R.id.new_transaction_accounts);
-        viewModel = ViewModelProviders.of(this)
-                                      .get(NewTransactionModel.class);
+        viewModel = new ViewModelProvider(activity).get(NewTransactionModel.class);
         mProfile = Data.profile.getValue();
         listAdapter = new NewTransactionItemsAdapter(viewModel, mProfile);
         list.setAdapter(listAdapter);
         list.setLayoutManager(new LinearLayoutManager(activity));
-        Data.profile.observe(this, profile -> {
+        Data.profile.observe(getViewLifecycleOwner(), profile -> {
             mProfile = profile;
             listAdapter.setProfile(profile);
         });
         listAdapter.notifyDataSetChanged();
-        new ItemTouchHelper(new ItemTouchHelper.Callback() {
-            @Override
-            public int getMovementFlags(@NonNull RecyclerView recyclerView,
-                                        @NonNull RecyclerView.ViewHolder viewHolder) {
-                int flags = makeFlag(ItemTouchHelper.ACTION_STATE_IDLE, ItemTouchHelper.END);
-                // the top item is always there (date and description)
-                if (viewHolder.getAdapterPosition() > 0) {
-                    if (viewModel.getAccountCount() > 2) {
-                        flags |= makeFlag(ItemTouchHelper.ACTION_STATE_SWIPE,
-                                ItemTouchHelper.START | ItemTouchHelper.END);
-                    }
-                }
-
-                return flags;
-            }
-            @Override
-            public boolean onMove(@NonNull RecyclerView recyclerView,
-                                  @NonNull RecyclerView.ViewHolder viewHolder,
-                                  @NonNull RecyclerView.ViewHolder target) {
-                return false;
-            }
-            @Override
-            public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
-                if (viewModel.getAccountCount() == 2)
-                    Snackbar.make(list, R.string.msg_at_least_two_accounts_are_required,
-                            Snackbar.LENGTH_LONG)
-                            .setAction("Action", null)
-                            .show();
-                else {
-                    int pos = viewHolder.getAdapterPosition();
-                    viewModel.removeItem(pos - 1);
-                    listAdapter.notifyItemRemoved(pos);
-                    viewModel.sendCountNotifications(); // needed after items re-arrangement
-                    viewModel.checkTransactionSubmittable(listAdapter);
-                }
-            }
-        }).attachToRecyclerView(list);
-
         viewModel.isSubmittable()
-                 .observe(this, isSubmittable -> {
+                 .observe(getViewLifecycleOwner(), isSubmittable -> {
                      if (isSubmittable) {
                          if (fab != null) {
                              fab.show();
@@ -159,19 +133,41 @@ public class NewTransactionFragment extends Fragment {
         fab = activity.findViewById(R.id.fab);
         fab.setOnClickListener(v -> onFabPressed());
 
+        boolean keep = false;
+
         Bundle args = getArguments();
         if (args != null) {
             String error = args.getString("error");
             if (error != null) {
-                // TODO display error
-            }
-            else {
-                viewModel.reset();
+                Logger.debug("new-trans-f", String.format("Got error: %s", error));
+                Snackbar.make(list, error, Snackbar.LENGTH_LONG)
+                        .show();
+                keep = true;
             }
         }
+
+        int focused = 0;
+        if (savedInstanceState != null) {
+            keep |= savedInstanceState.getBoolean("keep", true);
+            focused = savedInstanceState.getInt("focused", 0);
+        }
+
+        if (!keep)
+            viewModel.reset();
+        else {
+            viewModel.setFocusedItem(focused);
+        }
+    }
+    @Override
+    public void onSaveInstanceState(@NonNull Bundle outState) {
+        super.onSaveInstanceState(outState);
+        outState.putBoolean("keep", true);
+        final int focusedItem = viewModel.getFocusedItem();
+        outState.putInt("focused", focusedItem);
     }
     private void onFabPressed() {
         fab.setEnabled(false);
+        Misc.hideSoftKeyboard(this);
         if (mListener != null) {
             Date date = viewModel.getDate();
             LedgerTransaction tr =
@@ -180,7 +176,8 @@ public class NewTransactionFragment extends Fragment {
             LedgerTransactionAccount emptyAmountAccount = null;
             float emptyAmountAccountBalance = 0;
             for (int i = 0; i < viewModel.getAccountCount(); i++) {
-                LedgerTransactionAccount acc = viewModel.getAccount(i);
+                LedgerTransactionAccount acc =
+                        new LedgerTransactionAccount(viewModel.getAccount(i));
                 if (acc.getAccountName()
                        .trim()
                        .isEmpty())