diff --git a/android/src/main/kotlin/dev/msfjarvis/claw/android/viewmodel/ClawViewModel.kt b/android/src/main/kotlin/dev/msfjarvis/claw/android/viewmodel/ClawViewModel.kt index 89182bd6..31f837e9 100644 --- a/android/src/main/kotlin/dev/msfjarvis/claw/android/viewmodel/ClawViewModel.kt +++ b/android/src/main/kotlin/dev/msfjarvis/claw/android/viewmodel/ClawViewModel.kt @@ -39,6 +39,7 @@ class ClawViewModel constructor( private val api: LobstersApi, private val savedPostsRepository: SavedPostsRepository, + private val commentsRepository: CommentsRepository, private val linkMetadataRepository: LinkMetadataRepository, private val pagingSourceFactory: LobstersPagingSource.Factory, @IODispatcher private val ioDispatcher: CoroutineDispatcher, @@ -101,10 +102,10 @@ constructor( } } - suspend fun getSeenComments(postId: String) = savedPostsRepository.getSeenComments(postId) + suspend fun getSeenComments(postId: String) = commentsRepository.getSeenComments(postId) fun markSeenComments(postId: String, comments: List) { - viewModelScope.launch { savedPostsRepository.markSeenComments(postId, comments) } + viewModelScope.launch { commentsRepository.markSeenComments(postId, comments) } } suspend fun getLinkMetadata(url: String) = diff --git a/android/src/main/kotlin/dev/msfjarvis/claw/android/viewmodel/CommentsRepository.kt b/android/src/main/kotlin/dev/msfjarvis/claw/android/viewmodel/CommentsRepository.kt new file mode 100644 index 00000000..4c58aed7 --- /dev/null +++ b/android/src/main/kotlin/dev/msfjarvis/claw/android/viewmodel/CommentsRepository.kt @@ -0,0 +1,33 @@ +/* + * Copyright © 2021-2023 Harsh Shandilya. + * Use of this source code is governed by an MIT-style + * license that can be found in the LICENSE file or at + * https://opensource.org/licenses/MIT. + */ +package dev.msfjarvis.claw.android.viewmodel + +import dev.msfjarvis.claw.android.injection.DatabaseDispatcher +import dev.msfjarvis.claw.database.LobstersDatabase +import dev.msfjarvis.claw.database.local.PostComments +import dev.msfjarvis.claw.model.Comment +import javax.inject.Inject +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.withContext + +class CommentsRepository +@Inject +constructor( + database: LobstersDatabase, + @DatabaseDispatcher private val dbDispatcher: CoroutineDispatcher, +) { + private val postCommentsQueries = database.postCommentsQueries + + suspend fun getSeenComments(postId: String) = + withContext(dbDispatcher) { postCommentsQueries.getCommentIds(postId).executeAsOneOrNull() } + + suspend fun markSeenComments(postId: String, comments: List) { + withContext(dbDispatcher) { + postCommentsQueries.rememberComments(PostComments(postId, comments.map { it.shortId })) + } + } +} diff --git a/android/src/main/kotlin/dev/msfjarvis/claw/android/viewmodel/SavedPostsRepository.kt b/android/src/main/kotlin/dev/msfjarvis/claw/android/viewmodel/SavedPostsRepository.kt index 5ae2c04d..f69e80a7 100644 --- a/android/src/main/kotlin/dev/msfjarvis/claw/android/viewmodel/SavedPostsRepository.kt +++ b/android/src/main/kotlin/dev/msfjarvis/claw/android/viewmodel/SavedPostsRepository.kt @@ -10,9 +10,7 @@ import app.cash.sqldelight.coroutines.asFlow import app.cash.sqldelight.coroutines.mapToList import dev.msfjarvis.claw.android.injection.DatabaseDispatcher import dev.msfjarvis.claw.database.LobstersDatabase -import dev.msfjarvis.claw.database.local.PostComments import dev.msfjarvis.claw.database.local.SavedPost -import dev.msfjarvis.claw.model.Comment import io.github.aakira.napier.Napier import javax.inject.Inject import kotlinx.coroutines.CoroutineDispatcher @@ -25,7 +23,6 @@ constructor( @DatabaseDispatcher private val dbDispatcher: CoroutineDispatcher, ) { private val savedPostQueries = database.savedPostQueries - private val postCommentsQueries = database.postCommentsQueries val savedPosts = savedPostQueries.selectAllPosts().asFlow().mapToList(dbDispatcher) suspend fun savePost(post: SavedPost) { @@ -47,15 +44,6 @@ constructor( withContext(dbDispatcher) { savedPostQueries.deletePost(post.shortId) } } - suspend fun getSeenComments(postId: String) = - withContext(dbDispatcher) { postCommentsQueries.getCommentIds(postId).executeAsOneOrNull() } - - suspend fun markSeenComments(postId: String, comments: List) { - withContext(dbDispatcher) { - postCommentsQueries.rememberComments(PostComments(postId, comments.map { it.shortId })) - } - } - private companion object { private const val TAG = "SavedPostsRepository" }