introduced boolean form field wrapper

This commit is contained in:
Daniel Gultsch 2016-01-23 16:23:23 +01:00
parent 39fdf4a333
commit 0569a1e769
6 changed files with 76 additions and 14 deletions

View File

@ -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<String> getValues() {
List<String> 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;
}
}

View File

@ -17,6 +17,7 @@ public class FormFieldFactory {
typeTable.put("text-multi", FormTextFieldWrapper.class); typeTable.put("text-multi", FormTextFieldWrapper.class);
typeTable.put("text-private", FormTextFieldWrapper.class); typeTable.put("text-private", FormTextFieldWrapper.class);
typeTable.put("jid-single", FormJidSingleFieldWrapper.class); typeTable.put("jid-single", FormJidSingleFieldWrapper.class);
typeTable.put("boolean", FormBooleanFieldWrapper.class);
} }
public static FormFieldWrapper createFromField(Context context, Field field) { public static FormFieldWrapper createFromField(Context context, Field field) {

View File

@ -1,11 +1,15 @@
package eu.siacs.conversations.ui.forms; package eu.siacs.conversations.ui.forms;
import android.content.Context; 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.LayoutInflater;
import android.view.View; import android.view.View;
import java.util.List; import java.util.List;
import eu.siacs.conversations.R;
import eu.siacs.conversations.xmpp.forms.Field; import eu.siacs.conversations.xmpp.forms.Field;
public abstract class FormFieldWrapper { public abstract class FormFieldWrapper {
@ -26,11 +30,11 @@ public abstract class FormFieldWrapper {
setLabel(label, field.isRequired()); setLabel(label, field.isRequired());
} }
public void submit() { public final void submit() {
this.field.setValues(getValues()); this.field.setValues(getValues());
} }
public View getView() { public final View getView() {
return view; return view;
} }
@ -42,6 +46,17 @@ public abstract class FormFieldWrapper {
abstract protected int getLayoutResource(); 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 <F extends FormFieldWrapper> FormFieldWrapper createFromField(Class<F> c, Context context, Field field) { protected static <F extends FormFieldWrapper> FormFieldWrapper createFromField(Class<F> c, Context context, Field field) {
try { try {
return c.getDeclaredConstructor(Context.class, Field.class).newInstance(context,field); return c.getDeclaredConstructor(Context.class, Field.class).newInstance(context,field);

View File

@ -2,9 +2,6 @@ package eu.siacs.conversations.ui.forms;
import android.content.Context; import android.content.Context;
import android.text.InputType; 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.EditText;
import android.widget.TextView; import android.widget.TextView;
@ -30,14 +27,7 @@ public class FormTextFieldWrapper extends FormFieldWrapper {
@Override @Override
protected void setLabel(String label, boolean required) { protected void setLabel(String label, boolean required) {
TextView textView = (TextView) view.findViewById(R.id.label); TextView textView = (TextView) view.findViewById(R.id.label);
SpannableString spannableString = new SpannableString(label + (required ? " *" : "")); textView.setText(createSpannableLabelString(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);
} }
protected String getValue() { protected String getValue() {

View File

@ -0,0 +1,13 @@
<?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:paddingTop="8dp"
android:orientation="vertical">
<CheckBox
android:id="@+id/field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black87"
android:textSize="?attr/TextSizeBody"/>
</LinearLayout>

View File

@ -2,12 +2,12 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:paddingTop="8dp"
android:orientation="vertical"> android:orientation="vertical">
<TextView <TextView
android:id="@+id/label" android:id="@+id/label"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textColor="@color/black87" android:textColor="@color/black87"
android:textSize="?attr/TextSizeBody"/> android:textSize="?attr/TextSizeBody"/>