diff --git a/app/src/main/java/com/quran/labs/androidquran/model/bookmark/BookmarkModel.kt b/app/src/main/java/com/quran/labs/androidquran/model/bookmark/BookmarkModel.kt index b189a54aac..54a5e0ddef 100644 --- a/app/src/main/java/com/quran/labs/androidquran/model/bookmark/BookmarkModel.kt +++ b/app/src/main/java/com/quran/labs/androidquran/model/bookmark/BookmarkModel.kt @@ -95,7 +95,7 @@ open class BookmarkModel @Inject constructor( } bookmarksDBAdapter.bulkDelete(tagsToDelete, bookmarksToDelete, untag) bookmarksPublishSubject.onNext(true) - if (tagsToDelete.size > 0) { + if (tagsToDelete.isNotEmpty()) { tagPublishSubject.onNext(true) } null diff --git a/app/src/main/java/com/quran/labs/androidquran/widget/BookmarksWidgetSubscriber.kt b/app/src/main/java/com/quran/labs/androidquran/widget/BookmarksWidgetSubscriber.kt index 0c7fadfcec..96b5101e6c 100644 --- a/app/src/main/java/com/quran/labs/androidquran/widget/BookmarksWidgetSubscriber.kt +++ b/app/src/main/java/com/quran/labs/androidquran/widget/BookmarksWidgetSubscriber.kt @@ -1,8 +1,14 @@ package com.quran.labs.androidquran.widget +import com.quran.data.dao.BookmarksDao import com.quran.labs.androidquran.model.bookmark.BookmarkModel import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers import io.reactivex.rxjava3.disposables.Disposable +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.MainScope +import kotlinx.coroutines.cancel +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach import javax.inject.Inject import javax.inject.Singleton @@ -12,31 +18,38 @@ import javax.inject.Singleton */ @Singleton class BookmarksWidgetSubscriber @Inject constructor( - private val bookmarkModel: BookmarkModel, - private val bookmarksWidgetUpdater: BookmarksWidgetUpdater + private val bookmarksDao: BookmarksDao, + private val bookmarkModel: BookmarkModel, + private val bookmarksWidgetUpdater: BookmarksWidgetUpdater ) { - + private var scope: CoroutineScope = MainScope() private var bookmarksWidgetDisposable: Disposable? = null fun subscribeBookmarksWidgetIfNecessary() { if (bookmarksWidgetUpdater.checkForAnyBookmarksWidgets()) { subscribeBookmarksWidget() - } + } } private fun subscribeBookmarksWidget() { bookmarksWidgetDisposable = bookmarkModel.bookmarksObservable() - .observeOn(AndroidSchedulers.mainThread()) - .subscribe { bookmarksWidgetUpdater.updateBookmarksWidget() } + .observeOn(AndroidSchedulers.mainThread()) + .subscribe { bookmarksWidgetUpdater.updateBookmarksWidget() } } fun onEnabledBookmarksWidget() { if (bookmarksWidgetDisposable == null) { subscribeBookmarksWidget() } + + scope = MainScope() + bookmarksDao.changes + .onEach { bookmarksWidgetUpdater.updateBookmarksWidget() } + .launchIn(scope) } fun onDisabledBookmarksWidget() { bookmarksWidgetDisposable?.dispose() + scope.cancel() } } diff --git a/common/bookmark/src/main/java/com/quran/mobile/bookmark/model/BookmarksDaoImpl.kt b/common/bookmark/src/main/java/com/quran/mobile/bookmark/model/BookmarksDaoImpl.kt index 98680f91f8..e424605fb3 100644 --- a/common/bookmark/src/main/java/com/quran/mobile/bookmark/model/BookmarksDaoImpl.kt +++ b/common/bookmark/src/main/java/com/quran/mobile/bookmark/model/BookmarksDaoImpl.kt @@ -11,10 +11,14 @@ import com.quran.mobile.bookmark.mapper.Mappers import com.quran.mobile.bookmark.mapper.convergeCommonlyTagged import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.filterNotNull import kotlinx.coroutines.flow.first import kotlinx.coroutines.withContext import javax.inject.Inject +import javax.inject.Singleton +@Singleton class BookmarksDaoImpl @Inject constructor( bookmarksDatabase: BookmarksDatabase ) : BookmarksDao { @@ -22,6 +26,9 @@ class BookmarksDaoImpl @Inject constructor( private val lastPageQueries = bookmarksDatabase.lastPageQueries private val bookmarkTagQueries = bookmarksDatabase.bookmarkTagQueries + private val internalChanges = MutableStateFlow(null) + override val changes: Flow = internalChanges.filterNotNull() + override suspend fun bookmarks(): List { return withContext(Dispatchers.IO) { bookmarkQueries.getBookmarksByDateAdded(Mappers.bookmarkWithTagMapper) @@ -49,6 +56,7 @@ class BookmarksDaoImpl @Inject constructor( bookmarkQueries.update(it.sura, it.ayah, it.page, it.id) } } + internalChanges.value = System.currentTimeMillis() } } @@ -58,6 +66,7 @@ class BookmarksDaoImpl @Inject constructor( if (bookmarkId != null) { bookmarkTagQueries.deleteByBookmarkIds(listOf(bookmarkId)) bookmarkQueries.deleteByIds(listOf(bookmarkId)) + internalChanges.value = System.currentTimeMillis() } } } @@ -82,9 +91,11 @@ class BookmarksDaoImpl @Inject constructor( val bookmarkId = bookmarkIds.firstOrNull() if (bookmarkId != null) { deleteBookmarkById(bookmarkId) + internalChanges.value = System.currentTimeMillis() false } else { bookmarkQueries.addBookmark(null, null, page) + internalChanges.value = System.currentTimeMillis() true } } @@ -96,9 +107,11 @@ class BookmarksDaoImpl @Inject constructor( .executeAsOneOrNull() if (bookmarkId != null) { deleteBookmarkById(bookmarkId) + internalChanges.value = System.currentTimeMillis() false } else { bookmarkQueries.addBookmark(suraAyah.sura, suraAyah.ayah, page) + internalChanges.value = System.currentTimeMillis() true } } @@ -110,6 +123,7 @@ class BookmarksDaoImpl @Inject constructor( bookmarkTagQueries.deleteByBookmarkIds(listOf(bookmarkId)) bookmarkQueries.deleteByIds(listOf(bookmarkId)) } + internalChanges.value = System.currentTimeMillis() } } diff --git a/common/data/src/main/java/com/quran/data/dao/BookmarksDao.kt b/common/data/src/main/java/com/quran/data/dao/BookmarksDao.kt index b6f1fa70ef..a822582cc3 100644 --- a/common/data/src/main/java/com/quran/data/dao/BookmarksDao.kt +++ b/common/data/src/main/java/com/quran/data/dao/BookmarksDao.kt @@ -6,6 +6,8 @@ import com.quran.data.model.bookmark.RecentPage import kotlinx.coroutines.flow.Flow interface BookmarksDao { + val changes: Flow + suspend fun bookmarks(): List fun bookmarksForPage(page: Int): Flow> fun pageBookmarksWithoutTags(): Flow>