update studylist display when words added

Add listener interface to DBUtils and hook StudyListDelegate into it so
that if a word is added while the list is being displayed the new word
shows up immediately.
This commit is contained in:
Eric House 2017-02-04 09:34:07 -08:00
parent 6eb4a9b09b
commit b050ac5d62
2 changed files with 57 additions and 1 deletions

View file

@ -88,6 +88,12 @@ public class DBUtils {
private static HashSet<DBChangeListener> s_listeners =
new HashSet<DBChangeListener>();
public static interface StudyListListener {
void onWordAdded( String word, int langCode );
}
private static Set<StudyListListener> s_slListeners
= new HashSet<StudyListListener>();
private static SQLiteOpenHelper s_dbHelper = null;
public static class Obit {
@ -1896,6 +1902,20 @@ public class DBUtils {
}
}
protected static void addStudyListChangedListener( StudyListListener lnr )
{
synchronized( s_slListeners ) {
s_slListeners.add( lnr );
}
}
protected static void removeStudyListChangedListener( StudyListListener lnr )
{
synchronized( s_slListeners ) {
s_slListeners.remove( lnr );
}
}
public static void loadDB( Context context )
{
copyGameDB( context, false );
@ -2157,6 +2177,7 @@ public class DBUtils {
db.insert( DBHelper.TABLE_NAME_STUDYLIST, null, values );
db.close();
}
notifyStudyListListeners( word, lang );
}
public static int[] studyListLangs( Context context )
@ -2605,6 +2626,15 @@ public class DBUtils {
return result;
}
private static void notifyStudyListListeners( String word, int lang )
{
synchronized( s_slListeners ) {
for ( StudyListListener listener : s_slListeners ) {
listener.onWordAdded( word, lang );
}
}
}
private static void notifyListeners( long rowid, GameChangeType change )
{
synchronized( s_listeners ) {

View file

@ -48,7 +48,8 @@ import java.util.Set;
public class StudyListDelegate extends ListDelegateBase
implements OnItemSelectedListener, SelectableItem,
View.OnLongClickListener, View.OnClickListener {
View.OnLongClickListener, View.OnClickListener,
DBUtils.StudyListListener {
protected static final int NO_LANG = -1;
@ -87,6 +88,20 @@ public class StudyListDelegate extends ListDelegateBase
initOrFinish( getIntent() );
}
@Override
protected void onResume()
{
super.onResume();
DBUtils.addStudyListChangedListener( this );
}
@Override
protected void onPause()
{
DBUtils.removeStudyListChangedListener( this );
super.onPause();
}
@Override
protected boolean handleBackPressed()
{
@ -166,6 +181,17 @@ public class StudyListDelegate extends ListDelegateBase
}
}
//////////////////////////////////////////////////
// DBUtils.StudyListListener
//////////////////////////////////////////////////
@Override
public void onWordAdded( String word, int langCode )
{
if ( langCode == m_langCodes[m_langPosition] ) {
loadList();
}
}
//////////////////////////////////////////////////
// DlgDelegate.DlgClickNotify interface
//////////////////////////////////////////////////