add support for behavior: when the left fragment opens a new fragment

it replaces the right one rather than pushing the left off the
screen. Use that to make dictionary browser, chat, and studylist all
able to swap places while the open board is on the left.
This commit is contained in:
Eric House 2016-03-14 21:48:07 -07:00
parent f23a28cbce
commit 0bc6387714
8 changed files with 78 additions and 55 deletions

View file

@ -83,7 +83,6 @@ public class BoardDelegate extends DelegateBase
private static final String GETDICT = "GETDICT"; private static final String GETDICT = "GETDICT";
private Activity m_activity; private Activity m_activity;
private Delegator m_delegator;
private BoardView m_view; private BoardView m_view;
private GamePtr m_jniGamePtr; private GamePtr m_jniGamePtr;
private GameLock m_gameLock; private GameLock m_gameLock;
@ -581,7 +580,6 @@ public class BoardDelegate extends DelegateBase
{ {
super( delegator, savedInstanceState, R.layout.board, R.menu.board_menu ); super( delegator, savedInstanceState, R.layout.board, R.menu.board_menu );
m_activity = delegator.getActivity(); m_activity = delegator.getActivity();
m_delegator = delegator;
} }
protected void init( Bundle savedInstanceState ) protected void init( Bundle savedInstanceState )
@ -931,7 +929,7 @@ public class BoardDelegate extends DelegateBase
cmd = JNICmd.CMD_TOGGLE_TRAY; cmd = JNICmd.CMD_TOGGLE_TRAY;
break; break;
case R.id.games_menu_study: case R.id.games_menu_study:
StudyListDelegate.launchOrAlert( m_activity, m_gi.dictLang, this ); StudyListDelegate.launchOrAlert( getDelegator(), m_gi.dictLang, this );
break; break;
case R.id.board_menu_game_netstats: case R.id.board_menu_game_netstats:
m_jniThread.handle( JNICmd.CMD_NETSTATS, R.string.netstats_title ); m_jniThread.handle( JNICmd.CMD_NETSTATS, R.string.netstats_title );
@ -950,7 +948,7 @@ public class BoardDelegate extends DelegateBase
// small devices only // small devices only
case R.id.board_menu_dict: case R.id.board_menu_dict:
String dictName = m_gi.dictName( m_view.getCurPlayer() ); String dictName = m_gi.dictName( m_view.getCurPlayer() );
DictBrowseDelegate.launch( m_activity, dictName ); DictBrowseDelegate.launch( getDelegator(), dictName );
break; break;
case R.id.board_menu_game_counts: case R.id.board_menu_game_counts:
@ -1029,11 +1027,11 @@ public class BoardDelegate extends DelegateBase
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( Toolbar.BUTTON_BROWSE_DICT );
if ( Action.BUTTON_BROWSEALL_ACTION == action && if ( Action.BUTTON_BROWSEALL_ACTION == action &&
DictsDelegate.handleDictsPopup( m_activity, button, DictsDelegate.handleDictsPopup( getDelegator(), button,
curDict, m_gi.dictLang ) ){ curDict, m_gi.dictLang ) ){
break; break;
} }
DictBrowseDelegate.launch( m_activity, curDict ); DictBrowseDelegate.launch( getDelegator(), curDict );
break; break;
case PREV_HINT_ACTION: case PREV_HINT_ACTION:
cmd = JNICmd.CMD_PREV_HINT; cmd = JNICmd.CMD_PREV_HINT;
@ -1439,7 +1437,7 @@ public class BoardDelegate extends DelegateBase
String msg = getString( R.string.reload_new_dict_fmt, getDict ); String msg = getString( R.string.reload_new_dict_fmt, getDict );
showToast( msg ); showToast( msg );
m_delegator.finish(); finish();
GameUtils.launchGame( m_activity, m_rowid, false ); GameUtils.launchGame( m_activity, m_rowid, false );
} }
@ -2174,7 +2172,7 @@ public class BoardDelegate extends DelegateBase
} }
} catch ( GameUtils.NoSuchGameException nsge ) { } catch ( GameUtils.NoSuchGameException nsge ) {
DbgUtils.loge( nsge ); DbgUtils.loge( nsge );
m_delegator.finish(); finish();
} }
} }
} // loadGame } // loadGame
@ -2375,7 +2373,7 @@ public class BoardDelegate extends DelegateBase
int curPlayer = XwJNI.board_getSelPlayer( m_jniGamePtr ); int curPlayer = XwJNI.board_getSelPlayer( m_jniGamePtr );
String[] names = m_gi.playerNames(); String[] names = m_gi.playerNames();
boolean[] locs = m_gi.playersLocal(); // to convert old histories boolean[] locs = m_gi.playersLocal(); // to convert old histories
ChatDelegate.startForResult( m_activity, RequestCode.CHAT_REQUEST, ChatDelegate.startForResult( getDelegator(), RequestCode.CHAT_REQUEST,
m_rowid, curPlayer, names, locs ); m_rowid, curPlayer, names, locs );
} }
} }
@ -2656,10 +2654,6 @@ public class BoardDelegate extends DelegateBase
private void doRematchIf() private void doRematchIf()
{ {
// Intent intent = GamesListDelegate.makeRematchIntent( m_activity, m_gi, m_rowid );
// if ( null != intent ) {
// startActivity( intent );
// m_delegator.finish();
if ( doRematchIf( m_activity, this, m_rowid, m_summary, m_gi, if ( doRematchIf( m_activity, this, m_rowid, m_summary, m_gi,
m_jniGamePtr ) ) { m_jniGamePtr ) ) {
finish(); finish();

View file

@ -204,7 +204,8 @@ public class ChatDelegate extends DelegateBase {
} }
public static void startForResult( Activity parent, RequestCode requestCode, public static void startForResult( Delegator delegator,
RequestCode requestCode,
long rowID, int curPlayer, long rowID, int curPlayer,
String[] names, boolean[] locs ) String[] names, boolean[] locs )
{ {
@ -215,12 +216,13 @@ public class ChatDelegate extends DelegateBase {
bundle.putStringArray( INTENT_KEY_NAMES, names ); bundle.putStringArray( INTENT_KEY_NAMES, names );
bundle.putBooleanArray( INTENT_KEY_LOCS, locs ); bundle.putBooleanArray( INTENT_KEY_LOCS, locs );
if ( parent instanceof FragActivity ) { Activity activity = delegator.getActivity();
FragActivity.addFragment( new ChatFrag(), bundle ); if ( activity instanceof FragActivity ) {
FragActivity.addFragment( new ChatFrag(), bundle, delegator );
} else { } else {
Intent intent = new Intent( parent, ChatActivity.class ); Intent intent = new Intent( activity, ChatActivity.class );
intent.putExtras( bundle ); intent.putExtras( bundle );
parent.startActivityForResult( intent, requestCode.ordinal() ); activity.startActivityForResult( intent, requestCode.ordinal() );
} }
} }
} }

View file

@ -147,6 +147,8 @@ public class DelegateBase implements DlgClickNotify,
return m_activity.getIntent(); return m_activity.getIntent();
} }
protected Delegator getDelegator() { return m_delegator; }
protected int getLayoutID() protected int getLayoutID()
{ {
return m_layoutID; return m_layoutID;

View file

@ -366,7 +366,7 @@ public class DictBrowseDelegate extends ListDelegateBase
finish(); // pop fragment stack before adding new (only it doesn't work) finish(); // pop fragment stack before adding new (only it doesn't work)
launch( m_activity, getArguments() ); launch( getDelegator(), getArguments() );
} }
} }
@ -422,29 +422,31 @@ public class DictBrowseDelegate extends ListDelegateBase
m_maxSpinner.setOnItemSelectedListener( this ); m_maxSpinner.setOnItemSelectedListener( this );
} }
private static void launch( Context context, Bundle bundle ) private static void launch( Delegator delegator, Bundle bundle )
{ {
if ( context instanceof FragActivity ) { Activity activity = delegator.getActivity();
FragActivity.addFragment( new DictBrowseFrag(), bundle ); if ( activity instanceof FragActivity ) {
FragActivity.addFragment( new DictBrowseFrag(), bundle, delegator );
} else { } else {
Intent intent = new Intent( context, DictBrowseActivity.class ); Intent intent = new Intent( activity, DictBrowseActivity.class );
intent.putExtras( bundle ); intent.putExtras( bundle );
context.startActivity( intent ); activity.startActivity( intent );
} }
} }
public static void launch( Context caller, String name, public static void launch( Delegator delegator, String name,
DictUtils.DictLoc loc ) DictUtils.DictLoc loc )
{ {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putString( DICT_NAME, name ); bundle.putString( DICT_NAME, name );
bundle.putInt( DICT_LOC, loc.ordinal() ); bundle.putInt( DICT_LOC, loc.ordinal() );
launch( caller, bundle ); launch( delegator, bundle );
} }
public static void launch( Context caller, String name ) public static void launch( Delegator delegator, String name )
{ {
DictUtils.DictLoc loc = DictUtils.getDictLoc( caller, name ); DictUtils.DictLoc loc
launch( caller, name, loc ); = DictUtils.getDictLoc( delegator.getActivity(), name );
launch( delegator, name, loc );
} }
} }

