• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

opensrp / opensrp-client-native-form / #65

21 Dec 2023 07:55AM UTC coverage: 66.139% (-0.05%) from 66.188%
#65

Pull #670

github

web-flow
Merge f3ea44e34 into f8c80cf2b
Pull Request #670: Round height weight for optibp

8444 of 12767 relevant lines covered (66.14%)

0.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

64.23
android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/NativeEditTextFactory.java
1
package com.vijay.jsonwizard.widgets;
2

3
import android.content.Context;
4
import android.os.Build;
5
import android.text.InputType;
6
import android.text.TextUtils;
7
import android.text.method.PasswordTransformationMethod;
8
import android.util.Patterns;
9
import android.view.LayoutInflater;
10
import android.view.View;
11
import android.widget.ImageView;
12
import android.widget.RelativeLayout;
13

14
import com.rengwuxian.materialedittext.validation.RegexpValidator;
15
import com.rey.material.util.ViewUtil;
16
import com.vijay.jsonwizard.R;
17
import com.vijay.jsonwizard.constants.JsonFormConstants;
18
import com.vijay.jsonwizard.customviews.GenericTextWatcher;
19
import com.vijay.jsonwizard.customviews.NativeEditText;
20
import com.vijay.jsonwizard.fragments.JsonFormFragment;
21
import com.vijay.jsonwizard.interfaces.CommonListener;
22
import com.vijay.jsonwizard.interfaces.FormWidgetFactory;
23
import com.vijay.jsonwizard.interfaces.JsonApi;
24
import com.vijay.jsonwizard.utils.FormUtils;
25
import com.vijay.jsonwizard.utils.ValidationStatus;
26
import com.vijay.jsonwizard.validators.edittext.MaxNumericValidator;
27
import com.vijay.jsonwizard.validators.edittext.MinNumericValidator;
28
import com.vijay.jsonwizard.validators.edittext.RequiredValidator;
29
import com.vijay.jsonwizard.views.JsonFormFragmentView;
30

31
import org.json.JSONArray;
32
import org.json.JSONException;
33
import org.json.JSONObject;
34

35
import java.util.ArrayList;
36
import java.util.HashSet;
37
import java.util.List;
38
import java.util.Set;
39

40
/**
41
 * Created by vijay on 24-05-2015.
42
 */
