refactor: use builder for notAgain dialogs too

This commit is contained in:
Eric House 2016-08-15 12:10:36 -07:00
parent 7f211c6cb5
commit c36adeebbd
11 changed files with 172 additions and 138 deletions

View file

@ -296,10 +296,10 @@ public class BoardDelegate extends DelegateBase
public void onClick( DialogInterface dialog,
int whichButton ) {
curThis().
showNotAgainDlgThen( R.string.not_again_lookup,
R.string.
key_na_lookup,
Action.LOOKUP_ACTION );
makeNotAgainBuilder( R.string.not_again_lookup,
R.string.key_na_lookup,
Action.LOOKUP_ACTION )
.show();
}
};
ab.setNegativeButton( buttonTxt, lstnr );
@ -863,9 +863,10 @@ public class BoardDelegate extends DelegateBase
int nTiles = XwJNI.model_getNumTilesInTray( m_jniGamePtr,
m_view.getCurPlayer() );
if ( XWApp.MAX_TRAY_TILES > nTiles ) {
showNotAgainDlgThen( R.string.not_again_done,
makeNotAgainBuilder( R.string.not_again_done,
R.string.key_notagain_done,
Action.COMMIT_ACTION );
Action.COMMIT_ACTION )
.show();
} else {
dlgButtonClicked( Action.COMMIT_ACTION, AlertDialog.BUTTON_POSITIVE, null );
}
@ -909,8 +910,9 @@ public class BoardDelegate extends DelegateBase
int strID = ABUtils.haveActionBar() ? R.string.not_again_trading_menu
: R.string.not_again_trading_buttons;
msg += getString( strID );
showNotAgainDlgThen( msg, R.string.key_notagain_trading,
Action.START_TRADE_ACTION );
makeNotAgainBuilder( msg, R.string.key_notagain_trading,
Action.START_TRADE_ACTION )
.show();
break;
case R.id.board_menu_tray:
@ -961,9 +963,10 @@ public class BoardDelegate extends DelegateBase
break;
case R.id.gamel_menu_checkmoves:
showNotAgainDlgThen( R.string.not_again_sync,
makeNotAgainBuilder( R.string.not_again_sync,
R.string.key_notagain_sync,
Action.SYNC_ACTION );
Action.SYNC_ACTION )
.show();
break;
case R.id.board_menu_file_prefs:
@ -1580,7 +1583,8 @@ public class BoardDelegate extends DelegateBase
dlgButtonClicked( Action.SHOW_EXPL_ACTION,
AlertDialog.BUTTON_POSITIVE, null );
} else {
showNotAgainDlgThen( naMsg, naKey, Action.SHOW_EXPL_ACTION );
makeNotAgainBuilder( naMsg, naKey, Action.SHOW_EXPL_ACTION )
.show();
}
}
@ -1761,8 +1765,9 @@ public class BoardDelegate extends DelegateBase
m_nMissing = 0;
post( new Runnable() {
public void run() {
showNotAgainDlg( R.string.not_again_turnchanged,
R.string.key_notagain_turnchanged );
makeNotAgainBuilder( R.string.not_again_turnchanged,
R.string.key_notagain_turnchanged )
.show();
}
} );
handleViaThread( JNICmd.CMD_ZOOM, -8 );
@ -2645,8 +2650,9 @@ public class BoardDelegate extends DelegateBase
} else if ( 2 != gi.nPlayers ) {
Assert.assertNotNull( dlgt );
if ( null != dlgt ) {
dlgt.showNotAgainDlg( R.string.not_again_rematch_two_only,
R.string.key_na_rematch_two_only );
dlgt.makeNotAgainBuilder( R.string.not_again_rematch_two_only,
R.string.key_na_rematch_two_only )
.show();
}
doIt = false;
} else {

View file

@ -151,9 +151,8 @@ public class ConnViaViewLayout extends LinearLayout {
Assert.fail();
break;
}
m_dlgDlgt.showNotAgainDlgThen( msgID, keyID,
DlgDelegate.Action.SKIP_CALLBACK );
m_dlgDlgt.makeNotAgainBuilder( msgID, keyID )
.show();
}
}
}

View file

