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

Wip Collect 541 2 #1267

Closed
wants to merge 9 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
import org.odk.collect.android.dao.InstancesDao;
import org.odk.collect.android.exception.GDriveConnectionException;
import org.odk.collect.android.exception.JavaRosaException;
import org.odk.collect.android.fragments.dialogs.CollectAbstractDialog;
import org.odk.collect.android.fragments.dialogs.RemoveResponseDialog;
import org.odk.collect.android.listeners.AdvanceToNextListener;
import org.odk.collect.android.listeners.FormLoaderListener;
import org.odk.collect.android.listeners.FormSavedListener;
Expand Down Expand Up @@ -120,7 +122,7 @@
*/
public class FormEntryActivity extends AppCompatActivity implements AnimationListener,
FormLoaderListener, FormSavedListener, AdvanceToNextListener,
OnGestureListener, SavePointListener {
OnGestureListener, SavePointListener, RemoveResponseDialog.RemoveResponseDialogCallbacks {

// save with every swipe forward or back. Timings indicate this takes .25
// seconds.
Expand Down Expand Up @@ -183,6 +185,8 @@ public class FormEntryActivity extends AppCompatActivity implements AnimationLis
// Tracks whether we are autosaving
public static final String KEY_AUTO_SAVED = "autosaved";

public static final String KEY_QUESTION_WIDGET_ID = "questionWidgetId";

private static final int PROGRESS_DIALOG = 1;
private static final int SAVING_DIALOG = 2;
private static final int SAVING_IMAGE_DIALOG = 3;
Expand Down Expand Up @@ -225,6 +229,8 @@ public class FormEntryActivity extends AppCompatActivity implements AnimationLis
private String stepMessage = "";
private Toolbar toolbar;

private int questionWidgetId;

enum AnimationType {
LEFT, RIGHT, FADE
}
Expand Down Expand Up @@ -318,6 +324,7 @@ public void onClick(View v) {
if (savedInstanceState.containsKey(KEY_AUTO_SAVED)) {
autoSaved = savedInstanceState.getBoolean(KEY_AUTO_SAVED);
}
questionWidgetId = savedInstanceState.getInt(KEY_QUESTION_WIDGET_ID);
}

// If a parse error message is showing then nothing else is loaded
Expand Down Expand Up @@ -578,6 +585,7 @@ protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean(NEWFORM, false);
outState.putString(KEY_ERROR, errorMessage);
outState.putBoolean(KEY_AUTO_SAVED, autoSaved);
outState.putInt(KEY_QUESTION_WIDGET_ID, questionWidgetId);
}

@Override
Expand Down Expand Up @@ -1041,6 +1049,7 @@ public boolean onContextItemSelected(MenuItem item) {
.getActivityLogger()
.logInstanceAction(this, "onContextItemSelected",
"createClearDialog", qw.getPrompt().getIndex());
questionWidgetId = qw.getId();
createClearDialog(qw);
break;
}
Expand Down Expand Up @@ -2047,15 +2056,16 @@ private void removeTempInstance() {
* Confirm clear answer dialog
*/
private void createClearDialog(final QuestionWidget qw) {
Collect.getInstance()
Collect
.getInstance()
.getActivityLogger()
.logInstanceAction(this, "createClearDialog", "show",
qw.getPrompt().getIndex());
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setIcon(android.R.drawable.ic_dialog_info);
.logInstanceAction(this, "createClearDialog", "show", qw.getPrompt().getIndex());

alertDialog.setTitle(getString(R.string.clear_answer_ask));
RemoveResponseDialog dialogFragment = RemoveResponseDialog.newInstance(getString(R.string.clearanswer_confirm, getQuestionForRemoveResponseDialog(qw)));
dialogFragment.show(getSupportFragmentManager(), CollectAbstractDialog.COLLECT_DIALOG_TAG);
}

private String getQuestionForRemoveResponseDialog(QuestionWidget qw) {
String question = qw.getPrompt().getLongText();
if (question == null) {
question = "";
Expand All @@ -2064,37 +2074,7 @@ private void createClearDialog(final QuestionWidget qw) {
question = question.substring(0, 50) + "...";
}

alertDialog.setMessage(getString(R.string.clearanswer_confirm,
question));

DialogInterface.OnClickListener quitListener = new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int i) {
switch (i) {
case DialogInterface.BUTTON_POSITIVE: // yes
Collect.getInstance()
.getActivityLogger()
.logInstanceAction(this, "createClearDialog",
"clearAnswer", qw.getPrompt().getIndex());
clearAnswer(qw);
saveAnswersForCurrentScreen(DO_NOT_EVALUATE_CONSTRAINTS);
break;
case DialogInterface.BUTTON_NEGATIVE: // no
Collect.getInstance()
.getActivityLogger()
.logInstanceAction(this, "createClearDialog",
"cancel", qw.getPrompt().getIndex());
break;
}
}
};
alertDialog.setCancelable(false);
alertDialog
.setButton(getString(R.string.discard_answer), quitListener);
alertDialog.setButton2(getString(R.string.clear_answer_no),
quitListener);
alertDialog.show();
return question;
}

/**
Expand Down Expand Up @@ -2933,4 +2913,16 @@ public EmptyView(Context context) {
public void allowSwiping(boolean doSwipe) {
this.doSwipe = doSwipe;
}

@Override
public void removeAnswer(DialogInterface dialog) {
QuestionWidget qw = ((ODKView) currentView).getQuestionWidgetByID(questionWidgetId);
Collect
.getInstance()
.getActivityLogger()
.logInstanceAction(this, "createClearDialog", "clearAnswer", qw.getPrompt().getIndex());

clearAnswer(qw);
saveAnswersForCurrentScreen(DO_NOT_EVALUATE_CONSTRAINTS);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2017 Nafundi
*
* 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.fragments.dialogs;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

public abstract class CollectAbstractDialog extends DialogFragment {

public static final String COLLECT_DIALOG_TAG = "collectDialogTag";
protected static final String DIALOG_TITLE = "dialogTitle";
protected static final String ICON_ID = "iconId";
protected static final String MESSAGE = "message";
protected static final String BUTTON_TITLE = "buttonTitle";

protected AlertDialog.Builder builder;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
if (getArguments().getString(DIALOG_TITLE) != null) {
builder.setTitle(getArguments().getString(DIALOG_TITLE));
}

if (getArguments().getString(MESSAGE) != null) {
builder.setMessage(getArguments().getString(MESSAGE));
}

if (getArguments().getInt(ICON_ID) != 0) {
builder.setIcon(getArguments().getInt(ICON_ID));
}

setCancelable(false);

return builder.create();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2017 Nafundi
*
* 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.fragments.dialogs;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;

import org.odk.collect.android.R;

public class RemoveResponseDialog extends CollectAbstractDialog {

public interface RemoveResponseDialogCallbacks {
void removeAnswer(DialogInterface dialog);
}

private RemoveResponseDialogCallbacks callback;

public static RemoveResponseDialog newInstance(String message) {
RemoveResponseDialog dialogFragment = new RemoveResponseDialog();
Bundle bundle = new Bundle();
bundle.putString(MESSAGE, message);
dialogFragment.setArguments(bundle);
return dialogFragment;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
builder = new AlertDialog.Builder(getActivity());

builder
.setPositiveButton(getString(R.string.discard_answer), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
if (callback != null) {
callback.removeAnswer(dialog);
}
}
})
.setNegativeButton(getString(R.string.clear_answer_no), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setIcon(android.R.drawable.ic_dialog_info)
.setTitle(getContext().getString(R.string.clear_answer_ask));


super.onCreateDialog(savedInstanceState);

return builder.create();
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
callback = (RemoveResponseDialogCallbacks) context;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2017 Nafundi
*
* 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.fragments.dialogs;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;

/**
* This class might be used as an universal simple dialog. You can use it if you just need to
* display it and you don't need any callback.
*/
public class SimpleDialog extends CollectAbstractDialog {

public static SimpleDialog newInstance(String dialogTitle, int iconId, String message, String buttonTitle) {
SimpleDialog dialogFragment = new SimpleDialog();

Bundle bundle = new Bundle();
bundle.putString(DIALOG_TITLE, dialogTitle);
bundle.putInt(ICON_ID, iconId);
bundle.putString(MESSAGE, message);
bundle.putString(BUTTON_TITLE, buttonTitle);

dialogFragment.setArguments(bundle);
return dialogFragment;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
builder = new AlertDialog.Builder(getActivity());

builder.setPositiveButton(getArguments().getString(BUTTON_TITLE), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
});

super.onCreateDialog(savedInstanceState);

return builder.create();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.odk.collect.android.preferences;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
Expand All @@ -26,6 +25,8 @@
import android.widget.CheckBox;

import org.odk.collect.android.R;
import org.odk.collect.android.fragments.dialogs.CollectAbstractDialog;
import org.odk.collect.android.fragments.dialogs.SimpleDialog;
import org.odk.collect.android.utilities.ResetUtility;
import org.odk.collect.android.utilities.ToastUtils;

Expand Down Expand Up @@ -177,28 +178,11 @@ private void handleResult(List<Integer> resetActions, List<Integer> failedResetA
resultMessage.append("\n\n");
}
}
showResultDialog(String.valueOf(resultMessage));
buildAndShowResetSettingsFinalDialog(String.valueOf(resultMessage));
}