43
public class NativeEditTextFactory implements FormWidgetFactory {
1✔
44
    public static ValidationStatus validate(final JsonFormFragmentView formFragmentView,
45
                                            final NativeEditText editText) {
46
        ValidationStatus validationStatus = new ValidationStatus(true, null, formFragmentView, editText);
1✔
47

48
        if (editText.isEnabled()) {
1✔
49
            boolean validate = editText.validate();
1✔
50
            if (!validate) {
1✔
51
                String errorString = null;
1✔
52
                if (editText != null && editText.getError() != null) {
1✔
53
                    errorString = editText.getError().toString();
1✔
54
                }
55
                validationStatus = new ValidationStatus(false, errorString, formFragmentView, editText);
1✔
56
            }
57
        }
58

59
        return validationStatus;
1✔
60
    }
61

62
    @Override
63
    public List<View> getViewsFromJson(String stepName, Context context, JsonFormFragment formFragment, JSONObject jsonObject, CommonListener
64
            listener, boolean popup) throws Exception {
65
        return attachJson(stepName, context, formFragment, jsonObject, listener, popup);
1✔
66
    }
67

68
    @Override
69
    public List<View> getViewsFromJson(String stepName, Context context, JsonFormFragment formFragment, JSONObject jsonObject, CommonListener listener) throws Exception {
70
        return attachJson(stepName, context, formFragment, jsonObject, listener, false);
×
71
    }
72

73
    private List<View> attachJson(String stepName, Context context, JsonFormFragment formFragment, JSONObject jsonObject, CommonListener
74
            listener, boolean popup) throws Exception {
75
        List<View> views = new ArrayList<>(1);
1✔
76

77
        RelativeLayout rootLayout = (RelativeLayout) LayoutInflater.from(context).inflate(
1✔
78
                getLayout(), null);
1✔
79
        NativeEditText editText = rootLayout.findViewById(R.id.normal_edit_text);
1✔
80
        ImageView editButton = rootLayout.findViewById(R.id.normal_edit_text_edit_button);
1✔
81
        FormUtils.setEditButtonAttributes(jsonObject, editText, editButton, listener);
1✔
82
        makeFromJson(stepName, context, formFragment, jsonObject, editText, editButton);
1✔
83

84
        addRequiredValidator(jsonObject, editText);
1✔
85
        addRegexValidator(jsonObject, editText);
1✔
86
        addEmailValidator(jsonObject, editText);
1✔
87
        addUrlValidator(jsonObject, editText);
1✔
88
        addNumericValidator(jsonObject, editText);
1✔
89
        addNumericIntegerValidator(jsonObject, editText);
1✔
90

91
        JSONArray canvasIds = new JSONArray();
1✔
92
        rootLayout.setId(ViewUtil.generateViewId());
1✔
93
        canvasIds.put(rootLayout.getId());
1✔
94
        editText.setTag(R.id.canvas_ids, canvasIds.toString());
1✔
95
        editText.setTag(R.id.extraPopup, popup);
1✔
96

97
        ((JsonApi) context).addFormDataView(editText);
1✔
98
        views.add(rootLayout);
1✔
99
        return views;
1✔
100
    }
101

102
    protected void makeFromJson(String stepName, Context context, JsonFormFragment formFragment,
103
                                JSONObject jsonObject, NativeEditText editText, ImageView editButton) throws Exception {
104

105
        String openMrsEntityParent = jsonObject.getString(JsonFormConstants.OPENMRS_ENTITY_PARENT);
1✔
106
        String openMrsEntity = jsonObject.getString(JsonFormConstants.OPENMRS_ENTITY);
1✔
107
        String openMrsEntityId = jsonObject.getString(JsonFormConstants.OPENMRS_ENTITY_ID);
1✔
108
        String relevance = jsonObject.optString(JsonFormConstants.RELEVANCE);
1✔
109
        String constraints = jsonObject.optString(JsonFormConstants.CONSTRAINTS);
1✔
110
        String editTextStyle = jsonObject.optString(JsonFormConstants.EDIT_TEXT_STYLE, "");
1✔
111
        String editType = jsonObject.optString(JsonFormConstants.EDIT_TYPE);
1✔
112
        String calculation = jsonObject.optString(JsonFormConstants.CALCULATION);
1✔
113

114
        editText.setId(ViewUtil.generateViewId());
1✔
115
        editText.setTag(R.id.key, jsonObject.getString(JsonFormConstants.KEY));
1✔
116
        editText.setTag(R.id.openmrs_entity_parent, openMrsEntityParent);
1✔
117
        editText.setTag(R.id.openmrs_entity, openMrsEntity);
1✔
118
        editText.setTag(R.id.openmrs_entity_id, openMrsEntityId);
1✔
119
        editText.setTag(R.id.type, jsonObject.getString(JsonFormConstants.TYPE));
1✔
120
        editText.setTag(R.id.address, stepName + ":" + jsonObject.getString(JsonFormConstants.KEY));
1✔
121

122
        if (!TextUtils.isEmpty(jsonObject.optString(JsonFormConstants.VALUE))) {
1✔
123
            editText.setText(jsonObject.optString(JsonFormConstants.VALUE));
×
124
            editText.setSelection(editText.getText().length());
×
125
        }
126
        //Add edittext style
127
        if (!TextUtils.isEmpty(editTextStyle)) {
1✔
128
            if (JsonFormConstants.BORDERED_EDIT_TEXT.equals(editTextStyle)) {
×
129
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
×
130
                    editText.setBackground(context.getResources()
×
131
                            .getDrawable(R.drawable.edit_text_bg));
×
132
                } else {
133
                    editText.setBackgroundDrawable(context.getResources()
×
134
                            .getDrawable(R.drawable.edit_text_bg));
×
135
                }
136
            }
137
        }
138
        editText.setHint(jsonObject.optString(JsonFormConstants.HINT));
1✔
139

140
        FormUtils.setEditMode(jsonObject, editText, editButton);
1✔
141
        // edit type check
142
        if (!TextUtils.isEmpty(editType)) {
1✔
143
            if ("number".equals(editType)) {
1✔
144
                editText.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
×
145
            } else if ("name".equals(editType)) {
1✔
146
                editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
1✔
147
            }
148
            else if ("password".equals(editType))
×
149
            {
150
                editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
×
151
            }
152
        }
153

154
        editText.addTextChangedListener(new GenericTextWatcher(stepName, formFragment, editText));
1✔
155
        if (!TextUtils.isEmpty(relevance) && context instanceof JsonApi) {
1✔
156
            editText.setTag(R.id.relevance, relevance);
×
157
            ((JsonApi) context).addSkipLogicView(editText);
×
158
        }
159

160
        if (!TextUtils.isEmpty(constraints) && context instanceof JsonApi) {
1✔
161
            editText.setTag(R.id.constraints, constraints);
×
162
            ((JsonApi) context).addConstrainedView(editText);
×
163
        }
164

165
        if (!TextUtils.isEmpty(calculation) && context instanceof JsonApi) {
1✔
166
            editText.setTag(R.id.calculation, calculation);
×
167
            ((JsonApi) context).addCalculationLogicView(editText);
×
168
        }
169

170
        FormUtils.toggleEditTextVisibility(jsonObject, editText);
1✔
171
    }
