Commit d03bb2de authored by rahadi's avatar rahadi

Moved hierarchy to RecyclerView, added save mechanism

parent 349ed645
...@@ -423,10 +423,21 @@ public class CapiFormFragment extends Fragment implements FormListDownloaderList ...@@ -423,10 +423,21 @@ public class CapiFormFragment extends Fragment implements FormListDownloaderList
ArrayList<HashMap<String, String>> downloadedFilteredFormList = new ArrayList<>(); ArrayList<HashMap<String, String>> downloadedFilteredFormList = new ArrayList<>();
ArrayList<HashMap<String, String>> unDownloadedFilteredFormList = new ArrayList<>(); ArrayList<HashMap<String, String>> unDownloadedFilteredFormList = new ArrayList<>();
FormsDao formsDao = new FormsDao();
for (int idx = 0; idx < mFilteredFormList.size(); idx++) { for (int idx = 0; idx < mFilteredFormList.size(); idx++) {
HashMap<String, String> item = mFilteredFormList.get(idx); HashMap<String, String> item = mFilteredFormList.get(idx);
Cursor c = formsDao.getFormsCursor(FormsProviderAPI.FormsColumns.JR_FORM_ID + "=?",
new String[]{item.get(FORM_ID_KEY)});
c.moveToFirst();
if (item.get(HAS_BEEN_DOWNLOADED) != null && item.get(HAS_BEEN_DOWNLOADED).equals("true")) { if (item.get(HAS_BEEN_DOWNLOADED) != null && item.get(HAS_BEEN_DOWNLOADED).equals("true")) {
if (isFormInActiveSurvey(item)) downloadedFilteredFormList.add(item); if (isFormInActiveSurvey(item)) {
item.put(FORM_TYPE_KEY, c.getString(c.getColumnIndex(FormsProviderAPI.FormsColumns.FORM_TYPE)));
downloadedFilteredFormList.add(item);
}
} else { } else {
if (isLocalFormSuperseded(item.get(FORM_ID_KEY), item.get(FORM_VERSION_KEY))) { if (isLocalFormSuperseded(item.get(FORM_ID_KEY), item.get(FORM_VERSION_KEY))) {
//need to downloaded //need to downloaded
...@@ -435,9 +446,13 @@ public class CapiFormFragment extends Fragment implements FormListDownloaderList ...@@ -435,9 +446,13 @@ public class CapiFormFragment extends Fragment implements FormListDownloaderList
} else { } else {
//has been downloaded //has been downloaded
item.put(HAS_BEEN_DOWNLOADED, "true"); item.put(HAS_BEEN_DOWNLOADED, "true");
item.put(FORM_TYPE_KEY, c.getString(c.getColumnIndex(FormsProviderAPI.FormsColumns.FORM_TYPE)));
if (isFormInActiveSurvey(item)) downloadedFilteredFormList.add(item); if (isFormInActiveSurvey(item)) downloadedFilteredFormList.add(item);
} }
} }
c.close();
} }
downloadedFilteredFormList.addAll(unDownloadedFilteredFormList); downloadedFilteredFormList.addAll(unDownloadedFilteredFormList);
...@@ -497,10 +512,10 @@ public class CapiFormFragment extends Fragment implements FormListDownloaderList ...@@ -497,10 +512,10 @@ public class CapiFormFragment extends Fragment implements FormListDownloaderList
item.put(FORM_ID_KEY, details.formID); item.put(FORM_ID_KEY, details.formID);
item.put(FORM_VERSION_KEY, details.formVersion); item.put(FORM_VERSION_KEY, details.formVersion);
FormsDao formsDao = new FormsDao(); // FormsDao formsDao = new FormsDao();
Cursor c = formsDao.getFormsCursor(FormsProviderAPI.FormsColumns.JR_FORM_ID + "=?", // Cursor c = formsDao.getFormsCursor(FormsProviderAPI.FormsColumns.JR_FORM_ID + "=?",
new String[]{details.formID}); // new String[]{details.formID});
item.put(FORM_TYPE_KEY, c.getString(c.getColumnIndex(FormsProviderAPI.FormsColumns.FORM_TYPE))); // item.put(FORM_TYPE_KEY, c.getString(c.getColumnIndex(FormsProviderAPI.FormsColumns.FORM_TYPE)));
if (!mFormListContain(details.formName)) { if (!mFormListContain(details.formName)) {
// Insert the new form in alphabetical order. // Insert the new form in alphabetical order.
......
...@@ -17,7 +17,6 @@ import java.util.List; ...@@ -17,7 +17,6 @@ import java.util.List;
import id.ac.stis.capi.R; import id.ac.stis.capi.R;
import id.ac.stis.capi.collectiva.preferences.Constants; import id.ac.stis.capi.collectiva.preferences.Constants;
import id.ac.stis.capi.lessthink.fragments.FormEntryDialogFragment; import id.ac.stis.capi.lessthink.fragments.FormEntryDialogFragment;
import id.ac.stis.capi.lessthink.fragments.FormEntryRVDialogFragment;
import id.ac.stis.capi.lessthink.listeners.OnTableAddButtonClickListener; import id.ac.stis.capi.lessthink.listeners.OnTableAddButtonClickListener;
import id.ac.stis.capi.lessthink.models.FormLabels; import id.ac.stis.capi.lessthink.models.FormLabels;
import id.ac.stis.capi.lessthink.models.InstanceValues; import id.ac.stis.capi.lessthink.models.InstanceValues;
...@@ -44,7 +43,7 @@ public class ListingInstanceActivity extends AppCompatActivity { ...@@ -44,7 +43,7 @@ public class ListingInstanceActivity extends AppCompatActivity {
private TableFooterView tableFooterView; private TableFooterView tableFooterView;
private String formId, formName; private String formId, formName;
// private FormEntryDialogFragment entryDialogFragment; // private FormEntryDialogFragment entryDialogFragment;
private FormEntryRVDialogFragment entryDialogFragment; private FormEntryDialogFragment entryDialogFragment;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
...@@ -96,7 +95,7 @@ public class ListingInstanceActivity extends AppCompatActivity { ...@@ -96,7 +95,7 @@ public class ListingInstanceActivity extends AppCompatActivity {
String instanceUuid = view.getTag().toString(); String instanceUuid = view.getTag().toString();
// entryDialogFragment = FormEntryDialogFragment.newInstance(instanceUuid, // entryDialogFragment = FormEntryDialogFragment.newInstance(instanceUuid,
// FormEntryDialogFragment.MODE_EDIT, false); // FormEntryDialogFragment.MODE_EDIT, false);
entryDialogFragment = FormEntryRVDialogFragment.newInstance(instanceUuid, entryDialogFragment = FormEntryDialogFragment.newInstance(instanceUuid,
FormEntryDialogFragment.MODE_EDIT, false); FormEntryDialogFragment.MODE_EDIT, false);
entryDialogFragment.show(getSupportFragmentManager(), instanceUuid); entryDialogFragment.show(getSupportFragmentManager(), instanceUuid);
// //
......
package id.ac.stis.capi.lessthink.adapters; package id.ac.stis.capi.lessthink.adapters;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.BaseAdapter; import android.widget.LinearLayout;
import org.javarosa.core.model.FormIndex; import java.util.List;
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.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; import timber.log.Timber;
/** /**
...@@ -21,72 +16,70 @@ import timber.log.Timber; ...@@ -21,72 +16,70 @@ import timber.log.Timber;
* 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 implements OnChangeListener { public class HierarchyAdapter extends RecyclerView.Adapter<HierarchyAdapter.ViewHolder> {
private ArrayList<BaseHierarchy> hierarchies; private List<BaseHierarchy> hierarchies;
private OnChangeListener onChangeListener;
public HierarchyAdapter(ArrayList<BaseHierarchy> hierarchies) { public HierarchyAdapter(List<BaseHierarchy> hierarchies) {
this.hierarchies = hierarchies; this.hierarchies = hierarchies;
} }
public void setOnChangeListener(OnChangeListener onChangeListener) { public void setHierarchies(List<BaseHierarchy> hierarchies) {
this.onChangeListener = onChangeListener; this.hierarchies = hierarchies;
} }
@Override public BaseHierarchy getItem(int position) {
public int getCount() { return hierarchies.get(position);
return hierarchies.size();
} }
@Override @Override
public Object getItem(int position) { public int getItemViewType(int position) {
return hierarchies.get(position); return position;
} }
@NonNull
@Override @Override
public long getItemId(int position) { public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return 0; Timber.d("Creating with type %s", viewType);
// BaseHierarchy hierarchy = hierarchies.get(viewType);
// if (hierarchy instanceof PromptHierarchy) {
// ((PromptHierarchy) hierarchy).setOnChangeListener(onChangeListener);
// return new PromptViewHolder(hierarchy.getView());
// } else if (hierarchy instanceof CaptionHierarchy) {
// return new CaptionViewHolder(hierarchy.getView());
// } else {
// // Should not be happening
// return new ViewHolder(hierarchy.getView());
// }
return new ViewHolder(new LinearLayout(parent.getContext()));
} }
@Override @Override
public boolean isEnabled(int position) { public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
return false; // if (holder instanceof PromptViewHolder) {
// PromptHierarchy hierarchy = (PromptHierarchy) hierarchies.get(position);
// }
holder.bind(hierarchies.get(position));
} }
@Override @Override
public View getView(final int position, View view, final ViewGroup parent) { public int getItemCount() {
return hierarchies.size();
BaseHierarchy hierarchy = hierarchies.get(position); }
View v = hierarchy.getView();
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);
}
} class ViewHolder extends RecyclerView.ViewHolder {
return v; public ViewHolder(View itemView) {
} super(itemView);
}
@Override void bind(BaseHierarchy hierarchy) {
public void onChange(QuestionWidget questionWidget, FormIndex formIndex) { ViewGroup parent = (ViewGroup) itemView;
if (onChangeListener != null) { if (parent != null) {
onChangeListener.onChange(questionWidget, formIndex); parent.removeAllViews();
parent.addView(hierarchy.getView());
}
} }
} }
} }
\ No newline at end of file
package id.ac.stis.capi.lessthink.adapters;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import java.util.List;
import id.ac.stis.capi.lessthink.models.BaseHierarchy;
import timber.log.Timber;
/**
* Author : Rahadi Jalu
* Email : 14.8325@stis.ac.id
* Company: Politeknik Statistika STIS
*/
public class HierarchyRVAdapter extends RecyclerView.Adapter<HierarchyRVAdapter.ViewHolder> {
private List<BaseHierarchy> hierarchies;
public HierarchyRVAdapter(List<BaseHierarchy> hierarchies) {
this.hierarchies = hierarchies;
}
public void setHierarchies(List<BaseHierarchy> hierarchies) {
this.hierarchies = hierarchies;
}
public BaseHierarchy getItem(int position) {
return hierarchies.get(position);
}
@Override
public int getItemViewType(int position) {
return position;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Timber.d("Creating with type %s", viewType);
// BaseHierarchy hierarchy = hierarchies.get(viewType);
// if (hierarchy instanceof PromptHierarchy) {
// ((PromptHierarchy) hierarchy).setOnChangeListener(onChangeListener);
// return new PromptViewHolder(hierarchy.getView());
// } else if (hierarchy instanceof CaptionHierarchy) {
// return new CaptionViewHolder(hierarchy.getView());
// } else {
// // Should not be happening
// return new ViewHolder(hierarchy.getView());
// }
return new ViewHolder(new LinearLayout(parent.getContext()));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
// if (holder instanceof PromptViewHolder) {
// PromptHierarchy hierarchy = (PromptHierarchy) hierarchies.get(position);
// }
holder.bind(hierarchies.get(position));
}
@Override
public int getItemCount() {
return hierarchies.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
}
void bind(BaseHierarchy hierarchy) {
ViewGroup parent = (ViewGroup) itemView;
if (parent != null) {
parent.removeAllViews();
parent.addView(hierarchy.getView());
}
}
}
}
...@@ -11,6 +11,6 @@ import id.ac.stis.capi.odk.widgets.QuestionWidget; ...@@ -11,6 +11,6 @@ import id.ac.stis.capi.odk.widgets.QuestionWidget;
* Email : 14.8325@stis.ac.id * Email : 14.8325@stis.ac.id
* Company: Politeknik Statistika STIS * Company: Politeknik Statistika STIS
*/ */
public interface OnChangeListener { public interface OnAnswerChangedListener {
void onChange(QuestionWidget questionWidget, FormIndex formIndex); void onChange(QuestionWidget questionWidget, FormIndex formIndex);
} }
...@@ -2,7 +2,6 @@ package id.ac.stis.capi.lessthink.utils; ...@@ -2,7 +2,6 @@ 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;
...@@ -20,10 +19,9 @@ import java.io.InvalidObjectException; ...@@ -20,10 +19,9 @@ import java.io.InvalidObjectException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.logging.Handler;
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.OnAnswerChangedListener;
import id.ac.stis.capi.odk.widgets.BooleanWidget; import id.ac.stis.capi.odk.widgets.BooleanWidget;
import id.ac.stis.capi.odk.widgets.ExStringWidget; import id.ac.stis.capi.odk.widgets.ExStringWidget;
import id.ac.stis.capi.odk.widgets.ItemsetWidget; import id.ac.stis.capi.odk.widgets.ItemsetWidget;
...@@ -83,7 +81,7 @@ public class ViewUtils { ...@@ -83,7 +81,7 @@ public class ViewUtils {
} }
public static void setChangeListener(final QuestionWidget qw, final FormIndex index, public static void setChangeListener(final QuestionWidget qw, final FormIndex index,
final OnChangeListener onChangeListener) final OnAnswerChangedListener onAnswerChangedListener)
throws InvalidObjectException { throws InvalidObjectException {
ArrayList<RadioButton> radioButtons = null; ArrayList<RadioButton> radioButtons = null;
...@@ -103,8 +101,8 @@ public class ViewUtils { ...@@ -103,8 +101,8 @@ public class ViewUtils {
rb.setOnClickListener(new View.OnClickListener() { rb.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (onChangeListener != null) { if (onAnswerChangedListener != null) {
onChangeListener.onChange(qw, index); onAnswerChangedListener.onChange(qw, index);
} }
} }
}); });
...@@ -122,8 +120,8 @@ public class ViewUtils { ...@@ -122,8 +120,8 @@ public class ViewUtils {
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (onChangeListener != null) { if (onAnswerChangedListener != null) {
onChangeListener.onChange(qw, index); onAnswerChangedListener.onChange(qw, index);
} }
} }
}); });
...@@ -138,15 +136,15 @@ public class ViewUtils { ...@@ -138,15 +136,15 @@ public class ViewUtils {
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override @Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (onChangeListener != null) { if (onAnswerChangedListener != null) {
onChangeListener.onChange(qw, index); onAnswerChangedListener.onChange(qw, index);
} }
} }
@Override @Override
public void onNothingSelected(AdapterView<?> parent) { public void onNothingSelected(AdapterView<?> parent) {
if (onChangeListener != null) { if (onAnswerChangedListener != null) {
onChangeListener.onChange(qw, index); onAnswerChangedListener.onChange(qw, index);
} }
} }
}); });
...@@ -164,8 +162,8 @@ public class ViewUtils { ...@@ -164,8 +162,8 @@ public class ViewUtils {
cbx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { cbx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (onChangeListener != null) { if (onAnswerChangedListener != null) {
onChangeListener.onChange(qw, index); onAnswerChangedListener.onChange(qw, index);
} }
} }
}); });
...@@ -202,8 +200,8 @@ public class ViewUtils { ...@@ -202,8 +200,8 @@ public class ViewUtils {
timer.schedule(new TimerTask() { timer.schedule(new TimerTask() {
@Override @Override
public void run() { public void run() {
if (onChangeListener != null) { if (onAnswerChangedListener != null) {
onChangeListener.onChange(qw, index); onAnswerChangedListener.onChange(qw, index);
} }
// if (qw instanceof StringWidget) // if (qw instanceof StringWidget)
......
...@@ -22,6 +22,7 @@ import java.io.UnsupportedEncodingException; ...@@ -22,6 +22,7 @@ import java.io.UnsupportedEncodingException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
...@@ -140,26 +141,37 @@ public class XmlUtils { ...@@ -140,26 +141,37 @@ public class XmlUtils {
String html = doc.getRootElement().getNamespace(); String html = doc.getRootElement().getNamespace();
Element body = doc.getRootElement().getElement(html, "body"); Element body = doc.getRootElement().getElement(html, "body");
List<Element> inputs = findAllElementsByTagName(body, "input");
for (int i = 0; i < body.getChildCount(); i++) { for (Element input : inputs) {
if (body.isText(i)) { String ref = getAttributeValue(input, "ref");
continue; if (ref != null) {
} if (xPaths.contains(ref)) {
Element label = getChildElement(input, "label");
if (body.getType(i) == Node.ELEMENT) { if (label != null) {
Element curr = body.getElement(i); titles.put(ref, getText(label));
String ref = getAttributeValue(curr, "ref");
if (ref != null) {
if (xPaths.contains(ref)) {
Element label = getChildElement(curr, "label");
if (label != null) {
titles.put(ref, getText(label));
}
} }
} }
} }
} }
// for (int i = 0; i < body.getChildCount(); i++) {
// if (body.isText(i)) {
// continue;
// }
//
// if (body.getType(i) == Node.ELEMENT) {
// Element curr = body.getElement(i);
//
// String ref = getAttributeValue(curr, "ref");
// if (ref != null) {
// if (xPaths.contains(ref)) {
// Element label = getChildElement(curr, "label");
// if (label != null) {
// titles.put(ref, getText(label));
// }
// }
// }
// }
// }
return new FormLabels(formId, titles); return new FormLabels(formId, titles);
} }
...@@ -242,7 +254,7 @@ public class XmlUtils { ...@@ -242,7 +254,7 @@ public class XmlUtils {
return null; return null;
} }
public static File generateEmptyInstanceFile(File file,String formId, String formName) { public static File generateEmptyInstanceFile(File file, String formId, String formName) {
if (file.exists()) { if (file.exists()) {
try { try {
FileInputStream fis = new FileInputStream(file); FileInputStream fis = new FileInputStream(file);
...@@ -387,4 +399,59 @@ public class XmlUtils { ...@@ -387,4 +399,59 @@ public class XmlUtils {
return null; return null;
} }
public static List<Element> findAllElementsByTagName(Element elem, String tagName) {
List<Element> ret = new LinkedList<>();
findAllElementsByTagName(elem, tagName, ret);
return ret;
}
private static void findAllElementsByTagName(Element el, String tagName, List<Element> elementList) {
if (tagName.equals(el.getName())) {
elementList.add(el);
}
for (int i = 0; i < el.getChildCount(); i++) {
if (el.getType(i) == Node.ELEMENT) {
Element elem = el.getElement(i);
findAllElementsByTagName(elem, tagName, elementList);
}
}
}
private static void findAllElementsByTagNameNS(Element el, String nameSpaceURI, String localName,
List<Element> elementList) {
if (localName.equals(el.getName()) && nameSpaceURI.contains(el.getNamespace())) {
elementList.add(el);
}
for (int i = 0; i < el.getChildCount(); i++) {
if (el.getType(i) == Node.ELEMENT) {
Element elem = el.getElement(i);
findAllElementsByTagNameNS(elem, nameSpaceURI, localName, elementList);
}
}
}
public static List<Element> findAllElementsByAttrName(Element elem, String attrName) {
List<Element> ret = new LinkedList<>();
findAllElementsByAttrName(elem, attrName, ret);
return ret;
}
private static void findAllElementsByAttrName(Element el, String attrName, List<Element> elementList) {
for (int i = 0; i < el.getAttributeCount(); i++) {
if (attrName.equals(el.getAttributeName(i))) {
elementList.add(el);
}
}
for (int i = 0; i < el.getChildCount(); i++) {
if (el.getType(i) == Node.ELEMENT) {
Element elem = el.getElement(i);
findAllElementsByAttrName(elem, attrName, elementList);
}
}
}
} }
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