private void showResultDialog(final String resultMessage) {
((AdminPreferencesActivity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
AlertDialog.Builder b = new AlertDialog.Builder(getContext());
b.setTitle(getContext().getString(R.string.reset_app_state_result));
b.setMessage(resultMessage);
b.setCancelable(false);
b.setIcon(android.R.drawable.ic_dialog_info);
b.setPositiveButton(getContext().getString(R.string.ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
((AdminPreferencesActivity) context).recreate();
}
});
AlertDialog alertDialog = b.create();
alertDialog.show();
}
});
private void buildAndShowResetSettingsFinalDialog(String message) {
SimpleDialog dialogFragment = SimpleDialog.newInstance(getContext().getString(R.string.reset_app_state_result), android.R.drawable.ic_dialog_info, message, getContext().getString(R.string.ok));
dialogFragment.show(((AdminPreferencesActivity) context).getSupportFragmentManager(), CollectAbstractDialog.COLLECT_DIALOG_TAG);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -442,4 +442,13 @@ public void stopAudio() {
widgets.get(0).stopAudio();
}

public QuestionWidget getQuestionWidgetByID(int questionWidgetId) {
QuestionWidget questionWidget = null;
for (QuestionWidget qw : getWidgets()) {
if (qw.getId() == questionWidgetId) {
questionWidget = qw;
}
}
return questionWidget;
}
}