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;
}
@Override
protected void setValues(List<String> values) {
if (values.size() == 0) {
checkBox.setChecked(false);
} else {
checkBox.setChecked(Boolean.parseBoolean(values.get(0)));
}
}
@Override
public boolean validates() {
if (checkBox.isChecked() || !field.isRequired()) {
@ -63,4 +72,9 @@ public class FormBooleanFieldWrapper extends FormFieldWrapper {
protected int getLayoutResource() {
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();
protected abstract void setValues(List<String> values);
abstract boolean validates();
abstract protected int getLayoutResource();
abstract void setReadOnly(boolean readOnly);
protected SpannableString createSpannableLabelString(String label, boolean required) {
SpannableString spannableString = new SpannableString(label + (required ? " *" : ""));
if (required) {
@ -61,11 +65,8 @@ public abstract class FormFieldWrapper {
}
protected void invokeOnFormFieldValuesEdited() {
Log.d(Config.LOGTAG, "invoke on form field values edited");
if (this.onFormFieldValuesEditedListener != null) {
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) {
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) {
e.printStackTrace();
return null;

View file

@ -3,6 +3,8 @@ package eu.siacs.conversations.ui.forms;
import android.content.Context;
import android.text.InputType;
import java.util.List;
import eu.siacs.conversations.R;
import eu.siacs.conversations.xmpp.forms.Field;
import eu.siacs.conversations.xmpp.jid.InvalidJidException;
@ -30,4 +32,13 @@ public class FormJidSingleFieldWrapper extends FormTextFieldWrapper {
}
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;
}
@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
public boolean validates() {
if (getValue().trim().length() > 0 || !field.isRequired()) {
@ -77,4 +89,9 @@ public class FormTextFieldWrapper extends FormFieldWrapper {
protected int getLayoutResource() {
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() {
boolean edited = false;
for(FormFieldWrapper fieldWrapper : fieldWrappers) {

View file

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