From 0569a1e7698141262e3f8443be26b5cb053954b8 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 23 Jan 2016 16:23:23 +0100 Subject: [PATCH] introduced boolean form field wrapper --- .../ui/forms/FormBooleanFieldWrapper.java | 43 +++++++++++++++++++ .../ui/forms/FormFieldFactory.java | 1 + .../ui/forms/FormFieldWrapper.java | 19 +++++++- .../ui/forms/FormTextFieldWrapper.java | 12 +----- src/main/res/layout/form_boolean.xml | 13 ++++++ src/main/res/layout/form_text.xml | 2 +- 6 files changed, 76 insertions(+), 14 deletions(-) create mode 100644 src/main/java/eu/siacs/conversations/ui/forms/FormBooleanFieldWrapper.java create mode 100644 src/main/res/layout/form_boolean.xml diff --git a/src/main/java/eu/siacs/conversations/ui/forms/FormBooleanFieldWrapper.java b/src/main/java/eu/siacs/conversations/ui/forms/FormBooleanFieldWrapper.java new file mode 100644 index 000000000..e9adf15a3 --- /dev/null +++ b/src/main/java/eu/siacs/conversations/ui/forms/FormBooleanFieldWrapper.java @@ -0,0 +1,43 @@ +package eu.siacs.conversations.ui.forms; + +import android.content.Context; +import android.widget.CheckBox; + +import java.util.ArrayList; +import java.util.List; + +import eu.siacs.conversations.R; +import eu.siacs.conversations.xmpp.forms.Field; + +public class FormBooleanFieldWrapper extends FormFieldWrapper { + + protected CheckBox checkBox; + + protected FormBooleanFieldWrapper(Context context, Field field) { + super(context, field); + checkBox = (CheckBox) view.findViewById(R.id.field); + } + + @Override + protected void setLabel(String label, boolean required) { + CheckBox checkBox = (CheckBox) view.findViewById(R.id.field); + checkBox.setText(createSpannableLabelString(label, required)); + } + + @Override + public List getValues() { + List values = new ArrayList<>(); + values.add(Boolean.toString(checkBox.isChecked())); + return values; + } + + @Override + public boolean validates() { + return checkBox.isChecked() || !field.isRequired(); + } + + @Override + protected int getLayoutResource() { + return R.layout.form_boolean; + } +} diff --git a/src/main/java/eu/siacs/conversations/ui/forms/FormFieldFactory.java b/src/main/java/eu/siacs/conversations/ui/forms/FormFieldFactory.java index 2e7c17dc2..966c0e5aa 100644 --- a/src/main/java/eu/siacs/conversations/ui/forms/FormFieldFactory.java +++ b/src/main/java/eu/siacs/conversations/ui/forms/FormFieldFactory.java @@ -17,6 +17,7 @@ public class FormFieldFactory { typeTable.put("text-multi", FormTextFieldWrapper.class); typeTable.put("text-private", FormTextFieldWrapper.class); typeTable.put("jid-single", FormJidSingleFieldWrapper.class); + typeTable.put("boolean", FormBooleanFieldWrapper.class); } public static FormFieldWrapper createFromField(Context context, Field field) { diff --git a/src/main/java/eu/siacs/conversations/ui/forms/FormFieldWrapper.java b/src/main/java/eu/siacs/conversations/ui/forms/FormFieldWrapper.java index a0ed371a3..f2560ef51 100644 --- a/src/main/java/eu/siacs/conversations/ui/forms/FormFieldWrapper.java +++ b/src/main/java/eu/siacs/conversations/ui/forms/FormFieldWrapper.java @@ -1,11 +1,15 @@ package eu.siacs.conversations.ui.forms; import android.content.Context; +import android.text.SpannableString; +import android.text.style.ForegroundColorSpan; +import android.text.style.StyleSpan; import android.view.LayoutInflater; import android.view.View; import java.util.List; +import eu.siacs.conversations.R; import eu.siacs.conversations.xmpp.forms.Field; public abstract class FormFieldWrapper { @@ -26,11 +30,11 @@ public abstract class FormFieldWrapper { setLabel(label, field.isRequired()); } - public void submit() { + public final void submit() { this.field.setValues(getValues()); } - public View getView() { + public final View getView() { return view; } @@ -42,6 +46,17 @@ public abstract class FormFieldWrapper { abstract protected int getLayoutResource(); + protected SpannableString createSpannableLabelString(String label, boolean required) { + SpannableString spannableString = new SpannableString(label + (required ? " *" : "")); + if (required) { + int start = label.length(); + int end = label.length() + 2; + spannableString.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), start, end, 0); + spannableString.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.accent)), start, end, 0); + } + return spannableString; + } + protected static FormFieldWrapper createFromField(Class c, Context context, Field field) { try { return c.getDeclaredConstructor(Context.class, Field.class).newInstance(context,field); diff --git a/src/main/java/eu/siacs/conversations/ui/forms/FormTextFieldWrapper.java b/src/main/java/eu/siacs/conversations/ui/forms/FormTextFieldWrapper.java index 71ed6e4ab..274936e86 100644 --- a/src/main/java/eu/siacs/conversations/ui/forms/FormTextFieldWrapper.java +++ b/src/main/java/eu/siacs/conversations/ui/forms/FormTextFieldWrapper.java @@ -2,9 +2,6 @@ package eu.siacs.conversations.ui.forms; import android.content.Context; import android.text.InputType; -import android.text.SpannableString; -import android.text.style.ForegroundColorSpan; -import android.text.style.StyleSpan; import android.widget.EditText; import android.widget.TextView; @@ -30,14 +27,7 @@ public class FormTextFieldWrapper extends FormFieldWrapper { @Override protected void setLabel(String label, boolean required) { TextView textView = (TextView) view.findViewById(R.id.label); - SpannableString spannableString = new SpannableString(label + (required ? " *" : "")); - if (required) { - int start = label.length(); - int end = label.length() + 2; - spannableString.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), start, end, 0); - spannableString.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.accent)), start, end, 0); - } - textView.setText(spannableString); + textView.setText(createSpannableLabelString(label, required)); } protected String getValue() { diff --git a/src/main/res/layout/form_boolean.xml b/src/main/res/layout/form_boolean.xml new file mode 100644 index 000000000..fd553acb2 --- /dev/null +++ b/src/main/res/layout/form_boolean.xml @@ -0,0 +1,13 @@ + + + + \ No newline at end of file diff --git a/src/main/res/layout/form_text.xml b/src/main/res/layout/form_text.xml index e36ac74e9..31b521e89 100644 --- a/src/main/res/layout/form_text.xml +++ b/src/main/res/layout/form_text.xml @@ -2,12 +2,12 @@