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.FabManager;
49 import net.ktnx.mobileledger.ui.QR;
50 import net.ktnx.mobileledger.utils.Logger;
52 import org.jetbrains.annotations.NotNull;
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
63 public class NewTransactionFragment extends Fragment {
64 private NewTransactionItemsAdapter listAdapter;
65 private NewTransactionModel viewModel;
66 private OnNewTransactionFragmentInteractionListener mListener;
67 private MobileLedgerProfile mProfile;
68 public NewTransactionFragment() {
69 // Required empty public constructor
70 setHasOptionsMenu(true);
73 public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
74 super.onCreateOptionsMenu(menu, inflater);
75 final FragmentActivity activity = getActivity();
77 inflater.inflate(R.menu.new_transaction_fragment, menu);
79 menu.findItem(R.id.scan_qr)
80 .setOnMenuItemClickListener(this::onScanQrAction);
82 menu.findItem(R.id.action_reset_new_transaction_activity)
83 .setOnMenuItemClickListener(item -> {
88 final MenuItem toggleCurrencyItem = menu.findItem(R.id.toggle_currency);
89 toggleCurrencyItem.setOnMenuItemClickListener(item -> {
90 viewModel.toggleCurrencyVisible();
94 viewModel.getShowCurrency()
95 .observe(activity, toggleCurrencyItem::setChecked);
97 final MenuItem toggleCommentsItem = menu.findItem(R.id.toggle_comments);
98 toggleCommentsItem.setOnMenuItemClickListener(item -> {
99 viewModel.toggleShowComments();
102 if (activity != null)
103 viewModel.getShowComments()
104 .observe(activity, toggleCommentsItem::setChecked);
106 private boolean onScanQrAction(MenuItem item) {
108 Context ctx = requireContext();
109 if (ctx instanceof QR.QRScanTrigger)
110 ((QR.QRScanTrigger) ctx).triggerQRScan();
112 catch (Exception e) {
113 Logger.debug("qr", "Error launching QR scanner", e);
119 public View onCreateView(LayoutInflater inflater, ViewGroup container,
120 Bundle savedInstanceState) {
121 // Inflate the layout for this fragment
122 return inflater.inflate(R.layout.fragment_new_transaction, container, false);
126 public void onViewCreated(@NotNull View view, @Nullable Bundle savedInstanceState) {
127 super.onViewCreated(view, savedInstanceState);
128 FragmentActivity activity = getActivity();
129 if (activity == null)
130 throw new RSInvalidStateException(
131 "getActivity() returned null within onActivityCreated()");
133 viewModel = new ViewModelProvider(activity).get(NewTransactionModel.class);
134 viewModel.observeDataProfile(this);
135 mProfile = Data.getProfile();
136 listAdapter = new NewTransactionItemsAdapter(viewModel, mProfile);
139 .observe(getViewLifecycleOwner(), newList -> listAdapter.setItems(newList));
141 RecyclerView list = activity.findViewById(R.id.new_transaction_accounts);
142 list.setAdapter(listAdapter);
143 list.setLayoutManager(new LinearLayoutManager(activity));
145 Data.observeProfile(getViewLifecycleOwner(), profile -> {
147 listAdapter.setProfile(profile);
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));
157 Context context = getContext();
158 if (context != null) {
159 AlertDialog.Builder builder = new AlertDialog.Builder(context);
160 final Resources resources = context.getResources();
161 final StringBuilder message = new StringBuilder();
162 message.append(resources.getString(R.string.err_json_send_error_head))
166 if (mProfile.getApiVersion()
169 resources.getString(R.string.err_json_send_error_unsupported));
171 message.append(resources.getString(R.string.err_json_send_error_tail));
172 builder.setPositiveButton(R.string.btn_profile_options, (dialog, which) -> {
173 Logger.debug("error", "will start profile editor");
174 MobileLedgerProfile.startEditProfileActivity(context, mProfile);
177 builder.setMessage(message);
182 Snackbar.make(list, error, Snackbar.LENGTH_INDEFINITE)
190 FocusedElement element = null;
191 if (savedInstanceState != null) {
192 keep |= savedInstanceState.getBoolean("keep", true);
193 focused = savedInstanceState.getInt("focused-item", 0);
194 element = FocusedElement.valueOf(savedInstanceState.getString("focused-element"));
200 viewModel.noteFocusChanged(focused, element);
203 ProgressBar p = activity.findViewById(R.id.progressBar);
204 viewModel.getBusyFlag()
205 .observe(getViewLifecycleOwner(), isBusy -> {
207 // Handler h = new Handler();
208 // h.postDelayed(() -> {
209 // if (viewModel.getBusyFlag())
210 // p.setVisibility(View.VISIBLE);
213 p.setVisibility(View.VISIBLE);
216 p.setVisibility(View.INVISIBLE);
219 if (activity instanceof FabManager.FabHandler)
220 FabManager.handle((FabManager.FabHandler) activity, list);
223 public void onSaveInstanceState(@NonNull Bundle outState) {
224 super.onSaveInstanceState(outState);
225 outState.putBoolean("keep", true);
226 final NewTransactionModel.FocusInfo focusInfo = viewModel.getFocusInfo()
228 final int focusedItem = focusInfo.position;
229 if (focusedItem >= 0)
230 outState.putInt("focused-item", focusedItem);
231 outState.putString("focused-element", focusInfo.element.toString());
235 public void onAttach(@NotNull Context context) {
236 super.onAttach(context);
237 if (context instanceof OnNewTransactionFragmentInteractionListener) {
238 mListener = (OnNewTransactionFragmentInteractionListener) context;
241 throw new RuntimeException(
242 context.toString() + " must implement OnFragmentInteractionListener");
247 public void onDetach() {
253 * This interface must be implemented by activities that contain this
254 * fragment to allow an interaction in this fragment to be communicated
255 * to the activity and potentially other fragments contained in that
258 * See the Android Training lesson <a href=
259 * "http://developer.android.com/training/basics/fragments/communicating.html"
260 * >Communicating with Other Fragments</a> for more information.
262 public interface OnNewTransactionFragmentInteractionListener {
263 void onTransactionSave(LedgerTransaction tr);