Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactors FormHierarchyActivity #1038

Merged
merged 8 commits into from
Jan 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion collect_app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ the specific language governing permissions and limitations under the License.
android:label="@string/app_name"
android:theme="@style/AppTheme.SettingsTheme" />
<activity
android:name=".activities.FormHierarchyActivity"
android:name=".activities.ViewFormHierarchyActivity"
android:label="@string/app_name" />
<activity
android:name=".activities.EditFormHierarchyActivity"
android:label="@string/app_name" />
<activity
android:name=".activities.GeoPointActivity"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (C) 2017 Shobhit
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/


package org.odk.collect.android.activities;


import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.AdapterView;

import org.javarosa.core.model.FormIndex;
import org.odk.collect.android.R;
import org.odk.collect.android.adapters.HierarchyListAdapter;
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.exception.JavaRosaException;
import org.odk.collect.android.logic.HierarchyElement;

import java.util.ArrayList;

import timber.log.Timber;

public class EditFormHierarchyActivity extends FormHierarchyActivity {

@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
HierarchyElement h = (HierarchyElement) listView.getItemAtPosition(position);
FormIndex index = h.getFormIndex();
if (index == null) {
goUpLevel();
return;
}

switch (h.getType()) {
case EXPANDED:
Collect.getInstance().getActivityLogger().logInstanceAction(this, "onListItemClick",
"COLLAPSED", h.getFormIndex());
h.setType(COLLAPSED);
ArrayList<HierarchyElement> children = h.getChildren();
for (int i = 0; i < children.size(); i++) {
formList.remove(position + 1);
}
h.setIcon(ContextCompat.getDrawable(getApplicationContext(), R.drawable.expander_ic_minimized));
break;
case COLLAPSED:
Collect.getInstance().getActivityLogger().logInstanceAction(this, "onListItemClick",
"EXPANDED", h.getFormIndex());
h.setType(EXPANDED);
ArrayList<HierarchyElement> children1 = h.getChildren();
for (int i = 0; i < children1.size(); i++) {
Timber.i("adding child: %s", children1.get(i).getFormIndex());
formList.add(position + 1 + i, children1.get(i));

}
h.setIcon(ContextCompat.getDrawable(getApplicationContext(), R.drawable.expander_ic_maximized));
break;
case QUESTION:
Collect.getInstance().getActivityLogger().logInstanceAction(this, "onListItemClick",
"QUESTION-JUMP", index);
Collect.getInstance().getFormController().jumpToIndex(index);
if (Collect.getInstance().getFormController().indexIsInFieldList()) {
try {
Collect.getInstance().getFormController().stepToPreviousScreenEvent();
} catch (JavaRosaException e) {
Timber.e(e);
createErrorDialog(e.getCause().getMessage());
return;
}
}
setResult(RESULT_OK);
finish();
return;
case CHILD:
Collect.getInstance().getActivityLogger().logInstanceAction(this, "onListItemClick",
"REPEAT-JUMP", h.getFormIndex());
Collect.getInstance().getFormController().jumpToIndex(h.getFormIndex());
setResult(RESULT_OK);
refreshView();
return;
}

// Should only get here if we've expanded or collapsed a group
HierarchyListAdapter itla = new HierarchyListAdapter(this);
itla.setListItems(formList);
listView.setAdapter(itla);
listView.setSelection(position);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1015,8 +1015,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
0, null, false, true);
}

Intent i = new Intent(this, FormHierarchyActivity.class);
i.putExtra(ApplicationConstants.BundleKeys.FORM_MODE, ApplicationConstants.FormModes.EDIT_SAVED);
Intent i = new Intent(this, EditFormHierarchyActivity.class);
startActivityForResult(i, RequestCodes.HIERARCHY_ACTIVITY);
return true;
case R.id.menu_preferences:
Expand Down Expand Up @@ -2649,16 +2648,13 @@ public void run() {
}
}

Intent i = new Intent(this, FormHierarchyActivity.class);
String formMode = reqIntent.getStringExtra(ApplicationConstants.BundleKeys.FORM_MODE);
if (formMode == null || ApplicationConstants.FormModes.EDIT_SAVED.equalsIgnoreCase(formMode)) {
i.putExtra(ApplicationConstants.BundleKeys.FORM_MODE, ApplicationConstants.FormModes.EDIT_SAVED);
startActivity(i);
startActivity(new Intent(this, EditFormHierarchyActivity.class));
return; // so we don't show the intro screen before jumping to the hierarchy
} else {
if (ApplicationConstants.FormModes.VIEW_SENT.equalsIgnoreCase(formMode)) {
i.putExtra(ApplicationConstants.BundleKeys.FORM_MODE, ApplicationConstants.FormModes.VIEW_SENT);
startActivity(i);
startActivity(new Intent(this, ViewFormHierarchyActivity.class));
}
finish();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@
import org.odk.collect.android.R;
import org.odk.collect.android.adapters.HierarchyListAdapter;
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.exception.JavaRosaException;
import org.odk.collect.android.logic.FormController;
import org.odk.collect.android.logic.HierarchyElement;
import org.odk.collect.android.utilities.ApplicationConstants;
import org.odk.collect.android.utilities.FormEntryPromptUtils;
import org.odk.collect.android.views.ODKView;

Expand All @@ -50,24 +48,24 @@

import timber.log.Timber;

public class FormHierarchyActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
public abstract class FormHierarchyActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

private static final int CHILD = 1;
private static final int EXPANDED = 2;
private static final int COLLAPSED = 3;
private static final int QUESTION = 4;
protected static final int CHILD = 1;
protected static final int EXPANDED = 2;
protected static final int COLLAPSED = 3;
protected static final int QUESTION = 4;

private static final String mIndent = " ";

private Button jumpPreviousButton;

List<HierarchyElement> formList;
TextView path;

FormIndex startIndex;
private FormIndex currentIndex;
private ListView listView;
private TextView emptyView;
protected Button jumpPreviousButton;
protected Button jumpBeginningButton;
protected Button jumpEndButton;
protected ListView listView;


@Override
Expand All @@ -77,7 +75,7 @@ public void onCreate(Bundle savedInstanceState) {

listView = findViewById(android.R.id.list);
listView.setOnItemClickListener(this);
emptyView = findViewById(android.R.id.empty);
TextView emptyView = findViewById(android.R.id.empty);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

Expand Down Expand Up @@ -105,7 +103,7 @@ public void onClick(View v) {
}
});

