2 * Copyright © 2021 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.new_transaction;
20 import android.content.Context;
21 import android.content.res.Resources;
22 import android.os.Bundle;
23 import android.renderscript.RSInvalidStateException;
24 import android.view.LayoutInflater;
25 import android.view.Menu;
26 import android.view.MenuInflater;
27 import android.view.MenuItem;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.widget.ProgressBar;
32 import androidx.annotation.NonNull;
33 import androidx.annotation.Nullable;
34 import androidx.appcompat.app.AlertDialog;
35 import androidx.fragment.app.Fragment;
36 import androidx.fragment.app.FragmentActivity;
37 import androidx.lifecycle.ViewModelProvider;
38 import androidx.recyclerview.widget.LinearLayoutManager;
39 import androidx.recyclerview.widget.RecyclerView;
41 import com.google.android.material.snackbar.Snackbar;
43 import net.ktnx.mobileledger.R;
44 import net.ktnx.mobileledger.db.Profile;
45 import net.ktnx.mobileledger.json.API;
46 import net.ktnx.mobileledger.model.Data;
47 import net.ktnx.mobileledger.model.LedgerTransaction;
48 import net.ktnx.mobileledger.ui.FabManager;
49 import net.ktnx.mobileledger.ui.QR;
50 import net.ktnx.mobileledger.ui.profiles.ProfileDetailActivity;
51 import net.ktnx.mobileledger.utils.Logger;
53 import org.jetbrains.annotations.NotNull;
56 * A simple {@link Fragment} subclass.
57 * Activities that contain this fragment must implement the
58 * {@link OnNewTransactionFragmentInteractionListener} interface
59 * to handle interaction events.
62 // TODO: offer to undo account remove-on-swipe
64 public class NewTransactionFragment extends Fragment {
65 private NewTransactionItemsAdapter listAdapter;
66 private NewTransactionModel viewModel;
67 private OnNewTransactionFragmentInteractionListener mListener;
68 private Profile 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 final FragmentActivity activity = getActivity();
78 inflater.inflate(R.menu.new_transaction_fragment, menu);
80 menu.findItem(R.id.scan_qr)
81 .setOnMenuItemClickListener(this::onScanQrAction);
83 menu.findItem(R.id.action_reset_new_transaction_activity)
84 .setOnMenuItemClickListener(item -> {
89 final MenuItem toggleCurrencyItem = menu.findItem(R.id.toggle_currency);
90 toggleCurrencyItem.setOnMenuItemClickListener(item -> {
91 viewModel.toggleCurrencyVisible();
95 viewModel.getShowCurrency()
96 .observe(activity, toggleCurrencyItem::setChecked);
98 final MenuItem toggleCommentsItem = menu.findItem(R.id.toggle_comments);
99 toggleCommentsItem.setOnMenuItemClickListener(item -> {
100 viewModel.toggleShowComments();
103 if (activity != null)
104 viewModel.getShowComments()
105 .observe(activity, toggleCommentsItem::setChecked);
107 private boolean onScanQrAction(MenuItem item) {
109 Context ctx = requireContext();
110 if (ctx instanceof QR.QRScanTrigger)
111 ((QR.QRScanTrigger) ctx).triggerQRScan();
113 catch (Exception e) {
114 Logger.debug("qr", "Error launching QR scanner", e);
120 public View onCreateView(LayoutInflater inflater, ViewGroup container,
121 Bundle savedInstanceState) {
122 // Inflate the layout for this fragment
123 return inflater.inflate(R.layout.fragment_new_transaction, container, false);
127 public void onViewCreated(@NotNull View view, @Nullable Bundle savedInstanceState) {
128 super.onViewCreated(view, savedInstanceState);
129 FragmentActivity activity = getActivity();
130 if (activity == null)
131 throw new RSInvalidStateException(
132 "getActivity() returned null within onActivityCreated()");
134 viewModel = new ViewModelProvider(activity).get(NewTransactionModel.class);
135 viewModel.observeDataProfile(this);
136 mProfile = Data.getProfile();
137 listAdapter = new NewTransactionItemsAdapter(viewModel, mProfile);
140 .observe(getViewLifecycleOwner(), newList -> listAdapter.setItems(newList));
142 RecyclerView list = activity.findViewById(R.id.new_transaction_accounts);
143 list.setAdapter(listAdapter);
144 list.setLayoutManager(new LinearLayoutManager(activity));
146 Data.observeProfile(getViewLifecycleOwner(), profile -> {
148 listAdapter.setProfile(profile);
150 boolean keep = false;
152 Bundle args = getArguments();
154 String error = args.getString("error");
156 Logger.debug("new-trans-f", String.format("Got error: %s", error));
158 Context context = getContext();
159 if (context != null) {
160 AlertDialog.Builder builder = new AlertDialog.Builder(context);
161 final Resources resources = context.getResources();
162 final StringBuilder message = new StringBuilder();
163 message.append(resources.getString(R.string.err_json_send_error_head))
167 if (API.valueOf(mProfile.getApiVersion())
170 resources.getString(R.string.err_json_send_error_unsupported));
172 message.append(resources.getString(R.string.err_json_send_error_tail));
173 builder.setPositiveButton(R.string.btn_profile_options, (dialog, which) -> {
174 Logger.debug("error", "will start profile editor");
175 ProfileDetailActivity.start(context, mProfile);
178 builder.setMessage(message);
183 Snackbar.make(list, error, Snackbar.LENGTH_INDEFINITE)
191 FocusedElement element = null;
192 if (savedInstanceState != null) {
193 keep |= savedInstanceState.getBoolean("keep", true);
194 focused = savedInstanceState.getInt("focused-item", 0);
195 element = FocusedElement.valueOf(savedInstanceState.getString("focused-element"));
201 viewModel.noteFocusChanged(focused, element);
204 ProgressBar p = activity.findViewById(R.id.progressBar);
205 viewModel.getBusyFlag()
206 .observe(getViewLifecycleOwner(), isBusy -> {
208 // Handler h = new Handler();
209 // h.postDelayed(() -> {
210 // if (viewModel.getBusyFlag())
211 // p.setVisibility(View.VISIBLE);
214 p.setVisibility(View.VISIBLE);
217 p.setVisibility(View.INVISIBLE);
220 if (activity instanceof FabManager.FabHandler)
221 FabManager.handle((FabManager.FabHandler) activity, list);
224 public void onSaveInstanceState(@NonNull Bundle outState) {
225 super.onSaveInstanceState(outState);
226 outState.putBoolean("keep", true);
227 final NewTransactionModel.FocusInfo focusInfo = viewModel.getFocusInfo()
229 final int focusedItem = focusInfo.position;
230 if (focusedItem >= 0)
231 outState.putInt("focused-item", focusedItem);
232 outState.putString("focused-element", focusInfo.element.toString());
236 public void onAttach(@NotNull Context context) {
237 super.onAttach(context);
238 if (context instanceof OnNewTransactionFragmentInteractionListener) {
239 mListener = (OnNewTransactionFragmentInteractionListener) context;
242 throw new RuntimeException(
243 context.toString() + " must implement OnFragmentInteractionListener");
248 public void onDetach() {
254 * This interface must be implemented by activities that contain this
255 * fragment to allow an interaction in this fragment to be communicated
256 * to the activity and potentially other fragments contained in that
259 * See the Android Training lesson <a href=
260 * "http://developer.android.com/training/basics/fragments/communicating.html"
261 * >Communicating with Other Fragments</a> for more information.
263 public interface OnNewTransactionFragmentInteractionListener {
264 void onTransactionSave(LedgerTransaction tr);