From a6c5430cdd400389a311ea9c2ab8a6fabc2f72c5 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 22 Jan 2016 20:22:47 +0100 Subject: [PATCH] added UI wrapper for (some) form fields --- .../ui/forms/FormFieldFactory.java | 24 +++++++++ .../ui/forms/FormFieldWrapper.java | 47 +++++++++++++++++ .../ui/forms/FormTextFieldWrapper.java | 52 +++++++++++++++++++ .../siacs/conversations/xmpp/forms/Data.java | 6 ++- .../siacs/conversations/xmpp/forms/Field.java | 12 +++++ src/main/res/layout/form_text.xml | 21 ++++++++ 6 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 src/main/java/eu/siacs/conversations/ui/forms/FormFieldFactory.java create mode 100644 src/main/java/eu/siacs/conversations/ui/forms/FormFieldWrapper.java create mode 100644 src/main/java/eu/siacs/conversations/ui/forms/FormTextFieldWrapper.java create mode 100644 src/main/res/layout/form_text.xml diff --git a/src/main/java/eu/siacs/conversations/ui/forms/FormFieldFactory.java b/src/main/java/eu/siacs/conversations/ui/forms/FormFieldFactory.java new file mode 100644 index 000000000..9e54678a3 --- /dev/null +++ b/src/main/java/eu/siacs/conversations/ui/forms/FormFieldFactory.java @@ -0,0 +1,24 @@ +package eu.siacs.conversations.ui.forms; + +import android.content.Context; + +import java.util.Hashtable; + +import eu.siacs.conversations.xmpp.forms.Field; + + + +public class FormFieldFactory { + + private static final Hashtable typeTable = new Hashtable<>(); + + static { + typeTable.put("text-single", FormTextFieldWrapper.class); + typeTable.put("text-multi", FormTextFieldWrapper.class); + } + + public static FormFieldWrapper createFromField(Context context, Field field) { + Class clazz = typeTable.get(field.getType()); + return FormFieldWrapper.createFromField(clazz, context, 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 new file mode 100644 index 000000000..3af375363 --- /dev/null +++ b/src/main/java/eu/siacs/conversations/ui/forms/FormFieldWrapper.java @@ -0,0 +1,47 @@ +package eu.siacs.conversations.ui.forms; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; + +import java.util.List; + +import eu.siacs.conversations.xmpp.forms.Field; + +public abstract class FormFieldWrapper { + + protected final Context context; + protected final Field field; + protected final View view; + + protected FormFieldWrapper(Context context, Field field) { + this.context = context; + this.field = field; + LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + this.view = inflater.inflate(getLayoutResource(), null); + setLabel(field.getLabel(), field.isRequired()); + } + + public void submit() { + this.field.setValues(getValues()); + } + + public View getView() { + return view; + } + + protected abstract void setLabel(String label, boolean required); + + abstract List getValues(); + + abstract protected int getLayoutResource(); + + protected static FormFieldWrapper createFromField(Class c, Context context, Field field) { + try { + return c.getDeclaredConstructor(Context.class, Field.class).newInstance(context,field); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } +} diff --git a/src/main/java/eu/siacs/conversations/ui/forms/FormTextFieldWrapper.java b/src/main/java/eu/siacs/conversations/ui/forms/FormTextFieldWrapper.java new file mode 100644 index 000000000..8f70dd37e --- /dev/null +++ b/src/main/java/eu/siacs/conversations/ui/forms/FormTextFieldWrapper.java @@ -0,0 +1,52 @@ +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.widget.EditText; +import android.widget.TextView; + +import java.util.ArrayList; +import java.util.List; + +import eu.siacs.conversations.R; +import eu.siacs.conversations.xmpp.forms.Field; + +public class FormTextFieldWrapper extends FormFieldWrapper { + + protected EditText editText; + + protected FormTextFieldWrapper(Context context, Field field) { + super(context, field); + editText = (EditText) view.findViewById(R.id.field); + editText.setSingleLine("text-single".equals(field.getType())); + } + + @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); + } + + @Override + List getValues() { + List values = new ArrayList<>(); + for (String line : editText.getText().toString().split("\\n")) { + values.add(line); + } + return values; + } + + @Override + protected int getLayoutResource() { + return R.layout.form_text; + } +} diff --git a/src/main/java/eu/siacs/conversations/xmpp/forms/Data.java b/src/main/java/eu/siacs/conversations/xmpp/forms/Data.java index 44794c80c..d05c9abb3 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/forms/Data.java +++ b/src/main/java/eu/siacs/conversations/xmpp/forms/Data.java @@ -76,10 +76,14 @@ public class Data extends Element { } public void setFormType(String formType) { - this.put("FORM_TYPE",formType); + this.put("FORM_TYPE", formType); } public String getFormType() { return this.getAttribute("FORM_TYPE"); } + + public String getTitle() { + return findChildContent("title"); + } } diff --git a/src/main/java/eu/siacs/conversations/xmpp/forms/Field.java b/src/main/java/eu/siacs/conversations/xmpp/forms/Field.java index 3ec1f2149..c1fc808dc 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/forms/Field.java +++ b/src/main/java/eu/siacs/conversations/xmpp/forms/Field.java @@ -51,4 +51,16 @@ public class Field extends Element { public String getValue() { return findChildContent("value"); } + + public String getLabel() { + return getAttribute("label"); + } + + public String getType() { + return getAttribute("type"); + } + + public boolean isRequired() { + return hasChild("required"); + } } diff --git a/src/main/res/layout/form_text.xml b/src/main/res/layout/form_text.xml new file mode 100644 index 000000000..e36ac74e9 --- /dev/null +++ b/src/main/res/layout/form_text.xml @@ -0,0 +1,21 @@ + + + + + + \ No newline at end of file