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 final FragmentActivity activity = getActivity();
80 inflater.inflate(R.menu.new_transaction_fragment, menu);
81 menu.findItem(R.id.action_reset_new_transaction_activity)
82 .setOnMenuItemClickListener(item -> {
87 final MenuItem toggleCurrencyItem = menu.findItem(R.id.toggle_currency);
88 toggleCurrencyItem.setOnMenuItemClickListener(item -> {
89 viewModel.toggleCurrencyVisible();
93 viewModel.showCurrency.observe(activity, toggleCurrencyItem::setChecked);
95 final MenuItem toggleCommentsItem = menu.findItem(R.id.toggle_comments);
96 toggleCommentsItem.setOnMenuItemClickListener(item -> {
97 viewModel.toggleShowComments();
100 if (activity != null)
101 viewModel.showComments.observe(activity, toggleCommentsItem::setChecked);
104 public View onCreateView(LayoutInflater inflater, ViewGroup container,
105 Bundle savedInstanceState) {
106 // Inflate the layout for this fragment
107 return inflater.inflate(R.layout.fragment_new_transaction, container, false);
111 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
112 super.onActivityCreated(savedInstanceState);
113 FragmentActivity activity = getActivity();
114 if (activity == null)
115 throw new RSInvalidStateException(
116 "getActivity() returned null within onActivityCreated()");
118 list = activity.findViewById(R.id.new_transaction_accounts);
119 viewModel = new ViewModelProvider(activity).get(NewTransactionModel.class);
120 viewModel.observeDataProfile(this);
121 mProfile = Data.profile.getValue();
122 listAdapter = new NewTransactionItemsAdapter(viewModel, mProfile);
123 list.setAdapter(listAdapter);
124 list.setLayoutManager(new LinearLayoutManager(activity));
125 Data.profile.observe(getViewLifecycleOwner(), profile -> {
127 listAdapter.setProfile(profile);
129 listAdapter.notifyDataSetChanged();
130 viewModel.isSubmittable()
131 .observe(getViewLifecycleOwner(), isSubmittable -> {
135 fab.setEnabled(true);
144 // viewModel.checkTransactionSubmittable(listAdapter);
146 fab = activity.findViewById(R.id.fab);
147 fab.setOnClickListener(v -> onFabPressed());
149 boolean keep = false;
151 Bundle args = getArguments();
153 String error = args.getString("error");
155 Logger.debug("new-trans-f", String.format("Got error: %s", error));
156 Snackbar.make(list, error, Snackbar.LENGTH_LONG)
163 if (savedInstanceState != null) {
164 keep |= savedInstanceState.getBoolean("keep", true);
165 focused = savedInstanceState.getInt("focused", 0);
171 viewModel.setFocusedItem(focused);
174 ProgressBar p = activity.findViewById(R.id.progressBar);
175 viewModel.observeBusyFlag(getViewLifecycleOwner(), isBusy -> {
177 // Handler h = new Handler();
178 // h.postDelayed(() -> {
179 // if (viewModel.getBusyFlag())
180 // p.setVisibility(View.VISIBLE);
183 p.setVisibility(View.VISIBLE);
186 p.setVisibility(View.INVISIBLE);
190 public void onSaveInstanceState(@NonNull Bundle outState) {
191 super.onSaveInstanceState(outState);
192 outState.putBoolean("keep", true);
193 final int focusedItem = viewModel.getFocusedItem();
194 outState.putInt("focused", focusedItem);
196 private void onFabPressed() {
197 fab.setEnabled(false);
198 Misc.hideSoftKeyboard(this);
199 if (mListener != null) {
200 Date date = viewModel.getDate();
201 LedgerTransaction tr =
202 new LedgerTransaction(null, date, viewModel.getDescription(), mProfile);
204 tr.setComment(viewModel.getComment());
205 LedgerTransactionAccount emptyAmountAccount = null;
206 float emptyAmountAccountBalance = 0;
207 for (int i = 0; i < viewModel.getAccountCount(); i++) {
208 LedgerTransactionAccount acc =
209 new LedgerTransactionAccount(viewModel.getAccount(i));
210 if (acc.getAccountName()
215 if (acc.isAmountSet()) {
216 emptyAmountAccountBalance += acc.getAmount();
219 emptyAmountAccount = acc;
225 if (emptyAmountAccount != null)
226 emptyAmountAccount.setAmount(-emptyAmountAccountBalance);
228 mListener.onTransactionSave(tr);
233 public void onAttach(@NotNull Context context) {
234 super.onAttach(context);
235 if (context instanceof OnNewTransactionFragmentInteractionListener) {
236 mListener = (OnNewTransactionFragmentInteractionListener) context;
239 throw new RuntimeException(
240 context.toString() + " must implement OnFragmentInteractionListener");
245 public void onDetach() {
251 * This interface must be implemented by activities that contain this
252 * fragment to allow an interaction in this fragment to be communicated
253 * to the activity and potentially other fragments contained in that
256 * See the Android Training lesson <a href=
257 * "http://developer.android.com/training/basics/fragments/communicating.html"
258 * >Communicating with Other Fragments</a> for more information.
260 public interface OnNewTransactionFragmentInteractionListener {
261 void onTransactionSave(LedgerTransaction tr);