mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-14 05:57:03 +05:30
feat: load link metadata lazily
This commit is contained in:
parent
4337a19abc
commit
79aba9a187
7 changed files with 50 additions and 61 deletions
|
@ -206,6 +206,7 @@ fun LobstersApp(
|
|||
CommentsPage(
|
||||
postId = postId,
|
||||
getDetails = viewModel::getPostComments,
|
||||
getLinkMetadata = viewModel::getLinkMetadata,
|
||||
postActions = postActions,
|
||||
htmlConverter = htmlConverter,
|
||||
)
|
||||
|
|
|
@ -27,7 +27,7 @@ class ClawViewModel
|
|||
constructor(
|
||||
private val api: LobstersApi,
|
||||
private val savedPostsRepository: SavedPostsRepository,
|
||||
private val postDetailsRepository: PostDetailsRepository,
|
||||
private val linkMetadataRepository: LinkMetadataRepository,
|
||||
private val pagingSourceFactory: LobstersPagingSource.Factory,
|
||||
@IODispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||
) : ViewModel() {
|
||||
|
@ -76,18 +76,18 @@ constructor(
|
|||
|
||||
suspend fun getPostComments(postId: String) =
|
||||
withContext(ioDispatcher) {
|
||||
val details =
|
||||
when (val result = api.getPostDetails(postId)) {
|
||||
is Success -> result.value
|
||||
is Failure.NetworkFailure -> throw result.error
|
||||
is Failure.UnknownFailure -> throw result.error
|
||||
is Failure.HttpFailure,
|
||||
is Failure.ApiFailure -> throw IOException("API returned an invalid response")
|
||||
}
|
||||
val extendedDetails = postDetailsRepository.getExtendedDetails(details)
|
||||
extendedDetails
|
||||
when (val result = api.getPostDetails(postId)) {
|
||||
is Success -> result.value
|
||||
is Failure.NetworkFailure -> throw result.error
|
||||
is Failure.UnknownFailure -> throw result.error
|
||||
is Failure.HttpFailure,
|
||||
is Failure.ApiFailure -> throw IOException("API returned an invalid response")
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getLinkMetadata(url: String) =
|
||||
withContext(ioDispatcher) { linkMetadataRepository.getLinkMetadata(url) }
|
||||
|
||||
suspend fun getUserProfile(username: String) =
|
||||
withContext(ioDispatcher) {
|
||||
when (val result = api.getUser(username)) {
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
package dev.msfjarvis.claw.android.viewmodel
|
||||
|
||||
import dev.msfjarvis.claw.metadata.MetadataExtractor
|
||||
import dev.msfjarvis.claw.model.LinkMetadata
|
||||
import javax.inject.Inject
|
||||
|
||||
class LinkMetadataRepository
|
||||
@Inject
|
||||
constructor(
|
||||
private val metadataExtractor: MetadataExtractor,
|
||||
) {
|
||||
suspend fun getLinkMetadata(url: String): LinkMetadata {
|
||||
return metadataExtractor.getExtractedMetadata(url)
|
||||
}
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
package dev.msfjarvis.claw.android.viewmodel
|
||||
|
||||
import dev.msfjarvis.claw.metadata.MetadataExtractor
|
||||
import dev.msfjarvis.claw.model.ExtendedPostDetails
|
||||
import dev.msfjarvis.claw.model.LobstersPostDetails
|
||||
import javax.inject.Inject
|
||||
|
||||
class PostDetailsRepository
|
||||
@Inject
|
||||
constructor(
|
||||
private val metadataExtractor: MetadataExtractor,
|
||||
) {
|
||||
suspend fun getExtendedDetails(details: LobstersPostDetails): ExtendedPostDetails {
|
||||
val metadata = metadataExtractor.getExtractedMetadata(details.url)
|
||||
return ExtendedPostDetails(
|
||||
title = details.title,
|
||||
linkMetadata = metadata,
|
||||
description = details.description,
|
||||
submitter = details.submitter,
|
||||
tags = details.tags,
|
||||
comments = details.comments,
|
||||
commentsUrl = details.commentsUrl,
|
||||
)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue