Skip to content

Commit

Permalink
Improve feed items sync
Browse files Browse the repository at this point in the history
  • Loading branch information
prof18 committed Jul 15, 2024
1 parent 663ac8f commit 7ac2b82
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,6 @@ class DatabaseHelper(
}
}

suspend fun getAllFeedItemIds(): List<FeedItemId> = withContext(backgroundDispatcher) {
dbRef.feedItemQueries.selectAllUrlHashes().executeAsList().map { FeedItemId(it) }
}

private suspend fun Transacter.transactionWithContext(
coroutineContext: CoroutineContext,
noEnclosing: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.prof18.feedflow.feedsync.database.data
import app.cash.sqldelight.Transacter
import app.cash.sqldelight.TransactionWithoutReturn
import app.cash.sqldelight.db.SqlDriver
import co.touchlab.stately.concurrency.AtomicReference
import co.touchlab.stately.concurrency.value
import com.prof18.feedflow.core.model.CategoryId
import com.prof18.feedflow.core.model.FeedItemId
import com.prof18.feedflow.core.model.FeedSource
Expand All @@ -23,24 +25,24 @@ class SyncedDatabaseHelper(
private val backgroundDispatcher: CoroutineDispatcher,
) : KoinComponent {

private var dbRef: FeedFlowFeedSyncDB? = null
private var dbRef: AtomicReference<FeedFlowFeedSyncDB?> = AtomicReference(null)

private fun getDbRef(): FeedFlowFeedSyncDB {
if (dbRef == null) {
if (dbRef.value == null) {
val scope = getKoin().getOrCreateScope(FEED_SYNC_SCOPE_NAME, named(FEED_SYNC_SCOPE_NAME))

val driver = scope.get<SqlDriver>(qualifier = named(SYNC_DB_DRIVER))
dbRef = FeedFlowFeedSyncDB(driver)
dbRef.set(FeedFlowFeedSyncDB(driver))
}
return requireNotNull(dbRef)
return requireNotNull(dbRef.get())
}

fun closeScope() {
val scope = getKoin().getOrCreateScope(FEED_SYNC_SCOPE_NAME, named(FEED_SYNC_SCOPE_NAME))
val driver = scope.get<SqlDriver>(qualifier = named(SYNC_DB_DRIVER))
driver.close()
scope.close()
dbRef = null
dbRef.set(null)
}

suspend fun insertSyncedFeedSource(sources: List<FeedSource>) {
Expand Down Expand Up @@ -148,7 +150,7 @@ class SyncedDatabaseHelper(
suspend fun insertFeedItems(feedItems: List<SyncedFeedItem>) =
getDbRef().transactionWithContext(backgroundDispatcher) {
feedItems.forEach { feedItem ->
getDbRef().syncedFeedItemQueries.insertOrIgnoreSyncedFeedItem(
getDbRef().syncedFeedItemQueries.insertOrReplaceSyncedFeedItem(
url_hash = feedItem.id,
is_read = feedItem.isRead,
is_bookmarked = feedItem.isBookmarked,
Expand All @@ -169,34 +171,6 @@ class SyncedDatabaseHelper(
updateMetadata(SyncTable.SYNCED_FEED_ITEM)
}

suspend fun setFeedItemAsRead(feedItemId: FeedItemId, isRead: Boolean) =
getDbRef().transactionWithContext(backgroundDispatcher) {
getDbRef().syncedFeedItemQueries.insertOrIgnoreSyncedFeedItem(
url_hash = feedItemId.id,
is_read = isRead,
is_bookmarked = false,
)
getDbRef().syncedFeedItemQueries.updateIsRead(
isRead = isRead,
urlHash = feedItemId.id,
)
updateMetadata(SyncTable.SYNCED_FEED_ITEM)
}

suspend fun setFeedItemAsBookmarked(feedItemId: FeedItemId, isBookmarked: Boolean) =
getDbRef().transactionWithContext(backgroundDispatcher) {
getDbRef().syncedFeedItemQueries.insertOrIgnoreSyncedFeedItem(
url_hash = feedItemId.id,
is_read = false,
is_bookmarked = isBookmarked,
)
getDbRef().syncedFeedItemQueries.updateIsBookmarked(
isBookmarked = isBookmarked,
urlHash = feedItemId.id,
)
updateMetadata(SyncTable.SYNCED_FEED_ITEM)
}

private fun updateMetadata(table: SyncTable) {
getDbRef().syncedMetadataQueries.insertMetadata(
table_name = table.tableName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,12 @@ CREATE TABLE synced_feed_item(
is_bookmarked INTEGER AS Boolean NOT NULL DEFAULT 0
);

insertOrIgnoreSyncedFeedItem:
INSERT OR IGNORE INTO synced_feed_item(url_hash, is_read, is_bookmarked)
insertOrReplaceSyncedFeedItem:
INSERT OR REPLACE INTO synced_feed_item(url_hash, is_read, is_bookmarked)
VALUES (?, ?,?);

deleteSyncedFeedItem:
DELETE FROM synced_feed_item WHERE url_hash = ?;

selectAllSyncedFeedItems:
SELECT * FROM synced_feed_item;

updateIsRead:
UPDATE synced_feed_item
SET is_read = :isRead
WHERE url_hash = :urlHash;

updateIsBookmarked:
UPDATE synced_feed_item
SET is_bookmarked = :isBookmarked
WHERE url_hash = :urlHash;
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ internal class FeedSyncAndroidWorker(
private val dispatcherProvider: DispatcherProvider,
private val dropboxSettings: DropboxSettings,
) : FeedSyncWorker {

override suspend fun uploadImmediate() {
logger.d { "Start Immediate upload" }
performUpload()
Expand All @@ -56,6 +57,7 @@ internal class FeedSyncAndroidWorker(

try {
feedSyncer.populateSyncDbIfEmpty()
feedSyncer.updateFeedItemsToSyncDatabase()
feedSyncer.closeDB()

val databaseFile = generateDatabaseFile() ?: return@withContext SyncResult.Error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,7 @@ internal class FeedRetrieverRepository(
}.toImmutableList()
}
databaseHelper.markAsRead(itemsToUpdates.toList())
itemsToUpdates.forEach { feedItemId ->
feedSyncRepository.updateFeedItemReadStatus(feedItemId, true)
}
feedSyncRepository.setIsSyncUploadRequired()
}

suspend fun fetchFeeds(
Expand Down Expand Up @@ -217,8 +215,7 @@ internal class FeedRetrieverRepository(
suspend fun markAllFeedAsRead() {
val currentFilter = currentFeedFilterMutableState.value
databaseHelper.markAllFeedAsRead(currentFilter)
val allFeedItemIds = databaseHelper.getAllFeedItemIds()
feedSyncRepository.markAllFeedAsRead(allFeedItemIds)
feedSyncRepository.setIsSyncUploadRequired()
getFeeds()
}

Expand Down Expand Up @@ -312,7 +309,7 @@ internal class FeedRetrieverRepository(
}.toImmutableList()
}
databaseHelper.updateBookmarkStatus(feedItemId, isBookmarked)
feedSyncRepository.updateFeedItemBookmarkStatus(feedItemId, isBookmarked)
feedSyncRepository.setIsSyncUploadRequired()
}

suspend fun updateReadStatus(feedItemId: FeedItemId, isRead: Boolean) {
Expand All @@ -330,7 +327,7 @@ internal class FeedRetrieverRepository(
}.toImmutableList()
}
databaseHelper.updateReadStatus(feedItemId, isRead)
feedSyncRepository.updateFeedItemReadStatus(feedItemId, isRead)
feedSyncRepository.setIsSyncUploadRequired()
}

private suspend fun parseFeeds(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,27 +99,8 @@ class FeedSyncRepository internal constructor(
}
}

internal suspend fun updateFeedItemReadStatus(feedItemId: FeedItemId, isRead: Boolean) {
if (feedSyncAccountRepository.isSyncEnabled()) {
syncedDatabaseHelper.setFeedItemAsRead(feedItemId, isRead)
settingsHelper.setIsSyncUploadRequired(true)
}
}

internal suspend fun updateFeedItemBookmarkStatus(feedItemId: FeedItemId, isBookmarked: Boolean) {
if (feedSyncAccountRepository.isSyncEnabled()) {
syncedDatabaseHelper.setFeedItemAsBookmarked(feedItemId, isBookmarked)
settingsHelper.setIsSyncUploadRequired(true)
}
}

internal suspend fun markAllFeedAsRead(allFeedItemIds: List<FeedItemId>) {
if (feedSyncAccountRepository.isSyncEnabled()) {
for (feedItemId in allFeedItemIds) {
syncedDatabaseHelper.setFeedItemAsRead(feedItemId, true)
}
settingsHelper.setIsSyncUploadRequired(true)
}
internal fun setIsSyncUploadRequired() {
settingsHelper.setIsSyncUploadRequired(true)
}

internal suspend fun syncFeedSources() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ internal class FeedSyncer(
}
}

suspend fun updateFeedItemsToSyncDatabase() {
val allFeedItems = appDatabaseHelper.getFeedItemsForSync()
syncedDatabaseHelper.insertFeedItems(allFeedItems)
}

suspend fun syncFeedSource() {
val lastSyncTimestamp = appDatabaseHelper.getLastChangeTimestamp(DatabaseTables.FEED_SOURCE)
val remoteSyncTimestamp = syncedDatabaseHelper.getLastChangeTimestamp(SyncTable.SYNCED_FEED_SOURCE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ internal class FeedSyncIosWorker(

try {
feedSyncer.populateSyncDbIfEmpty()
feedSyncer.updateFeedItemsToSyncDatabase()
feedSyncer.closeDB()

val databasePath = getDatabaseUrl()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ internal class FeedSyncJvmWorker(

try {
feedSyncer.populateSyncDbIfEmpty()
feedSyncer.updateFeedItemsToSyncDatabase()
feedSyncer.closeDB()

val dropboxUploadParam = DropboxUploadParam(
Expand Down

0 comments on commit 7ac2b82

Please sign in to comment.