Skip to content

Commit

Permalink
fixups and refactoring for sharing by context menu
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mahibi committed Sep 4, 2023
1 parent 869ddd1 commit abb3389
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
55 changes: 22 additions & 33 deletions app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,9 @@ class ChatActivity :
}
} else {
Log.d(TAG, "Downloaded to cache")
downloadFileToCache(message)
downloadFileToCache(message, true) {
setUpWaveform(message)
}
}
}
}
Expand All @@ -901,31 +903,14 @@ 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()
}
}
} 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 {
val r = audioFileToFloatArray(file)
message.voiceMessageFloatArray = r
withContext(Dispatchers.Main) {
share(message)
startPlayback(message)
}
}
} else {
share(message)
startPlayback(message)
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,17 @@ abstract class SharedItemsViewHolder(
progressBar,
null,
image
)
),
true
)
}

fileViewerUtils.resumeToUpdateViewsByProgress(
item.name,
item.id,
item.mimeType,
FileViewerUtils.ProgressUi(progressBar, null, image)
true,
FileViewerUtils.ProgressUi(progressBar, null, image),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ class MessageActionsDialog(
private fun initMenuShare(visible: Boolean) {
if (visible) {
dialogMessageActionsBinding.menuShare.setOnClickListener {
chatActivity.checkifsharable(message)
chatActivity.checkIfSharable(message)
dismiss()
}
}
Expand Down
37 changes: 23 additions & 14 deletions app/src/main/java/com/nextcloud/talk/utils/FileViewerUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ class FileViewerUtils(private val context: Context, private val user: User) {
path,
link,
mimetype,
progressUi
progressUi,
message.openWhenDownloaded
)
}

Expand All @@ -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)
Expand All @@ -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()) {
Expand All @@ -148,7 +152,8 @@ class FileViewerUtils(private val context: Context, private val user: User) {
fileInfo,
path,
mimetype,
progressUi
progressUi,
openWhenDownloaded
)
}
}
Expand Down Expand Up @@ -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!!)
Expand Down Expand Up @@ -324,7 +330,8 @@ class FileViewerUtils(private val context: Context, private val user: User) {
fileInfo.fileName,
mimetype,
workInfo!!,
progressUi
progressUi,
openWhenDownloaded
)
}
}
Expand All @@ -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 -> {
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -390,7 +398,8 @@ class FileViewerUtils(private val context: Context, private val user: User) {
fileName,
mimeType,
info!!,
progressUi
progressUi,
openWhenDownloaded
)
}
}
Expand Down

0 comments on commit abb3389

Please sign in to comment.