2 * Copyright © 2019 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.activity;
20 import android.annotation.SuppressLint;
21 import android.text.Editable;
22 import android.text.TextWatcher;
23 import android.view.View;
24 import android.view.inputmethod.EditorInfo;
25 import android.widget.AutoCompleteTextView;
26 import android.widget.FrameLayout;
27 import android.widget.LinearLayout;
28 import android.widget.TextView;
30 import androidx.annotation.NonNull;
31 import androidx.constraintlayout.widget.ConstraintLayout;
32 import androidx.lifecycle.Observer;
33 import androidx.recyclerview.widget.RecyclerView;
35 import net.ktnx.mobileledger.R;
36 import net.ktnx.mobileledger.async.DescriptionSelectedCallback;
37 import net.ktnx.mobileledger.model.Data;
38 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
39 import net.ktnx.mobileledger.model.MobileLedgerProfile;
40 import net.ktnx.mobileledger.ui.DatePickerFragment;
41 import net.ktnx.mobileledger.utils.Logger;
42 import net.ktnx.mobileledger.utils.MLDB;
43 import net.ktnx.mobileledger.utils.Misc;
45 import java.util.Calendar;
46 import java.util.Date;
47 import java.util.GregorianCalendar;
48 import java.util.Locale;
50 class NewTransactionItemHolder extends RecyclerView.ViewHolder
51 implements DatePickerFragment.DatePickedListener, DescriptionSelectedCallback {
52 private NewTransactionModel.Item item;
53 private TextView tvDate;
54 private AutoCompleteTextView tvDescription;
55 private AutoCompleteTextView tvAccount;
56 private TextView tvAmount;
57 private ConstraintLayout lHead;
58 private LinearLayout lAccount;
59 private FrameLayout lPadding;
60 private MobileLedgerProfile mProfile;
62 private Observer<Date> dateObserver;
63 private Observer<String> descriptionObserver;
64 private Observer<String> hintObserver;
65 private Observer<Integer> focusedAccountObserver;
66 private Observer<Integer> accountCountObserver;
67 private Observer<Boolean> editableObserver;
68 private boolean inUpdate = false;
69 private boolean syncingData = false;
70 NewTransactionItemHolder(@NonNull View itemView, NewTransactionItemsAdapter adapter) {
72 tvAccount = itemView.findViewById(R.id.account_row_acc_name);
73 tvAmount = itemView.findViewById(R.id.account_row_acc_amounts);
74 tvDate = itemView.findViewById(R.id.new_transaction_date);
75 tvDescription = itemView.findViewById(R.id.new_transaction_description);
76 lHead = itemView.findViewById(R.id.ntr_data);
77 lAccount = itemView.findViewById(R.id.ntr_account);
78 lPadding = itemView.findViewById(R.id.ntr_padding);
80 tvDescription.setNextFocusForwardId(View.NO_ID);
81 tvAccount.setNextFocusForwardId(View.NO_ID);
82 tvAmount.setNextFocusForwardId(View.NO_ID); // magic!
84 tvDate.setOnFocusChangeListener((v, hasFocus) -> {
86 pickTransactionDate();
88 tvDate.setOnClickListener(v -> pickTransactionDate());
90 mProfile = Data.profile.getValue();
92 throw new AssertionError();
94 MLDB.hookAutocompletionAdapter(tvDescription.getContext(), tvDescription,
95 MLDB.DESCRIPTION_HISTORY_TABLE, "description", false, adapter, mProfile);
96 MLDB.hookAutocompletionAdapter(tvAccount.getContext(), tvAccount, MLDB.ACCOUNTS_TABLE,
97 "name", true, this, mProfile);
99 final TextWatcher tw = new TextWatcher() {
101 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
105 public void onTextChanged(CharSequence s, int start, int before, int count) {
109 public void afterTextChanged(Editable s) {
110 // debug("input", "text changed");
114 Logger.debug("textWatcher", "calling syncData()");
116 Logger.debug("textWatcher",
117 "syncData() returned, checking if transaction is submittable");
118 adapter.model.checkTransactionSubmittable(adapter);
119 Logger.debug("textWatcher", "done");
122 tvDescription.addTextChangedListener(tw);
123 tvAccount.addTextChangedListener(tw);
124 tvAmount.addTextChangedListener(tw);
126 dateObserver = date -> {
131 tvDate.setText(item.getFormattedDate());
137 descriptionObserver = description -> {
142 tvDescription.setText(description);
148 hintObserver = hint -> {
154 hint = tvAmount.getResources()
155 .getString(R.string.zero_amount);
156 tvAmount.setHint(hint);
162 editableObserver = this::setEditable;
163 focusedAccountObserver = index -> {
164 if ((index != null) && index.equals(getAdapterPosition())) {
165 switch (item.getType()) {
167 // bad idea - double pop-up, and not really necessary.
168 // the user can tap the input to get the calendar
169 //if (!tvDate.hasFocus()) tvDate.requestFocus();
170 boolean focused = tvDescription.requestFocus();
171 tvDescription.dismissDropDown();
173 Misc.showSoftKeyboard(
174 (NewTransactionActivity) tvDescription.getContext());
177 focused = tvAccount.requestFocus();
178 tvAccount.dismissDropDown();
180 Misc.showSoftKeyboard((NewTransactionActivity) tvAccount.getContext());
186 accountCountObserver = count -> {
187 final int adapterPosition = getAdapterPosition();
188 final int layoutPosition = getLayoutPosition();
189 Logger.debug("holder",
190 String.format(Locale.US, "count=%d; pos=%d, layoutPos=%d [%s]", count,
191 adapterPosition, layoutPosition, item.getType()
193 .concat(item.getType() ==
194 NewTransactionModel.ItemType.transactionRow
195 ? String.format(Locale.US,
201 ? String.format(Locale.US,
206 if (adapterPosition == count)
207 tvAmount.setImeOptions(EditorInfo.IME_ACTION_DONE);
209 tvAmount.setImeOptions(EditorInfo.IME_ACTION_NEXT);
212 private void setEditable(Boolean editable) {
213 tvDate.setEnabled(editable);
214 tvDescription.setEnabled(editable);
215 tvAccount.setEnabled(editable);
216 tvAmount.setEnabled(editable);
218 private void beginUpdates() {
220 throw new RuntimeException("Already in update mode");
223 private void endUpdates() {
225 throw new RuntimeException("Not in update mode");
231 * Stores the data from the UI elements into the model item
233 private void syncData() {
238 Logger.debug("new-trans", "skipping syncData() loop");
245 switch (item.getType()) {
247 item.setDate(String.valueOf(tvDate.getText()));
248 item.setDescription(String.valueOf(tvDescription.getText()));
252 .setAccountName(String.valueOf(tvAccount.getText()));
254 // TODO: handle multiple amounts
255 String amount = String.valueOf(tvAmount.getText());
256 amount = amount.trim();
258 if (!amount.isEmpty()) {
261 .setAmount(Float.parseFloat(amount));
263 catch (NumberFormatException e) {
264 Logger.debug("new-trans", String.format(
265 "assuming amount is not set due to number format exception. " +
278 throw new RuntimeException("Should not happen");
285 private void pickTransactionDate() {
286 DatePickerFragment picker = new DatePickerFragment();
287 picker.setOnDatePickedListener(this);
288 picker.show(((NewTransactionActivity) tvDate.getContext()).getSupportFragmentManager(),
294 * @param item updates the UI elements with the data from the model item
296 @SuppressLint("DefaultLocale")
297 public void setData(NewTransactionModel.Item item) {
300 if (this.item != null && !this.item.equals(item)) {
301 this.item.stopObservingDate(dateObserver);
302 this.item.stopObservingDescription(descriptionObserver);
303 this.item.stopObservingAmountHint(hintObserver);
304 this.item.stopObservingEditableFlag(editableObserver);
306 .stopObservingFocusedItem(focusedAccountObserver);
308 .stopObservingAccountCount(accountCountObserver);
313 switch (item.getType()) {
315 tvDate.setText(item.getFormattedDate());
316 tvDescription.setText(item.getDescription());
317 lHead.setVisibility(View.VISIBLE);
318 lAccount.setVisibility(View.GONE);
319 lPadding.setVisibility(View.GONE);
323 LedgerTransactionAccount acc = item.getAccount();
324 tvAccount.setText(acc.getAccountName());
326 acc.isAmountSet() ? String.format("%1.2f", acc.getAmount()) : "");
327 lHead.setVisibility(View.GONE);
328 lAccount.setVisibility(View.VISIBLE);
329 lPadding.setVisibility(View.GONE);
333 lHead.setVisibility(View.GONE);
334 lAccount.setVisibility(View.GONE);
335 lPadding.setVisibility(View.VISIBLE);
340 if (this.item == null) { // was null or has changed
342 final NewTransactionActivity activity =
343 (NewTransactionActivity) tvDescription.getContext();
344 item.observeDate(activity, dateObserver);
345 item.observeDescription(activity, descriptionObserver);
346 item.observeAmountHint(activity, hintObserver);
347 item.observeEditableFlag(activity, editableObserver);
349 .observeFocusedItem(activity, focusedAccountObserver);
351 .observeAccountCount(activity, accountCountObserver);
359 public void onDatePicked(int year, int month, int day) {
360 final Calendar c = GregorianCalendar.getInstance();
361 c.set(year, month, day);
362 item.setDate(c.getTime());
363 boolean focused = tvDescription.requestFocus();
365 Misc.showSoftKeyboard((NewTransactionActivity) tvAccount.getContext());
369 public void descriptionSelected(String description) {
370 tvAccount.setText(description);
371 tvAmount.requestFocus(View.FOCUS_FORWARD);