Commit 20553845 authored by rahadi's avatar rahadi

WIP: changed updating view dialog mechanism

parent aafebc22
package id.ac.stis.capi.lessthink.adapters; package id.ac.stis.capi.lessthink.adapters;
import android.content.pm.PackageManager;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.BaseAdapter; import android.widget.BaseAdapter;
...@@ -38,16 +37,6 @@ public class HierarchyAdapter extends BaseAdapter { ...@@ -38,16 +37,6 @@ public class HierarchyAdapter extends BaseAdapter {
return 0; return 0;
} }
@Override
public int getItemViewType(int position) {
return hierarchies.get(position).getType();
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override @Override
public boolean isEnabled(int position) { public boolean isEnabled(int position) {
return false; return false;
...@@ -63,16 +52,7 @@ public class HierarchyAdapter extends BaseAdapter { ...@@ -63,16 +52,7 @@ public class HierarchyAdapter extends BaseAdapter {
@Override @Override
public void onFocusChange(View v, boolean hasFocus) { public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) { if (hasFocus) {
for (BaseHierarchy hierarchy : hierarchies) { ((QuestionWidget) v).setFocus(parent.getContext());
View checkFocus = hierarchy.getView();
if (checkFocus != v) {
checkFocus.clearFocus();
} else {
v.requestFocus();
}
}
} else {
v.clearFocus();
} }
} }
}); });
......
...@@ -202,7 +202,7 @@ public class FormEntryDialogFragment extends DialogFragment implements FormLoade ...@@ -202,7 +202,7 @@ public class FormEntryDialogFragment extends DialogFragment implements FormLoade
private void setupChangeListener() { private void setupChangeListener() {
for (BaseHierarchy hierarchy : hierarchies) { for (BaseHierarchy hierarchy : hierarchies) {
if (hierarchy instanceof PromptHierarchy) { if (hierarchy instanceof PromptHierarchy) {
QuestionWidget widget = (QuestionWidget) hierarchy.getView(); QuestionWidget widget = ((PromptHierarchy) hierarchy).getWidget();
try { try {
ViewUtils.setChangeListener(widget, ((PromptHierarchy) hierarchy).getPrompt().getIndex(), this); ViewUtils.setChangeListener(widget, ((PromptHierarchy) hierarchy).getPrompt().getIndex(), this);
} catch (InvalidObjectException e) { } catch (InvalidObjectException e) {
...@@ -223,7 +223,7 @@ public class FormEntryDialogFragment extends DialogFragment implements FormLoade ...@@ -223,7 +223,7 @@ public class FormEntryDialogFragment extends DialogFragment implements FormLoade
// if (!hit) { // if (!hit) {
// hit = entry.getKey().equals(index.getReference().toString()); // hit = entry.getKey().equals(index.getReference().toString());
// } else { // } else {
//// questionContainer.removeView(getView().findViewWithTag(entry.getKey())); //// questionContainer.removeView(getTextView().findViewWithTag(entry.getKey()));
// ViewGroup parent = (ViewGroup) entry.getValue().getParent(); // ViewGroup parent = (ViewGroup) entry.getValue().getParent();
// while (parent != questionContainer) { // while (parent != questionContainer) {
// parent.removeView(parent.findViewWithTag(entry.getKey())); // parent.removeView(parent.findViewWithTag(entry.getKey()));
......
...@@ -9,25 +9,25 @@ import android.view.View; ...@@ -9,25 +9,25 @@ import android.view.View;
*/ */
public abstract class BaseHierarchy { 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 level;
private int prevLevel;
public BaseHierarchy(int level, int prevLevel) {
public BaseHierarchy(int type, int level) {
this.type = type;
this.level = level; this.level = level;
this.prevLevel = prevLevel;
} }
public abstract View getView(); public abstract View getView();
public int getType() { public abstract int getType();
return type;
} public abstract int getPrevType();
public int getLevel() { public int getLevel() {
return level; return level;
} }
public int getPrevLevel() {
return prevLevel;
}
} }
package id.ac.stis.capi.lessthink.models; package id.ac.stis.capi.lessthink.models;
import android.content.Context; 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.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView; import android.widget.TextView;
import org.javarosa.form.api.FormEntryCaption; import org.javarosa.form.api.FormEntryCaption;
import id.ac.stis.capi.R;
import id.ac.stis.capi.odk.application.Collect;
/** /**
* Author : Rahadi Jalu * Author : Rahadi Jalu
* Email : 14.8325@stis.ac.id * Email : 14.8325@stis.ac.id
...@@ -13,24 +26,164 @@ import org.javarosa.form.api.FormEntryCaption; ...@@ -13,24 +26,164 @@ import org.javarosa.form.api.FormEntryCaption;
*/ */
public class CaptionHierarchy extends BaseHierarchy { 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 TextView textView;
private int type, prevType;
private FormEntryCaption caption; private FormEntryCaption caption;
public CaptionHierarchy(Context context, FormEntryCaption caption, int level) { public CaptionHierarchy(Context context, FormEntryCaption caption, int type, int prevType, int level, int prevLevel) {
super(HIERARCHY_TYPE_CAPTION, level); super(level, prevLevel);
this.type = type;
this.prevType = prevType;
this.caption = caption; this.caption = caption;
this.context = context;
this.textView = new TextView(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() { public FormEntryCaption getCaption() {
return caption; 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 @Override
public View getView() { 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; package id.ac.stis.capi.lessthink.models;
import android.content.Context; import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View; 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 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.QuestionWidget;
import id.ac.stis.capi.odk.widgets.WidgetFactory; import id.ac.stis.capi.odk.widgets.WidgetFactory;
...@@ -15,22 +22,119 @@ 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 class PromptHierarchy extends BaseHierarchy {
public static int TYPE_QUESTION = 21;
private Context context;
private View widgetView;
private int type, prevType;
private QuestionWidget widget; private QuestionWidget widget;
private FormEntryPrompt prompt; private FormEntryPrompt prompt;
public PromptHierarchy(Context context, FormEntryPrompt prompt, int level) { public PromptHierarchy(Context context, FormEntryPrompt prompt, int type, int prevType, int level, int prevLevel) {
super(HIERARCHY_TYPE_PROMPT, level); super(level, prevLevel);
this.type = type;
this.prevType = prevType;
this.context = context;
this.prompt = prompt; this.prompt = prompt;
this.widget = WidgetFactory.createWidgetFromPrompt(prompt, context, false); 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() { public FormEntryPrompt getPrompt() {
return prompt; 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 @Override
public View getView() { 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 { ...@@ -219,34 +219,34 @@ public class ViewUtils {
* @see ViewUtils#clearError(QuestionWidget) * @see ViewUtils#clearError(QuestionWidget)
*/ */
public static void setError(QuestionWidget qw, String message) { public static void setError(QuestionWidget qw, String message) {
if (message != null) { // if (message != null) {
if (qw instanceof SelectOneWidget) // if (qw instanceof SelectOneWidget)
((SelectOneWidget) qw).setError(message); // ((SelectOneWidget) qw).setError(message);
else if (qw instanceof ItemsetWidget) // else if (qw instanceof ItemsetWidget)
((ItemsetWidget) qw).setError(message); // ((ItemsetWidget) qw).setError(message);
else if (qw instanceof SelectOneAutoAdvanceWidget) // else if (qw instanceof SelectOneAutoAdvanceWidget)
((SelectOneAutoAdvanceWidget) qw).setError(message); // ((SelectOneAutoAdvanceWidget) qw).setError(message);
else if (qw instanceof SelectOneSearchWidget) // else if (qw instanceof SelectOneSearchWidget)
((SelectOneSearchWidget) qw).setError(message); // ((SelectOneSearchWidget) qw).setError(message);
else if (qw instanceof ListWidget) // else if (qw instanceof ListWidget)
((ListWidget) qw).setError(message); // ((ListWidget) qw).setError(message);
else if (qw instanceof BooleanWidget) // else if (qw instanceof BooleanWidget)
((BooleanWidget) qw).setError(message); // ((BooleanWidget) qw).setError(message);
else if (qw instanceof TriggerWidget) // else if (qw instanceof TriggerWidget)
((TriggerWidget) qw).setError(message); // ((TriggerWidget) qw).setError(message);
else if (qw instanceof SpinnerWidget) // else if (qw instanceof SpinnerWidget)
((SpinnerWidget) qw).setError(message); // ((SpinnerWidget) qw).setError(message);
else if (qw instanceof ListMultiWidget) // else if (qw instanceof ListMultiWidget)
((ListMultiWidget) qw).setError(message); // ((ListMultiWidget) qw).setError(message);
else if (qw instanceof SelectMultiWidget) // else if (qw instanceof SelectMultiWidget)
((SelectMultiWidget) qw).setError(message); // ((SelectMultiWidget) qw).setError(message);
else if (qw instanceof StringWidget) // else if (qw instanceof StringWidget)
((StringWidget) qw).setError(message); // ((StringWidget) qw).setError(message);
else if (qw instanceof ExStringWidget) // else if (qw instanceof ExStringWidget)
((ExStringWidget) qw).setError(message); // ((ExStringWidget) qw).setError(message);
} else { // } else {
clearError(qw); // clearError(qw);
} // }
} }
/** /**
...@@ -257,29 +257,29 @@ public class ViewUtils { ...@@ -257,29 +257,29 @@ public class ViewUtils {
* @see UpdatingErrorInterface#clearError() * @see UpdatingErrorInterface#clearError()
*/ */
public static void clearError(QuestionWidget qw) { public static void clearError(QuestionWidget qw) {
if (qw instanceof SelectOneWidget) // if (qw instanceof SelectOneWidget)
((SelectOneWidget) qw).clearError(); // ((SelectOneWidget) qw).clearError();
else if (qw instanceof ItemsetWidget) // else if (qw instanceof ItemsetWidget)
((ItemsetWidget) qw).clearError(); // ((ItemsetWidget) qw).clearError();
else if (qw instanceof SelectOneAutoAdvanceWidget) // else if (qw instanceof SelectOneAutoAdvanceWidget)
((SelectOneAutoAdvanceWidget) qw).clearError(); // ((SelectOneAutoAdvanceWidget) qw).clearError();
else if (qw instanceof SelectOneSearchWidget) // else if (qw instanceof SelectOneSearchWidget)
((SelectOneSearchWidget) qw).clearError(); // ((SelectOneSearchWidget) qw).clearError();
else if (qw instanceof ListWidget) // else if (qw instanceof ListWidget)
((ListWidget) qw).clearError(); // ((ListWidget) qw).clearError();
else if (qw instanceof BooleanWidget) // else if (qw instanceof BooleanWidget)
((BooleanWidget) qw).clearError(); // ((BooleanWidget) qw).clearError();
else if (qw instanceof TriggerWidget) // else if (qw instanceof TriggerWidget)
((TriggerWidget) qw).clearError(); // ((TriggerWidget) qw).clearError();
else if (qw instanceof SpinnerWidget) // else if (qw instanceof SpinnerWidget)
((SpinnerWidget) qw).clearError(); // ((SpinnerWidget) qw).clearError();
else if (qw instanceof ListMultiWidget) // else if (qw instanceof ListMultiWidget)
((ListMultiWidget) qw).clearError(); // ((ListMultiWidget) qw).clearError();
else if (qw instanceof SelectMultiWidget) // else if (qw instanceof SelectMultiWidget)
((SelectMultiWidget) qw).clearError(); // ((SelectMultiWidget) qw).clearError();
else if (qw instanceof StringWidget) // else if (qw instanceof StringWidget)
((StringWidget) qw).clearError(); // ((StringWidget) qw).clearError();
else if (qw instanceof ExStringWidget) // else if (qw instanceof ExStringWidget)
((ExStringWidget) qw).clearError(); // ((ExStringWidget) qw).clearError();
} }
} }
...@@ -945,19 +945,20 @@ public class FormController { ...@@ -945,19 +945,20 @@ public class FormController {
return getAllQuestions(context, FormIndex.createBeginningOfFormIndex()); 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) { private ArrayList<BaseHierarchy> getAllQuestions(Context context, FormIndex formIndex) {
jumpToIndex(formIndex); jumpToIndex(formIndex);
int event = stepToNextEvent(true); int event = stepToNextEvent(true);
int currLevel = 0; int currLevel = 0;
boolean isFoundGroup = false; int prevLevel = 0;
int prevType = 0;
// boolean isFoundGroup = false;
String repeatGroupRef = null; String repeatGroupRef = null;
String contextGroupRef = getFormIndex().getReference().getParentRef().toString(true); String contextGroupRef = getFormIndex().getReference().getParentRef().toString(true);
int currLength;
int subtractor = getFormIndex().getReference().toString(true).split("/").length;
ArrayList<BaseHierarchy> prompts = new ArrayList<>(); ArrayList<BaseHierarchy> prompts = new ArrayList<>();
while (event != FormEntryController.EVENT_END_OF_FORM) { while (event != FormEntryController.EVENT_END_OF_FORM) {
...@@ -967,18 +968,28 @@ public class FormController { ...@@ -967,18 +968,28 @@ public class FormController {
// retrieve the current group // retrieve the current group
String curGroup = (repeatGroupRef == null) ? contextGroupRef : repeatGroupRef; String curGroup = (repeatGroupRef == null) ? contextGroupRef : repeatGroupRef;