Commit d542b9ff authored by rahadi's avatar rahadi

Added real time validation mechanism

parent 255714c9
...@@ -4,24 +4,36 @@ import android.view.View; ...@@ -4,24 +4,36 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.BaseAdapter; import android.widget.BaseAdapter;
import org.javarosa.core.model.FormIndex;
import java.io.InvalidObjectException;
import java.util.ArrayList; 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.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 id.ac.stis.capi.odk.widgets.QuestionWidget;
import timber.log.Timber;
/** /**
* Author : Rahadi Jalu * Author : Rahadi Jalu
* Email : 14.8325@stis.ac.id * Email : 14.8325@stis.ac.id
* Company: Politeknik Statistika STIS * Company: Politeknik Statistika STIS
*/ */
public class HierarchyAdapter extends BaseAdapter { public class HierarchyAdapter extends BaseAdapter implements OnChangeListener {
private ArrayList<BaseHierarchy> hierarchies; private ArrayList<BaseHierarchy> hierarchies;
private OnChangeListener onChangeListener;
public HierarchyAdapter(ArrayList<BaseHierarchy> hierarchies) { public HierarchyAdapter(ArrayList<BaseHierarchy> hierarchies) {
this.hierarchies = hierarchies; this.hierarchies = hierarchies;
} }
public void setOnChangeListener(OnChangeListener onChangeListener) {
this.onChangeListener = onChangeListener;
}
@Override @Override
public int getCount() { public int getCount() {
return hierarchies.size(); return hierarchies.size();
...@@ -45,19 +57,36 @@ public class HierarchyAdapter extends BaseAdapter { ...@@ -45,19 +57,36 @@ public class HierarchyAdapter extends BaseAdapter {
@Override @Override
public View getView(final int position, View view, final ViewGroup parent) { 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() { if (hierarchy instanceof PromptHierarchy) {
@Override final QuestionWidget widget = ((PromptHierarchy) hierarchy).getWidget();
public void onFocusChange(View v, boolean hasFocus) { // widget.setOnFocusChangeListener(new View.OnFocusChangeListener() {
if (hasFocus) { // @Override
((QuestionWidget) v).setFocus(parent.getContext()); // 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; return v;
} }
@Override
public void onChange(QuestionWidget questionWidget, FormIndex formIndex) {
if (onChangeListener != null) {
onChangeListener.onChange(questionWidget, formIndex);
}
}
} }
\ No newline at end of file
...@@ -20,4 +20,4 @@ public interface UpdatingErrorInterface { ...@@ -20,4 +20,4 @@ public interface UpdatingErrorInterface {
* Specifically made for updating mode. * Specifically made for updating mode.
* */ * */
void clearError(); void clearError();
} }
\ No newline at end of file
...@@ -54,7 +54,7 @@ public class CaptionHierarchy extends BaseHierarchy { ...@@ -54,7 +54,7 @@ public class CaptionHierarchy extends BaseHierarchy {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
75, 75); 75, 75);
params.topMargin = 15; params.topMargin = 26;
params.bottomMargin = 15; params.bottomMargin = 15;
textView.setLayoutParams(params); textView.setLayoutParams(params);
} else if (getType() == TYPE_NEW_REPEAT) { } else if (getType() == TYPE_NEW_REPEAT) {
...@@ -67,7 +67,7 @@ public class CaptionHierarchy extends BaseHierarchy { ...@@ -67,7 +67,7 @@ public class CaptionHierarchy extends BaseHierarchy {
ta.recycle(); ta.recycle();
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, Collect.getQuestionFontsize() - 1); 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.setTextColor(ContextCompat.getColor(context, R.color.white));
textView.setBackground(drawable); textView.setBackground(drawable);
...@@ -165,6 +165,20 @@ public class CaptionHierarchy extends BaseHierarchy { ...@@ -165,6 +165,20 @@ public class CaptionHierarchy extends BaseHierarchy {
this.captionView = container; 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() { public View getText() {
......
...@@ -8,12 +8,18 @@ import android.view.ViewGroup; ...@@ -8,12 +8,18 @@ import android.view.ViewGroup;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import org.javarosa.core.model.FormIndex;
import org.javarosa.form.api.FormEntryPrompt; import org.javarosa.form.api.FormEntryPrompt;
import java.io.InvalidObjectException;
import id.ac.stis.capi.R; 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.lessthink.views.HierarchyErrorView;
import id.ac.stis.capi.odk.widgets.QuestionWidget; import id.ac.stis.capi.odk.widgets.QuestionWidget;
import id.ac.stis.capi.odk.widgets.WidgetFactory; import id.ac.stis.capi.odk.widgets.WidgetFactory;
import timber.log.Timber;
/** /**
* Author : Rahadi Jalu * Author : Rahadi Jalu
...@@ -70,10 +76,10 @@ public class PromptHierarchy extends BaseHierarchy { ...@@ -70,10 +76,10 @@ public class PromptHierarchy extends BaseHierarchy {
LinearLayout.LayoutParams errorParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams errorParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
errorParams.leftMargin = 15; // errorParams.leftMargin = 20;
errorParams.bottomMargin = 15; // errorParams.bottomMargin = 14;
errorParams.rightMargin = 15; // errorParams.rightMargin = 20;
errorParams.topMargin = 15; // errorParams.topMargin = 14;
errorView.setLayoutParams(errorParams); errorView.setLayoutParams(errorParams);
widgetContainer.addView(errorView); widgetContainer.addView(errorView);
...@@ -138,6 +144,7 @@ public class PromptHierarchy extends BaseHierarchy { ...@@ -138,6 +144,7 @@ public class PromptHierarchy extends BaseHierarchy {
} }
public void setError(String message) { public void setError(String message) {
Timber.d("Set Error: %s %s", message, prompt.getIndex().getReference().toString());
if (message == null) { if (message == null) {
widgetContainer.setBackgroundColor(ContextCompat.getColor(context, R.color.white)); widgetContainer.setBackgroundColor(ContextCompat.getColor(context, R.color.white));
errorView.setVisibility(View.GONE); errorView.setVisibility(View.GONE);
......
...@@ -2,6 +2,7 @@ package id.ac.stis.capi.lessthink.utils; ...@@ -2,6 +2,7 @@ package id.ac.stis.capi.lessthink.utils;
import android.support.v7.widget.AppCompatButton; import android.support.v7.widget.AppCompatButton;
import android.text.Editable; import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
...@@ -17,6 +18,8 @@ import org.javarosa.core.model.FormIndex; ...@@ -17,6 +18,8 @@ import org.javarosa.core.model.FormIndex;
import java.io.InvalidObjectException; import java.io.InvalidObjectException;
import java.util.ArrayList; 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.interfaces.UpdatingErrorInterface;
import id.ac.stis.capi.lessthink.listeners.OnChangeListener; import id.ac.stis.capi.lessthink.listeners.OnChangeListener;
...@@ -33,7 +36,6 @@ import id.ac.stis.capi.odk.widgets.SelectOneWidget; ...@@ -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.SpinnerWidget;
import id.ac.stis.capi.odk.widgets.StringWidget; import id.ac.stis.capi.odk.widgets.StringWidget;
import id.ac.stis.capi.odk.widgets.TriggerWidget; import id.ac.stis.capi.odk.widgets.TriggerWidget;
import timber.log.Timber;
/** /**
* Author : Rahadi Jalu * Author : Rahadi Jalu
...@@ -178,6 +180,10 @@ public class ViewUtils { ...@@ -178,6 +180,10 @@ public class ViewUtils {
if (ed != null) { if (ed != null) {
ed.addTextChangedListener(new TextWatcher() { ed.addTextChangedListener(new TextWatcher() {
private final long DELAY = 500;
private Timer timer = new Timer();
@Override @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { public void beforeTextChanged(CharSequence s, int start, int count, int after) {
...@@ -190,14 +196,21 @@ public class ViewUtils { ...@@ -190,14 +196,21 @@ public class ViewUtils {
@Override @Override
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
if (onChangeListener != null) { timer.cancel();
onChangeListener.onChange(qw, index); timer = new Timer();
} timer.schedule(new TimerTask() {
@Override
public void run() {
if (onChangeListener != null) {
onChangeListener.onChange(qw, index);
}
if (qw instanceof StringWidget) if (qw instanceof StringWidget)
((StringWidget) qw).getEditText().requestFocus(); ((StringWidget) qw).getEditText().requestFocus();
else if (qw instanceof ExStringWidget) else if (qw instanceof ExStringWidget)
((ExStringWidget) qw).getEditText().requestFocus(); ((ExStringWidget) qw).getEditText().requestFocus();
}
}, DELAY);
} }
}); });
return; return;
......
package id.ac.stis.capi.lessthink.views; package id.ac.stis.capi.lessthink.views;
import android.content.Context; import android.content.Context;
import android.graphics.Paint;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.Gravity; import android.view.Gravity;
...@@ -30,7 +28,8 @@ public class HierarchyErrorView extends LinearLayout { ...@@ -30,7 +28,8 @@ public class HierarchyErrorView extends LinearLayout {
setGravity(Gravity.CENTER_VERTICAL); setGravity(Gravity.CENTER_VERTICAL);
setPadding(25, 25, 25, 25); 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); this.messageView = new TextView(context);
messageView.setGravity(Gravity.CENTER_VERTICAL); messageView.setGravity(Gravity.CENTER_VERTICAL);
......
...@@ -1032,7 +1032,7 @@ public class FormController { ...@@ -1032,7 +1032,7 @@ public class FormController {
prompts.add(new CaptionHierarchy(context, fcRep, CaptionHierarchy.TYPE_NEW_REPEAT, prompts.add(new CaptionHierarchy(context, fcRep, CaptionHierarchy.TYPE_NEW_REPEAT,
prevType, currLevel, prevLevel)); prevType, currLevel, prevLevel));
prevLevel = CaptionHierarchy.TYPE_NEW_REPEAT; prevType = CaptionHierarchy.TYPE_NEW_REPEAT;
prevLevel = currLevel; prevLevel = currLevel;
break; break;
case FormEntryController.EVENT_REPEAT: case FormEntryController.EVENT_REPEAT:
...@@ -1052,14 +1052,13 @@ public class FormController { ...@@ -1052,14 +1052,13 @@ public class FormController {
// Display the repeat header for the group. // Display the repeat header for the group.
prompts.add(new CaptionHierarchy(context, fc, CaptionHierarchy.TYPE_REPEAT, prompts.add(new CaptionHierarchy(context, fc, CaptionHierarchy.TYPE_REPEAT,
prevType, currLevel, prevLevel)); prevType, currLevel, prevLevel));
prevType = CaptionHierarchy.TYPE_REPEAT;
prevLevel = currLevel;
} }
prompts.add(new CaptionHierarchy(context, fc, CaptionHierarchy.TYPE_MULTIPLICITY, prompts.add(new CaptionHierarchy(context, fc, CaptionHierarchy.TYPE_MULTIPLICITY,
prevType, currLevel, prevLevel)); prevType, currLevel, prevLevel));
prevType = CaptionHierarchy.TYPE_REPEAT;
prevLevel = currLevel;
// prevLevel = currLevel; // prevLevel = currLevel;
// currLevel++; // currLevel++;
break; break;
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
</item> </item>
<item <item
android:top="-2dp"
android:left="-2dp" android:left="-2dp"
android:right="-2dp"> android:right="-2dp">
<shape> <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