@ -45,6 +45,7 @@ import org.eehouse.android.xw4.DlgDelegate.Action;
import org.eehouse.android.xw4.DlgDelegate.ActionPair;
import org.eehouse.android.xw4.DlgDelegate.ConfirmThenBuilder;
import org.eehouse.android.xw4.DlgDelegate.DlgClickNotify;
import org.eehouse.android.xw4.DlgDelegate.NotAgainBuilder;
import org.eehouse.android.xw4.MultiService.MultiEvent;
import org.eehouse.android.xw4.loc.LocUtils;
@ -472,37 +473,26 @@ public class DelegateBase implements DlgClickNotify,
Utils.setRemoveOnDismiss( m_activity, dialog, dlgID );
}
protected void showNotAgainDlgThen( int msgID, int prefsKey,
Action action, Object... params )
public NotAgainBuilder
makeNotAgainBuilder( String msg, int key, Action action )
{
m_dlgDelegate.showNotAgainDlgThen( msgID, prefsKey, action, null, params );
return m_dlgDelegate.makeNotAgainBuilder( msg, key, action );
}
public void showNotAgainDlgThen( int msgID, int prefsKey, Action action )
public NotAgainBuilder
makeNotAgainBuilder( int msgId, int key, Action action )
{
m_dlgDelegate.showNotAgainDlgThen( msgID, prefsKey, action );
return m_dlgDelegate.makeNotAgainBuilder( msgId, key, action );
}
public void showNotAgainDlgThen( int msgID, int prefsKey, Action action,
ActionPair more )
public NotAgainBuilder makeNotAgainBuilder( String msg, int key )
{
m_dlgDelegate.showNotAgainDlgThen( msgID, prefsKey, action, more );
return m_dlgDelegate.makeNotAgainBuilder( msg, key );
}
protected void showNotAgainDlgThen( String msg, int prefsKey,
Action action )
public NotAgainBuilder makeNotAgainBuilder( int msgId, int key )
{
m_dlgDelegate.showNotAgainDlgThen( msg, prefsKey, action, null, null );
}
protected void showNotAgainDlg( int msgID, int prefsKey )
{
m_dlgDelegate.showNotAgainDlgThen( msgID, prefsKey );
}
protected void showNotAgainDlgThen( int msgID, int prefsKey )
{
m_dlgDelegate.showNotAgainDlgThen( msgID, prefsKey );
return m_dlgDelegate.makeNotAgainBuilder( msgId, key );
}
// It sucks that these must be duplicated here and XWActivity

View file

@ -538,7 +538,8 @@ public class DictsDelegate extends ListDelegateBase
m_origTitle = getTitle();
showNotAgainDlg( R.string.not_again_dicts, R.string.key_na_dicts );
makeNotAgainBuilder( R.string.not_again_dicts, R.string.key_na_dicts )
.show();
} // init
@Override

View file

