diff options
6 files changed, 161 insertions, 1 deletions
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<String, Class> 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<String> getValues(); + + abstract protected int getLayoutResource(); + + protected static <F extends FormFieldWrapper> FormFieldWrapper createFromField(Class<F> 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<String> getValues() { + List<String> 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 @@ +<?xml version="1.0" encoding="utf-8"?> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:orientation="vertical"> + <TextView + android:id="@+id/label" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginTop="8dp" + android:textColor="@color/black87" + android:textSize="?attr/TextSizeBody"/> + + <EditText + android:id="@+id/field" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:textColor="@color/black87" + android:textColorHint="@color/black54" + android:textSize="?attr/TextSizeBody"/> +</LinearLayout>
\ No newline at end of file |