mirror of
https://github.com/nextcloud/talk-android
synced 2025-03-07 14:48:07 +00:00
fixups and refactoring for sharing by context menu
When sharing a file by context menu that is not downloaded yet, do not open it after download but just share it. This is done by 'openWhenDownloaded' variable in chatMessage. Pass a method to downloadFileToCache, so it's more flexible what to do when download finished. Add some minor changes
This commit is contained in:
parent
869ddd1f47
commit
abb3389308
app/src/main/java/com/nextcloud/talk
adapters/messages
chat
models/json/chat
shareditems/adapters
ui/dialog
utils
@ -138,6 +138,7 @@ abstract class PreviewMessageViewHolder(itemView: View?, payload: Any?) :
|
||||
message.selectedIndividualHashMap!![KEY_NAME]!!,
|
||||
message.selectedIndividualHashMap!![KEY_ID]!!,
|
||||
message.selectedIndividualHashMap!![KEY_MIMETYPE],
|
||||
message.openWhenDownloaded,
|
||||
ProgressUi(progressBar, messageText, image)
|
||||
)
|
||||
} else if (message.getCalculateMessageType() === ChatMessage.MessageType.SINGLE_LINK_GIPHY_MESSAGE) {
|
||||
|
@ -889,7 +889,9 @@ class ChatActivity :
|
||||
}
|
||||
} else {
|
||||
Log.d(TAG, "Downloaded to cache")
|
||||
downloadFileToCache(message)
|
||||
downloadFileToCache(message, true) {
|
||||
setUpWaveform(message)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -901,33 +903,16 @@ class ChatActivity :
|
||||
message.isDownloadingVoiceMessage = true
|
||||
adapter?.update(message)
|
||||
CoroutineScope(Dispatchers.Default).launch {
|
||||
try {
|
||||
val r = audioFileToFloatArray(file)
|
||||
message.voiceMessageFloatArray = r
|
||||
withContext(Dispatchers.Main) {
|
||||
startPlayback(message)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
val r = audioFileToFloatArray(file)
|
||||
message.voiceMessageFloatArray = r
|
||||
withContext(Dispatchers.Main) {
|
||||
startPlayback(message)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
startPlayback(message)
|
||||
}
|
||||
}
|
||||
private fun setUpshare(message: ChatMessage) {
|
||||
val filename = message.selectedIndividualHashMap!!["name"]
|
||||
val file = File(context.cacheDir, filename!!)
|
||||
if (file.exists()) {
|
||||
CoroutineScope(Dispatchers.Default).launch {
|
||||
withContext(Dispatchers.Main) {
|
||||
share(message)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
share(message)
|
||||
}
|
||||
}
|
||||
|
||||
private fun initMessageHolders(): MessageHolders {
|
||||
val messageHolders = MessageHolders()
|
||||
@ -1952,8 +1937,13 @@ class ChatActivity :
|
||||
}
|
||||
|
||||
@SuppressLint("LongLogTag")
|
||||
private fun downloadFileToCache(message: ChatMessage) {
|
||||
private fun downloadFileToCache(
|
||||
message: ChatMessage,
|
||||
openWhenDownloaded: Boolean,
|
||||
funToCallWhenDownloadSuccessful: (() -> Unit)
|
||||
) {
|
||||
message.isDownloadingVoiceMessage = true
|
||||
message.openWhenDownloaded = openWhenDownloaded
|
||||
adapter?.update(message)
|
||||
|
||||
val baseUrl = message.activeUser!!.baseUrl
|
||||
@ -2004,12 +1994,7 @@ class ChatActivity :
|
||||
WorkManager.getInstance(context).getWorkInfoByIdLiveData(downloadWorker.id)
|
||||
.observeForever { workInfo: WorkInfo ->
|
||||
if (workInfo.state == WorkInfo.State.SUCCEEDED) {
|
||||
setUpWaveform(message)
|
||||
// startPlayback(message)
|
||||
setUpshare(message)
|
||||
}
|
||||
else {
|
||||
Log.e(TAG, "Error")
|
||||
funToCallWhenDownloadSuccessful()
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -4019,6 +4004,7 @@ class ChatActivity :
|
||||
intent.putExtras(bundle)
|
||||
startActivity(intent)
|
||||
}
|
||||
|
||||
fun share(message: ChatMessage) {
|
||||
val filename = message.selectedIndividualHashMap!!["name"]
|
||||
path = applicationContext.cacheDir.absolutePath + "/" + filename
|
||||
@ -4036,15 +4022,18 @@ class ChatActivity :
|
||||
}
|
||||
startActivity(Intent.createChooser(shareIntent, resources.getText(R.string.send_to)))
|
||||
}
|
||||
fun checkifsharable(message: ChatMessage) {
|
||||
|
||||
fun checkIfSharable(message: ChatMessage) {
|
||||
val filename = message.selectedIndividualHashMap!!["name"]
|
||||
path = applicationContext.cacheDir.absolutePath + "/" + filename
|
||||
val file = File(context.cacheDir, filename!!)
|
||||
if (file.exists()) {
|
||||
if (file.exists()) {
|
||||
share(message)
|
||||
} else {
|
||||
downloadFileToCache(message, false) {
|
||||
share(message)
|
||||
}else{
|
||||
downloadFileToCache(message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun openInFilesApp(message: ChatMessage) {
|
||||
|
@ -145,7 +145,9 @@ data class ChatMessage(
|
||||
|
||||
var expandableChildrenAmount: Int = 0,
|
||||
|
||||
var hiddenByCollapse: Boolean = false
|
||||
var hiddenByCollapse: Boolean = false,
|
||||
|
||||
var openWhenDownloaded: Boolean = true
|
||||
|
||||
) : Parcelable, MessageContentType, MessageContentType.Image {
|
||||
|
||||
|
@ -83,7 +83,8 @@ abstract class SharedItemsViewHolder(
|
||||
progressBar,
|
||||
null,
|
||||
image
|
||||
)
|
||||
),
|
||||
true
|
||||
)
|
||||
}
|
||||
|
||||
@ -91,7 +92,8 @@ abstract class SharedItemsViewHolder(
|
||||
item.name,
|
||||
item.id,
|
||||
item.mimeType,
|
||||
FileViewerUtils.ProgressUi(progressBar, null, image)
|
||||
true,
|
||||
FileViewerUtils.ProgressUi(progressBar, null, image),
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -334,7 +334,7 @@ class MessageActionsDialog(
|
||||
private fun initMenuShare(visible: Boolean) {
|
||||
if (visible) {
|
||||
dialogMessageActionsBinding.menuShare.setOnClickListener {
|
||||
chatActivity.checkifsharable(message)
|
||||
chatActivity.checkIfSharable(message)
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
|
@ -95,7 +95,8 @@ class FileViewerUtils(private val context: Context, private val user: User) {
|
||||
path,
|
||||
link,
|
||||
mimetype,
|
||||
progressUi
|
||||
progressUi,
|
||||
message.openWhenDownloaded
|
||||
)
|
||||
}
|
||||
|
||||
@ -104,14 +105,16 @@ class FileViewerUtils(private val context: Context, private val user: User) {
|
||||
path: String,
|
||||
link: String?,
|
||||
mimetype: String?,
|
||||
progressUi: ProgressUi
|
||||
progressUi: ProgressUi,
|
||||
openWhenDownloaded: Boolean
|
||||
) {
|
||||
if (isSupportedForInternalViewer(mimetype) || canBeHandledByExternalApp(mimetype, fileInfo.fileName)) {
|
||||
openOrDownloadFile(
|
||||
fileInfo,
|
||||
path,
|
||||
mimetype,
|
||||
progressUi
|
||||
progressUi,
|
||||
openWhenDownloaded
|
||||
)
|
||||
} else if (!link.isNullOrEmpty()) {
|
||||
openFileInFilesApp(link, fileInfo.fileId)
|
||||
@ -138,7 +141,8 @@ class FileViewerUtils(private val context: Context, private val user: User) {
|
||||
fileInfo: FileInfo,
|
||||
path: String,
|
||||
mimetype: String?,
|
||||
progressUi: ProgressUi
|
||||
progressUi: ProgressUi,
|
||||
openWhenDownloaded: Boolean
|
||||
) {
|
||||
val file = File(context.cacheDir, fileInfo.fileName)
|
||||
if (file.exists()) {
|
||||
@ -148,7 +152,8 @@ class FileViewerUtils(private val context: Context, private val user: User) {
|
||||
fileInfo,
|
||||
path,
|
||||
mimetype,
|
||||
progressUi
|
||||
progressUi,
|
||||
openWhenDownloaded
|
||||
)
|
||||
}
|
||||
}
|
||||
@ -276,7 +281,8 @@ class FileViewerUtils(private val context: Context, private val user: User) {
|
||||
fileInfo: FileInfo,
|
||||
path: String,
|
||||
mimetype: String?,
|
||||
progressUi: ProgressUi
|
||||
progressUi: ProgressUi,
|
||||
openWhenDownloaded: Boolean
|
||||
) {
|
||||
// check if download worker is already running
|
||||
val workers = WorkManager.getInstance(context).getWorkInfosByTag(fileInfo.fileId!!)
|
||||
@ -324,7 +330,8 @@ class FileViewerUtils(private val context: Context, private val user: User) {
|
||||
fileInfo.fileName,
|
||||
mimetype,
|
||||
workInfo!!,
|
||||
progressUi
|
||||
progressUi,
|
||||
openWhenDownloaded
|
||||
)
|
||||
}
|
||||
}
|
||||
@ -333,7 +340,8 @@ class FileViewerUtils(private val context: Context, private val user: User) {
|
||||
fileName: String,
|
||||
mimetype: String?,
|
||||
workInfo: WorkInfo,
|
||||
progressUi: ProgressUi
|
||||
progressUi: ProgressUi,
|
||||
openWhenDownloaded: Boolean
|
||||
) {
|
||||
when (workInfo.state) {
|
||||
WorkInfo.State.RUNNING -> {
|
||||
@ -347,13 +355,12 @@ class FileViewerUtils(private val context: Context, private val user: User) {
|
||||
}
|
||||
}
|
||||
WorkInfo.State.SUCCEEDED -> {
|
||||
if (progressUi.previewImage.isShown) {
|
||||
if (progressUi.previewImage.isShown && openWhenDownloaded) {
|
||||
openFileByMimetype(fileName, mimetype)
|
||||
} else {
|
||||
Log.d(
|
||||
TAG,
|
||||
"file " + fileName +
|
||||
" was downloaded but it's not opened because view is not shown on screen"
|
||||
Log.d(TAG, "file " + fileName +
|
||||
" was downloaded but it's not opened because view is not shown on screen or " +
|
||||
"openWhenDownloaded is false"
|
||||
)
|
||||
}
|
||||
progressUi.messageText?.text = fileName
|
||||
@ -372,6 +379,7 @@ class FileViewerUtils(private val context: Context, private val user: User) {
|
||||
fileName: String,
|
||||
fileId: String,
|
||||
mimeType: String?,
|
||||
openWhenDownloaded: Boolean,
|
||||
progressUi: ProgressUi
|
||||
) {
|
||||
val workers = WorkManager.getInstance(context).getWorkInfosByTag(fileId)
|
||||
@ -390,7 +398,8 @@ class FileViewerUtils(private val context: Context, private val user: User) {
|
||||
fileName,
|
||||
mimeType,
|
||||
info!!,
|
||||
progressUi
|
||||
progressUi,
|
||||
openWhenDownloaded
|
||||
)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user