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.annotation.SuppressLint;
21 import android.graphics.Typeface;
22 import android.text.Editable;
23 import android.text.TextUtils;
24 import android.text.TextWatcher;
25 import android.view.View;
26 import android.widget.EditText;
27 import android.widget.ListAdapter;
28 import android.widget.TextView;
30 import androidx.annotation.ColorInt;
31 import androidx.annotation.NonNull;
33 import net.ktnx.mobileledger.R;
34 import net.ktnx.mobileledger.databinding.NewTransactionHeaderRowBinding;
35 import net.ktnx.mobileledger.db.TransactionDescriptionAutocompleteAdapter;
36 import net.ktnx.mobileledger.model.Data;
37 import net.ktnx.mobileledger.model.FutureDates;
38 import net.ktnx.mobileledger.ui.DatePickerFragment;
39 import net.ktnx.mobileledger.utils.Logger;
40 import net.ktnx.mobileledger.utils.Misc;
41 import net.ktnx.mobileledger.utils.SimpleDate;
43 import java.text.DecimalFormatSymbols;
44 import java.text.ParseException;
46 class NewTransactionHeaderItemHolder extends NewTransactionItemViewHolder
47 implements DatePickerFragment.DatePickedListener {
48 private final NewTransactionHeaderRowBinding b;
49 private boolean ignoreFocusChanges = false;
50 private String decimalSeparator;
51 private boolean inUpdate = false;
52 private boolean syncingData = false;
53 //TODO multiple amounts with different currencies per posting?
54 NewTransactionHeaderItemHolder(@NonNull NewTransactionHeaderRowBinding b,
55 NewTransactionItemsAdapter adapter) {
56 super(b.getRoot(), adapter);
59 b.newTransactionDescription.setNextFocusForwardId(View.NO_ID);
61 b.newTransactionDate.setOnClickListener(v -> pickTransactionDate());
63 b.transactionCommentButton.setOnClickListener(v -> {
64 b.transactionComment.setVisibility(View.VISIBLE);
65 b.transactionComment.requestFocus();
68 @SuppressLint("DefaultLocale") View.OnFocusChangeListener focusMonitor = (v, hasFocus) -> {
69 final int id = v.getId();
71 boolean wasSyncing = syncingData;
74 final int pos = getAdapterPosition();
75 if (id == R.id.transaction_comment) {
76 adapter.noteFocusIsOnTransactionComment(pos);
78 else if (id == R.id.new_transaction_description) {
79 adapter.noteFocusIsOnDescription(pos);
82 throw new IllegalStateException("Where is the focus? " + id);
85 syncingData = wasSyncing;
89 if (id == R.id.transaction_comment) {
90 commentFocusChanged(b.transactionComment, hasFocus);
94 b.newTransactionDescription.setOnFocusChangeListener(focusMonitor);
95 b.transactionComment.setOnFocusChangeListener(focusMonitor);
97 NewTransactionActivity activity = (NewTransactionActivity) b.getRoot()
100 b.newTransactionDescription.setAdapter(
101 new TransactionDescriptionAutocompleteAdapter(activity));
102 b.newTransactionDescription.setOnItemClickListener(
103 (parent, view, position, id) -> activity.descriptionSelected(
104 parent.getItemAtPosition(position)
107 decimalSeparator = "";
108 Data.locale.observe(activity, locale -> decimalSeparator = String.valueOf(
109 DecimalFormatSymbols.getInstance(locale)
110 .getMonetaryDecimalSeparator()));
112 final TextWatcher tw = new TextWatcher() {
114 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
118 public void onTextChanged(CharSequence s, int start, int before, int count) {
122 public void afterTextChanged(Editable s) {
123 // debug("input", "text changed");
127 Logger.debug("textWatcher", "calling syncData()");
129 Logger.debug("textWatcher",
130 "syncData() returned, checking if transaction is submittable");
131 adapter.model.checkTransactionSubmittable(null);
133 Logger.debug("textWatcher", "done");
136 b.newTransactionDescription.addTextChangedListener(tw);
137 monitorComment(b.transactionComment);
139 commentFocusChanged(b.transactionComment, false);
141 adapter.model.getFocusInfo()
142 .observe(activity, this::applyFocus);
144 adapter.model.getShowComments()
145 .observe(activity, show -> b.transactionCommentLayout.setVisibility(
146 show ? View.VISIBLE : View.GONE));
148 private void applyFocus(NewTransactionModel.FocusInfo focusInfo) {
149 if (ignoreFocusChanges) {
150 Logger.debug("new-trans", "Ignoring focus change");
153 ignoreFocusChanges = true;
155 if (((focusInfo == null) || (focusInfo.element == null) ||
156 focusInfo.position != getAdapterPosition()))
159 NewTransactionModel.Item head = getItem().toTransactionHead();
160 // bad idea - double pop-up, and not really necessary.
161 // the user can tap the input to get the calendar
162 //if (!tvDate.hasFocus()) tvDate.requestFocus();
163 switch (focusInfo.element) {
164 case TransactionComment:
165 b.transactionComment.setVisibility(View.VISIBLE);
166 b.transactionComment.requestFocus();
169 boolean focused = b.newTransactionDescription.requestFocus();
170 // tvDescription.dismissDropDown();
172 Misc.showSoftKeyboard((NewTransactionActivity) b.getRoot()
178 ignoreFocusChanges = false;
181 private void monitorComment(EditText editText) {
182 editText.addTextChangedListener(new TextWatcher() {
184 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
187 public void onTextChanged(CharSequence s, int start, int before, int count) {
190 public void afterTextChanged(Editable s) {
191 // debug("input", "text changed");
195 Logger.debug("textWatcher", "calling syncData()");
197 Logger.debug("textWatcher",
198 "syncData() returned, checking if transaction is submittable");
199 styleComment(editText, s.toString());
200 Logger.debug("textWatcher", "done");
204 private void commentFocusChanged(TextView textView, boolean hasFocus) {
205 @ColorInt int textColor;
206 textColor = b.dummyText.getTextColors()
209 textView.setTypeface(null, Typeface.NORMAL);
210 textView.setHint(R.string.transaction_account_comment_hint);
213 int alpha = (textColor >> 24 & 0xff);
214 alpha = 3 * alpha / 4;
215 textColor = (alpha << 24) | (0x00ffffff & textColor);
216 textView.setTypeface(null, Typeface.ITALIC);
217 textView.setHint("");
218 if (TextUtils.isEmpty(textView.getText())) {
219 textView.setVisibility(View.INVISIBLE);
222 textView.setTextColor(textColor);
225 private void setEditable(Boolean editable) {
226 b.newTransactionDate.setEnabled(editable);
227 b.newTransactionDescription.setEnabled(editable);
229 private void beginUpdates() {
231 throw new RuntimeException("Already in update mode");
234 private void endUpdates() {
236 throw new RuntimeException("Not in update mode");
242 * Stores the data from the UI elements into the model item
243 * Returns true if there were changes made that suggest transaction has to be
244 * checked for being submittable
246 private boolean syncData() {
248 Logger.debug("new-trans", "skipping syncData() loop");
252 if (getAdapterPosition() < 0) {
253 // probably the row was swiped out
254 Logger.debug("new-trans", "Ignoring request to suncData(): adapter position negative");
259 boolean significantChange = false;
263 NewTransactionModel.TransactionHead head = getItem().toTransactionHead();
265 head.setDate(String.valueOf(b.newTransactionDate.getText()));
267 // transaction description is required
268 if (TextUtils.isEmpty(head.getDescription()) !=
269 TextUtils.isEmpty(b.newTransactionDescription.getText()))
270 significantChange = true;
272 head.setDescription(String.valueOf(b.newTransactionDescription.getText()));
273 head.setComment(String.valueOf(b.transactionComment.getText()));
275 return significantChange;
277 catch (ParseException e) {
278 throw new RuntimeException("Should not happen", e);
284 private void pickTransactionDate() {
285 DatePickerFragment picker = new DatePickerFragment();
286 picker.setFutureDates(FutureDates.valueOf(mProfile.getFutureDates()));
287 picker.setOnDatePickedListener(this);
288 picker.setCurrentDateFromText(b.newTransactionDate.getText());
289 picker.show(((NewTransactionActivity) b.getRoot()
290 .getContext()).getSupportFragmentManager(), null);
295 * @param item updates the UI elements with the data from the model item
297 @SuppressLint("DefaultLocale")
298 public void bind(@NonNull NewTransactionModel.Item item) {
303 NewTransactionModel.TransactionHead head = item.toTransactionHead();
304 b.newTransactionDate.setText(head.getFormattedDate());
306 // avoid triggering completion pop-up
307 ListAdapter a = b.newTransactionDescription.getAdapter();
309 b.newTransactionDescription.setAdapter(null);
310 b.newTransactionDescription.setText(head.getDescription());
313 b.newTransactionDescription.setAdapter(
314 (TransactionDescriptionAutocompleteAdapter) a);
317 b.transactionComment.setText(head.getComment());
318 //styleComment(b.transactionComment, head.getComment());
322 applyFocus(mAdapter.model.getFocusInfo()
333 private void styleComment(EditText editText, String comment) {
334 final View focusedView = editText.findFocus();
335 editText.setTypeface(null, (focusedView == editText) ? Typeface.NORMAL : Typeface.ITALIC);
336 editText.setVisibility(
337 ((focusedView != editText) && TextUtils.isEmpty(comment)) ? View.INVISIBLE
341 public void onDatePicked(int year, int month, int day) {
342 final NewTransactionModel.TransactionHead head = getItem().toTransactionHead();
343 head.setDate(new SimpleDate(year, month + 1, day));
344 b.newTransactionDate.setText(head.getFormattedDate());
346 boolean focused = b.newTransactionDescription.requestFocus();
348 Misc.showSoftKeyboard((NewTransactionActivity) b.getRoot()