View file

@ -109,13 +109,13 @@ public class DictsDelegate extends ListDelegateBase
private String m_noteNone; private String m_noteNone;
private static interface SafePopup { private static interface SafePopup {
public void doPopup( Context context, View button, public void doPopup( Delegator dlgtor, View button,
String curDict, int lang ); String curDict, int lang );
} }
private static SafePopup s_safePopup = null; private static SafePopup s_safePopup = null;
private static class SafePopupImpl implements SafePopup { private static class SafePopupImpl implements SafePopup {
public void doPopup( final Context context, View button, public void doPopup( final Delegator dlgtor, View button,
String curDict, int lang ) { String curDict, int lang ) {
final HashMap<MenuItem, DictAndLoc> itemData final HashMap<MenuItem, DictAndLoc> itemData
@ -127,12 +127,13 @@ public class DictsDelegate extends ListDelegateBase
{ {
DictAndLoc dal = itemData.get( item ); DictAndLoc dal = itemData.get( item );
DictBrowseDelegate.launch( context, dal.name, DictBrowseDelegate.launch( dlgtor, dal.name,
dal.loc ); dal.loc );
return true; return true;
} }
}; };
Context context = dlgtor.getActivity();
PopupMenu popup = new PopupMenu( context, button ); PopupMenu popup = new PopupMenu( context, button );
Menu menu = popup.getMenu(); Menu menu = popup.getMenu();
@ -565,7 +566,7 @@ public class DictsDelegate extends ListDelegateBase
switchShowingRemote( m_checkbox.isChecked() ); switchShowingRemote( m_checkbox.isChecked() );
} else { } else {
XWListItem item = (XWListItem)view; XWListItem item = (XWListItem)view;
DictBrowseDelegate.launch( m_activity, item.getText(), DictBrowseDelegate.launch( getDelegator(), item.getText(),
(DictLoc)item.getCached() ); (DictLoc)item.getCached() );
} }
} }
@ -1064,9 +1065,10 @@ public class DictsDelegate extends ListDelegateBase
new GetDefaultDictTask( context, lc, lstnr ).execute(); new GetDefaultDictTask( context, lc, lstnr ).execute();
} }
public static boolean handleDictsPopup( Context context, View button, public static boolean handleDictsPopup( Delegator delegator, View button,
String curDict, int lang ) String curDict, int lang )
{ {
Context context = delegator.getActivity();
int nDicts = DictLangCache.getLangCount( context, lang ); int nDicts = DictLangCache.getLangCount( context, lang );
if ( null == s_safePopup && 1 < nDicts ) { if ( null == s_safePopup && 1 < nDicts ) {
int sdkVersion = Integer.valueOf( android.os.Build.VERSION.SDK ); int sdkVersion = Integer.valueOf( android.os.Build.VERSION.SDK );
@ -1077,7 +1079,7 @@ public class DictsDelegate extends ListDelegateBase
boolean canHandle = null != s_safePopup && 1 < nDicts; boolean canHandle = null != s_safePopup && 1 < nDicts;
if ( canHandle ) { if ( canHandle ) {
s_safePopup.doPopup( context, button, curDict, lang ); s_safePopup.doPopup( delegator, button, curDict, lang );
} }
return canHandle; return canHandle;
} }

View file

@ -26,6 +26,7 @@ import android.os.Bundle;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentManager.BackStackEntry;
import android.support.v4.app.FragmentTransaction; import android.support.v4.app.FragmentTransaction;
import android.view.View; import android.view.View;
import android.widget.FrameLayout; import android.widget.FrameLayout;
@ -64,7 +65,7 @@ public class FragActivity extends FragmentActivity
if ( savedInstanceState == null ) { if ( savedInstanceState == null ) {
// In case this activity was started with special instructions from an Intent, // In case this activity was started with special instructions from an Intent,
// pass the Intent's extras to the fragment as arguments // pass the Intent's extras to the fragment as arguments
addFragmentImpl( new GamesListFrag(), getIntent().getExtras() ); addFragmentImpl( new GamesListFrag(), getIntent().getExtras(), null );
} }
} }
@ -134,27 +135,40 @@ public class FragActivity extends FragmentActivity
getSupportFragmentManager().popBackStack(); getSupportFragmentManager().popBackStack();
} }
private void addFragmentImpl( Fragment fragment, Bundle bundle ) private void addFragmentImpl( Fragment fragment, Bundle bundle,
Delegator parent )
{ {
fragment.setArguments( bundle ); fragment.setArguments( bundle );
addFragmentImpl( fragment ); addFragmentImpl( fragment, parent );
} }
private void addFragmentImpl( Fragment fragment ) private void addFragmentImpl( Fragment fragment, Delegator delegator )
{ {
String newName = fragment.getClass().getName(); String newName = fragment.getClass().getName();
boolean replace = false; boolean replace = false;
FragmentManager fm = getSupportFragmentManager(); FragmentManager fm = getSupportFragmentManager();
int fragCount = fm.getBackStackEntryCount(); int fragCount = fm.getBackStackEntryCount();
int contCount = m_root.getChildCount(); int containerCount = m_root.getChildCount();
DbgUtils.logf( "fragCount: %d; contCount: %d", fragCount, contCount ); DbgUtils.logf( "fragCount: %d; containerCount: %d", fragCount, containerCount );
Assert.assertTrue( fragCount == contCount ); // Assert.assertTrue( fragCount == containerCount );
if ( 0 < contCount ) { // Replace IF we're adding something of the same class at right OR if
// we're adding something with the existing left pane as its parent
// (delegator)
if ( 0 < fragCount ) {
FragmentManager.BackStackEntry entry = fm.getBackStackEntryAt( fragCount - 1 ); FragmentManager.BackStackEntry entry = fm.getBackStackEntryAt( fragCount - 1 );
String curName = entry.getName(); String curName = entry.getName();
DbgUtils.logf( "name of last entry: %s", curName ); DbgUtils.logf( "name of last entry: %s", curName );
replace = curName.equals( newName ); replace = curName.equals( newName );
if ( !replace && 1 < fragCount ) {
entry = fm.getBackStackEntryAt( fragCount - 2 );
curName = entry.getName();
String delName = delegator.getClass().getName();
DbgUtils.logf( "comparing %s, %s", curName, delName );
replace = curName.equals( delName );
}
if ( replace ) { if ( replace ) {
fm.popBackStack(); fm.popBackStack();
} }
@ -167,10 +181,10 @@ public class FragActivity extends FragmentActivity
cont.setLayoutParams( new LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f) ); cont.setLayoutParams( new LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f) );
int id = --m_nextID; int id = --m_nextID;
cont.setId( id ); cont.setId( id );
m_root.addView( cont, replace ? contCount - 1 : contCount ); m_root.addView( cont, replace ? containerCount - 1 : containerCount );
if ( !replace && contCount >= m_maxPanes ) { if ( !replace && containerCount >= m_maxPanes ) {
int indx = contCount - m_maxPanes; int indx = containerCount - m_maxPanes;
View child = m_root.getChildAt( indx ); View child = m_root.getChildAt( indx );
child.setVisibility( View.GONE ); child.setVisibility( View.GONE );
@ -205,8 +219,10 @@ public class FragActivity extends FragmentActivity
FragmentManager fm = getSupportFragmentManager(); FragmentManager fm = getSupportFragmentManager();
int hidingId = layout.getId(); int hidingId = layout.getId();
Fragment frag = fm.findFragmentById( hidingId ); Fragment frag = fm.findFragmentById( hidingId );
if ( null != frag ) { // hasn't been popped?
frag.setMenuVisibility( visible ); frag.setMenuVisibility( visible );
} }
}
// Walk all Fragment children and if they care notify of change. // Walk all Fragment children and if they care notify of change.
private void tellOrientationChanged( int orientation ) private void tellOrientationChanged( int orientation )
@ -252,6 +268,12 @@ public class FragActivity extends FragmentActivity
public static void addFragment( Fragment fragment, Bundle bundle ) public static void addFragment( Fragment fragment, Bundle bundle )
{ {
getThis().addFragmentImpl( fragment, bundle ); addFragment( fragment, bundle, null );
}
public static void addFragment( Fragment fragment, Bundle bundle,
Delegator parent )
{
getThis().addFragmentImpl( fragment, bundle, parent );
} }
} }

View file

@ -566,7 +566,6 @@ public class GamesListDelegate extends ListDelegateBase
private Activity m_activity; private Activity m_activity;
private static GamesListDelegate s_self; private static GamesListDelegate s_self;
private GamesListDelegator m_delegator;
private GameListAdapter m_adapter; private GameListAdapter m_adapter;
private Handler m_handler; private Handler m_handler;
private String m_missingDict; private String m_missingDict;
@ -596,7 +595,6 @@ public class GamesListDelegate extends ListDelegateBase
public GamesListDelegate( GamesListDelegator delegator, Bundle sis ) public GamesListDelegate( GamesListDelegator delegator, Bundle sis )
{ {
super( delegator, sis, R.layout.game_list, R.menu.games_list_menu ); super( delegator, sis, R.layout.game_list, R.menu.games_list_menu );
m_delegator = delegator;
m_activity = delegator.getActivity(); m_activity = delegator.getActivity();
m_launchedGames = new HashSet<Long>(); m_launchedGames = new HashSet<Long>();
s_self = this; s_self = this;
@ -1490,7 +1488,7 @@ public class GamesListDelegate extends ListDelegateBase
break; break;
case R.id.games_menu_study: case R.id.games_menu_study:
StudyListDelegate.launchOrAlert( m_activity, StudyListDelegate.NO_LANG, this ); StudyListDelegate.launchOrAlert( getDelegator(), StudyListDelegate.NO_LANG, this );
break; break;
case R.id.games_menu_about: case R.id.games_menu_about:

View file

@ -345,10 +345,11 @@ public class StudyListDelegate extends ListDelegateBase
setTitleBar(); setTitleBar();
} }
public static void launchOrAlert( Activity activity, int lang, public static void launchOrAlert( Delegator delegator, int lang,
DlgDelegate.HasDlgDelegate dlg ) DlgDelegate.HasDlgDelegate dlg )
{ {
String msg = null; String msg = null;
Activity activity = delegator.getActivity();
if ( 0 == DBUtils.studyListLangs( activity ).length ) { if ( 0 == DBUtils.studyListLangs( activity ).length ) {
msg = LocUtils.getString( activity, R.string.study_no_lists ); msg = LocUtils.getString( activity, R.string.study_no_lists );
} else if ( NO_LANG != lang && } else if ( NO_LANG != lang &&
@ -364,7 +365,7 @@ public class StudyListDelegate extends ListDelegateBase
if ( activity instanceof FragActivity ) { if ( activity instanceof FragActivity ) {
StudyListFrag frag = new StudyListFrag(); StudyListFrag frag = new StudyListFrag();
((FragActivity)activity).addFragment( frag, bundle ); ((FragActivity)activity).addFragment( frag, bundle, delegator );
} else { } else {
Intent intent = new Intent( activity, StudyListActivity.class ); Intent intent = new Intent( activity, StudyListActivity.class );
intent.putExtras( bundle ); intent.putExtras( bundle );