refactor: mentions and replies simplification (#410)

* refactor: simplify mentions

* refactor: simplify replies
This commit is contained in:
Diego Beraldin 2024-01-02 09:18:24 +01:00 committed by GitHub
parent a68299ec1d
commit a9da144ae9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 132 deletions

View File

@ -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)
)
},
)
},
)

View File

@ -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)
}
}
}

View File

@ -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)
)
},
)
},
)

View File

@ -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)
}
}
}