1package eu.siacs.conversations.ui.forms;
2
3import android.content.Context;
4import android.text.InputType;
5import android.text.SpannableString;
6import android.text.style.ForegroundColorSpan;
7import android.text.style.StyleSpan;
8import android.widget.EditText;
9import android.widget.TextView;
10
11import java.util.ArrayList;
12import java.util.List;
13
14import eu.siacs.conversations.R;
15import eu.siacs.conversations.xmpp.forms.Field;
16
17public class FormTextFieldWrapper extends FormFieldWrapper {
18
19 protected EditText editText;
20
21 protected FormTextFieldWrapper(Context context, Field field) {
22 super(context, field);
23 editText = (EditText) view.findViewById(R.id.field);
24 editText.setSingleLine(!"text-multi".equals(field.getType()));
25 if ("text-private".equals(field.getType())) {
26 editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
27 }
28 }
29
30 @Override
31 protected void setLabel(String label, boolean required) {
32 TextView textView = (TextView) view.findViewById(R.id.label);
33 SpannableString spannableString = new SpannableString(label + (required ? " *" : ""));
34 if (required) {
35 int start = label.length();
36 int end = label.length() + 2;
37 spannableString.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), start, end, 0);
38 spannableString.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.accent)), start, end, 0);
39 }
40 textView.setText(spannableString);
41 }
42
43 protected String getValue() {
44 return editText.getText().toString();
45 }
46
47 @Override
48 public List<String> getValues() {
49 List<String> values = new ArrayList<>();
50 for (String line : getValue().split("\\n")) {
51 values.add(line);
52 }
53 return values;
54 }
55
56 @Override
57 public boolean validates() {
58 return getValue().trim().length() > 0 || !field.isRequired();
59 }
60
61 @Override
62 protected int getLayoutResource() {
63 return R.layout.form_text;
64 }
65}