added UI wrapper for (some) form fields

This commit is contained in:
Daniel Gultsch 2016-01-22 20:22:47 +01:00
parent a47430c2f7
commit a6c5430cdd
6 changed files with 161 additions and 1 deletions

View File

@ -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);
}
}

View File

@ -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;
}
}
}

View File

@ -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;
}
}

View File

@ -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");
}
}

View File

@ -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");
}
}

View File

@ -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>