]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionFragment.java
add a pattern flag for negation of amount
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / new_transaction / NewTransactionFragment.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.ui.new_transaction;
19
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.res.Resources;
23 import android.database.AbstractCursor;
24 import android.os.Bundle;
25 import android.os.ParcelFormatException;
26 import android.renderscript.RSInvalidStateException;
27 import android.view.LayoutInflater;
28 import android.view.Menu;
29 import android.view.MenuInflater;
30 import android.view.MenuItem;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.widget.ProgressBar;
34
35 import androidx.annotation.NonNull;
36 import androidx.annotation.Nullable;
37 import androidx.appcompat.app.AlertDialog;
38 import androidx.fragment.app.Fragment;
39 import androidx.fragment.app.FragmentActivity;
40 import androidx.lifecycle.LiveData;
41 import androidx.lifecycle.ViewModelProvider;
42 import androidx.recyclerview.widget.LinearLayoutManager;
43 import androidx.recyclerview.widget.RecyclerView;
44
45 import com.google.android.material.dialog.MaterialAlertDialogBuilder;
46 import com.google.android.material.floatingactionbutton.FloatingActionButton;
47 import com.google.android.material.snackbar.BaseTransientBottomBar;
48 import com.google.android.material.snackbar.Snackbar;
49
50 import net.ktnx.mobileledger.R;
51 import net.ktnx.mobileledger.db.DB;
52 import net.ktnx.mobileledger.db.PatternAccount;
53 import net.ktnx.mobileledger.db.PatternHeader;
54 import net.ktnx.mobileledger.json.API;
55 import net.ktnx.mobileledger.model.Data;
56 import net.ktnx.mobileledger.model.LedgerTransaction;
57 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
58 import net.ktnx.mobileledger.model.MobileLedgerProfile;
59 import net.ktnx.mobileledger.ui.QRScanCapableFragment;
60 import net.ktnx.mobileledger.ui.patterns.PatternsActivity;
61 import net.ktnx.mobileledger.utils.Logger;
62 import net.ktnx.mobileledger.utils.Misc;
63 import net.ktnx.mobileledger.utils.SimpleDate;
64
65 import org.jetbrains.annotations.NotNull;
66
67 import java.util.ArrayList;
68 import java.util.List;
69 import java.util.Locale;
70 import java.util.regex.Matcher;
71 import java.util.regex.Pattern;
72
73 /**
74  * A simple {@link Fragment} subclass.
75  * Activities that contain this fragment must implement the
76  * {@link OnNewTransactionFragmentInteractionListener} interface
77  * to handle interaction events.
78  */
79
80 // TODO: offer to undo account remove-on-swipe
81
82 public class NewTransactionFragment extends QRScanCapableFragment {
83     private NewTransactionItemsAdapter listAdapter;
84     private NewTransactionModel viewModel;
85     private FloatingActionButton fab;
86     private OnNewTransactionFragmentInteractionListener mListener;
87     private MobileLedgerProfile mProfile;
88     public NewTransactionFragment() {
89         // Required empty public constructor
90         setHasOptionsMenu(true);
91     }
92     private void startNewPatternActivity(String scanned) {
93         Intent intent = new Intent(requireContext(), PatternsActivity.class);
94         Bundle args = new Bundle();
95         args.putString(PatternsActivity.ARG_ADD_PATTERN, scanned);
96         requireContext().startActivity(intent, args);
97     }
98     private void alertNoPatternMatch(String scanned) {
99         MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(requireContext());
100         builder.setCancelable(true)
101                .setMessage(R.string.no_pattern_matches)
102                .setPositiveButton(R.string.add_button,
103                        (dialog, which) -> startNewPatternActivity(scanned))
104                .create()
105                .show();
106     }
107     protected void onQrScanned(String text) {
108         Logger.debug("qr", String.format("Got QR scan result [%s]", text));
109
110         if (Misc.emptyIsNull(text) == null)
111             return;
112
113         LiveData<List<PatternHeader>> allPatterns = DB.get()
114                                                       .getPatternDAO()
115                                                       .getPatterns();
116         allPatterns.observe(getViewLifecycleOwner(), patternHeaders -> {
117             ArrayList<PatternHeader> matchingPatterns = new ArrayList<>();
118
119             for (PatternHeader ph : patternHeaders) {
120                 String patternSource = ph.getRegularExpression();
121                 if (Misc.emptyIsNull(patternSource) == null)
122                     continue;
123                 try {
124                     Pattern pattern = Pattern.compile(patternSource);
125                     Matcher matcher = pattern.matcher(text);
126                     if (!matcher.matches())
127                         continue;
128
129                     Logger.debug("pattern",
130                             String.format("Pattern '%s' [%s] matches '%s'", ph.getName(),
131                                     patternSource, text));
132                     matchingPatterns.add(ph);
133                 }
134                 catch (ParcelFormatException e) {
135                     // ignored
136                     Logger.debug("pattern",
137                             String.format("Error compiling regular expression '%s'", patternSource),
138                             e);
139                 }
140             }
141
142             if (matchingPatterns.isEmpty())
143                 alertNoPatternMatch(text);
144             else if (matchingPatterns.size() == 1)
145                 applyPattern(matchingPatterns.get(0), text);
146             else
147                 choosePattern(matchingPatterns, text);
148         });
149     }
150     private void choosePattern(ArrayList<PatternHeader> matchingPatterns, String matchedText) {
151         final String patternNameColumn = getString(R.string.pattern_name);
152         AbstractCursor cursor = new AbstractCursor() {
153             @Override
154             public int getCount() {
155                 return matchingPatterns.size();
156             }
157             @Override
158             public String[] getColumnNames() {
159                 return new String[]{patternNameColumn};
160             }
161             @Override
162             public String getString(int column) {
163                 return matchingPatterns.get(getPosition())
164                                        .getName();
165             }
166             @Override
167             public short getShort(int column) {
168                 return -1;
169             }
170             @Override
171             public int getInt(int column) {
172                 return -1;
173             }
174             @Override
175             public long getLong(int column) {
176                 return -1;
177             }
178             @Override
179             public float getFloat(int column) {
180                 return -1;
181             }
182             @Override
183             public double getDouble(int column) {
184                 return -1;
185             }
186             @Override
187             public boolean isNull(int column) {
188                 return false;
189             }
190         };
191
192         MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(requireContext());
193         builder.setCancelable(true)
194                .setTitle(R.string.choose_pattern_to_apply)
195                .setSingleChoiceItems(cursor, -1, patternNameColumn,
196                        (dialog, which) -> applyPattern(matchingPatterns.get(which), matchedText))
197                .create()
198                .show();
199     }
200     private void applyPattern(PatternHeader patternHeader, String text) {
201         Pattern pattern = Pattern.compile(patternHeader.getRegularExpression());
202
203         Matcher m = pattern.matcher(text);
204
205         if (!m.matches()) {
206             Snackbar.make(requireView(), R.string.pattern_does_not_match,
207                     BaseTransientBottomBar.LENGTH_INDEFINITE)
208                     .show();
209             return;
210         }
211
212         SimpleDate transactionDate;
213         {
214             int day = extractIntFromMatches(m, patternHeader.getDateDayMatchGroup(),
215                     patternHeader.getDateDay());
216             int month = extractIntFromMatches(m, patternHeader.getDateMonthMatchGroup(),
217                     patternHeader.getDateMonth());
218             int year = extractIntFromMatches(m, patternHeader.getDateYearMatchGroup(),
219                     patternHeader.getDateYear());
220
221             SimpleDate today = SimpleDate.today();
222             if (year <= 0)
223                 year = today.year;
224             if (month <= 0)
225                 month = today.month;
226             if (day <= 0)
227                 day = today.day;
228
229             transactionDate = new SimpleDate(year, month, day);
230
231             Logger.debug("pattern", "setting transaction date to " + transactionDate);
232         }
233
234         NewTransactionModel.Item head = viewModel.getItem(0);
235         head.ensureType(NewTransactionModel.ItemType.generalData);
236         final String transactionDescription =
237                 extractStringFromMatches(m, patternHeader.getTransactionDescriptionMatchGroup(),
238                         patternHeader.getTransactionDescription());
239         head.setDescription(transactionDescription);
240         Logger.debug("pattern", "Setting transaction description to " + transactionDescription);
241         final String transactionComment =
242                 extractStringFromMatches(m, patternHeader.getTransactionCommentMatchGroup(),
243                         patternHeader.getTransactionComment());
244         head.setTransactionComment(transactionComment);
245         Logger.debug("pattern", "Setting transaction comment to " + transactionComment);
246         head.setDate(transactionDate);
247         listAdapter.notifyItemChanged(0);
248
249         DB.get()
250           .getPatternDAO()
251           .getPatternWithAccounts(patternHeader.getId())
252           .observe(getViewLifecycleOwner(), entry -> {
253               int rowIndex = 0;
254               final boolean accountsInInitialState = viewModel.accountsInInitialState();
255               for (PatternAccount acc : entry.accounts) {
256                   rowIndex++;
257
258                   String accountName = extractStringFromMatches(m, acc.getAccountNameMatchGroup(),
259                           acc.getAccountName());
260                   String accountComment =
261                           extractStringFromMatches(m, acc.getAccountCommentMatchGroup(),
262                                   acc.getAccountComment());
263                   Float amount =
264                           extractFloatFromMatches(m, acc.getAmountMatchGroup(), acc.getAmount());
265                   if (amount != null && acc.getNegateAmount())
266                       amount = -amount;
267
268                   if (accountsInInitialState) {
269                       NewTransactionModel.Item item = viewModel.getItem(rowIndex);
270                       if (item == null) {
271                           Logger.debug("pattern", String.format(Locale.US,
272                                   "Adding new account item [%s][c:%s][a:%s]", accountName,
273                                   accountComment, amount));
274                           final LedgerTransactionAccount ledgerAccount =
275                                   new LedgerTransactionAccount(accountName);
276                           ledgerAccount.setComment(accountComment);
277                           if (amount != null)
278                               ledgerAccount.setAmount(amount);
279                           // TODO currency
280                           viewModel.addAccount(ledgerAccount);
281                           listAdapter.notifyItemInserted(viewModel.items.size() - 1);
282                       }
283                       else {
284                           Logger.debug("pattern", String.format(Locale.US,
285                                   "Stamping account item #%d [%s][c:%s][a:%s]", rowIndex,
286                                   accountName, accountComment, amount));
287
288                           item.setAccountName(accountName);
289                           item.setComment(accountComment);
290                           if (amount != null)
291                               item.getAccount()
292                                   .setAmount(amount);
293
294                           listAdapter.notifyItemChanged(rowIndex);
295                       }
296                   }
297                   else {
298                       final LedgerTransactionAccount transactionAccount =
299                               new LedgerTransactionAccount(accountName);
300                       transactionAccount.setComment(accountComment);
301                       if (amount != null)
302                           transactionAccount.setAmount(amount);
303                       // TODO currency
304                       Logger.debug("pattern", String.format(Locale.US,
305                               "Adding trailing account item [%s][c:%s][a:%s]", accountName,
306                               accountComment, amount));
307
308                       viewModel.addAccount(transactionAccount);
309                       listAdapter.notifyItemInserted(viewModel.items.size() - 1);
310                   }
311               }
312
313               listAdapter.checkTransactionSubmittable();
314           });
315     }
316     private int extractIntFromMatches(Matcher m, Integer group, Integer literal) {
317         if (literal != null)
318             return literal;
319
320         if (group != null) {
321             int grp = group;
322             if (grp > 0 & grp <= m.groupCount())
323                 try {
324                     return Integer.parseInt(m.group(grp));
325                 }
326                 catch (NumberFormatException e) {
327                     Snackbar.make(requireView(),
328                             "Error extracting transaction date: " + e.getMessage(),
329                             BaseTransientBottomBar.LENGTH_INDEFINITE)
330                             .show();
331                 }
332         }
333
334         return 0;
335     }
336     private String extractStringFromMatches(Matcher m, Integer group, String literal) {
337         if (literal != null)
338             return literal;
339
340         if (group != null) {
341             int grp = group;
342             if (grp > 0 & grp <= m.groupCount())
343                 return m.group(grp);
344         }
345
346         return null;
347     }
348     private Float extractFloatFromMatches(Matcher m, Integer group, Float literal) {
349         if (literal != null)
350             return literal;
351
352         if (group != null) {
353             int grp = group;
354             if (grp > 0 & grp <= m.groupCount())
355                 try {
356                     return Float.valueOf(m.group(grp));
357                 }
358                 catch (NumberFormatException e) {
359                     Snackbar.make(requireView(),
360                             "Error extracting transaction amount: " + e.getMessage(),
361                             BaseTransientBottomBar.LENGTH_INDEFINITE)
362                             .show();
363                 }
364         }
365
366         return null;
367     }
368     @Override
369     public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
370         super.onCreateOptionsMenu(menu, inflater);
371         final FragmentActivity activity = getActivity();
372
373         inflater.inflate(R.menu.new_transaction_fragment, menu);
374
375         menu.findItem(R.id.scan_qr)
376             .setOnMenuItemClickListener(this::onScanQrAction);
377
378         menu.findItem(R.id.action_reset_new_transaction_activity)
379             .setOnMenuItemClickListener(item -> {
380                 listAdapter.reset();
381                 return true;
382             });
383
384         final MenuItem toggleCurrencyItem = menu.findItem(R.id.toggle_currency);
385         toggleCurrencyItem.setOnMenuItemClickListener(item -> {
386             viewModel.toggleCurrencyVisible();
387             return true;
388         });
389         if (activity != null)
390             viewModel.showCurrency.observe(activity, toggleCurrencyItem::setChecked);
391
392         final MenuItem toggleCommentsItem = menu.findItem(R.id.toggle_comments);
393         toggleCommentsItem.setOnMenuItemClickListener(item -> {
394             viewModel.toggleShowComments();
395             return true;
396         });
397         if (activity != null)
398             viewModel.showComments.observe(activity, toggleCommentsItem::setChecked);
399     }
400     private boolean onScanQrAction(MenuItem item) {
401         try {
402             scanQrLauncher.launch(null);
403         }
404         catch (Exception e) {
405             Logger.debug("qr", "Error launching QR scanner", e);
406         }
407
408         return true;
409     }
410     @Override
411     public View onCreateView(LayoutInflater inflater, ViewGroup container,
412                              Bundle savedInstanceState) {
413         // Inflate the layout for this fragment
414         return inflater.inflate(R.layout.fragment_new_transaction, container, false);
415     }
416
417     @Override
418     public void onViewCreated(@NotNull View view, @Nullable Bundle savedInstanceState) {
419         super.onViewCreated(view, savedInstanceState);
420         FragmentActivity activity = getActivity();
421         if (activity == null)
422             throw new RSInvalidStateException(
423                     "getActivity() returned null within onActivityCreated()");
424
425         viewModel = new ViewModelProvider(activity).get(NewTransactionModel.class);
426         viewModel.observeDataProfile(this);
427         mProfile = Data.getProfile();
428         listAdapter = new NewTransactionItemsAdapter(viewModel, mProfile);
429
430         RecyclerView list = activity.findViewById(R.id.new_transaction_accounts);
431         list.setAdapter(listAdapter);
432         list.setLayoutManager(new LinearLayoutManager(activity));
433
434         Data.observeProfile(getViewLifecycleOwner(), profile -> {
435             mProfile = profile;
436             listAdapter.setProfile(profile);
437         });
438         listAdapter.notifyDataSetChanged();
439         viewModel.isSubmittable()
440                  .observe(getViewLifecycleOwner(), isSubmittable -> {
441                      if (isSubmittable) {
442                          if (fab != null) {
443                              fab.show();
444                          }
445                      }
446                      else {
447                          if (fab != null) {
448                              fab.hide();
449                          }
450                      }
451                  });
452 //        viewModel.checkTransactionSubmittable(listAdapter);
453
454         fab = activity.findViewById(R.id.fabAdd);
455         fab.setOnClickListener(v -> onFabPressed());
456
457         boolean keep = false;
458
459         Bundle args = getArguments();
460         if (args != null) {
461             String error = args.getString("error");
462             if (error != null) {
463                 Logger.debug("new-trans-f", String.format("Got error: %s", error));
464
465                 Context context = getContext();
466                 if (context != null) {
467                     AlertDialog.Builder builder = new AlertDialog.Builder(context);
468                     final Resources resources = context.getResources();
469                     final StringBuilder message = new StringBuilder();
470                     message.append(resources.getString(R.string.err_json_send_error_head));
471                     message.append("\n\n");
472                     message.append(error);
473                     if (mProfile.getApiVersion()
474                                 .equals(API.auto))
475                         message.append(
476                                 resources.getString(R.string.err_json_send_error_unsupported));
477                     else {
478                         message.append(resources.getString(R.string.err_json_send_error_tail));
479                         builder.setPositiveButton(R.string.btn_profile_options, (dialog, which) -> {
480                             Logger.debug("error", "will start profile editor");
481                             MobileLedgerProfile.startEditProfileActivity(context, mProfile);
482                         });
483                     }
484                     builder.setMessage(message);
485                     builder.create()
486                            .show();
487                 }
488                 else {
489                     Snackbar.make(list, error, Snackbar.LENGTH_INDEFINITE)
490                             .show();
491                 }
492                 keep = true;
493             }
494         }
495
496         int focused = 0;
497         if (savedInstanceState != null) {
498             keep |= savedInstanceState.getBoolean("keep", true);
499             focused = savedInstanceState.getInt("focused", 0);
500         }
501
502         if (!keep)
503             viewModel.reset();
504         else {
505             viewModel.setFocusedItem(focused);
506         }
507
508         ProgressBar p = activity.findViewById(R.id.progressBar);
509         viewModel.observeBusyFlag(getViewLifecycleOwner(), isBusy -> {
510             if (isBusy) {
511 //                Handler h = new Handler();
512 //                h.postDelayed(() -> {
513 //                    if (viewModel.getBusyFlag())
514 //                        p.setVisibility(View.VISIBLE);
515 //
516 //                }, 10);
517                 p.setVisibility(View.VISIBLE);
518             }
519             else
520                 p.setVisibility(View.INVISIBLE);
521         });
522     }
523     @Override
524     public void onSaveInstanceState(@NonNull Bundle outState) {
525         super.onSaveInstanceState(outState);
526         outState.putBoolean("keep", true);
527         final int focusedItem = viewModel.getFocusedItem();
528         outState.putInt("focused", focusedItem);
529     }
530     private void onFabPressed() {
531         fab.hide();
532         Misc.hideSoftKeyboard(this);
533         if (mListener != null) {
534             SimpleDate date = viewModel.getDate();
535             LedgerTransaction tr =
536                     new LedgerTransaction(null, date, viewModel.getDescription(), mProfile);
537
538             tr.setComment(viewModel.getComment());
539             LedgerTransactionAccount emptyAmountAccount = null;
540             float emptyAmountAccountBalance = 0;
541             for (int i = 0; i < viewModel.getAccountCount(); i++) {
542                 LedgerTransactionAccount acc =
543                         new LedgerTransactionAccount(viewModel.getAccount(i));
544                 if (acc.getAccountName()
545                        .trim()
546                        .isEmpty())
547                     continue;
548
549                 if (acc.isAmountSet()) {
550                     emptyAmountAccountBalance += acc.getAmount();
551                 }
552                 else {
553                     emptyAmountAccount = acc;
554                 }
555
556                 tr.addAccount(acc);
557             }
558
559             if (emptyAmountAccount != null)
560                 emptyAmountAccount.setAmount(-emptyAmountAccountBalance);
561
562             mListener.onTransactionSave(tr);
563         }
564     }
565
566     @Override
567     public void onAttach(@NotNull Context context) {
568         super.onAttach(context);
569         if (context instanceof OnNewTransactionFragmentInteractionListener) {
570             mListener = (OnNewTransactionFragmentInteractionListener) context;
571         }
572         else {
573             throw new RuntimeException(
574                     context.toString() + " must implement OnFragmentInteractionListener");
575         }
576     }
577
578     @Override
579     public void onDetach() {
580         super.onDetach();
581         mListener = null;
582     }
583
584     /**
585      * This interface must be implemented by activities that contain this
586      * fragment to allow an interaction in this fragment to be communicated
587      * to the activity and potentially other fragments contained in that
588      * activity.
589      * <p>
590      * See the Android Training lesson <a href=
591      * "http://developer.android.com/training/basics/fragments/communicating.html"
592      * >Communicating with Other Fragments</a> for more information.
593      */
594     public interface OnNewTransactionFragmentInteractionListener {
595         void onTransactionSave(LedgerTransaction tr);
596     }
597 }