From 4c17ea88023dc326c1ea6563e417f0fa6f62254b Mon Sep 17 00:00:00 2001 From: Andy2 Date: Wed, 27 Oct 2010 06:25:29 -0700 Subject: [PATCH] generalize confirm-delete-all dialog into confirm-then-do --- .../org/eehouse/android/xw4/DlgDelegate.java | 32 +++++++++++++- .../org/eehouse/android/xw4/GamesList.java | 44 +++++-------------- .../eehouse/android/xw4/XWListActivity.java | 7 +++ 3 files changed, 48 insertions(+), 35 deletions(-) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgDelegate.java index bdf66b2ef..6f2da8a0a 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgDelegate.java @@ -41,13 +41,15 @@ public class DlgDelegate { public static final int DIALOG_OKONLY = 2; public static final int DIALOG_NOTAGAIN = 3; public static final int WARN_NODICT = 4; - public static final int DIALOG_LAST = WARN_NODICT; + public static final int CONFIRM_THEN = 5; + public static final int DIALOG_LAST = CONFIRM_THEN; private int m_msgID; private Runnable m_proc = null; private int m_prefsKey; private Activity m_activity; private String m_dictName = null; + DialogInterface.OnClickListener m_then; public DlgDelegate( Activity activity ) { m_activity = activity; @@ -69,6 +71,9 @@ public class DlgDelegate { case WARN_NODICT: dialog = createNoDictDialog(); break; + case CONFIRM_THEN: + dialog = createConfirmThenDialog(); + break; } return dialog; } @@ -90,7 +95,12 @@ public class DlgDelegate { case WARN_NODICT: String format = m_activity.getString( R.string.no_dictf ); String msg = String.format( format, m_dictName ); - ((AlertDialog)dialog).setMessage( msg ); + ad.setMessage( msg ); + break; + case CONFIRM_THEN: + ad.setMessage( m_activity.getString(m_msgID) ); + ad.setButton( AlertDialog.BUTTON_POSITIVE, + m_activity.getString( R.string.button_ok ), m_then ); break; } } @@ -129,6 +139,13 @@ public class DlgDelegate { m_activity.showDialog( WARN_NODICT ); } + public void showConfirmThen( int id, DialogInterface.OnClickListener then ) + { + m_msgID = id; + m_then = then; + m_activity.showDialog( CONFIRM_THEN ); + } + private Dialog createAboutDialog() { LayoutInflater factory = LayoutInflater.from( m_activity ); @@ -223,4 +240,15 @@ public class DlgDelegate { }); return dialog; } + + private Dialog createConfirmThenDialog() + { + Dialog dialog = new AlertDialog.Builder( m_activity ) + .setTitle( R.string.query_title ) + .setMessage( "" ) + .setPositiveButton( R.string.button_ok, null ) // will change + .setNegativeButton( R.string.button_cancel, null ) + .create(); + return dialog; + } } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java index 41d53a5e4..2c463a474 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java @@ -57,38 +57,6 @@ public class GamesList extends XWListActivity private String m_missingDict; private Handler m_handler; - @Override - protected Dialog onCreateDialog( int id ) - { - Dialog dialog = super.onCreateDialog( id ); - if ( null == dialog ) { - switch( id ) { - case CONFIRM_DELETE_ALL: - DialogInterface.OnClickListener lstnr = - new DialogInterface.OnClickListener() { - public void onClick( DialogInterface dlg, int item ) { - for( String game:GameUtils.gamesList(GamesList.this)) { - GameUtils.deleteGame( GamesList.this, game ); - } - m_adapter = new GameListAdapter( GamesList.this ); - setListAdapter( m_adapter ); - } - }; - dialog = new AlertDialog.Builder( this ) - .setTitle( R.string.query_title ) - .setMessage( R.string.confirm_delete_all ) - .setPositiveButton( R.string.button_ok, lstnr ) - .setNegativeButton( R.string.button_cancel, null ) - .create(); - break; - default: - Assert.assertTrue(false); - // dialog = Utils.onCreateDialog( this, id, m_dlgObjects ); - } - } - return dialog; - } - @Override protected void onCreate(Bundle savedInstanceState) { @@ -241,7 +209,17 @@ public class GamesList extends XWListActivity switch (item.getItemId()) { case R.id.gamel_menu_delete_all: if ( GameUtils.gamesList( this ).length > 0 ) { - showDialog( CONFIRM_DELETE_ALL ); + DialogInterface.OnClickListener lstnr = + new DialogInterface.OnClickListener() { + public void onClick( DialogInterface dlg, int item ) { + for( String game:GameUtils.gamesList(GamesList.this)) { + GameUtils.deleteGame( GamesList.this, game ); + } + m_adapter = new GameListAdapter( GamesList.this ); + setListAdapter( m_adapter ); + } + }; + showConfirmThen( R.string.confirm_delete_all, lstnr ); } handled = true; break; diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWListActivity.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWListActivity.java index 563297692..bff382b5c 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWListActivity.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWListActivity.java @@ -22,6 +22,7 @@ package org.eehouse.android.xw4; import android.app.ListActivity; import android.app.Dialog; +import android.content.DialogInterface; import android.os.Bundle; import org.eehouse.android.xw4.jni.CommonPrefs; @@ -90,4 +91,10 @@ public class XWListActivity extends ListActivity { m_delegate.showNoDict( name, lang ); } + protected void showConfirmThen( int msgID, + DialogInterface.OnClickListener action ) + { + m_delegate.showConfirmThen( msgID, action ); + } + }