cleanup: constants + parallel array => enum

I didn't always know enums could have constructors
This commit is contained in:
Eric House 2016-08-15 16:01:30 -07:00
parent 71ac6062f1
commit ab24140813
2 changed files with 43 additions and 59 deletions

View file

@ -46,6 +46,7 @@ import junit.framework.Assert;
import org.eehouse.android.xw4.DBUtils.SentInvitesInfo; import org.eehouse.android.xw4.DBUtils.SentInvitesInfo;
import org.eehouse.android.xw4.DlgDelegate.Action; import org.eehouse.android.xw4.DlgDelegate.Action;
import org.eehouse.android.xw4.Toolbar.Buttons;
import org.eehouse.android.xw4.jni.CommonPrefs; import org.eehouse.android.xw4.jni.CommonPrefs;
import org.eehouse.android.xw4.jni.CommsAddrRec; import org.eehouse.android.xw4.jni.CommsAddrRec;
import org.eehouse.android.xw4.jni.CommsAddrRec.CommsConnType; import org.eehouse.android.xw4.jni.CommsAddrRec.CommsConnType;
@ -1033,7 +1034,7 @@ public class BoardDelegate extends DelegateBase
case BUTTON_BROWSEALL_ACTION: case BUTTON_BROWSEALL_ACTION:
case BUTTON_BROWSE_ACTION: case BUTTON_BROWSE_ACTION:
String curDict = m_gi.dictName( m_view.getCurPlayer() ); String curDict = m_gi.dictName( m_view.getCurPlayer() );
View button = m_toolbar.getViewFor( Toolbar.BUTTON_BROWSE_DICT ); View button = m_toolbar.getViewFor( Buttons.BUTTON_BROWSE_DICT );
if ( Action.BUTTON_BROWSEALL_ACTION == action && if ( Action.BUTTON_BROWSEALL_ACTION == action &&
DictsDelegate.handleDictsPopup( getDelegator(), button, DictsDelegate.handleDictsPopup( getDelegator(), button,
curDict, m_gi.dictLang ) ){ curDict, m_gi.dictLang ) ){
@ -2225,43 +2226,43 @@ public class BoardDelegate extends DelegateBase
private void populateToolbar() private void populateToolbar()
{ {
if ( null != m_toolbar ) { if ( null != m_toolbar ) {
m_toolbar.setListener( Toolbar.BUTTON_BROWSE_DICT, m_toolbar.setListener( Buttons.BUTTON_BROWSE_DICT,
R.string.not_again_browseall, R.string.not_again_browseall,
R.string.key_na_browseall, R.string.key_na_browseall,
Action.BUTTON_BROWSEALL_ACTION ); Action.BUTTON_BROWSEALL_ACTION );
m_toolbar.setLongClickListener( Toolbar.BUTTON_BROWSE_DICT, m_toolbar.setLongClickListener( Buttons.BUTTON_BROWSE_DICT,
R.string.not_again_browse, R.string.not_again_browse,
R.string.key_na_browse, R.string.key_na_browse,
Action.BUTTON_BROWSE_ACTION ); Action.BUTTON_BROWSE_ACTION );
m_toolbar.setListener( Toolbar.BUTTON_HINT_PREV, m_toolbar.setListener( Buttons.BUTTON_HINT_PREV,
R.string.not_again_hintprev, R.string.not_again_hintprev,
R.string.key_notagain_hintprev, R.string.key_notagain_hintprev,
Action.PREV_HINT_ACTION ); Action.PREV_HINT_ACTION );
m_toolbar.setListener( Toolbar.BUTTON_HINT_NEXT, m_toolbar.setListener( Buttons.BUTTON_HINT_NEXT,
R.string.not_again_hintnext, R.string.not_again_hintnext,
R.string.key_notagain_hintnext, R.string.key_notagain_hintnext,
Action.NEXT_HINT_ACTION ); Action.NEXT_HINT_ACTION );
m_toolbar.setListener( Toolbar.BUTTON_JUGGLE, m_toolbar.setListener( Buttons.BUTTON_JUGGLE,
R.string.not_again_juggle, R.string.not_again_juggle,
R.string.key_notagain_juggle, R.string.key_notagain_juggle,
Action.JUGGLE_ACTION ); Action.JUGGLE_ACTION );
m_toolbar.setListener( Toolbar.BUTTON_FLIP, m_toolbar.setListener( Buttons.BUTTON_FLIP,
R.string.not_again_flip, R.string.not_again_flip,
R.string.key_notagain_flip, R.string.key_notagain_flip,
Action.FLIP_ACTION ); Action.FLIP_ACTION );
m_toolbar.setListener( Toolbar.BUTTON_ZOOM, m_toolbar.setListener( Buttons.BUTTON_ZOOM,
R.string.not_again_zoom, R.string.not_again_zoom,
R.string.key_notagain_zoom, R.string.key_notagain_zoom,
Action.ZOOM_ACTION ); Action.ZOOM_ACTION );
m_toolbar.setListener( Toolbar.BUTTON_VALUES, m_toolbar.setListener( Buttons.BUTTON_VALUES,
R.string.not_again_values, R.string.not_again_values,
R.string.key_na_values, R.string.key_na_values,
Action.VALUES_ACTION ); Action.VALUES_ACTION );
m_toolbar.setListener( Toolbar.BUTTON_UNDO, m_toolbar.setListener( Buttons.BUTTON_UNDO,
R.string.not_again_undo, R.string.not_again_undo,
R.string.key_notagain_undo, R.string.key_notagain_undo,
Action.UNDO_ACTION ); Action.UNDO_ACTION );
m_toolbar.setListener( Toolbar.BUTTON_CHAT, m_toolbar.setListener( Buttons.BUTTON_CHAT,
R.string.not_again_chat, R.string.not_again_chat,
R.string.key_notagain_chat, R.string.key_notagain_chat,
Action.CHAT_ACTION ); Action.CHAT_ACTION );
@ -2496,14 +2497,14 @@ public class BoardDelegate extends DelegateBase
private void updateToolbar() private void updateToolbar()
{ {
if ( null != m_toolbar ) { if ( null != m_toolbar ) {
m_toolbar.update( Toolbar.BUTTON_FLIP, m_gsi.visTileCount >= 1 ); m_toolbar.update( Buttons.BUTTON_FLIP, m_gsi.visTileCount >= 1 );
m_toolbar.update( Toolbar.BUTTON_VALUES, m_gsi.visTileCount >= 1 ); m_toolbar.update( Buttons.BUTTON_VALUES, m_gsi.visTileCount >= 1 );
m_toolbar.update( Toolbar.BUTTON_JUGGLE, m_gsi.canShuffle ); m_toolbar.update( Buttons.BUTTON_JUGGLE, m_gsi.canShuffle );
m_toolbar.update( Toolbar.BUTTON_UNDO, m_gsi.canRedo ); m_toolbar.update( Buttons.BUTTON_UNDO, m_gsi.canRedo );
m_toolbar.update( Toolbar.BUTTON_HINT_PREV, m_gsi.canHint ); m_toolbar.update( Buttons.BUTTON_HINT_PREV, m_gsi.canHint );
m_toolbar.update( Toolbar.BUTTON_HINT_NEXT, m_gsi.canHint ); m_toolbar.update( Buttons.BUTTON_HINT_NEXT, m_gsi.canHint );
m_toolbar.update( Toolbar.BUTTON_CHAT, m_gsi.canChat ); m_toolbar.update( Buttons.BUTTON_CHAT, m_gsi.canChat );
m_toolbar.update( Toolbar.BUTTON_BROWSE_DICT, m_toolbar.update( Buttons.BUTTON_BROWSE_DICT,
null != m_gi.dictName( m_view.getCurPlayer() ) ); null != m_gi.dictName( m_view.getCurPlayer() ) );
} }
} }

View file

@ -34,35 +34,21 @@ import org.eehouse.android.xw4.DlgDelegate.HasDlgDelegate;
import org.eehouse.android.xw4.loc.LocUtils; import org.eehouse.android.xw4.loc.LocUtils;
public class Toolbar { public class Toolbar {
public static final int BUTTON_BROWSE_DICT = 0; public enum Buttons {
public static final int BUTTON_HINT_PREV = 1; BUTTON_BROWSE_DICT(R.id.dictlist_button),
public static final int BUTTON_HINT_NEXT = 2; BUTTON_HINT_PREV(R.id.prevhint_button),
public static final int BUTTON_FLIP = 3; BUTTON_HINT_NEXT(R.id.nexthint_button),
public static final int BUTTON_JUGGLE = 4; BUTTON_FLIP(R.id.flip_button),
public static final int BUTTON_ZOOM = 5; BUTTON_JUGGLE(R.id.shuffle_button),
public static final int BUTTON_UNDO = 6; BUTTON_ZOOM(R.id.zoom_button),
public static final int BUTTON_CHAT = 7; BUTTON_UNDO(R.id.undo_button),
public static final int BUTTON_VALUES = 8; BUTTON_CHAT(R.id.chat_button),
BUTTON_VALUES(R.id.values_button)
;
private static int[] s_buttonInfo = { private int m_id;
// BUTTON_BROWSE_DICT private Buttons(int id) { m_id = id; }
R.id.dictlist_button, public int getResId() { return m_id; }
// BUTTON_HINT_PREV
R.id.prevhint_button,
// BUTTON_HINT_NEXT
R.id.nexthint_button,
// BUTTON_FLIP
R.id.flip_button,
// BUTTON_JUGGLE
R.id.shuffle_button,
// BUTTON_ZOOM
R.id.zoom_button,
// BUTTON_UNDO
R.id.undo_button,
// BUTTON_CHAT
R.id.chat_button,
// BUTTON_VALUES
R.id.values_button,
}; };
private Activity m_activity; private Activity m_activity;
@ -85,14 +71,12 @@ public class Toolbar {
} }
} }
public ImageButton getViewFor( int index ) public ImageButton getViewFor( Buttons index )
{ {
int id = s_buttonInfo[index]; return (ImageButton)m_activity.findViewById( index.getResId() );
ImageButton button = (ImageButton)m_activity.findViewById( id );
return button;
} }
public void setListener( int index, final int msgID, final int prefsKey, public void setListener( Buttons index, final int msgID, final int prefsKey,
final Action action ) final Action action )
{ {
View.OnClickListener listener = new View.OnClickListener() { View.OnClickListener listener = new View.OnClickListener() {
@ -104,7 +88,7 @@ public class Toolbar {
setListener( index, listener ); setListener( index, listener );
} }
public void setLongClickListener( int index, final int msgID, public void setLongClickListener( Buttons index, final int msgID,
final int prefsKey, final Action action ) final int prefsKey, final Action action )
{ {
View.OnLongClickListener listener = new View.OnLongClickListener() { View.OnLongClickListener listener = new View.OnLongClickListener() {
@ -125,13 +109,12 @@ public class Toolbar {
} }
} }
public void update( int index, boolean enable ) public void update( Buttons index, boolean enable )
{ {
int vis = enable ? View.VISIBLE : View.GONE; int id = index.getResId();
int id = s_buttonInfo[index];
ImageButton button = (ImageButton)m_activity.findViewById( id ); ImageButton button = (ImageButton)m_activity.findViewById( id );
if ( null != button ) { if ( null != button ) {
button.setVisibility( vis ); button.setVisibility( enable ? View.VISIBLE : View.GONE );
} }
} }
@ -157,7 +140,7 @@ public class Toolbar {
m_layout.setVisibility( m_visible? View.VISIBLE : View.GONE ); m_layout.setVisibility( m_visible? View.VISIBLE : View.GONE );
} }
private void setListener( int index, View.OnClickListener listener ) private void setListener( Buttons index, View.OnClickListener listener )
{ {
ImageButton button = getViewFor( index ); ImageButton button = getViewFor( index );
if ( null != button ) { if ( null != button ) {
@ -165,7 +148,7 @@ public class Toolbar {
} }
} }
private void setLongClickListener( int index, private void setLongClickListener( Buttons index,
View.OnLongClickListener listener ) View.OnLongClickListener listener )
{ {
ImageButton button = getViewFor( index ); ImageButton button = getViewFor( index );