]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionFragment.java
NT TODO: undo account removal after swipe
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / NewTransactionFragment.java
1 /*
2  * Copyright © 2019 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe is free software: you can distribute it and/or modify it
5  * under the term of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your opinion), any later version.
8  *
9  * MoLe is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License terms for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.ui.activity;
19
20 import android.content.Context;
21 import android.os.Bundle;
22 import android.renderscript.RSInvalidStateException;
23 import android.view.LayoutInflater;
24 import android.view.Menu;
25 import android.view.MenuInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28
29 import androidx.annotation.NonNull;
30 import androidx.annotation.Nullable;
31 import androidx.fragment.app.Fragment;
32 import androidx.fragment.app.FragmentActivity;
33 import androidx.lifecycle.ViewModelProvider;
34 import androidx.recyclerview.widget.ItemTouchHelper;
35 import androidx.recyclerview.widget.LinearLayoutManager;
36 import androidx.recyclerview.widget.RecyclerView;
37
38 import com.google.android.material.floatingactionbutton.FloatingActionButton;
39 import com.google.android.material.snackbar.Snackbar;
40
41 import net.ktnx.mobileledger.R;
42 import net.ktnx.mobileledger.model.Data;
43 import net.ktnx.mobileledger.model.LedgerTransaction;
44 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
45 import net.ktnx.mobileledger.model.MobileLedgerProfile;
46 import net.ktnx.mobileledger.utils.Logger;
47 import net.ktnx.mobileledger.utils.Misc;
48
49 import org.jetbrains.annotations.NotNull;
50
51 import java.util.Date;
52
53 /**
54  * A simple {@link Fragment} subclass.
55  * Activities that contain this fragment must implement the
56  * {@link OnNewTransactionFragmentInteractionListener} interface
57  * to handle interaction events.
58  */
59
60 // TODO: offer to undo account remove-on-swipe
61
62 public class NewTransactionFragment extends Fragment {
63     private NewTransactionItemsAdapter listAdapter;
64     private NewTransactionModel viewModel;
65     private RecyclerView list;
66     private FloatingActionButton fab;
67     private OnNewTransactionFragmentInteractionListener mListener;
68     private MobileLedgerProfile mProfile;
69     public NewTransactionFragment() {
70         // Required empty public constructor
71         setHasOptionsMenu(true);
72     }
73     @Override
74     public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
75         super.onCreateOptionsMenu(menu, inflater);
76         inflater.inflate(R.menu.new_transaction_fragment, menu);
77         menu.findItem(R.id.action_reset_new_transaction_activity)
78             .setOnMenuItemClickListener(item -> {
79                 listAdapter.reset();
80                 return true;
81             });
82     }
83     @Override
84     public View onCreateView(LayoutInflater inflater, ViewGroup container,
85                              Bundle savedInstanceState) {
86         // Inflate the layout for this fragment
87         return inflater.inflate(R.layout.fragment_new_transaction, container, false);
88     }
89
90     @Override
91     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
92         super.onActivityCreated(savedInstanceState);
93         FragmentActivity activity = getActivity();
94         if (activity == null)
95             throw new RSInvalidStateException(
96                     "getActivity() returned null within onActivityCreated()");
97
98         list = activity.findViewById(R.id.new_transaction_accounts);
99         viewModel = new ViewModelProvider(activity).get(NewTransactionModel.class);
100         mProfile = Data.profile.getValue();
101         listAdapter = new NewTransactionItemsAdapter(viewModel, mProfile);
102         list.setAdapter(listAdapter);
103         list.setLayoutManager(new LinearLayoutManager(activity));
104         Data.profile.observe(getViewLifecycleOwner(), profile -> {
105             mProfile = profile;
106             listAdapter.setProfile(profile);
107         });
108         listAdapter.notifyDataSetChanged();
109         new ItemTouchHelper(new ItemTouchHelper.Callback() {
110             @Override
111             public int getMovementFlags(@NonNull RecyclerView recyclerView,
112                                         @NonNull RecyclerView.ViewHolder viewHolder) {
113                 int flags = makeFlag(ItemTouchHelper.ACTION_STATE_IDLE, ItemTouchHelper.END);
114                 // the top item is always there (date and description)
115                 if (viewHolder.getAdapterPosition() > 0) {
116                     if (viewModel.getAccountCount() > 2) {
117                         flags |= makeFlag(ItemTouchHelper.ACTION_STATE_SWIPE,
118                                 ItemTouchHelper.START | ItemTouchHelper.END);
119                     }
120                 }
121
122                 return flags;
123             }
124             @Override
125             public boolean onMove(@NonNull RecyclerView recyclerView,
126                                   @NonNull RecyclerView.ViewHolder viewHolder,
127                                   @NonNull RecyclerView.ViewHolder target) {
128                 return false;
129             }
130             @Override
131             public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
132                 if (viewModel.getAccountCount() == 2)
133                     Snackbar.make(list, R.string.msg_at_least_two_accounts_are_required,
134                             Snackbar.LENGTH_LONG)
135                             .setAction("Action", null)
136                             .show();
137                 else {
138                     int pos = viewHolder.getAdapterPosition();
139                     viewModel.removeItem(pos - 1);
140                     listAdapter.notifyItemRemoved(pos);
141                     viewModel.sendCountNotifications(); // needed after items re-arrangement
142                     viewModel.checkTransactionSubmittable(listAdapter);
143                 }
144             }
145         }).attachToRecyclerView(list);
146
147         viewModel.isSubmittable()
148                  .observe(getViewLifecycleOwner(), isSubmittable -> {
149                      if (isSubmittable) {
150                          if (fab != null) {
151                              fab.show();
152                              fab.setEnabled(true);
153                          }
154                      }
155                      else {
156                          if (fab != null) {
157                              fab.hide();
158                          }
159                      }
160                  });
161         viewModel.checkTransactionSubmittable(listAdapter);
162
163         fab = activity.findViewById(R.id.fab);
164         fab.setOnClickListener(v -> onFabPressed());
165
166         boolean keep = false;
167
168         Bundle args = getArguments();
169         if (args != null) {
170             String error = args.getString("error");
171             if (error != null) {
172                 Logger.debug("new-trans-f", String.format("Got error: %s", error));
173                 Snackbar.make(list, error, Snackbar.LENGTH_LONG)
174                         .show();
175                 keep = true;
176             }
177         }
178
179         int focused = 0;
180         if (savedInstanceState != null) {
181             keep |= savedInstanceState.getBoolean("keep", true);
182             focused = savedInstanceState.getInt("focused", 0);
183         }
184
185         if (!keep)
186             viewModel.reset();
187         else {
188             viewModel.setFocusedItem(focused);
189         }
190     }
191     @Override
192     public void onSaveInstanceState(@NonNull Bundle outState) {
193         super.onSaveInstanceState(outState);
194         outState.putBoolean("keep", true);
195         final int focusedItem = viewModel.getFocusedItem();
196         outState.putInt("focused", focusedItem);
197     }
198     private void onFabPressed() {
199         fab.setEnabled(false);
200         Misc.hideSoftKeyboard(this);
201         if (mListener != null) {
202             Date date = viewModel.getDate();
203             LedgerTransaction tr =
204                     new LedgerTransaction(null, date, viewModel.getDescription(), mProfile);
205
206             LedgerTransactionAccount emptyAmountAccount = null;
207             float emptyAmountAccountBalance = 0;
208             for (int i = 0; i < viewModel.getAccountCount(); i++) {
209                 LedgerTransactionAccount acc =
210                         new LedgerTransactionAccount(viewModel.getAccount(i));
211                 if (acc.getAccountName()
212                        .trim()
213                        .isEmpty())
214                     continue;
215
216                 if (acc.isAmountSet()) {
217                     emptyAmountAccountBalance += acc.getAmount();
218                 }
219                 else {
220                     emptyAmountAccount = acc;
221                 }
222
223                 tr.addAccount(acc);
224             }
225
226             if (emptyAmountAccount != null)
227                 emptyAmountAccount.setAmount(-emptyAmountAccountBalance);
228
229             mListener.onTransactionSave(tr);
230         }
231     }
232
233     @Override
234     public void onAttach(@NotNull Context context) {
235         super.onAttach(context);
236         if (context instanceof OnNewTransactionFragmentInteractionListener) {
237             mListener = (OnNewTransactionFragmentInteractionListener) context;
238         }
239         else {
240             throw new RuntimeException(
241                     context.toString() + " must implement OnFragmentInteractionListener");
242         }
243     }
244
245     @Override
246     public void onDetach() {
247         super.onDetach();
248         mListener = null;
249     }
250
251     /**
252      * This interface must be implemented by activities that contain this
253      * fragment to allow an interaction in this fragment to be communicated
254      * to the activity and potentially other fragments contained in that
255      * activity.
256      * <p>
257      * See the Android Training lesson <a href=
258      * "http://developer.android.com/training/basics/fragments/communicating.html"
259      * >Communicating with Other Fragments</a> for more information.
260      */
261     public interface OnNewTransactionFragmentInteractionListener {
262         void onTransactionSave(LedgerTransaction tr);
263     }
264 }