fix(common-ui): delete option in post detail

This commit is contained in:
Diego Beraldin 2023-09-18 08:44:58 +02:00
parent f96afd1d62
commit 970299fdac
4 changed files with 14 additions and 4 deletions

View File

@ -33,7 +33,7 @@ interface PostDetailMviModel :
val canFetchMore: Boolean = true,
val sortType: SortType = SortType.New,
val comments: List<CommentModel> = emptyList(),
val currentUserId: Int? = 0,
val currentUserId: Int? = null,
val swipeActionsEnabled: Boolean = true,
)

View File

@ -293,7 +293,7 @@ class PostDetailScreen(
saved = statePost.saved,
date = statePost.publishDate,
options = buildList {
if (post.creator?.id == uiState.currentUserId) {
if (statePost.creator?.id == uiState.currentUserId) {
add(stringResource(MR.strings.comment_action_delete))
}
},

View File

@ -41,14 +41,19 @@ class PostDetailViewModel(
mvi.updateState {
it.copy(
sortType = sortType,
post = post,
)
}
mvi.scope?.launch {
if (uiState.value.currentUserId == null) {
val auth = identityRepository.authToken.value.orEmpty()
val user = siteRepository.getCurrentUser(auth)
mvi.updateState { it.copy(currentUserId = user?.id ?: 0) }
val p = postsRepository.get(id = post.id, auth = auth) ?: post
mvi.updateState {
it.copy(
currentUserId = user?.id ?: 0,
post = p,
)
}
}
if (mvi.uiState.value.comments.isEmpty()) {
refresh()

View File

@ -60,6 +60,11 @@ class PostsRepository(
dto.map { it.toModel() }
}.getOrElse { emptyList() }
suspend fun get(id: Int, auth: String? = null): PostModel? = runCatching {
val dto = services.post.get(auth, id).body()?.postView
dto?.toModel()
}.getOrNull()
fun asUpVoted(post: PostModel, voted: Boolean) = post.copy(
myVote = if (voted) 1 else 0,
score = when {