From b050ac5d62eeb59979a27fd51b6108909d17cc47 Mon Sep 17 00:00:00 2001 From: Eric House Date: Sat, 4 Feb 2017 09:34:07 -0800 Subject: [PATCH] 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. --- .../java/org/eehouse/android/xw4/DBUtils.java | 30 +++++++++++++++++++ .../android/xw4/StudyListDelegate.java | 28 ++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DBUtils.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DBUtils.java index 8b849f3e1..edb25c0fa 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DBUtils.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DBUtils.java @@ -88,6 +88,12 @@ public class DBUtils { private static HashSet s_listeners = new HashSet(); + public static interface StudyListListener { + void onWordAdded( String word, int langCode ); + } + private static Set s_slListeners + = new HashSet(); + 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 ) { diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/StudyListDelegate.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/StudyListDelegate.java index 3b5aa1c49..5096b1b48 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/StudyListDelegate.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/StudyListDelegate.java @@ -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 //////////////////////////////////////////////////