diff --git a/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/postdetail/PostDetailMviModel.kt b/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/postdetail/PostDetailMviModel.kt index e3454c952..d94a73e20 100644 --- a/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/postdetail/PostDetailMviModel.kt +++ b/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/postdetail/PostDetailMviModel.kt @@ -33,6 +33,7 @@ interface PostDetailMviModel : val post: PostModel = PostModel(), val refreshing: Boolean = false, val loading: Boolean = false, + val initial: Boolean = true, val canFetchMore: Boolean = true, val sortType: SortType = SortType.New, val comments: List = emptyList(), diff --git a/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/postdetail/PostDetailViewModel.kt b/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/postdetail/PostDetailViewModel.kt index d9c79a5c5..42dcb0bc6 100644 --- a/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/postdetail/PostDetailViewModel.kt +++ b/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/postdetail/PostDetailViewModel.kt @@ -58,11 +58,7 @@ class PostDetailViewModel( override fun onStarted() { mvi.onStarted() - mvi.updateState { - it.copy( - post = post, - ) - } + mvi.scope?.launch { themeRepository.postLayout.onEach { layout -> mvi.updateState { it.copy(postLayout = layout) } @@ -88,23 +84,28 @@ class PostDetailViewModel( } if (post.title.isEmpty()) { // empty post must be loaded - postRepository.get(post.id)?.also { updatedPost -> + val updatedPost = postRepository.get(post.id) + if (updatedPost != null) { mvi.updateState { it.copy( post = updatedPost, ) } } + } else { + mvi.updateState { + it.copy( + post = post, + ) + } } - mvi.scope?.launch { - if (highlightCommentId != null) { - val auth = identityRepository.authToken.value - val comment = commentRepository.getBy(highlightCommentId, auth) - highlightCommentPath = comment?.path - } - if (mvi.uiState.value.comments.isEmpty()) { - refresh() - } + if (highlightCommentId != null) { + val auth = identityRepository.authToken.value + val comment = commentRepository.getBy(highlightCommentId, auth) + highlightCommentPath = comment?.path + } + if (mvi.uiState.value.comments.isEmpty()) { + refresh() } } } @@ -140,7 +141,12 @@ class PostDetailViewModel( override fun reduce(intent: PostDetailMviModel.Intent) { when (intent) { - PostDetailMviModel.Intent.LoadNextPage -> loadNextPage() + PostDetailMviModel.Intent.LoadNextPage -> { + if (!uiState.value.initial) { + loadNextPage() + } + } + PostDetailMviModel.Intent.Refresh -> refresh() PostDetailMviModel.Intent.RefreshPost -> refreshPost() PostDetailMviModel.Intent.HapticIndication -> hapticFeedback.vibrate() @@ -242,6 +248,7 @@ class PostDetailViewModel( loading = false, canFetchMore = canFetchMore, refreshing = false, + initial = false, ) } if (highlightCommentPath != null && !commentWasHighlighted) {