aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/eu/siacs/conversations/ui/forms/FormFieldWrapper.java
blob: cb504576f8ff00eed9a4e225749125ada696ba83 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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.view.LayoutInflater;
import android.view.View;

import java.util.List;

import de.thedevstack.conversationsplus.ConversationsPlusColors;

import eu.siacs.conversations.R;
import eu.siacs.conversations.xmpp.forms.Field;

public abstract class FormFieldWrapper {

	protected final Context context;
	protected final Field field;
	protected final View view;
	protected OnFormFieldValuesEdited onFormFieldValuesEditedListener;

	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);
		String label = field.getLabel();
		if (label == null) {
			label = field.getFieldName();
		}
		setLabel(label, field.isRequired());
	}

	public final void submit() {
		this.field.setValues(getValues());
	}

	public final View getView() {
		return view;
	}

	protected abstract void setLabel(String label, boolean required);

	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) {
			int start = label.length();
			int end = label.length() + 2;
			spannableString.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), start, end, 0);
			spannableString.setSpan(new ForegroundColorSpan(ConversationsPlusColors.accent()), start, end, 0);
		}
		return spannableString;
	}

	protected void invokeOnFormFieldValuesEdited() {
		if (this.onFormFieldValuesEditedListener != null) {
			this.onFormFieldValuesEditedListener.onFormFieldValuesEdited();
		}
	}

	public boolean edited() {
		return !field.getValues().equals(getValues());
	}

	public void setOnFormFieldValuesEditedListener(OnFormFieldValuesEdited listener) {
		this.onFormFieldValuesEditedListener = listener;
	}

	protected static <F extends FormFieldWrapper> FormFieldWrapper createFromField(Class<F> c, Context context, Field field) {
		try {
			F fieldWrapper = c.getDeclaredConstructor(Context.class, Field.class).newInstance(context,field);
			fieldWrapper.setValues(field.getValues());
			return fieldWrapper;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public interface OnFormFieldValuesEdited {
		void onFormFieldValuesEdited();
	}
}