1✔
172

173
    protected int getLayout() {
174
        return R.layout.native_form_normal_edit_text;
1✔
175
    }
176

177
    private static void addRequiredValidator(JSONObject jsonObject, NativeEditText editText) throws JSONException {
178
        JSONObject requiredObject = jsonObject.optJSONObject(JsonFormConstants.V_REQUIRED);
1✔
179
        if (requiredObject != null) {
1✔
180
            boolean requiredValue = requiredObject.getBoolean(JsonFormConstants.VALUE);
1✔
181
            if (Boolean.TRUE.equals(requiredValue)) {
1✔
182
                editText.addValidator(new RequiredValidator(requiredObject.getString(JsonFormConstants.ERR)));
1✔
183
                FormUtils.setRequiredOnHint(editText);
1✔
184
            }
185
        }
186
    }
1✔
187

188

189
    private void addRegexValidator(JSONObject jsonObject, NativeEditText editText) throws JSONException {
190
        JSONObject regexObject = jsonObject.optJSONObject(JsonFormConstants.V_REGEX);
1✔
191
        if (regexObject != null) {
1✔
192
            String regexValue = regexObject.optString(JsonFormConstants.VALUE);
1✔
193
            if (!TextUtils.isEmpty(regexValue)) {
1✔
194
                editText.addValidator(new RegexpValidator(regexObject.getString(JsonFormConstants.ERR), regexValue));
1✔
195
            }
196
        }
197
    }
1✔
198

199
    private void addEmailValidator(JSONObject jsonObject, NativeEditText editText) throws JSONException {
200
        JSONObject emailObject = jsonObject.optJSONObject(JsonFormConstants.V_EMAIL);
1✔
201
        if (emailObject != null) {
1✔
202
            String emailValue = emailObject.optString(JsonFormConstants.VALUE);
×
203
            if (!TextUtils.isEmpty(emailValue) && Boolean.TRUE.toString().equalsIgnoreCase(emailValue)) {
×
204
                editText.addValidator(new RegexpValidator(emailObject.getString(JsonFormConstants.ERR), android.util.Patterns.EMAIL_ADDRESS
×
205
                        .toString()));
×
206
            }
207

208
        }
209

210
    }
1✔
211

