load summaries for game list items via async tasks whenever a cached

View isn't present.  Fixes failure to redraw after changing language,
and also should speed inital load.  Needs testing.
This commit is contained in:
Andy2 2011-04-23 22:58:11 -07:00
parent e9fadd4d96
commit 9d973cca15
2 changed files with 100 additions and 51 deletions

View file

@ -21,13 +21,14 @@ package org.eehouse.android.xw4;
import android.widget.ListAdapter;
import android.content.Context;
import android.os.AsyncTask;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.database.DataSetObserver;
import java.io.FileInputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.HashMap; // class is not synchronized
import java.text.DateFormat;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
@ -43,56 +44,42 @@ public class GameListAdapter extends XWListAdapter {
private LayoutInflater m_factory;
private HashMap<String,View> m_viewsCache;
private DateFormat m_df;
private LoadItemCB m_cb;
public GameListAdapter( Context context ) {
super( context, DBUtils.gamesList(context).length );
m_context = context;
m_factory = LayoutInflater.from( context );
m_df = DateFormat.getDateTimeInstance( DateFormat.SHORT,
DateFormat.SHORT );
int sdk_int = 0;
try {
sdk_int = Integer.decode( android.os.Build.VERSION.SDK );
} catch ( Exception ex ) {}
m_viewsCache = new HashMap<String,View>();
public interface LoadItemCB {
public void itemLoaded( String path );
}
public int getCount() {
return DBUtils.gamesList(m_context).length;
}
public Object getItem( int position )
private class LoadItemTask extends AsyncTask<Void, Void, Void> {
private String m_path;
private Context m_context;
public LoadItemTask( Context context, String path )
{
final String path = DBUtils.gamesList(m_context)[position];
View layout = m_viewsCache.get( path );
m_context = context;
m_path = path;
}
if ( null == layout ) {
layout = m_factory.inflate( R.layout.game_list_item, null );
final boolean hideTitle =
false;//CommonPrefs.getHideTitleBar( m_context );
GameSummary summary = DBUtils.getSummary( m_context, path, false );
@Override
protected Void doInBackground( Void... unused )
{
View layout = m_factory.inflate( R.layout.game_list_item, null );
boolean hideTitle = false;//CommonPrefs.getHideTitleBar(m_context);
GameSummary summary = DBUtils.getSummary( m_context, m_path, true );
Assert.assertNotNull( summary );
TextView view = (TextView)layout.findViewById( R.id.game_name );
if ( hideTitle ) {
view.setVisibility( View.GONE );
} else {
String text = GameUtils.gameName( m_context, path );
if ( null != summary ) {
String name = GameUtils.gameName( m_context, m_path );
String format =
m_context.getString( R.string.str_game_namef );
String lang =
DictLangCache.getLangName( m_context,
summary.dictLang );
text = String.format( format, text, lang );
}
view.setText( text );
view.setText( String.format( format, name, lang ) );
}
// If we can't read the summary right now we still need to
// return a view but shouldn't cache it
if ( null != summary ) {
LinearLayout list =
(LinearLayout)layout.findViewById( R.id.player_list );
for ( int ii = 0; ii < summary.nPlayers; ++ii ) {
@ -124,8 +111,61 @@ public class GameListAdapter extends XWListAdapter {
View marker = layout.findViewById( R.id.msg_marker );
marker.setVisibility( View.VISIBLE );
}
m_viewsCache.put( path, layout );
synchronized( m_viewsCache ) {
m_viewsCache.put( m_path, layout );
}
return null;
} // doInBackground
@Override
protected void onPostExecute( Void unused )
{
m_cb.itemLoaded( m_path );
}
} // class LoadItemTask
public GameListAdapter( Context context, LoadItemCB cb ) {
super( context, DBUtils.gamesList(context).length );
m_context = context;
m_cb = cb;
m_factory = LayoutInflater.from( context );
m_df = DateFormat.getDateTimeInstance( DateFormat.SHORT,
DateFormat.SHORT );
int sdk_int = 0;
try {
sdk_int = Integer.decode( android.os.Build.VERSION.SDK );
} catch ( Exception ex ) {}
m_viewsCache = new HashMap<String,View>();
}
public int getCount() {
return DBUtils.gamesList(m_context).length;
}
public Object getItem( int position )
{
final String path = DBUtils.gamesList(m_context)[position];
View layout;
synchronized( m_viewsCache ) {
layout = m_viewsCache.get( path );
}
if ( null == layout ) {
layout = m_factory.inflate( R.layout.game_list_item, null );
final boolean hideTitle =
false;//CommonPrefs.getHideTitleBar( m_context );
TextView view = (TextView)layout.findViewById( R.id.game_name );
if ( hideTitle ) {
view.setVisibility( View.GONE );
} else {
String text = GameUtils.gameName( m_context, path );
view.setText( text );
}
new LoadItemTask( m_context, path ).execute();
}
// this doesn't work. Rather, it breaks highlighting because
@ -148,6 +188,8 @@ public class GameListAdapter extends XWListAdapter {
public void inval( String key )
{
synchronized( m_viewsCache ) {
m_viewsCache.remove( key );
}
}
}

View file

@ -50,7 +50,8 @@ import org.eehouse.android.xw4.jni.*;
public class GamesList extends XWListActivity
implements DispatchNotify.HandleRelaysIface,
DBUtils.DBChangeListener {
DBUtils.DBChangeListener,
GameListAdapter.LoadItemCB {
private static final int WARN_NODICT = DlgDelegate.DIALOG_LAST + 1;
private static final int WARN_NODICT_SUBST = WARN_NODICT + 1;
@ -201,7 +202,7 @@ public class GamesList extends XWListActivity
GameConverter.convert( this );
m_adapter = new GameListAdapter( this );
m_adapter = new GameListAdapter( this, this );
setListAdapter( m_adapter );
NetUtils.informOfDeaths( this );
@ -281,6 +282,12 @@ public class GamesList extends XWListActivity
} );
}
// GameListAdapter.LoadItemCB interface
public void itemLoaded( String path )
{
onContentChanged();
}
@Override
public void onCreateContextMenu( ContextMenu menu, View view,
ContextMenuInfo menuInfo )