remember prev dict choice and use for longtap

Often I want to view a different wordlist from what's in use in the
game. So make the in-board long-tap shortcut to a wordlist remember what
the short-tap gesture chose most recently and use that instead of
assuming the game's list is what's wanted. Remember the choice on a
per-language rather than per-game basis.
This commit is contained in:
Eric House 2021-03-20 13:29:22 -07:00
parent e1712f4560
commit 677abe6fb8
2 changed files with 38 additions and 13 deletions

View file

@ -1009,10 +1009,15 @@ public class BoardDelegate extends DelegateBase
View button = m_toolbar.getButtonFor( Buttons.BUTTON_BROWSE_DICT ); View button = m_toolbar.getButtonFor( 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 ) ) {
break; // do nothing
} else {
String selDict = DictsDelegate.prevSelFor( m_activity, m_gi.dictLang );
if ( null == selDict ) {
selDict = curDict;
}
DictBrowseDelegate.launch( getDelegator(), selDict );
} }
DictBrowseDelegate.launch( getDelegator(), curDict );
break; break;
case PREV_HINT_ACTION: case PREV_HINT_ACTION:
cmd = JNICmd.CMD_PREV_HINT; cmd = JNICmd.CMD_PREV_HINT;

View file

@ -119,43 +119,52 @@ public class DictsDelegate extends ListDelegateBase
private static class SafePopupImpl implements SafePopup { private static class SafePopupImpl implements SafePopup {
public void doPopup( final Delegator dlgtor, View button, public void doPopup( final Delegator dlgtor, View button,
String curDict, int lang ) { String curDict, final int lang ) {
final HashMap<MenuItem, DictAndLoc> itemData final HashMap<MenuItem, DictAndLoc> itemData
= new HashMap<>(); = new HashMap<>();
final Context context = dlgtor.getActivity();
MenuItem.OnMenuItemClickListener listener = MenuItem.OnMenuItemClickListener listener =
new MenuItem.OnMenuItemClickListener() { new MenuItem.OnMenuItemClickListener() {
public boolean onMenuItemClick( MenuItem item ) public boolean onMenuItemClick( MenuItem item )
{ {
DictAndLoc dal = itemData.get( item ); DictAndLoc dal = itemData.get( item );
String prevKey = keyForLang( lang );
DBUtils.setStringFor( context, prevKey, dal.name );
DictBrowseDelegate.launch( dlgtor, dal.name, DictBrowseDelegate.launch( dlgtor, dal.name,
dal.loc ); dal.loc );
return true; return true;
} }
}; };
Context context = dlgtor.getActivity(); String prevSel = prevSelFor( context, lang );
PopupMenu popup = new PopupMenu( context, button ); PopupMenu popup = new PopupMenu( context, button );
Menu menu = popup.getMenu(); Menu menu = popup.getMenu();
// Add at top but save until have dal info // Add at top but save until have dal info
MenuItem curItem = MenuItem curItem = addItem( menu,
menu.add( LocUtils.getString( context, LocUtils.getString( context,
R.string.cur_menu_marker_fmt, R.string.cur_menu_marker_fmt,
curDict ) ); curDict ) );
DictAndLoc[] dals = DictLangCache.getDALsHaveLang( context, lang ); DictAndLoc[] dals = DictLangCache.getDALsHaveLang( context, lang );
for ( DictAndLoc dal : dals ) { for ( DictAndLoc dal : dals ) {
MenuItem item = dal.name.equals(curDict) boolean isCur = dal.name.equals(curDict);
? curItem : menu.add( dal.name ); MenuItem item = isCur ? curItem : addItem( menu, dal.name );
item.setOnMenuItemClickListener( listener ); item.setOnMenuItemClickListener( listener );
itemData.put( item, dal ); itemData.put( item, dal );
item.setChecked( dal.name.equals(prevSel) );
} }
menu.setGroupCheckable( FAKE_GROUP, true, true );
popup.show(); popup.show();
} }
private static final int FAKE_GROUP = 101;
private MenuItem addItem(Menu menu, String name)
{
return menu.add( FAKE_GROUP, Menu.NONE, Menu.NONE, name );
}
} }
private static class DictInfo implements Comparable, Serializable { private static class DictInfo implements Comparable, Serializable {
@ -1120,6 +1129,17 @@ public class DictsDelegate extends ListDelegateBase
return canHandle; return canHandle;
} }
private static String keyForLang(int lang)
{
return String.format( "%s:lang=%d", TAG, lang );
}
static String prevSelFor( Context context, int lang )
{
String key = keyForLang( lang );
return DBUtils.getStringFor( context, key, null );
}
@Override @Override
protected DictsDelegate curThis() protected DictsDelegate curThis()
{ {