/
CheckBoxStep.java
192 lines (164 loc) · 5.59 KB
/
CheckBoxStep.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
182
183
184
185
186
187
188
189
190
191
192
/*
* MIT License
*
* Copyright (c) 2017 Jan Heinrich Reimer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.heinrichreimersoftware.singleinputform.steps;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import com.heinrichreimersoftware.singleinputform.R;
public class CheckBoxStep extends Step{
public static final String DATA_CHECKED = "data_checked";
private int textResId;
private String text;
private int textColor;
private Validator validator;
protected CheckBoxStep(Builder builder) {
super(builder);
textResId = builder.textResId;
text = builder.text;
textColor = builder.textColor;
validator = builder.validator;
}
public static boolean checked(Bundle data, String dataKey){
boolean checked = false;
if(data != null && data.containsKey(dataKey)){
Bundle bundleChecked = data.getBundle(dataKey);
if(bundleChecked != null){
checked = bundleChecked.getBoolean(DATA_CHECKED);
}
}
return checked;
}
@Override
public View onCreateView(Context context) {
return View.inflate(context, R.layout.view_check_box, null);
}
@Override
public void updateView(boolean lastStep) {
if(textResId != 0){
getCheckBox().setText(textResId);
}
else{
getCheckBox().setText(text);
}
getCheckBox().setTextColor(textColor);
}
@Override
public FrameLayout getView(){
if(super.getView() instanceof FrameLayout){
return (FrameLayout) super.getView();
}
throw new ClassCastException("View view must be FrameLayout.");
}
private CheckBox getCheckBox(){
return (CheckBox) getView().getChildAt(0);
}
@Override
public boolean validate() {
return validator.validate(getCheckBox().isChecked());
}
@Override
protected void onSave() {
data().putBoolean(DATA_CHECKED, getCheckBox().isChecked());
}
@Override
protected void onRestore() {
getCheckBox().setChecked(data().getBoolean(DATA_CHECKED, false));
}
public static class Validator {
public boolean validate(boolean checked){
return true;
}
}
public static class Builder extends Step.Builder{
protected int textResId;
protected String text;
protected Validator validator;
protected int textColor;
public Builder(Context context, String key) {
super(context, key);
loadTheme();
validator = new Validator();
}
public int textResId() {
return textResId;
}
public Builder textResId(int textResId) {
this.textResId = textResId;
return this;
}
public String text() {
return text;
}
public Builder text(String text) {
this.text = text;
return this;
}
public Validator validator() {
return validator;
}
public Builder validator(Validator validator) {
this.validator = validator;
return this;
}
public int textColor() {
return textColor;
}
public Builder textColor(int textColor) {
this.textColor = textColor;
return this;
}
private void loadTheme(){
int[] attrs = {android.R.attr.textColorPrimaryInverse};
TypedArray array = context.obtainStyledAttributes(attrs);
textColor = array.getColor(0, 0);
array.recycle();
}
@Override
public Step build() {
return new CheckBoxStep(this);
}
/* Casted parent methods */
public Builder title(String title) {
return (Builder) super.title(title);
}
public Builder titleResId(int titleResId) {
return (Builder) super.titleResId(titleResId);
}
public Builder error(String error) {
return (Builder) super.error(error);
}
public Builder errorResId(int errorResId) {
return (Builder) super.errorResId(errorResId);
}
public Builder details(String details) {
return (Builder) super.details(details);
}
public Builder detailsResId(int detailsResId) {
return (Builder) super.detailsResId(detailsResId);
}
}
}