From 9e33865a18d55ac1cb4a5b6c2445221d762d990d Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Tue, 15 Jul 2014 18:10:07 -0700 Subject: Don't close dialog on input errors --- src/github/daneren2005/dsub/util/UserUtil.java | 266 +++++++++++++------------ 1 file changed, 139 insertions(+), 127 deletions(-) (limited to 'src') diff --git a/src/github/daneren2005/dsub/util/UserUtil.java b/src/github/daneren2005/dsub/util/UserUtil.java index 77e07f6b..5c57b422 100644 --- a/src/github/daneren2005/dsub/util/UserUtil.java +++ b/src/github/daneren2005/dsub/util/UserUtil.java @@ -191,48 +191,52 @@ public final class UserUtil { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle(R.string.admin_change_password) .setView(layout) - .setPositiveButton(R.string.common_save, new DialogInterface.OnClickListener() { + .setPositiveButton(R.string.common_save, null) + .setNegativeButton(R.string.common_cancel, null) + .setCancelable(true); + + final AlertDialog dialog = builder.create(); + dialog.show(); + + dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + final String password = passwordView.getText().toString(); + // Don't allow blank passwords + if ("".equals(password)) { + Util.toast(context, R.string.admin_change_password_invalid); + return; + } + + new SilentBackgroundTask(context) { @Override - public void onClick(DialogInterface dialog, int id) { - final String password = passwordView.getText().toString(); - // Don't allow blank passwords - if ("".equals(password)) { - Util.toast(context, R.string.admin_change_password_invalid); - return; + protected Void doInBackground() throws Throwable { + MusicService musicService = MusicServiceFactory.getMusicService(context); + musicService.changePassword(user.getUsername(), password, context, null); + return null; + } + + @Override + protected void done(Void v) { + Util.toast(context, context.getResources().getString(R.string.admin_change_password_success, user.getUsername())); + } + + @Override + protected void error(Throwable error) { + String msg; + if (error instanceof OfflineException || error instanceof ServerTooOldException) { + msg = getErrorMessage(error); + } else { + msg = context.getResources().getString(R.string.admin_change_password_error, user.getUsername()); } - new SilentBackgroundTask(context) { - @Override - protected Void doInBackground() throws Throwable { - MusicService musicService = MusicServiceFactory.getMusicService(context); - musicService.changePassword(user.getUsername(), password, context, null); - return null; - } - - @Override - protected void done(Void v) { - Util.toast(context, context.getResources().getString(R.string.admin_change_password_success, user.getUsername())); - } - - @Override - protected void error(Throwable error) { - String msg; - if (error instanceof OfflineException || error instanceof ServerTooOldException) { - msg = getErrorMessage(error); - } else { - msg = context.getResources().getString(R.string.admin_change_password_error, user.getUsername()); - } - - Util.toast(context, msg); - } - }.execute(); + Util.toast(context, msg); } - }) - .setNegativeButton(R.string.common_cancel, null) - .setCancelable(true); + }.execute(); - AlertDialog dialog = builder.create(); - dialog.show(); + dialog.dismiss(); + } + }); } public static void updateSettings(final Context context, final User user) { @@ -270,48 +274,52 @@ public final class UserUtil { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle(R.string.admin_change_email) .setView(layout) - .setPositiveButton(R.string.common_save, new DialogInterface.OnClickListener() { + .setPositiveButton(R.string.common_save, null) + .setNegativeButton(R.string.common_cancel, null) + .setCancelable(true); + + final AlertDialog dialog = builder.create(); + dialog.show(); + + dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + final String email = emailView.getText().toString(); + // Don't allow blank emails + if ("".equals(email)) { + Util.toast(context, R.string.admin_change_email_invalid); + return; + } + + new SilentBackgroundTask(context) { @Override - public void onClick(DialogInterface dialog, int id) { - final String email = emailView.getText().toString(); - // Don't allow blank emails - if ("".equals(email)) { - Util.toast(context, R.string.admin_change_email_invalid); - return; + protected Void doInBackground() throws Throwable { + MusicService musicService = MusicServiceFactory.getMusicService(context); + musicService.changeEmail(user.getUsername(), email, context, null); + return null; + } + + @Override + protected void done(Void v) { + Util.toast(context, context.getResources().getString(R.string.admin_change_email_success, user.getUsername())); + } + + @Override + protected void error(Throwable error) { + String msg; + if (error instanceof OfflineException || error instanceof ServerTooOldException) { + msg = getErrorMessage(error); + } else { + msg = context.getResources().getString(R.string.admin_change_email_error, user.getUsername()); } - new SilentBackgroundTask(context) { - @Override - protected Void doInBackground() throws Throwable { - MusicService musicService = MusicServiceFactory.getMusicService(context); - musicService.changeEmail(user.getUsername(), email, context, null); - return null; - } - - @Override - protected void done(Void v) { - Util.toast(context, context.getResources().getString(R.string.admin_change_email_success, user.getUsername())); - } - - @Override - protected void error(Throwable error) { - String msg; - if (error instanceof OfflineException || error instanceof ServerTooOldException) { - msg = getErrorMessage(error); - } else { - msg = context.getResources().getString(R.string.admin_change_email_error, user.getUsername()); - } - - Util.toast(context, msg); - } - }.execute(); + Util.toast(context, msg); } - }) - .setNegativeButton(R.string.common_cancel, null) - .setCancelable(true); + }.execute(); - AlertDialog dialog = builder.create(); - dialog.show(); + dialog.dismiss(); + } + }); } public static void deleteUser(final Context context, final User user, final ArrayAdapter adapter) { @@ -372,65 +380,69 @@ public final class UserUtil { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle(R.string.menu_add_user) .setView(layout) - .setPositiveButton(R.string.common_save, new DialogInterface.OnClickListener() { + .setPositiveButton(R.string.common_save, null) + .setNegativeButton(R.string.common_cancel, null) + .setCancelable(true); + + final AlertDialog dialog = builder.create(); + dialog.show(); + + dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + final String username = usernameView.getText().toString(); + // Don't allow blank emails + if ("".equals(username)) { + Util.toast(context, R.string.admin_change_username_invalid); + return; + } + + final String email = emailView.getText().toString(); + // Don't allow blank emails + if ("".equals(email)) { + Util.toast(context, R.string.admin_change_email_invalid); + return; + } + + final String password = passwordView.getText().toString(); + if ("".equals(password)) { + Util.toast(context, R.string.admin_change_password_invalid); + return; + } + + user.setUsername(username); + user.setEmail(email); + user.setPassword(password); + + new SilentBackgroundTask(context) { @Override - public void onClick(DialogInterface dialog, int id) { - final String username = usernameView.getText().toString(); - // Don't allow blank emails - if ("".equals(username)) { - Util.toast(context, R.string.admin_change_username_invalid); - return; - } + protected Void doInBackground() throws Throwable { + MusicService musicService = MusicServiceFactory.getMusicService(context); + musicService.createUser(user, context, null); + return null; + } - final String email = emailView.getText().toString(); - // Don't allow blank emails - if ("".equals(email)) { - Util.toast(context, R.string.admin_change_email_invalid); - return; - } + @Override + protected void done(Void v) { + fragment.onRefresh(); + Util.toast(context, context.getResources().getString(R.string.admin_create_user_success)); + } - final String password = passwordView.getText().toString(); - if ("".equals(password)) { - Util.toast(context, R.string.admin_change_password_invalid); - return; + @Override + protected void error(Throwable error) { + String msg; + if (error instanceof OfflineException || error instanceof ServerTooOldException) { + msg = getErrorMessage(error); + } else { + msg = context.getResources().getString(R.string.admin_create_user_error); } - user.setUsername(username); - user.setEmail(email); - user.setPassword(password); - - new SilentBackgroundTask(context) { - @Override - protected Void doInBackground() throws Throwable { - MusicService musicService = MusicServiceFactory.getMusicService(context); - musicService.createUser(user, context, null); - return null; - } - - @Override - protected void done(Void v) { - fragment.onRefresh(); - Util.toast(context, context.getResources().getString(R.string.admin_create_user_success)); - } - - @Override - protected void error(Throwable error) { - String msg; - if (error instanceof OfflineException || error instanceof ServerTooOldException) { - msg = getErrorMessage(error); - } else { - msg = context.getResources().getString(R.string.admin_create_user_error); - } - - Util.toast(context, msg); - } - }.execute(); + Util.toast(context, msg); } - }) - .setNegativeButton(R.string.common_cancel, null) - .setCancelable(true); + }.execute(); - AlertDialog dialog = builder.create(); - dialog.show(); + dialog.dismiss(); + } + }); } } -- cgit v1.2.3