Skip to content

Commit

Permalink
WIP file captions
Browse files Browse the repository at this point in the history
- Added dialog_file_attachment_preview.xml
- Added FileAttachmentPreviewFragment.kt
- Edited ChatActivity to add captions to uploaded files, also refactored some code
- Edited the Outgoing, Incoming, and Generic Preview holders to have a caption
- Fixed some bugs with RemoteFileBrowser

Signed-off-by: Julius Linus <[email protected]>
  • Loading branch information
rapterjet2004 committed Nov 3, 2023
1 parent e8870ac commit 37e3458
Show file tree
Hide file tree
Showing 11 changed files with 400 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

package com.nextcloud.talk.adapters.messages;

import android.text.Spanned;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
Expand All @@ -33,6 +35,9 @@
import com.nextcloud.talk.databinding.ItemCustomIncomingPreviewMessageBinding;
import com.nextcloud.talk.databinding.ReactionsInsideMessageBinding;
import com.nextcloud.talk.models.json.chat.ChatMessage;
import com.nextcloud.talk.utils.TextMatchers;

import java.util.HashMap;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
Expand All @@ -49,7 +54,36 @@ public IncomingPreviewMessageViewHolder(View itemView, Object payload) {
@Override
public void onBind(@NonNull ChatMessage message) {
super.onBind(message);

if(!message.isVoiceMessage()) {
assert viewThemeUtils != null;
Spanned processedMessageText = messageUtils.enrichChatMessageText(
binding.messageCaption.getContext(),
message,
true,
viewThemeUtils);

assert processedMessageText != null;
processedMessageText = messageUtils.processMessageParameters(
binding.messageCaption.getContext(),
viewThemeUtils,
processedMessageText,
message,
itemView);

assert context != null;
var textSize = context.getResources().getDimension(R.dimen.chat_text_size);
HashMap<String, HashMap<String, String>> messageParameters = message.getMessageParameters();
if (
(messageParameters == null || messageParameters.size() <= 0) &&
TextMatchers.isMessageWithSingleEmoticonOnly(message.getText())
) {
textSize = (float) (textSize * IncomingTextMessageViewHolder.TEXT_SIZE_MULTIPLIER);
itemView.setSelected(true);
}
binding.messageCaption.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
binding.messageCaption.setText(processedMessageText);
viewThemeUtils.talk.themeIncomingMessageBubble(binding.messageCaption, true, false);
}
binding.messageAuthor.setText(message.getActorDisplayName());
binding.messageText.setTextColor(ContextCompat.getColor(binding.messageText.getContext(),
R.color.no_emphasis_text));
Expand All @@ -63,6 +97,12 @@ public EmojiTextView getMessageText() {
return binding.messageText;
}

@NonNull
@Override
public EmojiTextView getMessageCaption() {
return binding.messageCaption;
}

@Override
public ProgressBar getProgressBar() {
return binding.progressBar;
Expand Down Expand Up @@ -99,5 +139,4 @@ public ProgressBar getPreviewContactProgressBar() {

@Override
public ReactionsInsideMessageBinding getReactionsBinding(){ return binding.reactions; }

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

package com.nextcloud.talk.adapters.messages;

import android.text.Spanned;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
Expand All @@ -31,6 +33,9 @@
import com.nextcloud.talk.databinding.ItemCustomOutcomingPreviewMessageBinding;
import com.nextcloud.talk.databinding.ReactionsInsideMessageBinding;
import com.nextcloud.talk.models.json.chat.ChatMessage;
import com.nextcloud.talk.utils.TextMatchers;

import java.util.HashMap;

import androidx.core.content.ContextCompat;
import androidx.emoji2.widget.EmojiTextView;
Expand All @@ -48,6 +53,38 @@ public OutcomingPreviewMessageViewHolder(View itemView) {
public void onBind(ChatMessage message) {
super.onBind(message);

if(!message.isVoiceMessage()) {
assert viewThemeUtils != null;
Spanned processedMessageText = messageUtils.enrichChatMessageText(
binding.messageCaption.getContext(),
message,
false,
viewThemeUtils);

assert processedMessageText != null;
processedMessageText = messageUtils.processMessageParameters(
binding.messageCaption.getContext(),
viewThemeUtils,
processedMessageText,
message,
itemView);

assert context != null;
var textSize = context.getResources().getDimension(R.dimen.chat_text_size);
HashMap<String, HashMap<String, String>> messageParameters = message.getMessageParameters();
if (
(messageParameters == null || messageParameters.size() <= 0) &&
TextMatchers.isMessageWithSingleEmoticonOnly(message.getText())
) {
textSize = (float)(textSize * IncomingTextMessageViewHolder.TEXT_SIZE_MULTIPLIER);
itemView.setSelected(true);
}
binding.messageCaption.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
binding.messageCaption.setText(processedMessageText);
viewThemeUtils.talk.themeOutgoingMessageBubble(binding.messageCaption, true, false);
}


binding.messageText.setTextColor(ContextCompat.getColor(binding.messageText.getContext(),
R.color.no_emphasis_text));
binding.messageTime.setTextColor(ContextCompat.getColor(binding.messageText.getContext(),
Expand Down Expand Up @@ -91,4 +128,7 @@ public ProgressBar getPreviewContactProgressBar() {

@Override
public ReactionsInsideMessageBinding getReactionsBinding() { return binding.reactions; }

@Override
public EmojiTextView getMessageCaption() { return binding.messageCaption; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ import com.nextcloud.talk.databinding.ReactionsInsideMessageBinding
import com.nextcloud.talk.extensions.loadChangelogBotAvatar
import com.nextcloud.talk.models.json.chat.ChatMessage
import com.nextcloud.talk.ui.theme.ViewThemeUtils
import com.nextcloud.talk.users.UserManager
import com.nextcloud.talk.utils.DateUtils
import com.nextcloud.talk.utils.DisplayUtils
import com.nextcloud.talk.utils.DrawableUtils.getDrawableResourceIdForMimeType
import com.nextcloud.talk.utils.FileViewerUtils
import com.nextcloud.talk.utils.FileViewerUtils.ProgressUi
import com.nextcloud.talk.utils.message.MessageUtils
import com.stfalcon.chatkit.messages.MessageHolders.IncomingImageMessageViewHolder
import io.reactivex.Single
import io.reactivex.SingleObserver
Expand All @@ -80,6 +82,12 @@ abstract class PreviewMessageViewHolder(itemView: View?, payload: Any?) :
@Inject
lateinit var dateUtils: DateUtils

@Inject
lateinit var messageUtils: MessageUtils

@Inject
lateinit var userManager: UserManager

@JvmField
@Inject
var okHttpClient: OkHttpClient? = null
Expand Down Expand Up @@ -111,6 +119,12 @@ abstract class PreviewMessageViewHolder(itemView: View?, payload: Any?) :
if (message.getCalculateMessageType() === ChatMessage.MessageType.SINGLE_NC_ATTACHMENT_MESSAGE) {
fileViewerUtils = FileViewerUtils(context!!, message.activeUser!!)
val fileName = message.selectedIndividualHashMap!![KEY_NAME]

messageCaption.visibility = if (message.message != "{file}") {
View.VISIBLE
} else {
View.GONE
}
messageText.text = fileName

if (message.activeUser != null &&
Expand Down Expand Up @@ -223,7 +237,11 @@ abstract class PreviewMessageViewHolder(itemView: View?, payload: Any?) :
image = previewContactPhoto
image.setImageDrawable(ContextCompat.getDrawable(context!!, R.drawable.ic_mimetype_text_vcard))
}
} else {
} else if (message.displayImageStack()) {
previewContainer.visibility = View.GONE
previewContactContainer.visibility = View.GONE
}
else {
previewContainer.visibility = View.VISIBLE
previewContactContainer.visibility = View.GONE
}
Expand Down Expand Up @@ -312,6 +330,7 @@ abstract class PreviewMessageViewHolder(itemView: View?, payload: Any?) :
}

abstract val messageText: EmojiTextView
abstract val messageCaption: EmojiTextView
abstract val previewContainer: View
abstract val previewContactContainer: MaterialCardView
abstract val previewContactPhoto: ImageView
Expand Down
125 changes: 60 additions & 65 deletions app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ import android.widget.RelativeLayout.LayoutParams
import android.widget.SeekBar
import android.widget.TextView
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.view.ContextThemeWrapper
import androidx.core.content.ContextCompat
import androidx.core.content.FileProvider
Expand Down Expand Up @@ -113,7 +112,6 @@ import coil.target.Target
import coil.transform.CircleCropTransformation
import com.google.android.flexbox.FlexboxLayout
import com.google.android.material.button.MaterialButton
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.snackbar.Snackbar
import com.nextcloud.android.common.ui.theme.utils.ColorRole
import com.nextcloud.talk.BuildConfig
Expand Down Expand Up @@ -188,6 +186,7 @@ import com.nextcloud.talk.ui.StatusDrawable
import com.nextcloud.talk.ui.bottom.sheet.ProfileBottomSheet
import com.nextcloud.talk.ui.dialog.AttachmentDialog
import com.nextcloud.talk.ui.dialog.DateTimePickerFragment
import com.nextcloud.talk.ui.dialog.FileAttachmentPreviewFragment
import com.nextcloud.talk.ui.dialog.MessageActionsDialog
import com.nextcloud.talk.ui.dialog.ShowReactionsDialog
import com.nextcloud.talk.ui.recyclerview.MessageSwipeActions
Expand Down Expand Up @@ -2411,40 +2410,12 @@ class ChatActivity :
filenamesWithLineBreaks.append(filename).append("\n")
}

val confirmationQuestion = when (filesToUpload.size) {
1 -> context.resources?.getString(R.string.nc_upload_confirm_send_single)?.let {
String.format(it, title.trim())
}

else -> context.resources?.getString(R.string.nc_upload_confirm_send_multiple)?.let {
String.format(it, title.trim())
}
}

binding.messageInputView.context?.let {
val materialAlertDialogBuilder = MaterialAlertDialogBuilder(it)
.setTitle(confirmationQuestion)
.setMessage(filenamesWithLineBreaks.toString())
.setPositiveButton(R.string.nc_yes) { _, _ ->
if (permissionUtil.isFilesPermissionGranted()) {
uploadFiles(filesToUpload)
} else {
UploadAndShareFilesWorker.requestStoragePermission(this)
}
}
.setNegativeButton(R.string.nc_no) { _, _ ->
// unused atm
}

viewThemeUtils.dialog.colorMaterialAlertDialogBackground(it, materialAlertDialogBuilder)

val dialog = materialAlertDialogBuilder.show()

viewThemeUtils.platform.colorTextButtons(
dialog.getButton(AlertDialog.BUTTON_POSITIVE),
dialog.getButton(AlertDialog.BUTTON_NEGATIVE)
)
}
val newFragment: DialogFragment = FileAttachmentPreviewFragment.newInstance(
filenamesWithLineBreaks.toString(),
filesToUpload,
this::uploadFiles
)
newFragment.show(supportFragmentManager, FileAttachmentPreviewFragment.TAG)
} catch (e: IllegalStateException) {
context.resources?.getString(R.string.nc_upload_failed)?.let {
Snackbar.make(
Expand Down Expand Up @@ -2506,7 +2477,19 @@ class ChatActivity :
}

if (permissionUtil.isFilesPermissionGranted()) {
uploadFiles(filesToUpload)
val filenamesWithLineBreaks = StringBuilder("\n")

for (file in filesToUpload) {
val filename = FileUtils.getFileName(Uri.parse(file), context)
filenamesWithLineBreaks.append(filename).append("\n")
}

val newFragment: DialogFragment = FileAttachmentPreviewFragment.newInstance(
filenamesWithLineBreaks.toString(),
filesToUpload,
this::uploadFiles
)
newFragment.show(supportFragmentManager, FileAttachmentPreviewFragment.TAG)
} else {
UploadAndShareFilesWorker.requestStoragePermission(this)
}
Expand Down Expand Up @@ -2630,13 +2613,17 @@ class ChatActivity :
}
}

private fun uploadFiles(files: MutableList<String>) {
for (file in files) {
uploadFile(file, false)
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)
} else {
uploadFile(files[i], false)
}
}
}

private fun uploadFile(fileUri: String, isVoiceMessage: Boolean) {
private fun uploadFile(fileUri: String, isVoiceMessage: Boolean, caption: String = "") {
var metaData = ""

if (!participantPermissions.hasChatPermission()) {
Expand All @@ -2648,6 +2635,10 @@ class ChatActivity :
metaData = VOICE_MESSAGE_META_DATA
}

if (caption != "") {
metaData = "{\"caption\":\"$caption\"}"
}

try {
require(fileUri.isNotEmpty())
UploadAndShareFilesWorker.upload(
Expand Down Expand Up @@ -3210,30 +3201,7 @@ class ChatActivity :
Integer.parseInt(it)
}

try {
val mostRecentCallSystemMessage = adapter?.items?.first {
it.item is ChatMessage &&
(it.item as ChatMessage).systemMessageType in
listOf(
ChatMessage.SystemMessageType.CALL_STARTED,
ChatMessage.SystemMessageType.CALL_JOINED,
ChatMessage.SystemMessageType.CALL_LEFT,
ChatMessage.SystemMessageType.CALL_ENDED,
ChatMessage.SystemMessageType.CALL_TRIED,
ChatMessage.SystemMessageType.CALL_ENDED_EVERYONE,
ChatMessage.SystemMessageType.CALL_MISSED
)
}?.item

if (mostRecentCallSystemMessage != null) {
processMostRecentMessage(
mostRecentCallSystemMessage as ChatMessage,
chatMessageList
)
}
} catch (e: java.util.NoSuchElementException) {
Log.d(TAG, "No System messages found $e")
}
processCallStartedMessages(chatMessageList)

updateReadStatusOfAllMessages(newXChatLastCommonRead)
adapter?.notifyDataSetChanged()
Expand Down Expand Up @@ -3268,6 +3236,33 @@ class ChatActivity :
})
}

private fun processCallStartedMessages(chatMessageList: List<ChatMessage>) {
try {
val mostRecentCallSystemMessage = adapter?.items?.first {
it.item is ChatMessage &&
(it.item as ChatMessage).systemMessageType in
listOf(
ChatMessage.SystemMessageType.CALL_STARTED,
ChatMessage.SystemMessageType.CALL_JOINED,
ChatMessage.SystemMessageType.CALL_LEFT,
ChatMessage.SystemMessageType.CALL_ENDED,
ChatMessage.SystemMessageType.CALL_TRIED,
ChatMessage.SystemMessageType.CALL_ENDED_EVERYONE,
ChatMessage.SystemMessageType.CALL_MISSED
)
}?.item

if (mostRecentCallSystemMessage != null) {
processMostRecentMessage(
mostRecentCallSystemMessage as ChatMessage,
chatMessageList
)
}
} catch (e: NoSuchElementException) {
Log.d(TAG, "No System messages found $e")
}
}

private fun setupFieldsForPullChatMessages(
lookIntoFuture: Boolean,
xChatLastCommonRead: Int?,
Expand Down
Loading

0 comments on commit 37e3458

Please sign in to comment.