Remove usage of GlobalScope

This commit is contained in:
Benoit Marty 2021-05-14 14:55:02 +02:00
parent bf14251c3d
commit 555eada37a
4 changed files with 40 additions and 45 deletions

View file

@ -178,5 +178,5 @@ abstract class BaseAttachmentProvider<Type>(
// TODO("Not yet implemented")
}
abstract fun getFileForSharing(position: Int, callback: ((File?) -> Unit))
abstract suspend fun getFileForSharing(position: Int): File?
}

View file

@ -19,10 +19,7 @@ package im.vector.app.features.media
import im.vector.app.core.date.VectorDateFormatter
import im.vector.app.core.resources.StringProvider
import im.vector.lib.attachmentviewer.AttachmentInfo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.session.file.FileService
import org.matrix.android.sdk.api.session.room.Room
import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
@ -78,20 +75,17 @@ class DataAttachmentRoomProvider(
return room?.getTimeLineEvent(item.eventId)
}
override fun getFileForSharing(position: Int, callback: (File?) -> Unit) {
val item = getItem(position)
GlobalScope.launch {
val result = runCatching {
fileService.downloadFile(
fileName = item.filename,
mimeType = item.mimeType,
url = item.url,
elementToDecrypt = item.elementToDecrypt
)
}
withContext(Dispatchers.Main) {
callback(result.getOrNull())
}
}
override suspend fun getFileForSharing(position: Int): File? {
return getItem(position)
.let { item ->
tryOrNull {
fileService.downloadFile(
fileName = item.filename,
mimeType = item.mimeType,
url = item.url,
elementToDecrypt = item.elementToDecrypt
)
}
}
}
}

View file

@ -19,10 +19,7 @@ package im.vector.app.features.media
import im.vector.app.core.date.VectorDateFormatter
import im.vector.app.core.resources.StringProvider
import im.vector.lib.attachmentviewer.AttachmentInfo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.session.events.model.toModel
import org.matrix.android.sdk.api.session.file.FileService
import org.matrix.android.sdk.api.session.room.model.message.MessageContent
@ -121,24 +118,19 @@ class RoomEventsAttachmentProvider(
return getItem(position)
}
override fun getFileForSharing(position: Int, callback: (File?) -> Unit) {
getItem(position).let { timelineEvent ->
val messageContent = timelineEvent.root.getClearContent().toModel<MessageContent>()
as? MessageWithAttachmentContent
?: return@let
GlobalScope.launch {
val result = runCatching {
fileService.downloadFile(
fileName = messageContent.body,
mimeType = messageContent.mimeType,
url = messageContent.getFileUrl(),
elementToDecrypt = messageContent.encryptedFileInfo?.toElementToDecrypt())
override suspend fun getFileForSharing(position: Int): File? {
return getItem(position)
.let { timelineEvent ->
timelineEvent.root.getClearContent().toModel<MessageContent>() as? MessageWithAttachmentContent
}
withContext(Dispatchers.Main) {
callback(result.getOrNull())
?.let { messageContent ->
tryOrNull {
fileService.downloadFile(
fileName = messageContent.body,
mimeType = messageContent.mimeType,
url = messageContent.getFileUrl(),
elementToDecrypt = messageContent.encryptedFileInfo?.toElementToDecrypt())
}
}
}
}
}
}

View file

@ -28,7 +28,7 @@ import androidx.core.transition.addListener
import androidx.core.view.ViewCompat
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.transition.Transition
import im.vector.app.R
import im.vector.app.core.di.ActiveSessionHolder
@ -42,6 +42,9 @@ import im.vector.app.features.themes.ActivityOtherThemes
import im.vector.app.features.themes.ThemeUtils
import im.vector.lib.attachmentviewer.AttachmentCommands
import im.vector.lib.attachmentviewer.AttachmentViewerActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.parcelize.Parcelize
import timber.log.Timber
import javax.inject.Inject
@ -264,9 +267,15 @@ class VectorAttachmentViewerActivity : AttachmentViewerActivity(), BaseAttachmen
}
override fun onShareTapped() {
currentSourceProvider?.getFileForSharing(currentPosition) { data ->
if (data != null && lifecycle.currentState.isAtLeast(Lifecycle.State.RESUMED)) {
shareMedia(this@VectorAttachmentViewerActivity, data, getMimeTypeFromUri(this@VectorAttachmentViewerActivity, data.toUri()))
lifecycleScope.launch(Dispatchers.IO) {
val file = currentSourceProvider?.getFileForSharing(currentPosition) ?: return@launch
withContext(Dispatchers.Main) {
shareMedia(
this@VectorAttachmentViewerActivity,
file,
getMimeTypeFromUri(this@VectorAttachmentViewerActivity, file.toUri())
)
}
}
}