@ -152,53 +152,44 @@ public class DlgDelegate {
}
}
public class ConfirmThenBuilder {
private String m_msgString;
private NAKey m_nakey;
private Runnable m_onNA;
private int m_posButton = android.R.string.ok;
private int m_negButton = android.R.string.cancel;
private Action m_action;
private Object[] m_params;
public abstract class DlgDelegateBuilder {
protected String m_msgString;
protected NAKey m_nakey;
protected Runnable m_onNA;
protected int m_posButton = android.R.string.ok;
protected int m_negButton = android.R.string.cancel;
protected Action m_action;
protected Object[] m_params;
public ConfirmThenBuilder( String msg, Action action )
{
m_msgString = msg;
m_action = action;
}
public ConfirmThenBuilder( int msgId, Action action )
{
this( getString(msgId), action );
}
public DlgDelegateBuilder( String msg, Action action )
{ m_msgString = msg; m_action = action; }
public ConfirmThenBuilder setNAKey( int keyId ) {
m_nakey = new NAKey( keyId );
return this;
}
public DlgDelegateBuilder( int msgId, Action action )
{ this( getString(msgId), action );}
public ConfirmThenBuilder setOnNA( Runnable proc ) {
m_onNA = proc;
return this;
}
public DlgDelegateBuilder setNAKey( int keyId )
{ m_nakey = new NAKey( keyId ); return this; }
public ConfirmThenBuilder setPosButton( int id )
{
m_posButton = id;
return this;
}
public DlgDelegateBuilder setOnNA( Runnable proc )
{ m_onNA = proc; return this; }
public ConfirmThenBuilder setNegButton( int id )
{
m_negButton = id;
return this;
}
public DlgDelegateBuilder setPosButton( int id )
{ m_posButton = id; return this; }
public ConfirmThenBuilder setParams( Object... params )
{
m_params = params;
return this;
}
public DlgDelegateBuilder setNegButton( int id )
{ m_negButton = id; return this; }
public DlgDelegateBuilder setParams( Object... params )
{ m_params = params; return this; }
abstract void show();
}
public class ConfirmThenBuilder extends DlgDelegateBuilder {
public ConfirmThenBuilder(String msg, Action action) {super(msg, action);}
public ConfirmThenBuilder(int msgId, Action action) {super(msgId, action);}
@Override
public void show()
{
showConfirmThen( m_nakey, m_onNA, m_msgString, m_posButton,
@ -206,13 +197,65 @@ public class DlgDelegate {
}
}
public ConfirmThenBuilder makeConfirmThenBuilder( String msg, Action action ) {
public class NotAgainBuilder extends DlgDelegateBuilder {
private int m_prefsKey;
private ActionPair m_actionPair;
public NotAgainBuilder(String msg, int key, Action action)
{ super(msg, action); m_prefsKey = key; }
public NotAgainBuilder(int msgId, int key, Action action)
{ super(msgId, action); m_prefsKey = key; }
public NotAgainBuilder( String msg, int key )
{ super( msg, Action.SKIP_CALLBACK ); m_prefsKey = key; }
public NotAgainBuilder( int msgId, int key )
{ super( msgId, Action.SKIP_CALLBACK ); m_prefsKey = key; }
public NotAgainBuilder setActionPair( ActionPair pr )
{ m_actionPair = pr; return this; }
@Override
public void show()
{
showNotAgainDlgThen( m_msgString, m_prefsKey,
m_action, m_actionPair,
m_params );
}
}
public ConfirmThenBuilder makeConfirmThenBuilder( String msg, Action action )
{
return new ConfirmThenBuilder( msg, action );
}
public ConfirmThenBuilder makeConfirmThenBuilder(int msgId, Action action) {
public ConfirmThenBuilder makeConfirmThenBuilder(int msgId, Action action)
{
return new ConfirmThenBuilder( msgId, action );
}
public NotAgainBuilder makeNotAgainBuilder( int msgId, int key,
Action action )
{
return new NotAgainBuilder( msgId, key, action );
}
public NotAgainBuilder makeNotAgainBuilder( String msg, int key,
Action action )
{
return new NotAgainBuilder( msg, key, action );
}
public NotAgainBuilder makeNotAgainBuilder( String msg, int key )
{
return new NotAgainBuilder( msg, key );
}
public NotAgainBuilder makeNotAgainBuilder( int msgId, int key ) {
return new NotAgainBuilder( msgId, key );
}
public static final int SMS_BTN = AlertDialog.BUTTON_POSITIVE;
public static final int NFC_BTN = AlertDialog.BUTTON_NEUTRAL;
public static final int DISMISS_BUTTON = 0;
@ -232,7 +275,9 @@ public class DlgDelegate {
public interface HasDlgDelegate {
void showOKOnlyDialog( int msgID );
void showOKOnlyDialog( String msg );
void showNotAgainDlgThen( int msgID, int prefsKey, Action action );
NotAgainBuilder makeNotAgainBuilder( int msgID, int prefsKey,
Action action );
NotAgainBuilder makeNotAgainBuilder( int msgID, int prefsKey );
}
private static Map<DlgID, WeakReference<DelegateBase>> s_pendings
@ -376,9 +421,9 @@ public class DlgDelegate {
showDialog( DlgID.DIALOG_ENABLESMS );
}
public void showNotAgainDlgThen( String msg, int prefsKey,
final Action action, ActionPair more,
final Object[] params )
private void showNotAgainDlgThen( String msg, int prefsKey,
final Action action, ActionPair more,
final Object[] params )
{
if ( XWPrefs.getPrefsBoolean( m_activity, prefsKey, false ) ) {
// If it's set, do the action without bothering with the
@ -402,34 +447,6 @@ public class DlgDelegate {
}
}
public void showNotAgainDlgThen( int msgID, int prefsKey, Action action,
ActionPair more, Object[] params )
{
showNotAgainDlgThen( getString( msgID ), prefsKey, action, more,
params );
}
public void showNotAgainDlgThen( int msgID, int prefsKey, Action action )
{
showNotAgainDlgThen( msgID, prefsKey, action, null, null );
}
public void showNotAgainDlgThen( int msgID, int prefsKey, Action action,
ActionPair more )
{
showNotAgainDlgThen( msgID, prefsKey, action, more, null );
}
public void showNotAgainDlgThen( int msgID, int prefsKey )
{
showNotAgainDlgThen( msgID, prefsKey, Action.SKIP_CALLBACK );
}
private void showNotAgainDlgThen( String msg, int prefsKey )
{
showNotAgainDlgThen( msg, prefsKey, Action.SKIP_CALLBACK, null, null );
}
private void showConfirmThen( NAKey nakey, Runnable onNA, String msg, int posButton,
int negButton, Action action, Object[] params )
{
@ -718,7 +735,8 @@ public class DlgDelegate {
String msg =
getString( R.string.not_again_clip_expl_fmt,
getString(R.string.slmenu_copy_sel) );
showNotAgainDlgThen( msg, R.string.key_na_clip_expl );
new NotAgainBuilder( msg, R.string.key_na_clip_expl )
.show();
break;
case SMS:
if ( ! XWPrefs.getSMSEnabled( m_activity ) ) {

View file

@ -510,9 +510,11 @@ public class GameConfigDelegate extends DelegateBase
&& !XWPrefs.getPublicRoomsEnabled( m_activity ) ) {
ActionPair pair = new ActionPair( Action.SET_ENABLE_PUBLIC,
R.string.enable_pubroom_title );
showNotAgainDlgThen( R.string.not_again_enablepublic,
makeNotAgainBuilder( R.string.not_again_enablepublic,
R.string.key_notagain_enablepublic,
Action.SKIP_CALLBACK, pair );
Action.SKIP_CALLBACK )
.setActionPair(pair)
.show();
}
} // init
@ -741,9 +743,10 @@ public class GameConfigDelegate extends DelegateBase
} else if ( m_joinPublicCheck == view ) {
adjustConnectStuff();
} else if ( m_gameLockedCheck == view ) {
showNotAgainDlgThen( R.string.not_again_unlock,
makeNotAgainBuilder( R.string.not_again_unlock,
R.string.key_notagain_unlock,
Action.LOCKED_CHANGE_ACTION );
Action.LOCKED_CHANGE_ACTION )
.show();
} else if ( m_refreshRoomsButton == view ) {
refreshNames();
} else if ( m_changeConnButton == view ) {

View file

@ -1144,9 +1144,11 @@ public class GamesListDelegate extends ListDelegateBase
if ( clicked instanceof GameListItem ) {
long rowid = ((GameListItem)clicked).getRowID();
if ( ! m_launchedGames.contains( rowid ) ) {
showNotAgainDlgThen( R.string.not_again_newselect,
makeNotAgainBuilder( R.string.not_again_newselect,
R.string.key_notagain_newselect,
Action.OPEN_GAME, rowid, summary );
Action.OPEN_GAME )
.setParams( rowid, summary )
.show();
}
}
}
@ -1358,9 +1360,10 @@ public class GamesListDelegate extends ListDelegateBase
{
boolean handled = 0 < m_selGames.size() || 0 < m_selGroupIDs.size();
if ( handled ) {
showNotAgainDlgThen( R.string.not_again_backclears,
makeNotAgainBuilder( R.string.not_again_backclears,
R.string.key_notagain_backclears,
Action.CLEAR_SELS );
Action.CLEAR_SELS )
.show();
}
return handled;
}
@ -1512,9 +1515,10 @@ public class GamesListDelegate extends ListDelegateBase
break;
case R.id.games_menu_checkmoves:
showNotAgainDlgThen( R.string.not_again_sync,
makeNotAgainBuilder( R.string.not_again_sync,
R.string.key_notagain_sync,
Action.SYNC_MENU );
Action.SYNC_MENU )
.show();
break;
case R.id.games_menu_checkupdates:
@ -1747,9 +1751,11 @@ public class GamesListDelegate extends ListDelegateBase
break;
case R.id.games_game_new_from:
dropSels = true; // will select the new game instead
showNotAgainDlgThen( R.string.not_again_newfrom,
makeNotAgainBuilder( R.string.not_again_newfrom,
R.string.key_notagain_newfrom,
Action.NEW_FROM, selRowIDs[0] );
Action.NEW_FROM )
.setParams(selRowIDs[0])
.show();
break;
case R.id.games_game_copy:
final GameSummary smry = GameUtils.getSummary( m_activity,
@ -1898,9 +1904,11 @@ public class GamesListDelegate extends ListDelegateBase
} else {
ActionPair pair = new ActionPair( Action.SET_HIDE_NEWGAME_BUTTONS,
R.string.set_pref );
showNotAgainDlgThen( R.string.not_again_hidenewgamebuttons,
makeNotAgainBuilder( R.string.not_again_hidenewgamebuttons,
R.string.key_notagain_hidenewgamebuttons,
Action.NEW_GAME_PRESSED, pair );
Action.NEW_GAME_PRESSED )
.setParams(pair)
.show();
}
}

View file

@ -29,6 +29,7 @@ import junit.framework.Assert;
import org.eehouse.android.xw4.DlgDelegate.Action;
import org.eehouse.android.xw4.DlgDelegate.ConfirmThenBuilder;
import org.eehouse.android.xw4.DlgDelegate.NotAgainBuilder;
import org.eehouse.android.xw4.loc.LocUtils;
public class PrefsActivity extends PreferenceActivity
@ -109,10 +110,14 @@ public class PrefsActivity extends PreferenceActivity
m_dlgt.showOKOnlyDialog( msg );
}
public void showNotAgainDlgThen( int msgID, int prefsKey,
Action action )
public NotAgainBuilder makeNotAgainBuilder(int msgId, int key, Action action)
{
m_dlgt.showNotAgainDlgThen( msgID, prefsKey, action );
return m_dlgt.makeNotAgainBuilder( msgId, key, action );
}
public NotAgainBuilder makeNotAgainBuilder( int msgId, int key )
{
return m_dlgt.makeNotAgainBuilder( msgId, key );
}
public ConfirmThenBuilder makeConfirmThenBuilder(String msg, Action action)

View file

@ -118,9 +118,10 @@ public class StudyListDelegate extends ListDelegateBase
boolean handled = true;
switch ( item.getItemId() ) {
case R.id.slmenu_copy_sel:
showNotAgainDlgThen( R.string.not_again_studycopy,
makeNotAgainBuilder( R.string.not_again_studycopy,
R.string.key_na_studycopy,
Action.SL_COPY_ACTION );
Action.SL_COPY_ACTION )
.show();
break;
case R.id.slmenu_clear_sel:
String msg = getQuantityString( R.plurals.confirm_studylist_clear_fmt,

View file

@ -97,7 +97,8 @@ public class Toolbar {
{
View.OnClickListener listener = new View.OnClickListener() {
public void onClick( View view ) {
m_dlgDlgt.showNotAgainDlgThen( msgID, prefsKey, action );
m_dlgDlgt.makeNotAgainBuilder( msgID, prefsKey, action )
.show();
}
};
setListener( index, listener );
@ -108,7 +109,8 @@ public class Toolbar {
{
View.OnLongClickListener listener = new View.OnLongClickListener() {
public boolean onLongClick( View view ) {
m_dlgDlgt.showNotAgainDlgThen( msgID, prefsKey, action );
m_dlgDlgt.makeNotAgainBuilder( msgID, prefsKey, action )
.show();
return true;
}
};

View file

@ -68,8 +68,9 @@ public class LocItemEditDelegate extends DelegateBase implements TextWatcher {
TextView view = (TextView)findViewById( R.id.english_view );
m_keyFmts = getFmtSet( key, view );
if ( 0 < m_keyFmts.size() ) {
showNotAgainDlg( R.string.not_again_fmt_expl,
R.string.key_na_fmt_expl );
makeNotAgainBuilder( R.string.not_again_fmt_expl,
R.string.key_na_fmt_expl )
.show();
}
view = (TextView)findViewById( R.id.xlated_view_blessed );