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.json.API;
45 import net.ktnx.mobileledger.model.Data;
46 import net.ktnx.mobileledger.model.LedgerTransaction;
47 import net.ktnx.mobileledger.model.MobileLedgerProfile;
48 import net.ktnx.mobileledger.ui.QR;
49 import net.ktnx.mobileledger.utils.Logger;
51 import org.jetbrains.annotations.NotNull;
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 OnNewTransactionFragmentInteractionListener mListener;
66 private MobileLedgerProfile mProfile;
67 public NewTransactionFragment() {
68 // Required empty public constructor
69 setHasOptionsMenu(true);
72 public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
73 super.onCreateOptionsMenu(menu, inflater);
74 final FragmentActivity activity = getActivity();
76 inflater.inflate(R.menu.new_transaction_fragment, menu);
78 menu.findItem(R.id.scan_qr)
79 .setOnMenuItemClickListener(this::onScanQrAction);
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.getShowCurrency()
94 .observe(activity, toggleCurrencyItem::setChecked);
96 final MenuItem toggleCommentsItem = menu.findItem(R.id.toggle_comments);
97 toggleCommentsItem.setOnMenuItemClickListener(item -> {
98 viewModel.toggleShowComments();
101 if (activity != null)
102 viewModel.getShowComments()
103 .observe(activity, toggleCommentsItem::setChecked);
105 private boolean onScanQrAction(MenuItem item) {
107 Context ctx = requireContext();
108 if (ctx instanceof QR.QRScanTrigger)
109 ((QR.QRScanTrigger) ctx).triggerQRScan();
111 catch (Exception e) {
112 Logger.debug("qr", "Error launching QR scanner", e);
118 public View onCreateView(LayoutInflater inflater, ViewGroup container,
119 Bundle savedInstanceState) {
120 // Inflate the layout for this fragment
121 return inflater.inflate(R.layout.fragment_new_transaction, container, false);
125 public void onViewCreated(@NotNull View view, @Nullable Bundle savedInstanceState) {
126 super.onViewCreated(view, savedInstanceState);
127 FragmentActivity activity = getActivity();
128 if (activity == null)
129 throw new RSInvalidStateException(
130 "getActivity() returned null within onActivityCreated()");
132 viewModel = new ViewModelProvider(activity).get(NewTransactionModel.class);
133 viewModel.observeDataProfile(this);
134 mProfile = Data.getProfile();
135 listAdapter = new NewTransactionItemsAdapter(viewModel, mProfile);
138 .observe(getViewLifecycleOwner(), newList -> listAdapter.setItems(newList));
140 RecyclerView list = activity.findViewById(R.id.new_transaction_accounts);
141 list.setAdapter(listAdapter);
142 list.setLayoutManager(new LinearLayoutManager(activity));
144 Data.observeProfile(getViewLifecycleOwner(), profile -> {
146 listAdapter.setProfile(profile);
148 boolean keep = false;
150 Bundle args = getArguments();
152 String error = args.getString("error");
154 Logger.debug("new-trans-f", String.format("Got error: %s", error));
156 Context context = getContext();
157 if (context != null) {
158 AlertDialog.Builder builder = new AlertDialog.Builder(context);
159 final Resources resources = context.getResources();
160 final StringBuilder message = new StringBuilder();
161 message.append(resources.getString(R.string.err_json_send_error_head));
162 message.append("\n\n");
163 message.append(error);
164 if (mProfile.getApiVersion()
167 resources.getString(R.string.err_json_send_error_unsupported));
169 message.append(resources.getString(R.string.err_json_send_error_tail));
170 builder.setPositiveButton(R.string.btn_profile_options, (dialog, which) -> {
171 Logger.debug("error", "will start profile editor");
172 MobileLedgerProfile.startEditProfileActivity(context, mProfile);
175 builder.setMessage(message);
180 Snackbar.make(list, error, Snackbar.LENGTH_INDEFINITE)
188 FocusedElement element = null;
189 if (savedInstanceState != null) {
190 keep |= savedInstanceState.getBoolean("keep", true);
191 focused = savedInstanceState.getInt("focused-item", 0);
192 element = FocusedElement.valueOf(savedInstanceState.getString("focused-element"));
198 viewModel.noteFocusChanged(focused, element);
201 ProgressBar p = activity.findViewById(R.id.progressBar);
202 viewModel.getBusyFlag()
203 .observe(getViewLifecycleOwner(), isBusy -> {
205 // Handler h = new Handler();
206 // h.postDelayed(() -> {
207 // if (viewModel.getBusyFlag())
208 // p.setVisibility(View.VISIBLE);
211 p.setVisibility(View.VISIBLE);
214 p.setVisibility(View.INVISIBLE);
218 public void onSaveInstanceState(@NonNull Bundle outState) {
219 super.onSaveInstanceState(outState);
220 outState.putBoolean("keep", true);
221 final NewTransactionModel.FocusInfo focusInfo = viewModel.getFocusInfo()
223 final int focusedItem = focusInfo.position;
224 if (focusedItem >= 0)
225 outState.putInt("focused-item", focusedItem);
226 outState.putString("focused-element", focusInfo.element.toString());
230 public void onAttach(@NotNull Context context) {
231 super.onAttach(context);
232 if (context instanceof OnNewTransactionFragmentInteractionListener) {
233 mListener = (OnNewTransactionFragmentInteractionListener) context;
236 throw new RuntimeException(
237 context.toString() + " must implement OnFragmentInteractionListener");
242 public void onDetach() {
248 * This interface must be implemented by activities that contain this
249 * fragment to allow an interaction in this fragment to be communicated
250 * to the activity and potentially other fragments contained in that
253 * See the Android Training lesson <a href=
254 * "http://developer.android.com/training/basics/fragments/communicating.html"
255 * >Communicating with Other Fragments</a> for more information.
257 public interface OnNewTransactionFragmentInteractionListener {
258 void onTransactionSave(LedgerTransaction tr);