From a9da144ae9a89ac0d38496874fc1a96c4fe659a7 Mon Sep 17 00:00:00 2001 From: Diego Beraldin Date: Tue, 2 Jan 2024 09:18:24 +0100 Subject: [PATCH] refactor: mentions and replies simplification (#410) * refactor: simplify mentions * refactor: simplify replies --- .../unit/mentions/InboxMentionsScreen.kt | 8 +- .../unit/mentions/InboxMentionsViewModel.kt | 92 ++++++------------- .../unit/replies/InboxRepliesScreen.kt | 8 +- .../unit/replies/InboxRepliesViewModel.kt | 78 ++++------------ 4 files changed, 54 insertions(+), 132 deletions(-) diff --git a/unit/mentions/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/mentions/InboxMentionsScreen.kt b/unit/mentions/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/mentions/InboxMentionsScreen.kt index e02ab930a..7d3085c9e 100644 --- a/unit/mentions/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/mentions/InboxMentionsScreen.kt +++ b/unit/mentions/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/mentions/InboxMentionsScreen.kt @@ -192,6 +192,9 @@ class InboxMentionsScreen : Tab { onOpenCommunity = rememberCallbackArgs { community -> detailOpener.openCommunityDetail(community, "") }, + onImageClick = rememberCallbackArgs { url -> + navigationCoordinator.pushScreen(ZoomableImageScreen(url)) + }, onUpVote = rememberCallbackArgs(model) { _ -> model.reduce(InboxMentionsMviModel.Intent.UpVoteComment(mention.id)) }, @@ -226,11 +229,6 @@ class InboxMentionsScreen : Tab { else -> Unit } }, - onImageClick = rememberCallbackArgs { url -> - navigationCoordinator.pushScreen( - ZoomableImageScreen(url) - ) - }, ) }, ) diff --git a/unit/mentions/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/mentions/InboxMentionsViewModel.kt b/unit/mentions/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/mentions/InboxMentionsViewModel.kt index 3bc8f56d5..63a30aa25 100644 --- a/unit/mentions/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/mentions/InboxMentionsViewModel.kt +++ b/unit/mentions/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/mentions/InboxMentionsViewModel.kt @@ -77,7 +77,6 @@ class InboxMentionsViewModel( InboxMentionsMviModel.Intent.Refresh -> mvi.scope?.launch(Dispatchers.IO) { refresh() - mvi.emitEffect(InboxMentionsMviModel.Effect.BackToTop) } is InboxMentionsMviModel.Intent.MarkAsRead -> { @@ -160,6 +159,20 @@ class InboxMentionsViewModel( } } + private fun handleItemUpdate(item: PersonMentionModel) { + mvi.updateState { + it.copy( + mentions = it.mentions.map { i -> + if (i.id == item.id) { + item + } else { + i + } + } + ) + } + } + private fun markAsRead(read: Boolean, mention: PersonMentionModel) { val auth = identityRepository.authToken.value mvi.scope?.launch(Dispatchers.IO) { @@ -178,17 +191,8 @@ class InboxMentionsViewModel( ) } } else { - mvi.updateState { - it.copy( - mentions = currentState.mentions.map { m -> - if (m.id == mention.id) { - m.copy(read = read) - } else { - m - } - } - ) - } + val newMention = mention.copy(read = read) + handleItemUpdate(newMention) } updateUnreadItems() } @@ -200,20 +204,11 @@ class InboxMentionsViewModel( comment = mention.comment, voted = newValue, ) - mvi.updateState { - it.copy( - mentions = it.mentions.map { m -> - if (m.comment.id != mention.comment.id) { - m - } else { - m.copy( - myVote = newComment.myVote, - score = newComment.score, - ) - } - }, - ) - } + val newMention = mention.copy( + myVote = newComment.myVote, + score = newComment.score, + ) + handleItemUpdate(newMention) mvi.scope?.launch(Dispatchers.IO) { try { val auth = identityRepository.authToken.value.orEmpty() @@ -223,18 +218,7 @@ class InboxMentionsViewModel( voted = newValue, ) } catch (e: Throwable) { - e.printStackTrace() - mvi.updateState { - it.copy( - mentions = it.mentions.map { m -> - if (m.comment.id != mention.comment.id) { - m - } else { - mention - } - }, - ) - } + handleItemUpdate(mention) } } } @@ -242,20 +226,11 @@ class InboxMentionsViewModel( private fun toggleDownVoteComment(mention: PersonMentionModel) { val newValue = mention.myVote >= 0 val newComment = commentRepository.asDownVoted(mention.comment, newValue) - mvi.updateState { - it.copy( - mentions = it.mentions.map { m -> - if (m.comment.id != mention.comment.id) { - m - } else { - m.copy( - myVote = newComment.myVote, - score = newComment.score - ) - } - }, - ) - } + val newMention = mention.copy( + myVote = newComment.myVote, + score = newComment.score, + ) + handleItemUpdate(newMention) mvi.scope?.launch(Dispatchers.IO) { try { val auth = identityRepository.authToken.value.orEmpty() @@ -265,18 +240,7 @@ class InboxMentionsViewModel( downVoted = newValue, ) } catch (e: Throwable) { - e.printStackTrace() - mvi.updateState { - it.copy( - mentions = it.mentions.map { m -> - if (m.comment.id != mention.comment.id) { - m - } else { - mention - } - }, - ) - } + handleItemUpdate(mention) } } } diff --git a/unit/replies/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/replies/InboxRepliesScreen.kt b/unit/replies/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/replies/InboxRepliesScreen.kt index d0637e218..330829699 100644 --- a/unit/replies/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/replies/InboxRepliesScreen.kt +++ b/unit/replies/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/replies/InboxRepliesScreen.kt @@ -190,6 +190,9 @@ class InboxRepliesScreen : Tab { onOpenCommunity = rememberCallbackArgs { community -> detailOpener.openCommunityDetail(community, "") }, + onImageClick = rememberCallbackArgs { url -> + navigationCoordinator.pushScreen(ZoomableImageScreen(url)) + }, onUpVote = rememberCallbackArgs(model) { _ -> model.reduce(InboxRepliesMviModel.Intent.UpVoteComment(reply.id)) }, @@ -220,11 +223,6 @@ class InboxRepliesScreen : Tab { else -> Unit } }, - onImageClick = rememberCallbackArgs { url -> - navigationCoordinator.pushScreen( - ZoomableImageScreen(url) - ) - }, ) }, ) diff --git a/unit/replies/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/replies/InboxRepliesViewModel.kt b/unit/replies/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/replies/InboxRepliesViewModel.kt index 4f9f2f0ce..7f1bc694d 100644 --- a/unit/replies/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/replies/InboxRepliesViewModel.kt +++ b/unit/replies/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/replies/InboxRepliesViewModel.kt @@ -80,7 +80,6 @@ class InboxRepliesViewModel( InboxRepliesMviModel.Intent.Refresh -> mvi.scope?.launch(Dispatchers.IO) { refresh() - mvi.emitEffect(InboxRepliesMviModel.Effect.BackToTop) } is InboxRepliesMviModel.Intent.MarkAsRead -> { @@ -170,6 +169,20 @@ class InboxRepliesViewModel( } } + private fun handleItemUpdate(item: PersonMentionModel) { + mvi.updateState { + it.copy( + replies = it.replies.map { i -> + if (i.id == item.id) { + item + } else { + i + } + } + ) + } + } + private fun markAsRead(read: Boolean, reply: PersonMentionModel) { val auth = identityRepository.authToken.value mvi.scope?.launch(Dispatchers.IO) { @@ -188,17 +201,8 @@ class InboxRepliesViewModel( ) } } else { - mvi.updateState { - it.copy( - replies = currentState.replies.map { r -> - if (r.id == reply.id) { - r.copy(read = read) - } else { - r - } - } - ) - } + val newItem = reply.copy(read = read) + handleItemUpdate(newItem) } updateUnreadItems() } @@ -210,17 +214,7 @@ class InboxRepliesViewModel( mention = mention, voted = newValue, ) - mvi.updateState { - it.copy( - replies = it.replies.map { m -> - if (m.comment.id != mention.comment.id) { - m - } else { - newMention - } - }, - ) - } + handleItemUpdate(newMention) mvi.scope?.launch(Dispatchers.IO) { try { val auth = identityRepository.authToken.value.orEmpty() @@ -230,18 +224,7 @@ class InboxRepliesViewModel( voted = newValue, ) } catch (e: Throwable) { - e.printStackTrace() - mvi.updateState { - it.copy( - replies = it.replies.map { m -> - if (m.comment.id != mention.comment.id) { - m - } else { - mention - } - }, - ) - } + handleItemUpdate(mention) } } } @@ -252,17 +235,7 @@ class InboxRepliesViewModel( mention = mention, downVoted = newValue ) - mvi.updateState { - it.copy( - replies = it.replies.map { m -> - if (m.comment.id != mention.comment.id) { - m - } else { - newMention - } - }, - ) - } + handleItemUpdate(newMention) mvi.scope?.launch(Dispatchers.IO) { try { val auth = identityRepository.authToken.value.orEmpty() @@ -272,18 +245,7 @@ class InboxRepliesViewModel( downVoted = newValue, ) } catch (e: Throwable) { - e.printStackTrace() - mvi.updateState { - it.copy( - replies = it.replies.map { m -> - if (m.comment.id != mention.comment.id) { - m - } else { - mention - } - }, - ) - } + handleItemUpdate(mention) } } }