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;
30 import androidx.annotation.NonNull;
31 import androidx.annotation.Nullable;
32 import androidx.fragment.app.Fragment;
33 import androidx.fragment.app.FragmentActivity;
34 import androidx.lifecycle.ViewModelProvider;
35 import androidx.recyclerview.widget.LinearLayoutManager;
36 import androidx.recyclerview.widget.RecyclerView;
38 import com.google.android.material.floatingactionbutton.FloatingActionButton;
39 import com.google.android.material.snackbar.Snackbar;
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;
49 import org.jetbrains.annotations.NotNull;
51 import java.util.Date;
54 * A simple {@link Fragment} subclass.
55 * Activities that contain this fragment must implement the
56 * {@link OnNewTransactionFragmentInteractionListener} interface
57 * to handle interaction events.
60 // TODO: offer to undo account remove-on-swipe
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);
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 -> {
82 final MenuItem toggleCurrencyItem = menu.findItem(R.id.toggle_currency);
83 toggleCurrencyItem.setOnMenuItemClickListener(item -> {
84 viewModel.toggleCurrencyVisible();
87 final FragmentActivity activity = getActivity();
89 viewModel.showCurrency.observe(activity, toggleCurrencyItem::setChecked);
92 public View onCreateView(LayoutInflater inflater, ViewGroup container,
93 Bundle savedInstanceState) {
94 // Inflate the layout for this fragment
95 return inflater.inflate(R.layout.fragment_new_transaction, container, false);
99 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
100 super.onActivityCreated(savedInstanceState);
101 FragmentActivity activity = getActivity();
102 if (activity == null)
103 throw new RSInvalidStateException(
104 "getActivity() returned null within onActivityCreated()");
106 list = activity.findViewById(R.id.new_transaction_accounts);
107 viewModel = new ViewModelProvider(activity).get(NewTransactionModel.class);
108 viewModel.observeDataProfile(this);
109 mProfile = Data.profile.getValue();
110 listAdapter = new NewTransactionItemsAdapter(viewModel, mProfile);
111 list.setAdapter(listAdapter);
112 list.setLayoutManager(new LinearLayoutManager(activity));
113 Data.profile.observe(getViewLifecycleOwner(), profile -> {
115 listAdapter.setProfile(profile);
117 listAdapter.notifyDataSetChanged();
118 viewModel.isSubmittable()
119 .observe(getViewLifecycleOwner(), isSubmittable -> {
123 fab.setEnabled(true);
132 // viewModel.checkTransactionSubmittable(listAdapter);
134 fab = activity.findViewById(R.id.fab);
135 fab.setOnClickListener(v -> onFabPressed());
137 boolean keep = false;
139 Bundle args = getArguments();
141 String error = args.getString("error");
143 Logger.debug("new-trans-f", String.format("Got error: %s", error));
144 Snackbar.make(list, error, Snackbar.LENGTH_LONG)
151 if (savedInstanceState != null) {
152 keep |= savedInstanceState.getBoolean("keep", true);
153 focused = savedInstanceState.getInt("focused", 0);
159 viewModel.setFocusedItem(focused);
163 public void onSaveInstanceState(@NonNull Bundle outState) {
164 super.onSaveInstanceState(outState);
165 outState.putBoolean("keep", true);
166 final int focusedItem = viewModel.getFocusedItem();
167 outState.putInt("focused", focusedItem);
169 private void onFabPressed() {
170 fab.setEnabled(false);
171 Misc.hideSoftKeyboard(this);
172 if (mListener != null) {
173 Date date = viewModel.getDate();
174 LedgerTransaction tr =
175 new LedgerTransaction(null, date, viewModel.getDescription(), mProfile);
177 LedgerTransactionAccount emptyAmountAccount = null;
178 float emptyAmountAccountBalance = 0;
179 for (int i = 0; i < viewModel.getAccountCount(); i++) {
180 LedgerTransactionAccount acc =
181 new LedgerTransactionAccount(viewModel.getAccount(i));
182 if (acc.getAccountName()
187 if (acc.isAmountSet()) {
188 emptyAmountAccountBalance += acc.getAmount();
191 emptyAmountAccount = acc;
197 if (emptyAmountAccount != null)
198 emptyAmountAccount.setAmount(-emptyAmountAccountBalance);
200 mListener.onTransactionSave(tr);
205 public void onAttach(@NotNull Context context) {
206 super.onAttach(context);
207 if (context instanceof OnNewTransactionFragmentInteractionListener) {
208 mListener = (OnNewTransactionFragmentInteractionListener) context;
211 throw new RuntimeException(
212 context.toString() + " must implement OnFragmentInteractionListener");
217 public void onDetach() {
223 * This interface must be implemented by activities that contain this
224 * fragment to allow an interaction in this fragment to be communicated
225 * to the activity and potentially other fragments contained in that
228 * See the Android Training lesson <a href=
229 * "http://developer.android.com/training/basics/fragments/communicating.html"
230 * >Communicating with Other Fragments</a> for more information.
232 public interface OnNewTransactionFragmentInteractionListener {
233 void onTransactionSave(LedgerTransaction tr);