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);
if(!message.isVoiceMessage()
&& !Objects.equals(message.getMessage(), "{file}")
&& message.getEnableCaption()
) {
Spanned processedMessageText = null;
binding.incomingPreviewMessageBubble.setBackgroundResource(R.drawable.shape_grouped_incoming_message);

View file

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

View file

@ -246,6 +246,8 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.json.JSONException
import org.json.JSONObject
import retrofit2.HttpException
import retrofit2.Response
import java.io.File
@ -2673,16 +2675,25 @@ class ChatActivity :
}
private fun uploadFiles(files: MutableList<String>, caption: String = "") {
for (i in 0 until files.size) {
if (i == files.size - 1) {
uploadFile(files[i], false, caption)
val captionJsonObject = if (caption != "") {
JSONObject(
mapOf(
"caption" to mapOf(
"groupID" to SystemClock.elapsedRealtime(),
"captionText" to "\"$caption\""
).toString()
)
)
} else {
uploadFile(files[i], false)
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 = ""
if (!participantPermissions.hasChatPermission()) {
@ -2694,8 +2705,9 @@ class ChatActivity :
metaData = VOICE_MESSAGE_META_DATA
}
if (caption != "") {
metaData = "{\"caption\":\"$caption\"}"
metaDataJSONObject?.let {
metaData = metaDataJSONObject.toString()
Log.d("Julius", "MetaData: $metaData")
}
try {
@ -3256,6 +3268,14 @@ class ChatActivity :
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)
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(
lookIntoFuture: Boolean,
xChatLastCommonRead: Int?,
@ -4608,5 +4650,6 @@ class ChatActivity :
private const val MILISEC_15: Long = 15
private const val LINEBREAK = "\n"
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 openWhenDownloaded: Boolean = true
var openWhenDownloaded: Boolean = true,
var enableCaption: Boolean = false
) : 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.tasklist.TaskListDrawable
import io.noties.markwon.ext.tasklist.TaskListPlugin
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
class MessageUtils(val context: Context) {
fun enrichChatReplyMessageText(
@ -171,7 +174,20 @@ class MessageUtils(val context: Context) {
return markwon.toMarkdown(markdown)
}
@Suppress("SwallowedException")
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"
const val MAX_REPLY_LENGTH = 250
}