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

View file

@ -19,10 +19,7 @@ package im.vector.app.features.media
import im.vector.app.core.date.VectorDateFormatter import im.vector.app.core.date.VectorDateFormatter
import im.vector.app.core.resources.StringProvider import im.vector.app.core.resources.StringProvider
import im.vector.lib.attachmentviewer.AttachmentInfo import im.vector.lib.attachmentviewer.AttachmentInfo
import kotlinx.coroutines.Dispatchers import org.matrix.android.sdk.api.extensions.tryOrNull
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.matrix.android.sdk.api.session.events.model.toModel 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.file.FileService
import org.matrix.android.sdk.api.session.room.model.message.MessageContent import org.matrix.android.sdk.api.session.room.model.message.MessageContent
@ -121,24 +118,19 @@ class RoomEventsAttachmentProvider(
return getItem(position) return getItem(position)
} }
override fun getFileForSharing(position: Int, callback: (File?) -> Unit) { override suspend fun getFileForSharing(position: Int): File? {
getItem(position).let { timelineEvent -> return getItem(position)
.let { timelineEvent ->
val messageContent = timelineEvent.root.getClearContent().toModel<MessageContent>() timelineEvent.root.getClearContent().toModel<MessageContent>() as? MessageWithAttachmentContent
as? MessageWithAttachmentContent
?: return@let
GlobalScope.launch {
val result = runCatching {
fileService.downloadFile(
fileName = messageContent.body,
mimeType = messageContent.mimeType,
url = messageContent.getFileUrl(),
elementToDecrypt = messageContent.encryptedFileInfo?.toElementToDecrypt())
} }
withContext(Dispatchers.Main) { ?.let { messageContent ->
callback(result.getOrNull()) 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.ViewCompat
import androidx.core.view.isInvisible import androidx.core.view.isInvisible
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope
import androidx.transition.Transition import androidx.transition.Transition
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.di.ActiveSessionHolder 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.app.features.themes.ThemeUtils
import im.vector.lib.attachmentviewer.AttachmentCommands import im.vector.lib.attachmentviewer.AttachmentCommands
import im.vector.lib.attachmentviewer.AttachmentViewerActivity import im.vector.lib.attachmentviewer.AttachmentViewerActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.parcelize.Parcelize import kotlinx.parcelize.Parcelize
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
@ -264,9 +267,15 @@ class VectorAttachmentViewerActivity : AttachmentViewerActivity(), BaseAttachmen
} }
override fun onShareTapped() { override fun onShareTapped() {
currentSourceProvider?.getFileForSharing(currentPosition) { data -> lifecycleScope.launch(Dispatchers.IO) {
if (data != null && lifecycle.currentState.isAtLeast(Lifecycle.State.RESUMED)) { val file = currentSourceProvider?.getFileForSharing(currentPosition) ?: return@launch
shareMedia(this@VectorAttachmentViewerActivity, data, getMimeTypeFromUri(this@VectorAttachmentViewerActivity, data.toUri()))
withContext(Dispatchers.Main) {
shareMedia(
this@VectorAttachmentViewerActivity,
file,
getMimeTypeFromUri(this@VectorAttachmentViewerActivity, file.toUri())
)
} }
} }
} }