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.app.Activity;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.res.Resources;
24 import android.os.Bundle;
25 import android.renderscript.RSInvalidStateException;
26 import android.view.LayoutInflater;
27 import android.view.Menu;
28 import android.view.MenuInflater;
29 import android.view.MenuItem;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.widget.ProgressBar;
34 import androidx.activity.result.ActivityResultLauncher;
35 import androidx.activity.result.contract.ActivityResultContract;
36 import androidx.annotation.NonNull;
37 import androidx.annotation.Nullable;
38 import androidx.appcompat.app.AlertDialog;
39 import androidx.fragment.app.Fragment;
40 import androidx.fragment.app.FragmentActivity;
41 import androidx.lifecycle.ViewModelProvider;
42 import androidx.recyclerview.widget.LinearLayoutManager;
43 import androidx.recyclerview.widget.RecyclerView;
45 import com.google.android.material.floatingactionbutton.FloatingActionButton;
46 import com.google.android.material.snackbar.Snackbar;
48 import net.ktnx.mobileledger.R;
49 import net.ktnx.mobileledger.json.API;
50 import net.ktnx.mobileledger.model.Data;
51 import net.ktnx.mobileledger.model.LedgerTransaction;
52 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
53 import net.ktnx.mobileledger.model.MobileLedgerProfile;
54 import net.ktnx.mobileledger.utils.Logger;
55 import net.ktnx.mobileledger.utils.Misc;
56 import net.ktnx.mobileledger.utils.SimpleDate;
58 import org.jetbrains.annotations.NotNull;
60 import java.util.regex.Matcher;
61 import java.util.regex.Pattern;
64 * A simple {@link Fragment} subclass.
65 * Activities that contain this fragment must implement the
66 * {@link OnNewTransactionFragmentInteractionListener} interface
67 * to handle interaction events.
70 // TODO: offer to undo account remove-on-swipe
72 public class NewTransactionFragment extends Fragment {
73 private NewTransactionItemsAdapter listAdapter;
74 private NewTransactionModel viewModel;
75 final ActivityResultLauncher<Void> scanQrLauncher =
76 registerForActivityResult(new ActivityResultContract<Void, String>() {
79 public Intent createIntent(@NonNull Context context, Void input) {
80 final Intent intent = new Intent("com.google.zxing.client.android.SCAN");
81 intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
85 public String parseResult(int resultCode, @Nullable Intent intent) {
86 if (resultCode == Activity.RESULT_CANCELED)
88 return intent.getStringExtra("SCAN_RESULT");
90 }, this::onQrScanned);
91 private FloatingActionButton fab;
92 private OnNewTransactionFragmentInteractionListener mListener;
93 private MobileLedgerProfile mProfile;
94 public NewTransactionFragment() {
95 // Required empty public constructor
96 setHasOptionsMenu(true);
98 private void onQrScanned(String text) {
99 Logger.debug("qr", String.format("Got QR scan result [%s]", text));
101 Pattern.compile("^(\\d+)\\*(\\d+)\\*(\\d+)-(\\d+)-(\\d+)\\*([:\\d]+)\\*([\\d.]+)$");
102 Matcher m = p.matcher(text);
104 float amount = Float.parseFloat(m.group(7));
106 new SimpleDate(Integer.parseInt(m.group(3)), Integer.parseInt(m.group(4)),
107 Integer.parseInt(m.group(5))));
109 if (viewModel.accountsInInitialState()) {
111 NewTransactionModel.Item firstItem = viewModel.getItem(1);
112 if (firstItem == null) {
113 viewModel.addAccount(new LedgerTransactionAccount("разход:пазар"));
114 listAdapter.notifyItemInserted(viewModel.items.size() - 1);
117 firstItem.setAccountName("разход:пазар");
118 firstItem.getAccount()
120 listAdapter.notifyItemChanged(1);
124 NewTransactionModel.Item secondItem = viewModel.getItem(2);
125 if (secondItem == null) {
126 viewModel.addAccount(
127 new LedgerTransactionAccount("актив:кеш:дам", -amount, null, null));
128 listAdapter.notifyItemInserted(viewModel.items.size() - 1);
131 secondItem.setAccountName("актив:кеш:дам");
132 secondItem.getAccount()
134 listAdapter.notifyItemChanged(2);
139 viewModel.addAccount(new LedgerTransactionAccount("разход:пазар"));
140 viewModel.addAccount(
141 new LedgerTransactionAccount("актив:кеш:дам", -amount, null, null));
142 listAdapter.notifyItemRangeInserted(viewModel.items.size() - 1, 2);
145 listAdapter.checkTransactionSubmittable();
149 public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
150 super.onCreateOptionsMenu(menu, inflater);
151 final FragmentActivity activity = getActivity();
153 inflater.inflate(R.menu.new_transaction_fragment, menu);
155 menu.findItem(R.id.scan_qr)
156 .setOnMenuItemClickListener(this::onScanQrAction);
158 menu.findItem(R.id.action_reset_new_transaction_activity)
159 .setOnMenuItemClickListener(item -> {
164 final MenuItem toggleCurrencyItem = menu.findItem(R.id.toggle_currency);
165 toggleCurrencyItem.setOnMenuItemClickListener(item -> {
166 viewModel.toggleCurrencyVisible();
169 if (activity != null)
170 viewModel.showCurrency.observe(activity, toggleCurrencyItem::setChecked);
172 final MenuItem toggleCommentsItem = menu.findItem(R.id.toggle_comments);
173 toggleCommentsItem.setOnMenuItemClickListener(item -> {
174 viewModel.toggleShowComments();
177 if (activity != null)
178 viewModel.showComments.observe(activity, toggleCommentsItem::setChecked);
180 private boolean onScanQrAction(MenuItem item) {
182 scanQrLauncher.launch(null);
184 catch (Exception e) {
185 Logger.debug("qr", "Error launching QR scanner", e);
191 public View onCreateView(LayoutInflater inflater, ViewGroup container,
192 Bundle savedInstanceState) {
193 // Inflate the layout for this fragment
194 return inflater.inflate(R.layout.fragment_new_transaction, container, false);
198 public void onViewCreated(@NotNull View view, @Nullable Bundle savedInstanceState) {
199 super.onViewCreated(view, savedInstanceState);
200 FragmentActivity activity = getActivity();
201 if (activity == null)
202 throw new RSInvalidStateException(
203 "getActivity() returned null within onActivityCreated()");
205 viewModel = new ViewModelProvider(activity).get(NewTransactionModel.class);
206 viewModel.observeDataProfile(this);
207 mProfile = Data.getProfile();
208 listAdapter = new NewTransactionItemsAdapter(viewModel, mProfile);
210 RecyclerView list = activity.findViewById(R.id.new_transaction_accounts);
211 list.setAdapter(listAdapter);
212 list.setLayoutManager(new LinearLayoutManager(activity));
214 Data.observeProfile(getViewLifecycleOwner(), profile -> {
216 listAdapter.setProfile(profile);
218 listAdapter.notifyDataSetChanged();
219 viewModel.isSubmittable()
220 .observe(getViewLifecycleOwner(), isSubmittable -> {
232 // viewModel.checkTransactionSubmittable(listAdapter);
234 fab = activity.findViewById(R.id.fab);
235 fab.setOnClickListener(v -> onFabPressed());
237 boolean keep = false;
239 Bundle args = getArguments();
241 String error = args.getString("error");
243 Logger.debug("new-trans-f", String.format("Got error: %s", error));
245 Context context = getContext();
246 if (context != null) {
247 AlertDialog.Builder builder = new AlertDialog.Builder(context);
248 final Resources resources = context.getResources();
249 final StringBuilder message = new StringBuilder();
250 message.append(resources.getString(R.string.err_json_send_error_head));
251 message.append("\n\n");
252 message.append(error);
253 if (mProfile.getApiVersion()
256 resources.getString(R.string.err_json_send_error_unsupported));
258 message.append(resources.getString(R.string.err_json_send_error_tail));
259 builder.setPositiveButton(R.string.btn_profile_options, (dialog, which) -> {
260 Logger.debug("error", "will start profile editor");
261 MobileLedgerProfile.startEditProfileActivity(context, mProfile);
264 builder.setMessage(message);
269 Snackbar.make(list, error, Snackbar.LENGTH_INDEFINITE)
277 if (savedInstanceState != null) {
278 keep |= savedInstanceState.getBoolean("keep", true);
279 focused = savedInstanceState.getInt("focused", 0);
285 viewModel.setFocusedItem(focused);
288 ProgressBar p = activity.findViewById(R.id.progressBar);
289 viewModel.observeBusyFlag(getViewLifecycleOwner(), isBusy -> {
291 // Handler h = new Handler();
292 // h.postDelayed(() -> {
293 // if (viewModel.getBusyFlag())
294 // p.setVisibility(View.VISIBLE);
297 p.setVisibility(View.VISIBLE);
300 p.setVisibility(View.INVISIBLE);
304 public void onSaveInstanceState(@NonNull Bundle outState) {
305 super.onSaveInstanceState(outState);
306 outState.putBoolean("keep", true);
307 final int focusedItem = viewModel.getFocusedItem();
308 outState.putInt("focused", focusedItem);
310 private void onFabPressed() {
312 Misc.hideSoftKeyboard(this);
313 if (mListener != null) {
314 SimpleDate date = viewModel.getDate();
315 LedgerTransaction tr =
316 new LedgerTransaction(null, date, viewModel.getDescription(), mProfile);
318 tr.setComment(viewModel.getComment());
319 LedgerTransactionAccount emptyAmountAccount = null;
320 float emptyAmountAccountBalance = 0;
321 for (int i = 0; i < viewModel.getAccountCount(); i++) {
322 LedgerTransactionAccount acc =
323 new LedgerTransactionAccount(viewModel.getAccount(i));
324 if (acc.getAccountName()
329 if (acc.isAmountSet()) {
330 emptyAmountAccountBalance += acc.getAmount();
333 emptyAmountAccount = acc;
339 if (emptyAmountAccount != null)
340 emptyAmountAccount.setAmount(-emptyAmountAccountBalance);
342 mListener.onTransactionSave(tr);
347 public void onAttach(@NotNull Context context) {
348 super.onAttach(context);
349 if (context instanceof OnNewTransactionFragmentInteractionListener) {
350 mListener = (OnNewTransactionFragmentInteractionListener) context;
353 throw new RuntimeException(
354 context.toString() + " must implement OnFragmentInteractionListener");
359 public void onDetach() {
365 * This interface must be implemented by activities that contain this
366 * fragment to allow an interaction in this fragment to be communicated
367 * to the activity and potentially other fragments contained in that
370 * See the Android Training lesson <a href=
371 * "http://developer.android.com/training/basics/fragments/communicating.html"
372 * >Communicating with Other Fragments</a> for more information.
374 public interface OnNewTransactionFragmentInteractionListener {
375 void onTransactionSave(LedgerTransaction tr);