212
    private void addUrlValidator(JSONObject jsonObject, NativeEditText editText) throws JSONException {
213
        JSONObject urlObject = jsonObject.optJSONObject(JsonFormConstants.V_URL);
1✔
214
        if (urlObject != null) {
1✔
215
            String urlValue = urlObject.optString(JsonFormConstants.VALUE);
×
216
            if (!TextUtils.isEmpty(urlValue) && Boolean.TRUE.toString().equalsIgnoreCase(urlValue)) {
×
217
                editText.addValidator(new RegexpValidator(urlObject.getString(JsonFormConstants.ERR), Patterns.WEB_URL.toString()));
×
218
            }
219

220
        }
221
    }
1✔
222

223
    private void addNumericValidator(JSONObject jsonObject, NativeEditText editText) throws JSONException {
224
        JSONObject numericObject = jsonObject.optJSONObject(JsonFormConstants.V_NUMERIC);
1✔
225
        if (numericObject != null) {
1✔
226
            String numericValue = numericObject.optString(JsonFormConstants.VALUE);
×
227
            if (!TextUtils.isEmpty(numericValue) && Boolean.TRUE.toString().equalsIgnoreCase(numericValue)) {
×
228
                editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
×
229
                editText.addValidator(new RegexpValidator(numericObject.getString(JsonFormConstants.ERR),
×
230
                        "[0-9]*\\.?[0-9]*"));
231

232
                if (jsonObject.has(JsonFormConstants.V_MIN)) {
×
233
                    JSONObject minValidation = jsonObject.getJSONObject(JsonFormConstants.V_MIN);
×
234
                    editText.addValidator(new MinNumericValidator(minValidation.getString(JsonFormConstants.ERR),
×
235
                            Double.parseDouble(minValidation.getString(JsonFormConstants.VALUE))));
×
236
                }
237

238
                if (jsonObject.has(JsonFormConstants.V_MAX)) {
×
239
                    JSONObject minValidation = jsonObject.getJSONObject(JsonFormConstants.V_MAX);
×
240
                    editText.addValidator(new MaxNumericValidator(minValidation.getString(JsonFormConstants.ERR),
×
241
                            Double.parseDouble(minValidation.getString(JsonFormConstants.VALUE))));
×
242
                }
243
            }
244
        }
245
    }
1✔
246

247
    private void addNumericIntegerValidator(JSONObject jsonObject, NativeEditText editText) throws JSONException {
248
        JSONObject numericIntegerObject = jsonObject.optJSONObject(JsonFormConstants.V_NUMERIC_INTEGER);
1✔
249
        if (numericIntegerObject != null) {
1✔
250
            String numericValue = numericIntegerObject.optString(JsonFormConstants.VALUE);
×
251
            if (!TextUtils.isEmpty(numericValue) && Boolean.TRUE.toString().equalsIgnoreCase(numericValue)) {
×
252
                editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);
×
253
                editText.addValidator(new RegexpValidator(numericIntegerObject.getString(JsonFormConstants.ERR),
×
254
                        "\\d*"));
255

256
                if (jsonObject.has(JsonFormConstants.V_MIN)) {
×
257
                    JSONObject minValidation = jsonObject.getJSONObject(JsonFormConstants.V_MIN);
×
258
                    editText.addValidator(new MinNumericValidator(minValidation.getString(JsonFormConstants.ERR),
×
259
                            Double.parseDouble(minValidation.getString(JsonFormConstants.VALUE))));
×
260
                }
261

262
                if (jsonObject.has(JsonFormConstants.V_MAX)) {
×
263
                    JSONObject minValidation = jsonObject.getJSONObject(JsonFormConstants.V_MAX);
×
264
                    editText.addValidator(new MaxNumericValidator(minValidation.getString(JsonFormConstants.ERR),
×
265
                            Double.parseDouble(minValidation.getString(JsonFormConstants.VALUE))));
×
266
                }
267
            }
268
        }
269
    }
1✔
270

271
    @Override
272
    public Set<String> getCustomTranslatableWidgetFields() {
273
        return new HashSet<>();
1✔
274
    }
275
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc