Commit d542b9ff authored by rahadi's avatar rahadi

Added real time validation mechanism

parent 255714c9
......@@ -4,24 +4,36 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import org.javarosa.core.model.FormIndex;
import java.io.InvalidObjectException;
import java.util.ArrayList;
import id.ac.stis.capi.lessthink.listeners.OnChangeListener;
import id.ac.stis.capi.lessthink.models.BaseHierarchy;
import id.ac.stis.capi.lessthink.models.PromptHierarchy;
import id.ac.stis.capi.lessthink.utils.ViewUtils;
import id.ac.stis.capi.odk.widgets.QuestionWidget;
import timber.log.Timber;
/**
* Author : Rahadi Jalu
* Email : 14.8325@stis.ac.id
* Company: Politeknik Statistika STIS
*/
public class HierarchyAdapter extends BaseAdapter {
public class HierarchyAdapter extends BaseAdapter implements OnChangeListener {
private ArrayList<BaseHierarchy> hierarchies;
private OnChangeListener onChangeListener;
public HierarchyAdapter(ArrayList<BaseHierarchy> hierarchies) {
this.hierarchies = hierarchies;
}
public void setOnChangeListener(OnChangeListener onChangeListener) {
this.onChangeListener = onChangeListener;
}
@Override
public int getCount() {
return hierarchies.size();
......@@ -45,19 +57,36 @@ public class HierarchyAdapter extends BaseAdapter {
@Override
public View getView(final int position, View view, final ViewGroup parent) {
View v = hierarchies.get(position).getView();
BaseHierarchy hierarchy = hierarchies.get(position);
View v = hierarchy.getView();
if (v instanceof QuestionWidget) {
v.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
((QuestionWidget) v).setFocus(parent.getContext());
}
if (hierarchy instanceof PromptHierarchy) {
final QuestionWidget widget = ((PromptHierarchy) hierarchy).getWidget();
// widget.setOnFocusChangeListener(new View.OnFocusChangeListener() {
// @Override
// public void onFocusChange(View v, boolean hasFocus) {
// if (hasFocus) {
// widget.setFocus(parent.getContext());
// }
// }
// });
try {
ViewUtils.setChangeListener(widget, ((PromptHierarchy) hierarchy).getPrompt().getIndex(),
this);
} catch (InvalidObjectException e) {
Timber.e(e);
}
});
}
return v;
}
@Override
public void onChange(QuestionWidget questionWidget, FormIndex formIndex) {
if (onChangeListener != null) {
onChangeListener.onChange(questionWidget, formIndex);
}
}
}
\ No newline at end of file
......@@ -54,7 +54,7 @@ public class CaptionHierarchy extends BaseHierarchy {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
75, 75);
params.topMargin = 15;
params.topMargin = 26;
params.bottomMargin = 15;
textView.setLayoutParams(params);
} else if (getType() == TYPE_NEW_REPEAT) {
......@@ -67,7 +67,7 @@ public class CaptionHierarchy extends BaseHierarchy {
ta.recycle();
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, Collect.getQuestionFontsize() - 1);
textView.setPadding(25, 45, 25, 45);
textView.setPadding(15, 45, 15, 45);
textView.setTextColor(ContextCompat.getColor(context, R.color.white));
textView.setBackground(drawable);
......@@ -165,6 +165,20 @@ public class CaptionHierarchy extends BaseHierarchy {
this.captionView = container;
// View v = LayoutInflater.from(context).inflate(R.layout.item_updating_hierarchy, null);
// View mainContent = v.findViewById(R.id.main_content);
//
// ViewGroup parent = (ViewGroup) mainContent.getParent();
// int index = parent.indexOfChild(mainContent);
//
// parent.removeView(mainContent);
// parent.addView(textView, index);
//
// ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) parent.getLayoutParams();
// params.setMargins(6 * getLevel(), 0, 6 * getLevel(), 0);
// parent.setLayoutParams(params);
//
// this.captionView = v;
}
public View getText() {
......
......@@ -8,12 +8,18 @@ import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import org.javarosa.core.model.FormIndex;
import org.javarosa.form.api.FormEntryPrompt;
import java.io.InvalidObjectException;
import id.ac.stis.capi.R;
import id.ac.stis.capi.lessthink.listeners.OnChangeListener;
import id.ac.stis.capi.lessthink.utils.ViewUtils;
import id.ac.stis.capi.lessthink.views.HierarchyErrorView;
import id.ac.stis.capi.odk.widgets.QuestionWidget;
import id.ac.stis.capi.odk.widgets.WidgetFactory;
import timber.log.Timber;
/**
* Author : Rahadi Jalu
......@@ -70,10 +76,10 @@ public class PromptHierarchy extends BaseHierarchy {
LinearLayout.LayoutParams errorParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
errorParams.leftMargin = 15;
errorParams.bottomMargin = 15;
errorParams.rightMargin = 15;
errorParams.topMargin = 15;
// errorParams.leftMargin = 20;
// errorParams.bottomMargin = 14;
// errorParams.rightMargin = 20;
// errorParams.topMargin = 14;
errorView.setLayoutParams(errorParams);
widgetContainer.addView(errorView);
......@@ -138,6 +144,7 @@ public class PromptHierarchy extends BaseHierarchy {
}
public void setError(String message) {
Timber.d("Set Error: %s %s", message, prompt.getIndex().getReference().toString());
if (message == null) {
widgetContainer.setBackgroundColor(ContextCompat.getColor(context, R.color.white));
errorView.setVisibility(View.GONE);
......
......@@ -2,6 +2,7 @@ package id.ac.stis.capi.lessthink.utils;
import android.support.v7.widget.AppCompatButton;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
......@@ -17,6 +18,8 @@ import org.javarosa.core.model.FormIndex;
import java.io.InvalidObjectException;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import id.ac.stis.capi.lessthink.interfaces.UpdatingErrorInterface;
import id.ac.stis.capi.lessthink.listeners.OnChangeListener;
......@@ -33,7 +36,6 @@ import id.ac.stis.capi.odk.widgets.SelectOneWidget;
import id.ac.stis.capi.odk.widgets.SpinnerWidget;
import id.ac.stis.capi.odk.widgets.StringWidget;
import id.ac.stis.capi.odk.widgets.TriggerWidget;
import timber.log.Timber;
/**
* Author : Rahadi Jalu
......@@ -178,6 +180,10 @@ public class ViewUtils {
if (ed != null) {
ed.addTextChangedListener(new TextWatcher() {
private final long DELAY = 500;
private Timer timer = new Timer();
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
......@@ -190,6 +196,11 @@ public class ViewUtils {
@Override
public void afterTextChanged(Editable s) {
timer.cancel();
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if (onChangeListener != null) {
onChangeListener.onChange(qw, index);
}
......@@ -199,6 +210,8 @@ public class ViewUtils {
else if (qw instanceof ExStringWidget)
((ExStringWidget) qw).getEditText().requestFocus();
}
}, DELAY);
}
});
return;
}
......
package id.ac.stis.capi.lessthink.views;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.TypedValue;
import android.view.Gravity;
......@@ -30,7 +28,8 @@ public class HierarchyErrorView extends LinearLayout {
setGravity(Gravity.CENTER_VERTICAL);
setPadding(25, 25, 25, 25);
setBackground(ContextCompat.getDrawable(context, R.drawable.bg_border_tb));
// setBackground(ContextCompat.getDrawable(context, R.drawable.bg_border_t));
setBackgroundColor(ContextCompat.getColor(context, R.color.smooth_red));
this.messageView = new TextView(context);
messageView.setGravity(Gravity.CENTER_VERTICAL);
......
......@@ -1032,7 +1032,7 @@ public class FormController {
prompts.add(new CaptionHierarchy(context, fcRep, CaptionHierarchy.TYPE_NEW_REPEAT,
prevType, currLevel, prevLevel));
prevLevel = CaptionHierarchy.TYPE_NEW_REPEAT;
prevType = CaptionHierarchy.TYPE_NEW_REPEAT;
prevLevel = currLevel;
break;
case FormEntryController.EVENT_REPEAT:
......@@ -1052,14 +1052,13 @@ public class FormController {
// Display the repeat header for the group.
prompts.add(new CaptionHierarchy(context, fc, CaptionHierarchy.TYPE_REPEAT,
prevType, currLevel, prevLevel));
prevType = CaptionHierarchy.TYPE_REPEAT;
prevLevel = currLevel;
}
prompts.add(new CaptionHierarchy(context, fc, CaptionHierarchy.TYPE_MULTIPLICITY,
prevType, currLevel, prevLevel));
prevType = CaptionHierarchy.TYPE_REPEAT;
prevLevel = currLevel;
// prevLevel = currLevel;
// currLevel++;
break;
......
......@@ -8,6 +8,7 @@
</item>
<item
android:top="-2dp"
android:left="-2dp"
android:right="-2dp">
<shape>
......
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/smooth_red" />
</shape>
</item>
<item
android:bottom="-2dp"
android:left="-2dp"
android:right="-2dp">
<shape>
<solid android:color="@android:color/transparent" />
<stroke
android:width="1dp"
android:color="@color/red_latte" />
</shape>
</item>
</layer-list>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment