fix NPE and let GameSummary stay until replaced

GameListItem was clearing its summary when kicking off a replacement
load. Instead keep the old around in case somebody wants it (e.g. to
figure out what menus to enable) until the reload finishes.

Also add logging of how long loading takes. I think a cache might be
called for.
This commit is contained in:
Eric House 2019-02-05 07:44:16 -08:00 committed by Eric House
parent 6193c376db
commit 122b270025
3 changed files with 20 additions and 8 deletions

View file

@ -131,6 +131,7 @@ public class DBUtils {
public static GameSummary getSummary( Context context, public static GameSummary getSummary( Context context,
GameLock lock ) GameLock lock )
{ {
long startMS = System.currentTimeMillis();
initDB( context ); initDB( context );
GameSummary summary = null; GameSummary summary = null;
String[] columns = { ROW_ID, String[] columns = { ROW_ID,
@ -154,8 +155,8 @@ public class DBUtils {
selection, null, null, null, null ); selection, null, null, null, null );
if ( 1 == cursor.getCount() && cursor.moveToFirst() ) { if ( 1 == cursor.getCount() && cursor.moveToFirst() ) {
summary = new GameSummary(); summary = new GameSummary();
summary.nMoves = cursor.getInt(cursor. summary.nMoves = cursor
getColumnIndex(DBHelper.NUM_MOVES)); .getInt( cursor.getColumnIndex(DBHelper.NUM_MOVES) );
summary.nPlayers = summary.nPlayers =
cursor.getInt(cursor. cursor.getInt(cursor.
getColumnIndex(DBHelper.NUM_PLAYERS)); getColumnIndex(DBHelper.NUM_PLAYERS));
@ -264,6 +265,11 @@ public class DBUtils {
if ( null == summary && lock.canWrite() ) { if ( null == summary && lock.canWrite() ) {
summary = GameUtils.summarize( context, lock ); summary = GameUtils.summarize( context, lock );
} }
long endMS = System.currentTimeMillis();
// Might want to be cacheing this...
Log.d( TAG, "getSummary(rowid=%d) => %s (took %dms)",
lock.getRowid(), summary, endMS - startMS );
return summary; return summary;
} // getSummary } // getSummary

View file

@ -91,9 +91,9 @@ public class GameListItem extends LinearLayout
m_dsdel = new DrawSelDelegate( this ); m_dsdel = new DrawSelDelegate( this );
} }
// Might return null!!
public GameSummary getSummary() public GameSummary getSummary()
{ {
Assert.assertNotNull( m_summary );
return m_summary; return m_summary;
} }
@ -111,7 +111,6 @@ public class GameListItem extends LinearLayout
public void forceReload() public void forceReload()
{ {
// DbgUtils.logf( "GameListItem.forceReload: rowid=%d", m_rowid ); // DbgUtils.logf( "GameListItem.forceReload: rowid=%d", m_rowid );
m_summary = null;
setLoaded( false ); setLoaded( false );
// Apparently it's impossible to reliably cancel an existing // Apparently it's impossible to reliably cancel an existing
// AsyncTask, so let it complete, but drop the results as soon // AsyncTask, so let it complete, but drop the results as soon

View file

@ -1624,6 +1624,7 @@ public class GamesListDelegate extends ListDelegateBase
return handled;// || super.onOptionsItemSelected( item ); return handled;// || super.onOptionsItemSelected( item );
} }
@Override
public void onCreateContextMenu( ContextMenu menu, View view, public void onCreateContextMenu( ContextMenu menu, View view,
ContextMenuInfo menuInfo ) ContextMenuInfo menuInfo )
{ {
@ -1664,9 +1665,15 @@ public class GamesListDelegate extends ListDelegateBase
enable = BoardDelegate.rematchSupported( m_activity, rowID ); enable = BoardDelegate.rematchSupported( m_activity, rowID );
Utils.setItemVisible( menu, R.id.games_game_rematch, enable ); Utils.setItemVisible( menu, R.id.games_game_rematch, enable );
boolean isMultiGame = item.getSummary().isMultiGame(); // Deal with possibility summary's temporarily null....
GameSummary summary = item.getSummary();
enable = false;
boolean isMultiGame = false;
if ( null != summary ) {
isMultiGame = summary.isMultiGame();
enable = isMultiGame enable = isMultiGame
&& (BuildConfig.DEBUG || XWPrefs.getDebugEnabled( m_activity )); && (BuildConfig.DEBUG || XWPrefs.getDebugEnabled( m_activity ));
}
Utils.setItemVisible( menu, R.id.games_game_invites, enable ); Utils.setItemVisible( menu, R.id.games_game_invites, enable );
Utils.setItemVisible( menu, R.id.games_game_netstats, isMultiGame ); Utils.setItemVisible( menu, R.id.games_game_netstats, isMultiGame );
@ -1675,7 +1682,7 @@ public class GamesListDelegate extends ListDelegateBase
Utils.setItemVisible( menu, R.id.games_game_reset, enable ); Utils.setItemVisible( menu, R.id.games_game_reset, enable );
} }
} }
} } // onCreateContextMenu
public boolean onContextItemSelected( MenuItem item ) public boolean onContextItemSelected( MenuItem item )
{ {