aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/eu/siacs/conversations/ui/forms/FormFieldWrapper.java
blob: 3af375363cee655de7d504bd82e0c6e1492ccfdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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;
		}
	}
}