]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/TextViewClearHelper.java
implement stable IDs for the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / TextViewClearHelper.java
1 /*
2  * Copyright © 2020 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;
19
20 import android.annotation.SuppressLint;
21 import android.graphics.drawable.Drawable;
22 import android.text.Editable;
23 import android.text.TextWatcher;
24 import android.view.MotionEvent;
25 import android.view.View;
26 import android.widget.EditText;
27
28 import net.ktnx.mobileledger.R;
29
30 public class TextViewClearHelper {
31     private boolean hadText = false;
32     private boolean hasFocus = false;
33     private EditText view;
34     private TextWatcher textWatcher;
35     private View.OnFocusChangeListener prevOnFocusChangeListener;
36     public void detachFromView() {
37         if (view == null)
38             return;
39         view.removeTextChangedListener(textWatcher);
40         prevOnFocusChangeListener = null;
41         textWatcher = null;
42         hasFocus = false;
43         hadText = false;
44         view = null;
45     }
46     @SuppressLint("ClickableViewAccessibility")
47     public void attachToTextView(EditText view) {
48         if (this.view != null)
49             detachFromView();
50         this.view = view;
51         textWatcher = new TextWatcher() {
52             @Override
53             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
54
55             }
56             @Override
57             public void onTextChanged(CharSequence s, int start, int before, int count) {
58
59             }
60             @Override
61             public void afterTextChanged(Editable s) {
62                 boolean hasText = s.length() > 0;
63
64                 if (hasFocus) {
65                     if (hadText && !hasText)
66                         hideClearDrawable();
67                     if (!hadText && hasText)
68                         showClearDrawable();
69                 }
70
71                 hadText = hasText;
72
73             }
74         };
75         view.addTextChangedListener(textWatcher);
76         prevOnFocusChangeListener = view.getOnFocusChangeListener();
77         view.setOnFocusChangeListener((v, hasFocus) -> {
78             if (hasFocus) {
79                 if (view.getText()
80                         .length() > 0)
81                 {
82                     showClearDrawable();
83                 }
84             }
85             else {
86                 hideClearDrawable();
87             }
88
89             this.hasFocus = hasFocus;
90
91             if (prevOnFocusChangeListener != null)
92                 prevOnFocusChangeListener.onFocusChange(v, hasFocus);
93         });
94
95         view.setOnTouchListener((v, event) -> this.onTouchEvent(view, event));
96     }
97     private void hideClearDrawable() {
98         view.setCompoundDrawablesRelative(null, null, null, null);
99     }
100     private void showClearDrawable() {
101         view.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, R.drawable.ic_clear_accent_24dp,
102                 0);
103     }
104     public boolean onTouchEvent(EditText view, MotionEvent event) {
105         if ((event.getAction() == MotionEvent.ACTION_UP) && (view.getText()
106                                                                  .length() > 0))
107         {
108             boolean clearClicked = false;
109             final float x = event.getX();
110             final int vw = view.getWidth();
111             // start, top, end, bottom (end == 2)
112             Drawable dwb = view.getCompoundDrawablesRelative()[2];
113             if (dwb != null) {
114                 final int dw = dwb.getBounds()
115                                   .width();
116                 if (view.getLayoutDirection() == View.LAYOUT_DIRECTION_LTR) {
117                     if ((x > vw - dw))
118                         clearClicked = true;
119                 }
120                 else {
121                     if (x < vw - dw)
122                         clearClicked = true;
123                 }
124                 if (clearClicked) {
125                     view.setText("");
126                     view.requestFocus();
127                     return true;
128                 }
129             }
130         }
131
132         return false;
133     }
134
135 }