Skip to content

Commit

Permalink
支持剧集评论, 但不幸的是目前 bangumi 没提供评论的 API
Browse files Browse the repository at this point in the history
  • Loading branch information
Him188 committed Mar 31, 2024
1 parent 11446f1 commit 5c6b148
Show file tree
Hide file tree
Showing 14 changed files with 425 additions and 167 deletions.
66 changes: 66 additions & 0 deletions app/shared/data/common/data/RevisionRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package me.him188.ani.app.data

import kotlinx.coroutines.flow.Flow
import me.him188.ani.datasources.api.PageBasedSearchSession
import me.him188.ani.datasources.api.Paged
import me.him188.ani.datasources.api.processPagedResponse
import me.him188.ani.datasources.bangumi.BangumiClient
import me.him188.ani.utils.logging.logger
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import org.openapitools.client.infrastructure.ClientException
import org.openapitools.client.models.Revision

sealed interface RevisionRepository {
fun getCommentsByEpisodeId(episodeId: Int): Flow<Comment>
}

interface EpisodeRevisionRepository : RevisionRepository
interface SubjectRevisionRepository : RevisionRepository

class Comment(
val id: String,
val type: Int,
val summary: String,
val createdAt: Long, // timestamp millis
val authorUsername: String?,
)

class EpisodeRevisionRepositoryImpl : EpisodeRevisionRepository, KoinComponent {
private val client by inject<BangumiClient>()
private val logger = logger(EpisodeRepositoryImpl::class)

override fun getCommentsByEpisodeId(episodeId: Int): Flow<Comment> {
return PageBasedSearchSession { page ->
try {
val pageSize = 30
client.api.getEpisodeRevisions(
offset = page * pageSize, limit = pageSize,
episodeId = episodeId,
).run {
Paged.processPagedResponse(total, pageSize, data?.map {
it.toComment()
})
}
} catch (e: ClientException) {
logger.warn("Exception in getCollections", e)
null
}
}.results
}
// override fun getCommentById(commentId: Int): Comment? {
// return client.api.getEpisodeRevisionByRevisionId(
// revisionId = commentId,
// )
// }
}

private fun Revision.toComment(): Comment {
return Comment(
id = id.toString(),
type = type,
summary = summary,
createdAt = createdAt.toEpochSecond() * 1000,
authorUsername = creator?.username,
)
}
24 changes: 24 additions & 0 deletions app/shared/data/common/data/UserRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package me.him188.ani.app.data

import me.him188.ani.datasources.bangumi.BangumiClient
import me.him188.ani.utils.logging.logger
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import org.openapitools.client.models.User

interface UserRepository {
fun getUserByUsername(username: String): User?
}

class UserRepositoryImpl : UserRepository, KoinComponent {
private val client by inject<BangumiClient>()
private val logger = logger(UserRepositoryImpl::class)

override fun getUserByUsername(username: String): User? {
return kotlin.runCatching {
client.api.getUserByName(username)
}.onFailure {
logger.warn("Exception in getUserByUsername", it)
}.getOrNull()
}
}
5 changes: 2 additions & 3 deletions app/shared/pages/episode-play/android/EpisodePage.android.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import androidx.compose.runtime.remember
import androidx.compose.ui.tooling.preview.Preview
import me.him188.ani.app.platform.LocalContext
import me.him188.ani.app.ui.foundation.ProvideCompositionLocalsForPreview
import me.him188.ani.app.videoplayer.ExoPlayerControllerFactory

@Composable
@Preview(widthDp = 1080 / 3, heightDp = 2400 / 3, showBackground = true)
internal actual fun PreviewEpisodePage() {
ProvideCompositionLocalsForPreview(playerControllerFactory = ExoPlayerControllerFactory()) {
ProvideCompositionLocalsForPreview {
val context = LocalContext.current
EpisodePageContent(
EpisodePage(
remember {
EpisodeViewModel(
424663,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,28 @@ package me.him188.ani.app.ui.subject.episode.comments
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewFontScale
import me.him188.ani.app.data.Comment
import me.him188.ani.app.ui.foundation.PreviewData
import me.him188.ani.app.ui.foundation.ProvideCompositionLocalsForPreview
import org.openapitools.client.models.Avatar
import org.openapitools.client.models.User
import org.openapitools.client.models.UserGroup
import kotlin.random.Random
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.minutes

@Preview
@Composable
private fun PreviewCommentColumn() {
ProvideCompositionLocalsForPreview {
CommentColumn()
CommentColumn(
remember {
CommentViewModel(PreviewData.SOSOU_NO_FURILEN_EPISODE_ID)
})
}
}

@PreviewFontScale
@Preview
@Composable
private fun PreviewComment() {
ProvideCompositionLocalsForPreview {
Expand All @@ -41,12 +46,12 @@ private fun PreviewComment() {

comment = remember {
Comment(
id = "nominavi",
id = "1",
type = 8010,
summary = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet.",
createdAt = run {
if (Random.nextBoolean()) {
System.currentTimeMillis()
System.currentTimeMillis() - 1.minutes.inWholeMilliseconds
} else {
System.currentTimeMillis() - 2.days.inWholeMilliseconds
}
Expand All @@ -57,4 +62,4 @@ private fun PreviewComment() {
)

}
}
}
Loading

0 comments on commit 5c6b148

Please sign in to comment.