From 120b2a27fb68831fbabbcd5b13c31695337f0fdb Mon Sep 17 00:00:00 2001 From: Bananeweizen Date: Fri, 27 Dec 2013 16:38:46 +0100 Subject: refactoring: extract input dialog as reusable code --- main/src/cgeo/geocaching/list/StoredList.java | 49 ++-------------- main/src/cgeo/geocaching/ui/dialog/Dialogs.java | 76 +++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 44 deletions(-) diff --git a/main/src/cgeo/geocaching/list/StoredList.java b/main/src/cgeo/geocaching/list/StoredList.java index fd16347..8106073 100644 --- a/main/src/cgeo/geocaching/list/StoredList.java +++ b/main/src/cgeo/geocaching/list/StoredList.java @@ -4,6 +4,7 @@ import cgeo.geocaching.CgeoApplication; import cgeo.geocaching.DataStore; import cgeo.geocaching.R; import cgeo.geocaching.activity.ActivityMixin; +import cgeo.geocaching.ui.dialog.Dialogs; import cgeo.geocaching.utils.RunnableWithArgument; import org.apache.commons.lang3.StringUtils; @@ -13,10 +14,6 @@ import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.res.Resources; -import android.text.Editable; -import android.text.InputType; -import android.text.TextWatcher; -import android.widget.EditText; import java.text.Collator; import java.util.ArrayList; @@ -160,50 +157,17 @@ public final class StoredList extends AbstractList { } private void handleListNameInput(final String defaultValue, int dialogTitle, int buttonTitle, final RunnableWithArgument runnable) { - final EditText input = new EditText(activity); - input.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_CLASS_TEXT); - input.setText(defaultValue); - - final AlertDialog.Builder builder = new AlertDialog.Builder(activity); - builder.setTitle(dialogTitle); - builder.setView(input); - builder.setPositiveButton(buttonTitle, new DialogInterface.OnClickListener() { + Dialogs.input(activity, dialogTitle, defaultValue, buttonTitle, new RunnableWithArgument() { + @Override - public void onClick(DialogInterface dialog, int whichButton) { + public void run(final String input) { // remove whitespaces added by autocompletion of Android keyboard - String listName = StringUtils.trim(input.getText().toString()); + String listName = StringUtils.trim(input); if (StringUtils.isNotBlank(listName)) { runnable.run(listName); } } }); - builder.setNegativeButton(res.getString(R.string.list_dialog_cancel), new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int whichButton) { - dialog.dismiss(); - } - }); - final AlertDialog dialog = builder.create(); - input.addTextChangedListener(new TextWatcher() { - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - // empty - } - - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - // empty - } - - @Override - public void afterTextChanged(Editable editable) { - enableDialogButton(dialog, editable.toString()); - } - }); - dialog.show(); - enableDialogButton(dialog, defaultValue); - input.setSelection(input.getText().length()); } public void promptForListRename(final int listId, @NonNull final Runnable runAfterRename) { @@ -218,9 +182,6 @@ public final class StoredList extends AbstractList { }); } - private static void enableDialogButton(final AlertDialog dialog, final String input) { - dialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(StringUtils.isNotBlank(input)); - } } /** diff --git a/main/src/cgeo/geocaching/ui/dialog/Dialogs.java b/main/src/cgeo/geocaching/ui/dialog/Dialogs.java index 6064c41..865ba70 100644 --- a/main/src/cgeo/geocaching/ui/dialog/Dialogs.java +++ b/main/src/cgeo/geocaching/ui/dialog/Dialogs.java @@ -1,14 +1,22 @@ package cgeo.geocaching.ui.dialog; import cgeo.geocaching.CgeoApplication; +import cgeo.geocaching.utils.RunnableWithArgument; +import org.apache.commons.lang3.StringUtils; import org.eclipse.jdt.annotation.Nullable; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; +import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.graphics.drawable.Drawable; +import android.text.Editable; +import android.text.InputType; +import android.text.TextWatcher; +import android.view.WindowManager; +import android.widget.EditText; /** * Wrapper for {@link AlertDialog}. If you want to show a simple text, use one of the @@ -288,4 +296,72 @@ public final class Dialogs { message(context, getString(title), getString(message), icon); } + /** + * Show a message dialog for input from the user. The okay button is only enabled on non empty input. + * + * @param context + * activity owning the dialog + * @param title + * message dialog title + * @param defaultValue + * default input value + * @param buttonTitle + * title of the okay button + * @param okayListener + * listener to be run on okay + */ + public static void input(final Activity context, final int title, final String defaultValue, final int buttonTitle, final RunnableWithArgument okayListener) { + final EditText input = new EditText(context); + input.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_CLASS_TEXT); + input.setText(defaultValue); + + final AlertDialog.Builder builder = new AlertDialog.Builder(context); + builder.setTitle(title); + builder.setView(input); + builder.setPositiveButton(buttonTitle, new OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + okayListener.run(input.getText().toString()); + } + }); + builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int whichButton) { + dialog.dismiss(); + } + }); + final AlertDialog dialog = builder.create(); + + input.addTextChangedListener(new TextWatcher() { + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + // empty + } + + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + // empty + } + + @Override + public void afterTextChanged(Editable editable) { + enableDialogButtonIfNotEmpty(dialog, editable.toString()); + } + }); + // force keyboard + dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); + + // disable button + dialog.show(); + enableDialogButtonIfNotEmpty(dialog, defaultValue); + + // position cursor after text + input.setSelection(input.getText().length()); + } + + private static void enableDialogButtonIfNotEmpty(final AlertDialog dialog, final String input) { + dialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(StringUtils.isNotBlank(input)); + } } -- cgit v1.1