Commit 20553845 authored by rahadi's avatar rahadi

WIP: changed updating view dialog mechanism

parent aafebc22
package id.ac.stis.capi.lessthink.adapters;
import android.content.pm.PackageManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
......@@ -38,16 +37,6 @@ public class HierarchyAdapter extends BaseAdapter {
return 0;
}
@Override
public int getItemViewType(int position) {
return hierarchies.get(position).getType();
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public boolean isEnabled(int position) {
return false;
......@@ -63,16 +52,7 @@ public class HierarchyAdapter extends BaseAdapter {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
for (BaseHierarchy hierarchy : hierarchies) {
View checkFocus = hierarchy.getView();
if (checkFocus != v) {
checkFocus.clearFocus();
} else {
v.requestFocus();
}
}
} else {
v.clearFocus();
((QuestionWidget) v).setFocus(parent.getContext());
}
}
});
......
......@@ -202,7 +202,7 @@ public class FormEntryDialogFragment extends DialogFragment implements FormLoade
private void setupChangeListener() {
for (BaseHierarchy hierarchy : hierarchies) {
if (hierarchy instanceof PromptHierarchy) {
QuestionWidget widget = (QuestionWidget) hierarchy.getView();
QuestionWidget widget = ((PromptHierarchy) hierarchy).getWidget();
try {
ViewUtils.setChangeListener(widget, ((PromptHierarchy) hierarchy).getPrompt().getIndex(), this);
} catch (InvalidObjectException e) {
......@@ -223,7 +223,7 @@ public class FormEntryDialogFragment extends DialogFragment implements FormLoade
// if (!hit) {
// hit = entry.getKey().equals(index.getReference().toString());
// } else {
//// questionContainer.removeView(getView().findViewWithTag(entry.getKey()));
//// questionContainer.removeView(getTextView().findViewWithTag(entry.getKey()));
// ViewGroup parent = (ViewGroup) entry.getValue().getParent();
// while (parent != questionContainer) {
// parent.removeView(parent.findViewWithTag(entry.getKey()));
......
......@@ -9,25 +9,25 @@ import android.view.View;
*/
public abstract class BaseHierarchy {
public static final int HIERARCHY_TYPE_PROMPT = 0;
public static final int HIERARCHY_TYPE_CAPTION = 1;
private int type;
private int level;
private int prevLevel;
public BaseHierarchy(int type, int level) {
this.type = type;
public BaseHierarchy(int level, int prevLevel) {
this.level = level;
this.prevLevel = prevLevel;
}
public abstract View getView();
public int getType() {
return type;
}
public abstract int getType();
public abstract int getPrevType();
public int getLevel() {
return level;
}
public int getPrevLevel() {
return prevLevel;
}
}
package id.ac.stis.capi.lessthink.models;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import org.javarosa.form.api.FormEntryCaption;
import id.ac.stis.capi.R;
import id.ac.stis.capi.odk.application.Collect;
/**
* Author : Rahadi Jalu
* Email : 14.8325@stis.ac.id
......@@ -13,24 +26,164 @@ import org.javarosa.form.api.FormEntryCaption;
*/
public class CaptionHierarchy extends BaseHierarchy {
public static int TYPE_GROUP = 10;
public static int TYPE_REPEAT = 11;
public static int TYPE_MULTIPLICITY = 12;
public static int TYPE_NEW_REPEAT = 13;
private Context context;
private View captionView;
private TextView textView;
private int type, prevType;
private FormEntryCaption caption;
public CaptionHierarchy(Context context, FormEntryCaption caption, int level) {
super(HIERARCHY_TYPE_CAPTION, level);
public CaptionHierarchy(Context context, FormEntryCaption caption, int type, int prevType, int level, int prevLevel) {
super(level, prevLevel);
this.type = type;
this.prevType = prevType;
this.caption = caption;
this.context = context;
this.textView = new TextView(context);
textView.setText(caption.getLongText());
textView.setGravity(Gravity.CENTER);
if (getType() == TYPE_MULTIPLICITY) {
textView.setText(String.valueOf(caption.getMultiplicity() + 1));
textView.setTextColor(ContextCompat.getColor(context, R.color.primary_dark));
textView.setBackground(ContextCompat.getDrawable(context, R.drawable.wb_circle_btn));
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, Collect.getQuestionFontsize() - 3);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
75, 75);
params.topMargin = 15;
params.bottomMargin = 15;
textView.setLayoutParams(params);
} else if (getType() == TYPE_NEW_REPEAT) {
textView.setText(context.getResources().getString(R.string.add_another));
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_add, 0, 0, 0);
int[] attrs = new int[]{R.attr.selectableItemBackground /* index 0 */};
TypedArray ta = context.obtainStyledAttributes(attrs);
Drawable drawable = ta.getDrawable(0 /* index */);
ta.recycle();
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, Collect.getQuestionFontsize() - 1);
textView.setPadding(0, 45, 0, 45);
textView.setTextColor(ContextCompat.getColor(context, R.color.white));
textView.setBackground(drawable);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
} else {
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, Collect.getQuestionFontsize() - 1);
textView.setText(caption.getLongText());
textView.setTextColor(ContextCompat.getColor(context, R.color.white));
textView.setPadding(15, 20, 15, 20);
}
textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
createWidgetView();
}
public FormEntryCaption getCaption() {
return caption;
}
public View getTextView() {
return textView;
}
private void createWidgetView() {
RelativeLayout container = new RelativeLayout(context);
container.setBackgroundColor(ContextCompat.getColor(context, R.color.primary));
LinearLayout widgetContainer = new LinearLayout(context);
widgetContainer.setOrientation(LinearLayout.VERTICAL);
widgetContainer.setGravity(Gravity.CENTER);
widgetContainer.setBackgroundColor(ContextCompat.getColor(context, R.color.primary_dark));
widgetContainer.addView(textView);
RelativeLayout.LayoutParams widgetContainerParams = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
widgetContainerParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
widgetContainerParams.rightMargin = 14 * getLevel();
widgetContainerParams.leftMargin = 14 * getLevel();
widgetContainer.setLayoutParams(widgetContainerParams);
container.addView(widgetContainer);
if (getLevel() != getPrevLevel()) {
View shadowView;
View protector;
RelativeLayout.LayoutParams protectorParams;
View shadow;
RelativeLayout.LayoutParams shadowParams;
if (getLevel() > getPrevLevel()) {
shadowView = LayoutInflater.from(context).inflate(R.layout.view_shadow_protector, null);
shadow = shadowView.findViewById(R.id.shadow);
shadowParams = (RelativeLayout.LayoutParams) shadow.getLayoutParams();
shadowParams.rightMargin = 14 * getPrevLevel();
shadowParams.leftMargin = 14 * getPrevLevel();
protector = shadowView.findViewById(R.id.protector);
protectorParams = (RelativeLayout.LayoutParams) protector.getLayoutParams();
protectorParams.rightMargin = 14 * getLevel();
protectorParams.leftMargin = 14 * getLevel();
} else {
shadowView = LayoutInflater.from(context).inflate(R.layout.view_shadow_inverted_protector, null);
shadow = shadowView.findViewById(R.id.shadow);
shadowParams = (RelativeLayout.LayoutParams) shadow.getLayoutParams();
shadowParams.rightMargin = 14 * getLevel();
shadowParams.leftMargin = 14 * getLevel();
protector = shadowView.findViewById(R.id.protector);
protectorParams = (RelativeLayout.LayoutParams) protector.getLayoutParams();
protectorParams.rightMargin = 14 * getPrevLevel();
protectorParams.leftMargin = 14 * getPrevLevel();
}
shadowView.setBackgroundColor(ContextCompat.getColor(context, R.color.primary));
protector.setLayoutParams(protectorParams);
shadow.setLayoutParams(shadowParams);
container.addView(shadowView);
}
this.captionView = container;
}
public View getText() {
return textView;
}
@Override
public View getView() {
return textView;
return captionView;
}
@Override
public int getType() {
return type;
}
@Override
public int getPrevType() {
return prevType;
}
}
package id.ac.stis.capi.lessthink.models;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import org.javarosa.form.api.FormEntryPrompt;
import id.ac.stis.capi.R;
import id.ac.stis.capi.odk.widgets.QuestionWidget;
import id.ac.stis.capi.odk.widgets.WidgetFactory;
......@@ -15,22 +22,119 @@ import id.ac.stis.capi.odk.widgets.WidgetFactory;
*/
public class PromptHierarchy extends BaseHierarchy {
public static int TYPE_QUESTION = 21;
private Context context;
private View widgetView;
private int type, prevType;
private QuestionWidget widget;
private FormEntryPrompt prompt;
public PromptHierarchy(Context context, FormEntryPrompt prompt, int level) {
super(HIERARCHY_TYPE_PROMPT, level);
public PromptHierarchy(Context context, FormEntryPrompt prompt, int type, int prevType, int level, int prevLevel) {
super(level, prevLevel);
this.type = type;
this.prevType = prevType;
this.context = context;
this.prompt = prompt;
this.widget = WidgetFactory.createWidgetFromPrompt(prompt, context, false);
LinearLayout.LayoutParams widgetParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
widgetParams.setMargins(12, 12, 12, 12);
widget.setLayoutParams(widgetParams);
createWidgetView();
}
public FormEntryPrompt getPrompt() {
return prompt;
}
public QuestionWidget getWidget() {
return widget;
}
private void createWidgetView() {
RelativeLayout container = new RelativeLayout(context);
container.setBackgroundColor(ContextCompat.getColor(context, R.color.primary));
LinearLayout widgetContainer = new LinearLayout(context);
widgetContainer.setOrientation(LinearLayout.VERTICAL);
widgetContainer.setBackgroundColor(ContextCompat.getColor(context, R.color.white));
widgetContainer.addView(widget);
RelativeLayout.LayoutParams widgetContainerParams = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
widgetContainerParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
widgetContainerParams.rightMargin = 14 * getLevel();
widgetContainerParams.leftMargin = 14 * getLevel();
widgetContainer.setLayoutParams(widgetContainerParams);
container.addView(widgetContainer);
if (getLevel() != getPrevLevel()) {
View shadowView;
View protector;
RelativeLayout.LayoutParams protectorParams;
View shadow;
RelativeLayout.LayoutParams shadowParams;
if (getLevel() > getPrevLevel()) {
shadowView = LayoutInflater.from(context).inflate(R.layout.view_shadow_protector, null);
shadow = shadowView.findViewById(R.id.shadow);
shadowParams = (RelativeLayout.LayoutParams) shadow.getLayoutParams();
shadowParams.rightMargin = 14 * getPrevLevel();
shadowParams.leftMargin = 14 * getPrevLevel();
protector = shadowView.findViewById(R.id.protector);
protectorParams = (RelativeLayout.LayoutParams) protector.getLayoutParams();
protectorParams.rightMargin = 14 * getLevel();
protectorParams.leftMargin = 14 * getLevel();
} else {
shadowView = LayoutInflater.from(context).inflate(R.layout.view_shadow_inverted_protector, null);
shadow = shadowView.findViewById(R.id.shadow);
shadowParams = (RelativeLayout.LayoutParams) shadow.getLayoutParams();
shadowParams.rightMargin = 14 * getLevel();
shadowParams.leftMargin = 14 * getLevel();
protector = shadowView.findViewById(R.id.protector);
protectorParams = (RelativeLayout.LayoutParams) protector.getLayoutParams();
protectorParams.rightMargin = 14 * getPrevLevel();
protectorParams.leftMargin = 14 * getPrevLevel();
}
shadowView.setBackgroundColor(ContextCompat.getColor(context, R.color.primary));
protector.setLayoutParams(protectorParams);
shadow.setLayoutParams(shadowParams);
container.addView(shadowView);
}
this.widgetView = container;
}
@Override
public View getView() {
return widget;
return widgetView;
}
@Override
public int getType() {
return type;
}
@Override
public int getPrevType() {
return prevType;
}
}
......@@ -219,34 +219,34 @@ public class ViewUtils {
* @see ViewUtils#clearError(QuestionWidget)
*/
public static void setError(QuestionWidget qw, String message) {
if (message != null) {
if (qw instanceof SelectOneWidget)
((SelectOneWidget) qw).setError(message);
else if (qw instanceof ItemsetWidget)
((ItemsetWidget) qw).setError(message);
else if (qw instanceof SelectOneAutoAdvanceWidget)
((SelectOneAutoAdvanceWidget) qw).setError(message);
else if (qw instanceof SelectOneSearchWidget)
((SelectOneSearchWidget) qw).setError(message);
else if (qw instanceof ListWidget)
((ListWidget) qw).setError(message);
else if (qw instanceof BooleanWidget)
((BooleanWidget) qw).setError(message);
else if (qw instanceof TriggerWidget)
((TriggerWidget) qw).setError(message);
else if (qw instanceof SpinnerWidget)
((SpinnerWidget) qw).setError(message);
else if (qw instanceof ListMultiWidget)
((ListMultiWidget) qw).setError(message);
else if (qw instanceof SelectMultiWidget)
((SelectMultiWidget) qw).setError(message);
else if (qw instanceof StringWidget)
((StringWidget) qw).setError(message);
else if (qw instanceof ExStringWidget)
((ExStringWidget) qw).setError(message);
} else {
clearError(qw);
}
// if (message != null) {
// if (qw instanceof SelectOneWidget)
// ((SelectOneWidget) qw).setError(message);
// else if (qw instanceof ItemsetWidget)
// ((ItemsetWidget) qw).setError(message);
// else if (qw instanceof SelectOneAutoAdvanceWidget)
// ((SelectOneAutoAdvanceWidget) qw).setError(message);
// else if (qw instanceof SelectOneSearchWidget)
// ((SelectOneSearchWidget) qw).setError(message);
// else if (qw instanceof ListWidget)
// ((ListWidget) qw).setError(message);
// else if (qw instanceof BooleanWidget)
// ((BooleanWidget) qw).setError(message);
// else if (qw instanceof TriggerWidget)
// ((TriggerWidget) qw).setError(message);
// else if (qw instanceof SpinnerWidget)
// ((SpinnerWidget) qw).setError(message);
// else if (qw instanceof ListMultiWidget)
// ((ListMultiWidget) qw).setError(message);
// else if (qw instanceof SelectMultiWidget)
// ((SelectMultiWidget) qw).setError(message);
// else if (qw instanceof StringWidget)
// ((StringWidget) qw).setError(message);
// else if (qw instanceof ExStringWidget)
// ((ExStringWidget) qw).setError(message);
// } else {
// clearError(qw);
// }
}
/**
......@@ -257,29 +257,29 @@ public class ViewUtils {
* @see UpdatingErrorInterface#clearError()
*/
public static void clearError(QuestionWidget qw) {
if (qw instanceof SelectOneWidget)
((SelectOneWidget) qw).clearError();
else if (qw instanceof ItemsetWidget)
((ItemsetWidget) qw).clearError();
else if (qw instanceof SelectOneAutoAdvanceWidget)
((SelectOneAutoAdvanceWidget) qw).clearError();
else if (qw instanceof SelectOneSearchWidget)
((SelectOneSearchWidget) qw).clearError();
else if (qw instanceof ListWidget)
((ListWidget) qw).clearError();
else if (qw instanceof BooleanWidget)
((BooleanWidget) qw).clearError();
else if (qw instanceof TriggerWidget)
((TriggerWidget) qw).clearError();
else if (qw instanceof SpinnerWidget)
((SpinnerWidget) qw).clearError();
else if (qw instanceof ListMultiWidget)
((ListMultiWidget) qw).clearError();
else if (qw instanceof SelectMultiWidget)
((SelectMultiWidget) qw).clearError();
else if (qw instanceof StringWidget)
((StringWidget) qw).clearError();
else if (qw instanceof ExStringWidget)
((ExStringWidget) qw).clearError();
// if (qw instanceof SelectOneWidget)
// ((SelectOneWidget) qw).clearError();
// else if (qw instanceof ItemsetWidget)
// ((ItemsetWidget) qw).clearError();
// else if (qw instanceof SelectOneAutoAdvanceWidget)
// ((SelectOneAutoAdvanceWidget) qw).clearError();
// else if (qw instanceof SelectOneSearchWidget)
// ((SelectOneSearchWidget) qw).clearError();
// else if (qw instanceof ListWidget)
// ((ListWidget) qw).clearError();
// else if (qw instanceof BooleanWidget)
// ((BooleanWidget) qw).clearError();
// else if (qw instanceof TriggerWidget)
// ((TriggerWidget) qw).clearError();
// else if (qw instanceof SpinnerWidget)
// ((SpinnerWidget) qw).clearError();
// else if (qw instanceof ListMultiWidget)
// ((ListMultiWidget) qw).clearError();
// else if (qw instanceof SelectMultiWidget)
// ((SelectMultiWidget) qw).clearError();
// else if (qw instanceof StringWidget)
// ((StringWidget) qw).clearError();
// else if (qw instanceof ExStringWidget)
// ((ExStringWidget) qw).clearError();
}
}
......@@ -945,19 +945,20 @@ public class FormController {
return getAllQuestions(context, FormIndex.createBeginningOfFormIndex());
}
public ArrayList<BaseHierarchy> getQuestionsAfter(Context context, FormIndex formIndex) {
return getAllQuestions(context, formIndex);
}
private ArrayList<BaseHierarchy> getAllQuestions(Context context, FormIndex formIndex) {
jumpToIndex(formIndex);
int event = stepToNextEvent(true);
int currLevel = 0;
boolean isFoundGroup = false;
int prevLevel = 0;
int prevType = 0;
// boolean isFoundGroup = false;
String repeatGroupRef = null;
String contextGroupRef = getFormIndex().getReference().getParentRef().toString(true);
int currLength;
int subtractor = getFormIndex().getReference().toString(true).split("/").length;
ArrayList<BaseHierarchy> prompts = new ArrayList<>();
while (event != FormEntryController.EVENT_END_OF_FORM) {
......@@ -967,18 +968,28 @@ public class FormController {
// retrieve the current group
String curGroup = (repeatGroupRef == null) ? contextGroupRef : repeatGroupRef;
if (!currentRef.startsWith(curGroup)) {
// We have left the current group
if (repeatGroupRef == null) {
// We are done.
break;
} else {
// exit the inner repeat group
repeatGroupRef = null;
}
//reset founded group
isFoundGroup = false;
}
Timber.d("CurrentRef %s %s", currentRef, curGroup);
currLength = currentRef.split("/").length;
currLevel = currLength - subtractor;
// if (!currentRef.startsWith(curGroup)) {
// // We have left the current group
// if (repeatGroupRef == null) {
// // We are done.
// break;
// } else {
// // exit the inner repeat group
// repeatGroupRef = null;
// prevLevel = currLevel;
// currLevel--;
// }
// //reset founded group
// isFoundGroup = false;
//// prevLevel = currLevel;
//// currLevel--;
// }
// if (repeatGroupRef != null) {
// // We're in a repeat group within the one we want to list
......@@ -992,21 +1003,37 @@ public class FormController {
switch (event) {
case FormEntryController.EVENT_QUESTION:
FormEntryPrompt fp = getQuestionPrompt();
if (isFoundGroup || repeatGroupRef != null) {
currLevel++;
}
prompts.add(new PromptHierarchy(context, fp, currLevel));
// if (isFoundGroup || repeatGroupRef != null) {
//// currLevel++;
// }
prompts.add(new PromptHierarchy(context, fp, PromptHierarchy.TYPE_QUESTION,
prevType, currLevel, prevLevel));
prevType = PromptHierarchy.TYPE_QUESTION;
prevLevel = currLevel;
break;
case FormEntryController.EVENT_GROUP:
isFoundGroup = true;
// isFoundGroup = true;
FormEntryCaption formC = getCaptionPrompt();
repeatGroupRef = currentRef;
// currLevel++;
prompts.add(new CaptionHierarchy(context, formC, CaptionHierarchy.TYPE_GROUP,
prevType, currLevel, prevLevel));
prompts.add(new CaptionHierarchy(context, formC, currLevel));
prevType = CaptionHierarchy.TYPE_GROUP;
prevLevel = currLevel;
break;
case FormEntryController.EVENT_PROMPT_NEW_REPEAT:
// this would display the 'add new repeat' dialog
// ignore it.
FormEntryCaption fcRep = getCaptionPrompt();
prompts.add(new CaptionHierarchy(context, fcRep, CaptionHierarchy.TYPE_NEW_REPEAT,
prevType, currLevel, prevLevel));
prevLevel = CaptionHierarchy.TYPE_NEW_REPEAT;
prevLevel = currLevel;
break;
case FormEntryController.EVENT_REPEAT:
FormEntryCaption fc = getCaptionPrompt();
......@@ -1023,8 +1050,18 @@ public class FormController {
if (fc.getMultiplicity() == 0) {
// Display the repeat header for the group.
prompts.add(new CaptionHierarchy(context, fc, currLevel));
prompts.add(new CaptionHierarchy(context, fc, CaptionHierarchy.TYPE_REPEAT,
prevType, currLevel, prevLevel));