refactor: naming

Signed-off-by: Infi <wingit@geist.ga>
This commit is contained in:
Infi 2023-06-12 18:29:32 +02:00
parent add6f6d557
commit 75c7ec95a4
2 changed files with 29 additions and 33 deletions

View File

@ -223,7 +223,7 @@ fun ChannelScreen(
.collect { .collect {
if (it) { if (it) {
coroutineScope.launch { coroutineScope.launch {
if (viewModel.noMoreMessages) return@launch if (viewModel.hasNoMoreMessages) return@launch
viewModel.fetchOlderMessages() viewModel.fetchOlderMessages()
} }
} }
@ -284,7 +284,7 @@ fun ChannelScreen(
}, },
canReply = true, canReply = true,
onReply = { onReply = {
if (viewModel.replies.size >= 5) { if (viewModel.pendingReplies.size >= 5) {
Toast.makeText( Toast.makeText(
context, context,
context.getString(R.string.too_many_replies, 5), context.getString(R.string.too_many_replies, 5),
@ -298,7 +298,7 @@ fun ChannelScreen(
} }
item { item {
if (viewModel.noMoreMessages) { if (viewModel.hasNoMoreMessages) {
Text( Text(
text = stringResource(R.string.start_of_conversation), text = stringResource(R.string.start_of_conversation),
modifier = Modifier modifier = Modifier
@ -373,10 +373,10 @@ fun ChannelScreen(
) )
.clip(MaterialTheme.shapes.medium) .clip(MaterialTheme.shapes.medium)
) { ) {
AnimatedVisibility(visible = viewModel.replies.isNotEmpty()) { AnimatedVisibility(visible = viewModel.pendingReplies.isNotEmpty()) {
ReplyManager( ReplyManager(
replies = viewModel.replies, replies = viewModel.pendingReplies,
onRemove = { viewModel.replies.remove(it) }, onRemove = { viewModel.pendingReplies.remove(it) },
onToggleMention = viewModel::toggleReplyMentionFor onToggleMention = viewModel::toggleReplyMentionFor
) )
} }
@ -384,15 +384,15 @@ fun ChannelScreen(
AnimatedVisibility(visible = viewModel.pendingAttachments.isNotEmpty()) { AnimatedVisibility(visible = viewModel.pendingAttachments.isNotEmpty()) {
AttachmentManager( AttachmentManager(
attachments = viewModel.pendingAttachments, attachments = viewModel.pendingAttachments,
uploading = viewModel.sendingMessage, uploading = viewModel.isSendingMessage,
onRemove = { viewModel.pendingAttachments.remove(it) } onRemove = { viewModel.pendingAttachments.remove(it) }
) )
} }
MessageField( MessageField(
messageContent = viewModel.messageContent, messageContent = viewModel.pendingMessageContent,
onMessageContentChange = { onMessageContentChange = {
viewModel.messageContent = it viewModel.pendingMessageContent = it
}, },
onSendMessage = viewModel::sendPendingMessage, onSendMessage = viewModel::sendPendingMessage,
onAddAttachment = { onAddAttachment = {
@ -403,7 +403,7 @@ fun ChannelScreen(
R.string.unknown R.string.unknown
), ),
forceSendButton = viewModel.pendingAttachments.isNotEmpty(), forceSendButton = viewModel.pendingAttachments.isNotEmpty(),
disabled = viewModel.pendingAttachments.isNotEmpty() && viewModel.sendingMessage disabled = viewModel.pendingAttachments.isNotEmpty() && viewModel.isSendingMessage
) )
} }
} }

View File

@ -46,16 +46,13 @@ class ChannelScreenViewModel : ViewModel() {
var activeChannel by mutableStateOf<Channel?>(null) var activeChannel by mutableStateOf<Channel?>(null)
var renderableMessages = mutableStateListOf<Message>() var renderableMessages = mutableStateListOf<Message>()
private fun setRenderableMessages(messages: List<Message>) {
renderableMessages.clear()
renderableMessages.addAll(messages)
}
var typingUsers = mutableStateListOf<String>() var typingUsers = mutableStateListOf<String>()
var messageContent by mutableStateOf("") var isSendingMessage by mutableStateOf(false)
var hasNoMoreMessages by mutableStateOf(false)
var pendingMessageContent by mutableStateOf("")
var pendingReplies = mutableStateListOf<SendMessageReply>()
var pendingAttachments = mutableStateListOf<FileArgs>() var pendingAttachments = mutableStateListOf<FileArgs>()
private fun popAttachmentBatch() { private fun popAttachmentBatch() {
@ -63,31 +60,30 @@ class ChannelScreenViewModel : ViewModel() {
pendingAttachments.drop(MAX_ATTACHMENTS_PER_MESSAGE) as SnapshotStateList<FileArgs> pendingAttachments.drop(MAX_ATTACHMENTS_PER_MESSAGE) as SnapshotStateList<FileArgs>
} }
var sendingMessage by mutableStateOf(false) private fun setRenderableMessages(messages: List<Message>) {
renderableMessages.clear()
var replies = mutableStateListOf<SendMessageReply>() renderableMessages.addAll(messages)
}
private fun addReply(reply: SendMessageReply) { private fun addReply(reply: SendMessageReply) {
if (replies.any { it.id == reply.id }) return if (pendingReplies.any { it.id == reply.id }) return
replies.add(reply) pendingReplies.add(reply)
} }
fun toggleReplyMentionFor(reply: SendMessageReply) { fun toggleReplyMentionFor(reply: SendMessageReply) {
val index = replies.indexOf(reply) val index = pendingReplies.indexOf(reply)
val newReply = SendMessageReply( val newReply = SendMessageReply(
reply.id, reply.id,
!reply.mention !reply.mention
) )
replies[index] = newReply pendingReplies[index] = newReply
} }
private fun clearInReplyTo() { private fun clearInReplyTo() {
replies.clear() pendingReplies.clear()
} }
var noMoreMessages by mutableStateOf(false)
fun fetchOlderMessages() { fun fetchOlderMessages() {
if (activeChannel == null) { if (activeChannel == null) {
return return
@ -107,7 +103,7 @@ class ChannelScreenViewModel : ViewModel() {
} }
).let { ).let {
if (it.messages.isNullOrEmpty() || it.messages.size < 50) { if (it.messages.isNullOrEmpty() || it.messages.size < 50) {
noMoreMessages = true hasNoMoreMessages = true
} }
it.messages?.forEach { message -> it.messages?.forEach { message ->
@ -142,7 +138,7 @@ class ChannelScreenViewModel : ViewModel() {
} }
fun sendPendingMessage() { fun sendPendingMessage() {
sendingMessage = true isSendingMessage = true
viewModelScope.launch { viewModelScope.launch {
val attachmentIds = arrayListOf<String>() val attachmentIds = arrayListOf<String>()
@ -165,13 +161,13 @@ class ChannelScreenViewModel : ViewModel() {
sendMessage( sendMessage(
activeChannel!!.id!!, activeChannel!!.id!!,
messageContent.trimIndent(), pendingMessageContent.trimIndent(),
attachments = if (attachmentIds.isEmpty()) null else attachmentIds, attachments = if (attachmentIds.isEmpty()) null else attachmentIds,
replies = replies replies = pendingReplies
) )
messageContent = "" pendingMessageContent = ""
noMoreMessages = false hasNoMoreMessages = false
popAttachmentBatch() popAttachmentBatch()
clearInReplyTo() clearInReplyTo()
} }