-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBindingAdapterHelper.java
181 lines (167 loc) · 7.06 KB
/
BindingAdapterHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package by.mvvmwrapper.utils.binding;
import android.databinding.BindingAdapter;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.util.Log;
import android.util.Pair;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import java.io.File;
import java.util.List;
import by.mvvmwrapper.utils.SeekBarChangeAdapter;
import by.mvvmwrapper.utils.TextWatcherAdapter;
import by.mvvmwrapper.utils.TypefaceHelper;
import by.mvvmwrapper.widget.BindableEditText;
import by.mvvmwrapper.wrapper.BindableGeneric;
/**
* Create with Android Studio<br>
* Created by Pavlovskii Ilya<br>
* E-mail: [email protected], [email protected]<br>
* Skype: trane9119<br>
* Date: 29.01.16<br>
* Time: 1:06<br>
* Project name: MVVMtest<br>
* ===================================================================================
* All {@link BindingAdapter} methods used in base pack methods<br>
* ===================================================================================
*/
public class BindingAdapterHelper {
//======================================================
//-----------------------Constants----------------------
//======================================================
private static final String ATTR_TAG = "attr";
//======================================================
//---------------------Constructors---------------------
//======================================================
/**
* Implement base helper realization constructor
*/
private BindingAdapterHelper() {
}
//======================================================
//---------------------Public methods-------------------
//======================================================
/**
* Fill text and tracking changes on target {@see EditText} component
* !ATTENTION!
* Current method have some problems with subscribe multiply {@see EditText} components to one
* bindable argument. To avoid this problems use {@link BindableEditText}.
*
* @param view target {@link BindableEditText} component
* @param bindableString target text*/
@BindingAdapter({ATTR_TAG + ":binding"})
public static void bindEditText(@NonNull EditText view,
@Nullable final BindableGeneric<String> bindableString) {
if (bindableString == null) {
return;
}
Pair<BindableGeneric, TextWatcherAdapter> pair = (Pair) view.getTag();
if (pair == null || pair.first != bindableString) {
if (pair != null) {
view.removeTextChangedListener(pair.second);
}
TextWatcherAdapter watcher = new TextWatcherAdapter() {
@Override
public void afterTextChanged(Editable s) {
bindableString.setValue(s.toString());
}
};
pair = new Pair<>(bindableString, watcher);
view.setTag(pair);
view.addTextChangedListener(watcher);
}
}
/**
* Fill text and tracking changes on target {@see BindableEditText} component
*
* @param view target {@link BindableEditText} component
* @param bindableString target text*/
@BindingAdapter({ATTR_TAG + ":binding"})
public static void bindBindableEditText(@NonNull BindableEditText view,
@Nullable final BindableGeneric<String> bindableString) {
if (bindableString == null) {
return;
}
Pair<BindableGeneric, TextWatcherAdapter> pair = (Pair) view.getTag();
if (pair == null || pair.first != bindableString) {
if (pair != null) {
view.removeTextChangedListener(pair.second);
}
TextWatcherAdapter watcher = new TextWatcherAdapter() {
@Override
public void afterTextChanged(Editable s) {
bindableString.set(s.toString());
}
};
pair = new Pair<>(bindableString, watcher);
view.setTag(pair);
view.addTextChangedListener(watcher);
}
}
/**
* Fill checked/unchecked states to {@see CompoundButton} component and tracking changes
*
* @param checkBox target {@link CompoundButton} component
* @param bindableBoolean observable checked/unchecked state data*/
@BindingAdapter({ATTR_TAG + ":binding"})
public static void bindCompoundButton(@NonNull CompoundButton checkBox,
@Nullable BindableGeneric<Boolean> bindableBoolean) {
if (bindableBoolean == null) {
return;
}
CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked != bindableBoolean.getValue()) {
bindableBoolean.set(isChecked);
}
}
};
checkBox.setOnCheckedChangeListener(listener);
}
/**
* Set progress state value and tracking changes on {@see SeekBar} component
*
* @param seekBar target {@link SeekBar} component
* @param obsValue observable progress state value*/
@BindingAdapter({ATTR_TAG + ":binding"})
public static void bindSeekBar(@NonNull SeekBar seekBar, @Nullable BindableGeneric<Integer> obsValue) {
if (obsValue != null) {
seekBar.setOnSeekBarChangeListener(new SeekBarChangeAdapter() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
Log.d("ACTION", "onProgressChanged. Val: " + obsValue.getValue() + " Progres: " + progress);
if (progress != obsValue.getValue()) {
obsValue.set(progress);
}
}
});
}
}
@BindingAdapter({ATTR_TAG + ":assetTypeface"})
public static void bindAssetTypeface(@NonNull TextView textView, @Nullable String assetPath) {
Typeface typeface = TypefaceHelper.getTypefaceFromAssets(textView.getContext(), assetPath);
if (typeface != null) {
textView.setTypeface(typeface);
}
}
@BindingAdapter({ATTR_TAG + ":fileTypeface"})
public static void bindFileTypeface(@NonNull TextView textView, @Nullable String filePath) {
Typeface typeface = TypefaceHelper.getTypefaceFromFile(filePath);
if (typeface != null) {
textView.setTypeface(typeface);
}
}
@BindingAdapter({ATTR_TAG + ":fileTypeface"})
public static void bindFileTypeface(@NonNull TextView textView, @Nullable File typefaceFile) {
Typeface typeface = TypefaceHelper.getTypefaceFromFile(typefaceFile);
if (typeface != null) {
textView.setTypeface(typeface);
}
}
}