Skip to content

Commit

Permalink
Fix bookmarks widget not updating
Browse files Browse the repository at this point in the history
This patch fixes a bug where the bookmarks widgets were no longer being
updated due to the recent refactor of bookmarks. Since the deprecated
BookmarkModel was being watched for changes, and since most new code
updates BookmarksDao instead, the widget never got the changes. This
patch fixes it so that the widgets update whenever either get updates.
  • Loading branch information
ahmedre committed Dec 5, 2024
1 parent 46437ef commit ad07fc0
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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()
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,24 @@ 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 {
private val bookmarkQueries = bookmarksDatabase.bookmarkQueries
private val lastPageQueries = bookmarksDatabase.lastPageQueries
private val bookmarkTagQueries = bookmarksDatabase.bookmarkTagQueries

private val internalChanges = MutableStateFlow<Long?>(null)
override val changes: Flow<Long> = internalChanges.filterNotNull()

override suspend fun bookmarks(): List<Bookmark> {
return withContext(Dispatchers.IO) {
bookmarkQueries.getBookmarksByDateAdded(Mappers.bookmarkWithTagMapper)
Expand Down Expand Up @@ -49,6 +56,7 @@ class BookmarksDaoImpl @Inject constructor(
bookmarkQueries.update(it.sura, it.ayah, it.page, it.id)
}
}
internalChanges.value = System.currentTimeMillis()
}
}

Expand All @@ -58,6 +66,7 @@ class BookmarksDaoImpl @Inject constructor(
if (bookmarkId != null) {
bookmarkTagQueries.deleteByBookmarkIds(listOf(bookmarkId))
bookmarkQueries.deleteByIds(listOf(bookmarkId))
internalChanges.value = System.currentTimeMillis()
}
}
}
Expand All @@ -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
}
}
Expand All @@ -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
}
}
Expand All @@ -110,6 +123,7 @@ class BookmarksDaoImpl @Inject constructor(
bookmarkTagQueries.deleteByBookmarkIds(listOf(bookmarkId))
bookmarkQueries.deleteByIds(listOf(bookmarkId))
}
internalChanges.value = System.currentTimeMillis()
}
}

Expand Down
2 changes: 2 additions & 0 deletions common/data/src/main/java/com/quran/data/dao/BookmarksDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.quran.data.model.bookmark.RecentPage
import kotlinx.coroutines.flow.Flow

interface BookmarksDao {
val changes: Flow<Long>

suspend fun bookmarks(): List<Bookmark>
fun bookmarksForPage(page: Int): Flow<List<Bookmark>>
fun pageBookmarksWithoutTags(): Flow<List<Bookmark>>
Expand Down

0 comments on commit ad07fc0

Please sign in to comment.