Adds support for uploading attachments
This commit is contained in:
@@ -159,8 +159,12 @@ fun MessageListScreen(
|
||||
onSendMessage = { text ->
|
||||
viewModel.enqueueOutgoingMessage(
|
||||
text = text,
|
||||
attachmentUris = attachmentUris
|
||||
attachmentUris = attachmentUris,
|
||||
context = context
|
||||
)
|
||||
|
||||
// Clear pending attachments
|
||||
attachmentUris = setOf()
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.util.Log
|
||||
import androidx.compose.runtime.mutableStateListOf
|
||||
import androidx.core.net.toFile
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import coil.Coil
|
||||
@@ -35,12 +36,14 @@ import kotlinx.coroutines.launch
|
||||
import net.buzzert.kordophone.backend.model.Conversation
|
||||
import net.buzzert.kordophone.backend.model.GUID
|
||||
import net.buzzert.kordophone.backend.model.Message
|
||||
import net.buzzert.kordophone.backend.model.OutgoingMessage
|
||||
import net.buzzert.kordophone.backend.server.ChatRepository
|
||||
import net.buzzert.kordophonedroid.ui.attachments.AttachmentFetchData
|
||||
import net.buzzert.kordophonedroid.ui.attachments.AttachmentViewModel
|
||||
import okio.ByteString.Companion.toByteString
|
||||
import java.io.BufferedInputStream
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.InputStream
|
||||
import java.nio.file.FileSystem
|
||||
import java.util.Date
|
||||
import java.util.UUID
|
||||
@@ -64,11 +67,6 @@ class MessageListViewModel @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private data class OutgoingMessage(
|
||||
val requestGuid: String,
|
||||
val message: Message,
|
||||
)
|
||||
|
||||
private var conversation: Conversation? = null
|
||||
private val pendingMessages: MutableStateFlow<List<OutgoingMessage>> = MutableStateFlow(listOf())
|
||||
|
||||
@@ -80,14 +78,14 @@ class MessageListViewModel @Inject constructor(
|
||||
// By now, the repository should've committed this to the store.
|
||||
repository.messageDeliveredChannel.collectLatest { event ->
|
||||
pendingMessages.value =
|
||||
pendingMessages.value.filter { it.requestGuid != event.requestGuid }
|
||||
pendingMessages.value.filter { it.guid != event.requestGuid }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val messages: Flow<List<Message>>
|
||||
get() = repository.messagesChanged(conversation!!)
|
||||
.combine(pendingMessages) { a, b -> a.union(b.map { it.message }) }
|
||||
.combine(pendingMessages) { a, b -> a.union(b.map { it.asMessage() }) }
|
||||
.map { messages ->
|
||||
messages
|
||||
.sortedBy { it.date }
|
||||
@@ -100,27 +98,24 @@ class MessageListViewModel @Inject constructor(
|
||||
|
||||
fun enqueueOutgoingMessage(
|
||||
text: String,
|
||||
attachmentUris: Set<Uri>
|
||||
attachmentUris: Set<Uri>,
|
||||
context: Context,
|
||||
) {
|
||||
// TODO: Handle attachments!
|
||||
// Probably make a special OutgoingMessage object for this, since a lot of Message fields are
|
||||
// meaningless here. We don't have GUIDs yet either.
|
||||
|
||||
val outgoingMessage = Message(
|
||||
guid = UUID.randomUUID().toString(),
|
||||
text = text,
|
||||
sender = null,
|
||||
date = Date(),
|
||||
val outgoingMessage = OutgoingMessage(
|
||||
body = text,
|
||||
conversation = conversation!!,
|
||||
attachmentGUIDs = emptyList(),
|
||||
attachmentUris = attachmentUris,
|
||||
attachmentDataSource = { uri ->
|
||||
context.contentResolver.openInputStream(uri)
|
||||
}
|
||||
)
|
||||
|
||||
val outgoingGUID = repository.enqueueOutgoingMessage(outgoingMessage, conversation!!)
|
||||
pendingMessages.value = pendingMessages.value + listOf(OutgoingMessage(outgoingGUID, outgoingMessage))
|
||||
val outgoingGUID = repository.enqueueOutgoingMessage(outgoingMessage)
|
||||
pendingMessages.value = pendingMessages.value + listOf(outgoingMessage)
|
||||
}
|
||||
|
||||
fun isPendingMessage(message: Message): Boolean {
|
||||
return pendingMessages.value.any { it.message.guid == message.guid }
|
||||
return pendingMessages.value.any { it.guid == message.guid }
|
||||
}
|
||||
|
||||
fun markAsRead() = viewModelScope.launch {
|
||||
|
||||
Reference in New Issue
Block a user