Captions should now be rendered last in order

- Created a kind of proto-group of files, by injecting JSON with the caption
- Works but not ideal. Planning on replacing solution when implementing new database schema

Signed-off-by: Julius Linus <julius.linus@nextcloud.com>
This commit is contained in:
Julius Linus 2024-01-12 11:51:01 -06:00 committed by rapterjet2004
parent f75d98c573
commit 20906eced2
5 changed files with 73 additions and 10 deletions

View File

@ -57,6 +57,7 @@ public class IncomingPreviewMessageViewHolder extends PreviewMessageViewHolder {
super.onBind(message); super.onBind(message);
if(!message.isVoiceMessage() if(!message.isVoiceMessage()
&& !Objects.equals(message.getMessage(), "{file}") && !Objects.equals(message.getMessage(), "{file}")
&& message.getEnableCaption()
) { ) {
Spanned processedMessageText = null; Spanned processedMessageText = null;
binding.incomingPreviewMessageBubble.setBackgroundResource(R.drawable.shape_grouped_incoming_message); binding.incomingPreviewMessageBubble.setBackgroundResource(R.drawable.shape_grouped_incoming_message);

View File

@ -56,6 +56,7 @@ public class OutcomingPreviewMessageViewHolder extends PreviewMessageViewHolder
super.onBind(message); super.onBind(message);
if(!message.isVoiceMessage() if(!message.isVoiceMessage()
&& !Objects.equals(message.getMessage(), "{file}") && !Objects.equals(message.getMessage(), "{file}")
&& message.getEnableCaption()
) { ) {
Spanned processedMessageText = null; Spanned processedMessageText = null;
binding.outgoingPreviewMessageBubble.setBackgroundResource(R.drawable.shape_grouped_outcoming_message); binding.outgoingPreviewMessageBubble.setBackgroundResource(R.drawable.shape_grouped_outcoming_message);

View File

@ -246,6 +246,8 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.greenrobot.eventbus.Subscribe import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode import org.greenrobot.eventbus.ThreadMode
import org.json.JSONException
import org.json.JSONObject
import retrofit2.HttpException import retrofit2.HttpException
import retrofit2.Response import retrofit2.Response
import java.io.File import java.io.File
@ -2673,16 +2675,25 @@ class ChatActivity :
} }
private fun uploadFiles(files: MutableList<String>, caption: String = "") { private fun uploadFiles(files: MutableList<String>, caption: String = "") {
for (i in 0 until files.size) { val captionJsonObject = if (caption != "") {
if (i == files.size - 1) { JSONObject(
uploadFile(files[i], false, caption) mapOf(
} else { "caption" to mapOf(
uploadFile(files[i], false) "groupID" to SystemClock.elapsedRealtime(),
} "captionText" to "\"$caption\""
).toString()
)
)
} else {
null
}
for (file in files) {
uploadFile(file, false, captionJsonObject)
} }
} }
private fun uploadFile(fileUri: String, isVoiceMessage: Boolean, caption: String = "") { private fun uploadFile(fileUri: String, isVoiceMessage: Boolean, metaDataJSONObject: JSONObject? = null) {
var metaData = "" var metaData = ""
if (!participantPermissions.hasChatPermission()) { if (!participantPermissions.hasChatPermission()) {
@ -2694,8 +2705,9 @@ class ChatActivity :
metaData = VOICE_MESSAGE_META_DATA metaData = VOICE_MESSAGE_META_DATA
} }
if (caption != "") { metaDataJSONObject?.let {
metaData = "{\"caption\":\"$caption\"}" metaData = metaDataJSONObject.toString()
Log.d("Julius", "MetaData: $metaData")
} }
try { try {
@ -3256,6 +3268,14 @@ class ChatActivity :
processCallStartedMessages(chatMessageList) processCallStartedMessages(chatMessageList)
val lastTen = adapter?.items?.take(ADAPTER_FILTER_LIMIT)
val prunedList = mutableListOf<ChatMessage>()
for (item in lastTen!!) {
if (item.item is ChatMessage) prunedList.add(item.item as ChatMessage)
}
prunedList.addAll(chatMessageList)
processFileCaptions(prunedList)
updateReadStatusOfAllMessages(newXChatLastCommonRead) updateReadStatusOfAllMessages(newXChatLastCommonRead)
adapter?.notifyDataSetChanged() adapter?.notifyDataSetChanged()
@ -3316,6 +3336,28 @@ class ChatActivity :
} }
} }
@SuppressLint("NotifyDataSetChanged", "NestedBlockDepth")
private fun processFileCaptions(chatMessageList: List<ChatMessage>) {
val captionMap = mutableMapOf<Long, ChatMessage>()
for (message in chatMessageList.reversed()) {
if (message.hasFileAttachment()) {
try {
val obj = JSONObject(message.message!!)
val groupID = obj.getLong("groupID")
val caption = obj.getString("captionText")
message.message = caption
if (captionMap.contains(groupID)) {
captionMap[groupID]!!.enableCaption = false
}
captionMap[groupID] = message
captionMap[groupID]!!.enableCaption = true
} catch (e: JSONException) {
Log.w(TAG, "File caption not in JSON form: $e")
}
}
}
}
private fun setupFieldsForPullChatMessages( private fun setupFieldsForPullChatMessages(
lookIntoFuture: Boolean, lookIntoFuture: Boolean,
xChatLastCommonRead: Int?, xChatLastCommonRead: Int?,
@ -4608,5 +4650,6 @@ class ChatActivity :
private const val MILISEC_15: Long = 15 private const val MILISEC_15: Long = 15
private const val LINEBREAK = "\n" private const val LINEBREAK = "\n"
private const val CURSOR_KEY = "_cursor" private const val CURSOR_KEY = "_cursor"
private const val ADAPTER_FILTER_LIMIT = 3
} }
} }

View File

@ -147,7 +147,9 @@ data class ChatMessage(
var hiddenByCollapse: Boolean = false, var hiddenByCollapse: Boolean = false,
var openWhenDownloaded: Boolean = true var openWhenDownloaded: Boolean = true,
var enableCaption: Boolean = false
) : Parcelable, MessageContentType, MessageContentType.Image { ) : Parcelable, MessageContentType, MessageContentType.Image {

View File

@ -38,6 +38,9 @@ import io.noties.markwon.core.MarkwonTheme
import io.noties.markwon.ext.strikethrough.StrikethroughPlugin import io.noties.markwon.ext.strikethrough.StrikethroughPlugin
import io.noties.markwon.ext.tasklist.TaskListDrawable import io.noties.markwon.ext.tasklist.TaskListDrawable
import io.noties.markwon.ext.tasklist.TaskListPlugin import io.noties.markwon.ext.tasklist.TaskListPlugin
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
class MessageUtils(val context: Context) { class MessageUtils(val context: Context) {
fun enrichChatReplyMessageText( fun enrichChatReplyMessageText(
@ -171,7 +174,20 @@ class MessageUtils(val context: Context) {
return markwon.toMarkdown(markdown) return markwon.toMarkdown(markdown)
} }
@Suppress("SwallowedException")
companion object { companion object {
fun isJSONValid(test: String): Boolean {
try {
JSONObject(test)
} catch (ex: JSONException) {
try {
JSONArray(test)
} catch (ex1: JSONException) {
return false
}
}
return true
}
private const val TAG = "MessageUtils" private const val TAG = "MessageUtils"
const val MAX_REPLY_LENGTH = 250 const val MAX_REPLY_LENGTH = 250
} }