Button jumpBeginningButton = findViewById(R.id.jumpBeginningButton);
jumpBeginningButton = findViewById(R.id.jumpBeginningButton);
jumpBeginningButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Expand All @@ -118,7 +116,7 @@ public void onClick(View v) {
}
});

Button jumpEndButton = findViewById(R.id.jumpEndButton);
jumpEndButton = findViewById(R.id.jumpEndButton);
jumpEndButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Expand All @@ -131,25 +129,6 @@ public void onClick(View v) {
}
});

String formMode = getIntent().getStringExtra(ApplicationConstants.BundleKeys.FORM_MODE);
if (ApplicationConstants.FormModes.VIEW_SENT.equalsIgnoreCase(formMode)) {
Collect.getInstance().getFormController().stepToOuterScreenEvent();

Button exitButton = findViewById(R.id.exitButton);
exitButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Collect.getInstance().getActivityLogger().logInstanceAction(this, "exit",
"click");
setResult(RESULT_OK);
finish();
}
});

exitButton.setVisibility(View.VISIBLE);
jumpBeginningButton.setVisibility(View.GONE);
jumpEndButton.setVisibility(View.GONE);
}

refreshView();

Expand Down Expand Up @@ -190,7 +169,7 @@ protected void onStop() {
super.onStop();
}

private void goUpLevel() {
protected void goUpLevel() {
Collect.getInstance().getFormController().stepToOuterScreenEvent();

refreshView();
Expand Down Expand Up @@ -390,7 +369,7 @@ public void refreshView() {
/**
* Creates and displays dialog with the given errorMsg.
*/
private void createErrorDialog(String errorMsg) {
protected void createErrorDialog(String errorMsg) {
Collect.getInstance()
.getActivityLogger()
.logInstanceAction(this, "createErrorDialog", "show.");
Expand Down Expand Up @@ -418,72 +397,6 @@ public void onClick(DialogInterface dialog, int i) {
alertDialog.show();
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HierarchyElement h = (HierarchyElement) listView.getItemAtPosition(position);
FormIndex index = h.getFormIndex();
if (index == null) {
goUpLevel();
return;
}

switch (h.getType()) {
case EXPANDED:
Collect.getInstance().getActivityLogger().logInstanceAction(this, "onListItemClick",
"COLLAPSED", h.getFormIndex());
h.setType(COLLAPSED);
ArrayList<HierarchyElement> children = h.getChildren();
for (int i = 0; i < children.size(); i++) {
formList.remove(position + 1);
}
h.setIcon(ContextCompat.getDrawable(getApplicationContext(), R.drawable.expander_ic_minimized));
break;
case COLLAPSED:
Collect.getInstance().getActivityLogger().logInstanceAction(this, "onListItemClick",
"EXPANDED", h.getFormIndex());
h.setType(EXPANDED);
ArrayList<HierarchyElement> children1 = h.getChildren();
for (int i = 0; i < children1.size(); i++) {
Timber.i("adding child: %s", children1.get(i).getFormIndex());
formList.add(position + 1 + i, children1.get(i));

}
h.setIcon(ContextCompat.getDrawable(getApplicationContext(), R.drawable.expander_ic_maximized));
break;
case QUESTION:
Collect.getInstance().getActivityLogger().logInstanceAction(this, "onListItemClick",
"QUESTION-JUMP", index);
Collect.getInstance().getFormController().jumpToIndex(index);
if (Collect.getInstance().getFormController().indexIsInFieldList()) {
try {
Collect.getInstance().getFormController().stepToPreviousScreenEvent();
} catch (JavaRosaException e) {
Timber.e(e);
createErrorDialog(e.getCause().getMessage());
return;
}
}
setResult(RESULT_OK);
String formMode = getIntent().getStringExtra(ApplicationConstants.BundleKeys.FORM_MODE);
if (formMode == null || ApplicationConstants.FormModes.EDIT_SAVED.equalsIgnoreCase(formMode)) {
finish();
}
return;
case CHILD:
Collect.getInstance().getActivityLogger().logInstanceAction(this, "onListItemClick",
"REPEAT-JUMP", h.getFormIndex());
Collect.getInstance().getFormController().jumpToIndex(h.getFormIndex());
setResult(RESULT_OK);
refreshView();
return;
}

// Should only get here if we've expanded or collapsed a group
HierarchyListAdapter itla = new HierarchyListAdapter(this);
itla.setListItems(formList);
listView.setAdapter(itla);
listView.setSelection(position);
}


@Override
Expand Down
Loading