show values in formfieldwrappers and allow form to be set to read only

This commit is contained in:
Daniel Gultsch 2016-01-26 17:23:24 +01:00
parent edc6ce4ff2
commit d2c5a939ed
6 changed files with 58 additions and 7 deletions

View File

@ -39,6 +39,15 @@ public class FormBooleanFieldWrapper extends FormFieldWrapper {
return values; return values;
} }
@Override
protected void setValues(List<String> values) {
if (values.size() == 0) {
checkBox.setChecked(false);
} else {
checkBox.setChecked(Boolean.parseBoolean(values.get(0)));
}
}
@Override @Override
public boolean validates() { public boolean validates() {
if (checkBox.isChecked() || !field.isRequired()) { if (checkBox.isChecked() || !field.isRequired()) {
@ -63,4 +72,9 @@ public class FormBooleanFieldWrapper extends FormFieldWrapper {
protected int getLayoutResource() { protected int getLayoutResource() {
return R.layout.form_boolean; return R.layout.form_boolean;
} }
@Override
void setReadOnly(boolean readOnly) {
checkBox.setEnabled(!readOnly);
}
} }

View File

@ -45,10 +45,14 @@ public abstract class FormFieldWrapper {
abstract List<String> getValues(); abstract List<String> getValues();
protected abstract void setValues(List<String> values);
abstract boolean validates(); abstract boolean validates();
abstract protected int getLayoutResource(); abstract protected int getLayoutResource();
abstract void setReadOnly(boolean readOnly);
protected SpannableString createSpannableLabelString(String label, boolean required) { protected SpannableString createSpannableLabelString(String label, boolean required) {
SpannableString spannableString = new SpannableString(label + (required ? " *" : "")); SpannableString spannableString = new SpannableString(label + (required ? " *" : ""));
if (required) { if (required) {
@ -61,11 +65,8 @@ public abstract class FormFieldWrapper {
} }
protected void invokeOnFormFieldValuesEdited() { protected void invokeOnFormFieldValuesEdited() {
Log.d(Config.LOGTAG, "invoke on form field values edited");
if (this.onFormFieldValuesEditedListener != null) { if (this.onFormFieldValuesEditedListener != null) {
this.onFormFieldValuesEditedListener.onFormFieldValuesEdited(); this.onFormFieldValuesEditedListener.onFormFieldValuesEdited();
} else {
Log.d(Config.LOGTAG,"listener is null");
} }
} }
@ -79,7 +80,9 @@ public abstract class FormFieldWrapper {
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); F fieldWrapper = c.getDeclaredConstructor(Context.class, Field.class).newInstance(context,field);
fieldWrapper.setValues(field.getValues());
return fieldWrapper;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;

View File

@ -3,6 +3,8 @@ package eu.siacs.conversations.ui.forms;
import android.content.Context; import android.content.Context;
import android.text.InputType; import android.text.InputType;
import java.util.List;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
import eu.siacs.conversations.xmpp.forms.Field; import eu.siacs.conversations.xmpp.forms.Field;
import eu.siacs.conversations.xmpp.jid.InvalidJidException; import eu.siacs.conversations.xmpp.jid.InvalidJidException;
@ -30,4 +32,13 @@ public class FormJidSingleFieldWrapper extends FormTextFieldWrapper {
} }
return super.validates(); return super.validates();
} }
@Override
protected void setValues(List<String> values) {
StringBuilder builder = new StringBuilder("");
for(String value : values) {
builder.append(value);
}
editText.setText(builder.toString());
}
} }

View File

@ -62,6 +62,18 @@ public class FormTextFieldWrapper extends FormFieldWrapper {
return values; return values;
} }
@Override
protected void setValues(List<String> values) {
StringBuilder builder = new StringBuilder("");
for(int i = 0; i < values.size(); ++i) {
builder.append(values.get(i));
if (i < values.size() - 1 && "text-multi".equals(field.getType())) {
builder.append("\n");
}
}
editText.setText(builder.toString());
}
@Override @Override
public boolean validates() { public boolean validates() {
if (getValue().trim().length() > 0 || !field.isRequired()) { if (getValue().trim().length() > 0 || !field.isRequired()) {
@ -77,4 +89,9 @@ public class FormTextFieldWrapper extends FormFieldWrapper {
protected int getLayoutResource() { protected int getLayoutResource() {
return R.layout.form_text; return R.layout.form_text;
} }
@Override
void setReadOnly(boolean readOnly) {
editText.setEnabled(!readOnly);
}
} }

View File

@ -52,6 +52,12 @@ public class FormWrapper {
} }
} }
public void setReadOnly(boolean b) {
for(FormFieldWrapper fieldWrapper : fieldWrappers) {
fieldWrapper.setReadOnly(b);
}
}
public boolean edited() { public boolean edited() {
boolean edited = false; boolean edited = false;
for(FormFieldWrapper fieldWrapper : fieldWrappers) { for(FormFieldWrapper fieldWrapper : fieldWrappers) {

View File

@ -53,16 +53,16 @@ public class Data extends Element {
public void submit() { public void submit() {
this.setAttribute("type","submit"); this.setAttribute("type","submit");
removeNonFieldChildren(); removeUnnecessaryChildren();
for(Field field : getFields()) { for(Field field : getFields()) {
field.removeNonValueChildren(); field.removeNonValueChildren();
} }
} }
private void removeNonFieldChildren() { private void removeUnnecessaryChildren() {
for(Iterator<Element> iterator = this.children.iterator(); iterator.hasNext();) { for(Iterator<Element> iterator = this.children.iterator(); iterator.hasNext();) {
Element element = iterator.next(); Element element = iterator.next();
if (!element.getName().equals("field")) { if (!element.getName().equals("field") && !element.getName().equals("title")) {
iterator.remove(); iterator.remove();
} }
} }