fix group-order storage

This commit is contained in:
Eric House 2020-08-19 09:08:31 -07:00
parent e5eb1aab6e
commit 0e58e24c3d
3 changed files with 57 additions and 71 deletions

View file

@ -147,11 +147,15 @@ public class DbgUtils {
// public static String toString( long[] longs ) // public static String toString( long[] longs )
// { // {
// String result = "";
// if ( null != longs && 0 < longs.length ) {
// String[] asStrs = new String[longs.length]; // String[] asStrs = new String[longs.length];
// for ( int ii = 0; ii < longs.length; ++ii ) { // for ( int ii = 0; ii < longs.length; ++ii ) {
// asStrs[ii] = String.format("%d", longs[ii] ); // asStrs[ii] = String.format("%d", longs[ii] );
// } // }
// return TextUtils.join( ", ", asStrs ); // result = TextUtils.join( ", ", asStrs );
// }
// return result;
// } // }
// public static String toString( Object[] objs ) // public static String toString( Object[] objs )

View file

@ -142,7 +142,7 @@ public class GamesListDelegate extends ListDelegateBase
GameListAdapter() GameListAdapter()
{ {
super( new Class[] { GroupRec.class, GameRec.class } ); super( new Class[] { GroupRec.class, GameRec.class } );
m_groupPositions = checkGroupPositions(); m_groupPositions = null;
} }
protected Object[] makeListData() protected Object[] makeListData()
@ -310,31 +310,36 @@ public class GamesListDelegate extends ListDelegateBase
long[] getGroupPositions() long[] getGroupPositions()
{ {
// do not modify!!!! // do not modify!!!!
final Set<Long> keys = DBUtils.getGroups( m_activity ).keySet(); final Set<Long> dbGroups = DBUtils.getGroups( m_activity ).keySet();
if ( null == m_groupPositions || if ( null == m_groupPositions || m_groupPositions.length != dbGroups.size() ) {
m_groupPositions.length != keys.size() ) { long[] groupPositions = loadGroupPositions();
HashSet<Long> unused = new HashSet<>( keys ); // If the stored order is out-of-sync with the DB, e.g. if
long[] newArray = new long[unused.size()]; // there have been additions or deletions, keep the ordering
// of groups that we have ordering for. Then add the rest.
m_groupPositions = new long[dbGroups.size()];
Set<Long> added = new HashSet<>();
// First copy the existing values, in order
int nextIndx = 0; int nextIndx = 0;
if ( null != m_groupPositions ) { for ( long posn : groupPositions ) {
for ( long id: m_groupPositions ) { if ( dbGroups.contains(posn) ) {
if ( unused.contains( id ) ) { m_groupPositions[nextIndx++] = posn;
newArray[nextIndx++] = id; added.add(posn);
unused.remove( id );
}
} }
} }
// Then copy in what's left // Now add at the end the ones we're missing
Iterator<Long> iter = unused.iterator(); for ( long posn : dbGroups ) {
while ( iter.hasNext() ) { if ( !added.contains(posn) ) {
newArray[nextIndx++] = iter.next(); m_groupPositions[nextIndx++] = posn;
}
}
} else if ( BuildConfig.DEBUG ) {
for ( long posn : m_groupPositions ) {
Assert.assertTrueNR( dbGroups.contains(posn) );
} }
m_groupPositions = newArray;
} }
return m_groupPositions; return m_groupPositions;
} }
@ -354,7 +359,7 @@ public class GamesListDelegate extends ListDelegateBase
long tmp = positions[src]; long tmp = positions[src];
positions[src] = positions[dest]; positions[src] = positions[dest];
positions[dest] = tmp; positions[dest] = tmp;
// DbgUtils.logf( "positions now %s", DbgUtils.toString( positions ) ); storeGroupPositions( positions );
swapGroups( src, dest ); swapGroups( src, dest );
} }
@ -542,36 +547,6 @@ public class GamesListDelegate extends ListDelegateBase
} }
return result; return result;
} }
private long[] checkGroupPositions()
{
long[] result = XWPrefs.getGroupPositions( m_activity );
if ( null != result ) {
final Map<Long,GameGroupInfo> groups =
DBUtils.getGroups( m_activity );
Set<Long> posns = groups.keySet();
if ( result.length != posns.size() ) {
result = null;
} else {
for ( long id : result ) {
if ( ! posns.contains( id ) ) {
result = null;
break;
}
}
}
}
if ( BuildConfig.DEBUG && null != result ) {
List<Long> list = new ArrayList<>();
for ( long ll : result ) {
list.add( ll );
}
Log.d( TAG, "checkGroupPositions() => %s", TextUtils.join(",", list ));
}
return result;
}
} // class GameListAdapter } // class GameListAdapter
private static final int[] DEBUG_ITEMS = { private static final int[] DEBUG_ITEMS = {
@ -1055,7 +1030,7 @@ public class GamesListDelegate extends ListDelegateBase
// mgr.listen( m_phoneStateListener, PhoneStateListener.LISTEN_NONE ); // mgr.listen( m_phoneStateListener, PhoneStateListener.LISTEN_NONE );
// m_phoneStateListener = null; // m_phoneStateListener = null;
long[] positions = m_adapter.getGroupPositions(); long[] positions = m_adapter.getGroupPositions();
XWPrefs.setGroupPositions( m_activity, positions ); storeGroupPositions( positions );
super.onStop(); super.onStop();
} }
@ -1460,7 +1435,7 @@ public class GamesListDelegate extends ListDelegateBase
int id = (Integer)params[0]; int id = (Integer)params[0];
if ( R.id.games_menu_loaddb == id ) { if ( R.id.games_menu_loaddb == id ) {
DBUtils.loadDB( m_activity ); DBUtils.loadDB( m_activity );
XWPrefs.clearGroupPositions( m_activity ); storeGroupPositions( null );
mkListAdapter(); mkListAdapter();
} else if ( R.id.games_menu_storedb == id ) { } else if ( R.id.games_menu_storedb == id ) {
DBUtils.saveDB( m_activity ); DBUtils.saveDB( m_activity );
@ -1890,6 +1865,26 @@ public class GamesListDelegate extends ListDelegateBase
} }
} }
private static final String GROUP_POSNS_KEY = TAG + "/group_posns";
private void storeGroupPositions( long[] posns )
{
// Log.d( TAG, "storeGroupPositions(%s)", DbgUtils.toString(posns) );
DBUtils.setSerializableFor( m_activity, GROUP_POSNS_KEY, posns );
}
private long[] loadGroupPositions()
{
long[] result;
Serializable obj = DBUtils.getSerializableFor( m_activity, GROUP_POSNS_KEY );
if ( null != obj && obj instanceof long[] ) {
result = (long[])obj;
} else {
result = XWPrefs.getGroupPositions( m_activity );
}
// Log.d( TAG, "loadGroupPositions() => %s", DbgUtils.toString(result) );
return result;
}
private void reloadGame( long rowID ) private void reloadGame( long rowID )
{ {
if ( null != m_adapter ) { if ( null != m_adapter ) {

View file

@ -392,20 +392,7 @@ public class XWPrefs {
setPrefsLong( context, R.string.key_default_group, val ); setPrefsLong( context, R.string.key_default_group, val );
} }
public static void clearGroupPositions( Context context ) // PENDING: remove this in a release or two.
{
setPrefsString( context, R.string.key_group_posns, null );
}
public static void setGroupPositions( Context context, long[] posns )
{
String[] asStrs = new String[posns.length];
for ( int ii = 0; ii < posns.length; ++ii ) {
asStrs[ii] = String.format( "%d", posns[ii] );
}
setPrefsStringArray( context, R.string.key_group_posns, asStrs );
}
public static long[] getGroupPositions( Context context ) public static long[] getGroupPositions( Context context )
{ {
long[] posns = null; long[] posns = null;