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.
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.
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/>.
18 package net.ktnx.mobileledger.ui.activity;
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.MenuItem;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.widget.ProgressBar;
31 import androidx.annotation.NonNull;
32 import androidx.annotation.Nullable;
33 import androidx.fragment.app.Fragment;
34 import androidx.fragment.app.FragmentActivity;
35 import androidx.lifecycle.ViewModelProvider;
36 import androidx.recyclerview.widget.LinearLayoutManager;
37 import androidx.recyclerview.widget.RecyclerView;
39 import com.google.android.material.floatingactionbutton.FloatingActionButton;
40 import com.google.android.material.snackbar.Snackbar;
42 import net.ktnx.mobileledger.R;
43 import net.ktnx.mobileledger.model.Data;
44 import net.ktnx.mobileledger.model.LedgerTransaction;
45 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
46 import net.ktnx.mobileledger.model.MobileLedgerProfile;
47 import net.ktnx.mobileledger.utils.Logger;
48 import net.ktnx.mobileledger.utils.Misc;
50 import org.jetbrains.annotations.NotNull;
52 import java.util.Date;
55 * A simple {@link Fragment} subclass.
56 * Activities that contain this fragment must implement the
57 * {@link OnNewTransactionFragmentInteractionListener} interface
58 * to handle interaction events.
61 // TODO: offer to undo account remove-on-swipe
62 // TODO: transaction-level comment
64 public class NewTransactionFragment extends Fragment {
65 private NewTransactionItemsAdapter listAdapter;
66 private NewTransactionModel viewModel;
67 private RecyclerView list;
68 private FloatingActionButton fab;
69 private OnNewTransactionFragmentInteractionListener mListener;
70 private MobileLedgerProfile mProfile;
71 public NewTransactionFragment() {
72 // Required empty public constructor
73 setHasOptionsMenu(true);
76 public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
77 super.onCreateOptionsMenu(menu, inflater);
78 inflater.inflate(R.menu.new_transaction_fragment, menu);
79 menu.findItem(R.id.action_reset_new_transaction_activity)
80 .setOnMenuItemClickListener(item -> {
84 final MenuItem toggleCurrencyItem = menu.findItem(R.id.toggle_currency);
85 toggleCurrencyItem.setOnMenuItemClickListener(item -> {
86 viewModel.toggleCurrencyVisible();
89 final FragmentActivity activity = getActivity();
91 viewModel.showCurrency.observe(activity, toggleCurrencyItem::setChecked);
94 public View onCreateView(LayoutInflater inflater, ViewGroup container,
95 Bundle savedInstanceState) {
96 // Inflate the layout for this fragment
97 return inflater.inflate(R.layout.fragment_new_transaction, container, false);
101 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
102 super.onActivityCreated(savedInstanceState);
103 FragmentActivity activity = getActivity();
104 if (activity == null)
105 throw new RSInvalidStateException(
106 "getActivity() returned null within onActivityCreated()");
108 list = activity.findViewById(R.id.new_transaction_accounts);
109 viewModel = new ViewModelProvider(activity).get(NewTransactionModel.class);
110 viewModel.observeDataProfile(this);
111 mProfile = Data.profile.getValue();
112 listAdapter = new NewTransactionItemsAdapter(viewModel, mProfile);
113 list.setAdapter(listAdapter);
114 list.setLayoutManager(new LinearLayoutManager(activity));
115 Data.profile.observe(getViewLifecycleOwner(), profile -> {
117 listAdapter.setProfile(profile);
119 listAdapter.notifyDataSetChanged();
120 viewModel.isSubmittable()
121 .observe(getViewLifecycleOwner(), isSubmittable -> {
125 fab.setEnabled(true);
134 // viewModel.checkTransactionSubmittable(listAdapter);
136 fab = activity.findViewById(R.id.fab);
137 fab.setOnClickListener(v -> onFabPressed());
139 boolean keep = false;
141 Bundle args = getArguments();
143 String error = args.getString("error");
145 Logger.debug("new-trans-f", String.format("Got error: %s", error));
146 Snackbar.make(list, error, Snackbar.LENGTH_LONG)
153 if (savedInstanceState != null) {
154 keep |= savedInstanceState.getBoolean("keep", true);
155 focused = savedInstanceState.getInt("focused", 0);
161 viewModel.setFocusedItem(focused);
164 ProgressBar p = activity.findViewById(R.id.progressBar);
165 viewModel.observeBusyFlag(getViewLifecycleOwner(), isBusy -> {
167 // Handler h = new Handler();
168 // h.postDelayed(() -> {
169 // if (viewModel.getBusyFlag())
170 // p.setVisibility(View.VISIBLE);
173 p.setVisibility(View.VISIBLE);
176 p.setVisibility(View.INVISIBLE);
180 public void onSaveInstanceState(@NonNull Bundle outState) {
181 super.onSaveInstanceState(outState);
182 outState.putBoolean("keep", true);
183 final int focusedItem = viewModel.getFocusedItem();
184 outState.putInt("focused", focusedItem);
186 private void onFabPressed() {
187 fab.setEnabled(false);
188 Misc.hideSoftKeyboard(this);
189 if (mListener != null) {
190 Date date = viewModel.getDate();
191 LedgerTransaction tr =
192 new LedgerTransaction(null, date, viewModel.getDescription(), mProfile);
194 LedgerTransactionAccount emptyAmountAccount = null;
195 float emptyAmountAccountBalance = 0;
196 for (int i = 0; i < viewModel.getAccountCount(); i++) {
197 LedgerTransactionAccount acc =
198 new LedgerTransactionAccount(viewModel.getAccount(i));
199 if (acc.getAccountName()
204 if (acc.isAmountSet()) {
205 emptyAmountAccountBalance += acc.getAmount();
208 emptyAmountAccount = acc;
214 if (emptyAmountAccount != null)
215 emptyAmountAccount.setAmount(-emptyAmountAccountBalance);
217 mListener.onTransactionSave(tr);
222 public void onAttach(@NotNull Context context) {
223 super.onAttach(context);
224 if (context instanceof OnNewTransactionFragmentInteractionListener) {
225 mListener = (OnNewTransactionFragmentInteractionListener) context;
228 throw new RuntimeException(
229 context.toString() + " must implement OnFragmentInteractionListener");
234 public void onDetach() {
240 * This interface must be implemented by activities that contain this
241 * fragment to allow an interaction in this fragment to be communicated
242 * to the activity and potentially other fragments contained in that
245 * See the Android Training lesson <a href=
246 * "http://developer.android.com/training/basics/fragments/communicating.html"
247 * >Communicating with Other Fragments</a> for more information.
249 public interface OnNewTransactionFragmentInteractionListener {
250 void onTransactionSave(LedgerTransaction tr);