Commit aafebc22 authored by rahadi's avatar rahadi

WIP: added updating and its error interface

parent 537fbff6
......@@ -87,6 +87,8 @@ public class CapiFormFragment extends Fragment implements FormListDownloaderList
private static final String FORM_VERSION_KEY = "formversion";
private static final String HAS_BEEN_DOWNLOADED = "hasbeendownloaded";
private static final String FORM_TYPE_KEY = "formtype";
private static final boolean EXIT = true;
private static final boolean DO_NOT_EXIT = false;
......@@ -226,11 +228,14 @@ public class CapiFormFragment extends Fragment implements FormListDownloaderList
String formName = c.getString(c.getColumnIndex(FormsProviderAPI.FormsColumns.DISPLAY_NAME));
String formVersion = c.getString(c.getColumnIndex(FormsProviderAPI.FormsColumns.JR_FORM_ID));
String formId = c.getString(c.getColumnIndex(FormsProviderAPI.FormsColumns.JR_FORM_ID));
String formType = c.getString(c.getColumnIndex(FormsProviderAPI.FormsColumns.FORM_TYPE));
item.put(FORMNAME, formName);
item.put(FORM_ID_KEY, formId);
item.put(FORM_VERSION_KEY, formVersion);
item.put(HAS_BEEN_DOWNLOADED, "true");
item.put(FORM_TYPE_KEY, formType);
downloadedFormsBefore.add(item);
} while (c.moveToNext());
......@@ -492,6 +497,11 @@ public class CapiFormFragment extends Fragment implements FormListDownloaderList
item.put(FORM_ID_KEY, details.formID);
item.put(FORM_VERSION_KEY, details.formVersion);
FormsDao formsDao = new FormsDao();
Cursor c = formsDao.getFormsCursor(FormsProviderAPI.FormsColumns.JR_FORM_ID + "=?",
new String[]{details.formID});
item.put(FORM_TYPE_KEY, c.getString(c.getColumnIndex(FormsProviderAPI.FormsColumns.FORM_TYPE)));
if (!mFormListContain(details.formName)) {
// Insert the new form in alphabetical order.
if (mFormList.size() == 0) {
......
......@@ -321,7 +321,7 @@ public class FormHierarchyFragment extends Fragment implements AdapterView.OnIte
}
// Add this group name to the drop down list for this repeating group.
HierarchyElement h = formList.get(formList.size() - 1);
h.addChild(new HierarchyElement("rensponse "
h.addChild(new HierarchyElement("response "
+ (fc.getMultiplicity() + 1), null, null, Color.WHITE, CHILD, fc
.getIndex(),1));
break;
......
package id.ac.stis.capi.lessthink.activities;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
......@@ -8,12 +11,13 @@ import android.view.View;
import android.widget.Toast;
import java.io.File;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import id.ac.stis.capi.R;
import id.ac.stis.capi.collectiva.preferences.Constants;
import id.ac.stis.capi.lessthink.fragments.FormEntryDialogFragment;
import id.ac.stis.capi.lessthink.listeners.OnAddButtonClickListener;
import id.ac.stis.capi.lessthink.models.FormLabels;
import id.ac.stis.capi.lessthink.models.InstanceValues;
import id.ac.stis.capi.lessthink.utils.XmlUtils;
......@@ -24,8 +28,9 @@ import id.ac.stis.capi.odk.dao.FormsDao;
import id.ac.stis.capi.odk.dao.InstancesDao;
import id.ac.stis.capi.odk.dto.Form;
import id.ac.stis.capi.odk.dto.Instance;
import id.ac.stis.capi.odk.provider.FormsProviderAPI;
import id.ac.stis.capi.odk.provider.InstanceProviderAPI;
import timber.log.Timber;
import id.ac.stis.capi.odk.utilities.ApplicationConstants;
public class ListingInstanceActivity extends AppCompatActivity {
......@@ -36,6 +41,7 @@ public class ListingInstanceActivity extends AppCompatActivity {
private TableRowView tableRowView;
private TableFooterView tableFooterView;
private String formId, formName;
private FormEntryDialogFragment entryDialogFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -82,8 +88,53 @@ public class ListingInstanceActivity extends AppCompatActivity {
tableRowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(ListingInstanceActivity.this, "Click : " + view.getTag(), Toast.LENGTH_LONG).show();
// Toast.makeText(ListingInstanceActivity.this, "Click : " + view.getTag(), Toast.LENGTH_LONG).show();
String instanceUuid = view.getTag().toString();
entryDialogFragment = FormEntryDialogFragment.newInstance(instanceUuid,
FormEntryDialogFragment.MODE_EDIT, false);
entryDialogFragment.show(getSupportFragmentManager(), instanceUuid);
//
// Intent i =new Intent(ListingInstanceActivity.this, ListingFormEntryActivity.class);
// i.putExtra("instanceUuid", view.getTag().toString());
// startActivity(i);
}
});
tableFooterView.setOnAddButtonClickListener(new OnAddButtonClickListener() {
@Override
public void onAddButtonClick(View v) {
fillBlankForm();
}
});
}
//todo: update yow
private void fillBlankForm() {
Cursor formslist = new FormsDao().getFormsCursor();
//getting id
long itemIdPosition = -1;
if (formslist.moveToFirst()) {
do {
if (formslist.getString(formslist.getColumnIndex(FormsProviderAPI.FormsColumns.JR_FORM_ID)).equals(formId)) {
itemIdPosition = formslist.getLong(formslist.getColumnIndex(FormsProviderAPI.FormsColumns._ID));
}
} while (formslist.moveToNext());
}
if (itemIdPosition > -1) {
// get uri to form
Uri formUri = ContentUris.withAppendedId(FormsProviderAPI.FormsColumns.CONTENT_URI, itemIdPosition);
// caller wants to view/edit a form, so launch formentryactivity
Intent intent = new Intent(Intent.ACTION_EDIT, formUri);
intent.putExtra("fillblankform", true);
intent.putExtra(Constants.CURRENT_FORMNAME, getIntent().getStringExtra(FORMNAME));
intent.putExtra(ApplicationConstants.BundleKeys.FORM_MODE, ApplicationConstants.FormModes.EDIT_SAVED);
startActivity(intent);
} else {
//xxxseharusnya tidak terjadi, tidak mungkin itemIdposition < 1
Toast.makeText(this, "Form not exist", Toast.LENGTH_SHORT).show();
}
}
}
package id.ac.stis.capi.lessthink.adapters;
import android.content.pm.PackageManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.ArrayList;
import id.ac.stis.capi.lessthink.models.BaseHierarchy;
import id.ac.stis.capi.odk.widgets.QuestionWidget;
/**
* Author : Rahadi Jalu
* Email : 14.8325@stis.ac.id
* Company: Politeknik Statistika STIS
*/
public class HierarchyAdapter extends BaseAdapter {
private ArrayList<BaseHierarchy> hierarchies;
public HierarchyAdapter(ArrayList<BaseHierarchy> hierarchies) {
this.hierarchies = hierarchies;
}
@Override
public int getCount() {
return hierarchies.size();
}
@Override
public Object getItem(int position) {
return hierarchies.get(position);
}
@Override
public long getItemId(int position) {
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;
}
@Override
public View getView(final int position, View view, final ViewGroup parent) {
View v = hierarchies.get(position).getView();
if (v instanceof QuestionWidget) {
v.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@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();
}
}
});
}
return v;
}
}
\ No newline at end of file
package id.ac.stis.capi.lessthink.interfaces;
/**
* Author : Rahadi Jalu
* Email : 14.8325@stis.ac.id
* Company: Politeknik Statistika STIS
*/
public interface UpdatingErrorInterface {
/**
* Shows error message to the view.
* Specifically made for updating mode.
*
* @param message error message to be shown. maybe constraintMsg or requiredMsg.
* */
void setError(String message);
/**
* Clears error message of the view.
* Specifically made for updating mode.
* */
void clearError();
}
package id.ac.stis.capi.lessthink.listeners;
import android.view.View;
/**
* Author : Rahadi Jalu
* Email : 14.8325@stis.ac.id
* Company: Politeknik Statistika STIS
*/
public interface OnAddButtonClickListener {
void onAddButtonClick(View v);
}
package id.ac.stis.capi.lessthink.listeners;
import android.view.View;
import org.javarosa.core.model.FormIndex;
import id.ac.stis.capi.odk.widgets.QuestionWidget;
/**
* Author : Rahadi Jalu
* Email : 14.8325@stis.ac.id
* Company: Politeknik Statistika STIS
*/
public interface OnChangeListener {
void onChange(QuestionWidget questionWidget, FormIndex formIndex);
}
package id.ac.stis.capi.lessthink.listeners;
/**
* Author : Rahadi Jalu
* Email : 14.8325@stis.ac.id
* Company: Politeknik Statistika STIS
*/
public interface OnEditRutaSavedListener {
void onEditRutaSaved();
}
package id.ac.stis.capi.lessthink.models;
import android.view.View;
/**
* Author : Rahadi Jalu
* Email : 14.8325@stis.ac.id
* Company: Politeknik Statistika STIS
*/
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;
public BaseHierarchy(int type, int level) {
this.type = type;
this.level = level;
}
public abstract View getView();
public int getType() {
return type;
}
public int getLevel() {
return level;
}
}
package id.ac.stis.capi.lessthink.models;
import android.content.Context;
import android.view.View;
import android.widget.TextView;
import org.javarosa.form.api.FormEntryCaption;
/**
* Author : Rahadi Jalu
* Email : 14.8325@stis.ac.id
* Company: Politeknik Statistika STIS
*/
public class CaptionHierarchy extends BaseHierarchy {
private TextView textView;
private FormEntryCaption caption;
public CaptionHierarchy(Context context, FormEntryCaption caption, int level) {
super(HIERARCHY_TYPE_CAPTION, level);
this.caption = caption;
this.textView = new TextView(context);
textView.setText(caption.getLongText());
}
public FormEntryCaption getCaption() {
return caption;
}
@Override
public View getView() {
return textView;
}
}
package id.ac.stis.capi.lessthink.models;
import android.content.Context;
import android.view.View;
import org.javarosa.form.api.FormEntryPrompt;
import id.ac.stis.capi.odk.widgets.QuestionWidget;
import id.ac.stis.capi.odk.widgets.WidgetFactory;
/**
* Author : Rahadi Jalu
* Email : 14.8325@stis.ac.id
* Company: Politeknik Statistika STIS
*/
public class PromptHierarchy extends BaseHierarchy {
private QuestionWidget widget;
private FormEntryPrompt prompt;
public PromptHierarchy(Context context, FormEntryPrompt prompt, int level) {
super(HIERARCHY_TYPE_PROMPT, level);
this.prompt = prompt;
this.widget = WidgetFactory.createWidgetFromPrompt(prompt, context, false);
}
public FormEntryPrompt getPrompt() {
return prompt;
}
@Override
public View getView() {
return widget;
}
}
package id.ac.stis.capi.lessthink.utils;
import android.support.v7.widget.AppCompatButton;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import org.javarosa.core.model.FormIndex;
import java.io.InvalidObjectException;
import java.util.ArrayList;
import id.ac.stis.capi.lessthink.interfaces.UpdatingErrorInterface;
import id.ac.stis.capi.lessthink.listeners.OnChangeListener;
import id.ac.stis.capi.odk.widgets.BooleanWidget;
import id.ac.stis.capi.odk.widgets.ExStringWidget;
import id.ac.stis.capi.odk.widgets.ItemsetWidget;
import id.ac.stis.capi.odk.widgets.ListMultiWidget;
import id.ac.stis.capi.odk.widgets.ListWidget;
import id.ac.stis.capi.odk.widgets.QuestionWidget;
import id.ac.stis.capi.odk.widgets.SelectMultiWidget;
import id.ac.stis.capi.odk.widgets.SelectOneAutoAdvanceWidget;
import id.ac.stis.capi.odk.widgets.SelectOneSearchWidget;
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
* Email : 14.8325@stis.ac.id
* Company: Politeknik Statistika STIS
*/
public class ViewUtils {
public static View debugViewIds(View view, String logtag) {
Log.v(logtag, "traversing: " + view.getClass().getSimpleName() + ", id: " + view.getId());
if (view.getParent() != null && (view.getParent() instanceof ViewGroup)) {
return debugViewIds((View) view.getParent(), logtag);
} else {
debugChildViewIds(view, logtag, 0);
return view;
}
}
private static void debugChildViewIds(View view, String logtag, int spaces) {
if (view instanceof ViewGroup) {
ViewGroup group = (ViewGroup) view;
for (int i = 0; i < group.getChildCount(); i++) {
View child = group.getChildAt(i);
if (child instanceof AppCompatButton) {
AppCompatButton button = (AppCompatButton) child;
Log.v(logtag, padString("view: " + child.getClass().getSimpleName() + "(" + child.getId() + ":" + button.getText().toString() + ")", spaces));
} else {
Log.v(logtag, padString("view: " + child.getClass().getSimpleName() + "(" + child.getId() + ")", spaces));
}
debugChildViewIds(child, logtag, spaces + 1);
}
}
}
private static String padString(String str, int noOfSpaces) {
if (noOfSpaces <= 0) {
return str;
}
StringBuilder builder = new StringBuilder(str.length() + noOfSpaces);
for (int i = 0; i < noOfSpaces; i++) {
builder.append(' ');
}
return builder.append(str).toString();
}
public static void setChangeListener(final QuestionWidget qw, final FormIndex index,
final OnChangeListener onChangeListener)
throws InvalidObjectException {
ArrayList<RadioButton> radioButtons = null;
if (qw instanceof SelectOneWidget)
radioButtons = ((SelectOneWidget) qw).getRadioButtons();
else if (qw instanceof ItemsetWidget)
radioButtons = ((ItemsetWidget) qw).getRadioButtons();
else if (qw instanceof SelectOneAutoAdvanceWidget)
radioButtons = ((SelectOneAutoAdvanceWidget) qw).getRadioButtons();
else if (qw instanceof SelectOneSearchWidget)
radioButtons = ((SelectOneSearchWidget) qw).getRadioButtons();
else if (qw instanceof ListWidget)
radioButtons = ((ListWidget) qw).getRadioButtons();
if (radioButtons != null) {
for (RadioButton rb : radioButtons) {
rb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onChangeListener != null) {
onChangeListener.onChange(qw, index);
}
}
});
}
return;
}
CheckBox cb = null;
if (qw instanceof BooleanWidget)
cb = ((BooleanWidget) qw).getCheckbox();
else if (qw instanceof TriggerWidget)
cb = ((TriggerWidget) qw).getCheckbox();
if (cb != null) {
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (onChangeListener != null) {
onChangeListener.onChange(qw, index);
}
}
});
return;
}
Spinner sp = null;
if (qw instanceof SpinnerWidget)
sp = ((SpinnerWidget) qw).getSpinner();
if (sp != null) {
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (onChangeListener != null) {
onChangeListener.onChange(qw, index);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
if (onChangeListener != null) {
onChangeListener.onChange(qw, index);
}
}
});
return;
}
ArrayList<CheckBox> cbs = null;