Skip to content

Commit

Permalink
Implement better findRemoteKey logic
Browse files Browse the repository at this point in the history
  • Loading branch information
markocic committed Feb 20, 2025
1 parent d90853b commit 7b3541b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import net.primal.android.nostr.ext.orderByPagingIfNotNull
import net.primal.android.nostr.model.NostrEvent
import net.primal.android.nostr.model.primal.content.ContentPrimalPaging
import net.primal.android.notes.db.FeedPostRemoteKey
import timber.log.Timber

@OptIn(ExperimentalPagingApi::class)
class ArticleFeedMediator(
Expand Down Expand Up @@ -52,10 +53,17 @@ class ArticleFeedMediator(
@Suppress("ReturnCount")
override suspend fun load(loadType: LoadType, state: PagingState<Int, Article>): MediatorResult {
val nextUntil = when (loadType) {
LoadType.APPEND -> findLastRemoteKey()?.sinceId
?: return MediatorResult.Success(endOfPaginationReached = true)
LoadType.APPEND -> findLastRemoteKey(state = state)?.sinceId
?: run {
Timber.d("APPEND no remote key found exit.")
return MediatorResult.Success(endOfPaginationReached = true)
}

LoadType.PREPEND -> {
Timber.d("PREPEND end of pagination exit.")
return MediatorResult.Success(endOfPaginationReached = true)
}

LoadType.PREPEND -> return MediatorResult.Success(endOfPaginationReached = true)
LoadType.REFRESH -> null
}

Expand All @@ -72,6 +80,7 @@ class ArticleFeedMediator(
} catch (error: WssException) {
MediatorResult.Error(error)
} catch (_: RepeatingRequestBodyException) {
Timber.d("RepeatingRequestBody exit.")
MediatorResult.Success(endOfPaginationReached = true)
}
}
Expand Down Expand Up @@ -107,9 +116,19 @@ class ArticleFeedMediator(
return response
}

private suspend fun findLastRemoteKey(): FeedPostRemoteKey? =
private suspend fun findLastRemoteKey(state: PagingState<Int, Article>): FeedPostRemoteKey? {
val lastItemId = state.lastItemOrNull()?.data?.eventId
?: findLastItemOrNull()?.articleId

return withContext(dispatcherProvider.io()) {
lastItemId?.let { database.feedPostsRemoteKeys().findByEventId(eventId = lastItemId) }
?: database.feedPostsRemoteKeys().findLatestByDirective(directive = feedSpec)
}
}

private suspend fun findLastItemOrNull(): ArticleFeedCrossRef? =
withContext(dispatcherProvider.io()) {
database.feedPostsRemoteKeys().findLatestByDirective(directive = feedSpec)
database.articleFeedsConnections().findLastBySpec(spec = feedSpec)
}

private suspend fun processAndPersistToDatabase(response: ArticleResponse, clearFeed: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ interface ArticleFeedCrossRefDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun connect(data: List<ArticleFeedCrossRef>)

@Query("SELECT * FROM ArticleFeedCrossRef WHERE spec = :spec ORDER BY position DESC LIMIT 1")
fun findLastBySpec(spec: String): ArticleFeedCrossRef?

@Query("DELETE FROM ArticleFeedCrossRef WHERE spec = :spec")
fun deleteConnectionsBySpec(spec: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ class NostrSocketClient(
chunks.forEachIndexed { index, chunk ->
val prefix = if (incoming) "<--" else "-->"
val suffix = if (index == chunksCount - 1) "[$url]" else ""
Timber.d("$prefix $chunk $suffix")
Timber.tag(if (incoming) "NostrSocketClientIncoming" else "NostrSocketClientOutgoing")
.d("$prefix $chunk $suffix")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ interface FeedPostRemoteKeyDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun upsert(data: List<FeedPostRemoteKey>)

@Query("SELECT * FROM FeedPostRemoteKey WHERE eventId = :eventId LIMIT 1")
fun findByEventId(eventId: String): FeedPostRemoteKey?

@Query(
"""
SELECT * FROM FeedPostRemoteKey
Expand Down

0 comments on commit 7b3541b

Please sign in to comment.