mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-15 07:37:03 +05:30
refactor(android): split out comments functionality
This commit is contained in:
parent
f102166fae
commit
2895e7ffe7
3 changed files with 36 additions and 14 deletions
|
@ -39,6 +39,7 @@ class ClawViewModel
|
||||||
constructor(
|
constructor(
|
||||||
private val api: LobstersApi,
|
private val api: LobstersApi,
|
||||||
private val savedPostsRepository: SavedPostsRepository,
|
private val savedPostsRepository: SavedPostsRepository,
|
||||||
|
private val commentsRepository: CommentsRepository,
|
||||||
private val linkMetadataRepository: LinkMetadataRepository,
|
private val linkMetadataRepository: LinkMetadataRepository,
|
||||||
private val pagingSourceFactory: LobstersPagingSource.Factory,
|
private val pagingSourceFactory: LobstersPagingSource.Factory,
|
||||||
@IODispatcher private val ioDispatcher: CoroutineDispatcher,
|
@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<Comment>) {
|
fun markSeenComments(postId: String, comments: List<Comment>) {
|
||||||
viewModelScope.launch { savedPostsRepository.markSeenComments(postId, comments) }
|
viewModelScope.launch { commentsRepository.markSeenComments(postId, comments) }
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun getLinkMetadata(url: String) =
|
suspend fun getLinkMetadata(url: String) =
|
||||||
|
|
|
@ -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<Comment>) {
|
||||||
|
withContext(dbDispatcher) {
|
||||||
|
postCommentsQueries.rememberComments(PostComments(postId, comments.map { it.shortId }))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,9 +10,7 @@ import app.cash.sqldelight.coroutines.asFlow
|
||||||
import app.cash.sqldelight.coroutines.mapToList
|
import app.cash.sqldelight.coroutines.mapToList
|
||||||
import dev.msfjarvis.claw.android.injection.DatabaseDispatcher
|
import dev.msfjarvis.claw.android.injection.DatabaseDispatcher
|
||||||
import dev.msfjarvis.claw.database.LobstersDatabase
|
import dev.msfjarvis.claw.database.LobstersDatabase
|
||||||
import dev.msfjarvis.claw.database.local.PostComments
|
|
||||||
import dev.msfjarvis.claw.database.local.SavedPost
|
import dev.msfjarvis.claw.database.local.SavedPost
|
||||||
import dev.msfjarvis.claw.model.Comment
|
|
||||||
import io.github.aakira.napier.Napier
|
import io.github.aakira.napier.Napier
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
|
@ -25,7 +23,6 @@ constructor(
|
||||||
@DatabaseDispatcher private val dbDispatcher: CoroutineDispatcher,
|
@DatabaseDispatcher private val dbDispatcher: CoroutineDispatcher,
|
||||||
) {
|
) {
|
||||||
private val savedPostQueries = database.savedPostQueries
|
private val savedPostQueries = database.savedPostQueries
|
||||||
private val postCommentsQueries = database.postCommentsQueries
|
|
||||||
val savedPosts = savedPostQueries.selectAllPosts().asFlow().mapToList(dbDispatcher)
|
val savedPosts = savedPostQueries.selectAllPosts().asFlow().mapToList(dbDispatcher)
|
||||||
|
|
||||||
suspend fun savePost(post: SavedPost) {
|
suspend fun savePost(post: SavedPost) {
|
||||||
|
@ -47,15 +44,6 @@ constructor(
|
||||||
withContext(dbDispatcher) { savedPostQueries.deletePost(post.shortId) }
|
withContext(dbDispatcher) { savedPostQueries.deletePost(post.shortId) }
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun getSeenComments(postId: String) =
|
|
||||||
withContext(dbDispatcher) { postCommentsQueries.getCommentIds(postId).executeAsOneOrNull() }
|
|
||||||
|
|
||||||
suspend fun markSeenComments(postId: String, comments: List<Comment>) {
|
|
||||||
withContext(dbDispatcher) {
|
|
||||||
postCommentsQueries.rememberComments(PostComments(postId, comments.map { it.shortId }))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private companion object {
|
private companion object {
|
||||||
private const val TAG = "SavedPostsRepository"
|
private const val TAG = "SavedPostsRepository"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue