Remove usage of GlobalScope

This commit is contained in:
Benoit Marty 2021-05-14 15:44:22 +02:00
parent 8211cc266f
commit 1f7482922d
5 changed files with 46 additions and 19 deletions

View file

@ -18,6 +18,7 @@ package im.vector.app.features.media
import im.vector.app.core.date.VectorDateFormatter
import im.vector.app.core.resources.StringProvider
import kotlinx.coroutines.CoroutineScope
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.room.Room
import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
@ -30,24 +31,31 @@ class AttachmentProviderFactory @Inject constructor(
private val session: Session
) {
fun createProvider(attachments: List<TimelineEvent>): RoomEventsAttachmentProvider {
fun createProvider(attachments: List<TimelineEvent>,
coroutineScope: CoroutineScope
): RoomEventsAttachmentProvider {
return RoomEventsAttachmentProvider(
attachments,
imageContentRenderer,
vectorDateFormatter,
session.fileService(),
stringProvider
attachments = attachments,
imageContentRenderer = imageContentRenderer,
dateFormatter = vectorDateFormatter,
fileService = session.fileService(),
coroutineScope = coroutineScope,
stringProvider = stringProvider
)
}
fun createProvider(attachments: List<AttachmentData>, room: Room?): DataAttachmentRoomProvider {
fun createProvider(attachments: List<AttachmentData>,
room: Room?,
coroutineScope: CoroutineScope
): DataAttachmentRoomProvider {
return DataAttachmentRoomProvider(
attachments,
room,
imageContentRenderer,
vectorDateFormatter,
session.fileService(),
stringProvider
attachments = attachments,
room = room,
imageContentRenderer = imageContentRenderer,
dateFormatter = vectorDateFormatter,
fileService = session.fileService(),
coroutineScope = coroutineScope,
stringProvider = stringProvider
)
}
}

View file

@ -31,8 +31,8 @@ import im.vector.lib.attachmentviewer.AttachmentInfo
import im.vector.lib.attachmentviewer.AttachmentSourceProvider
import im.vector.lib.attachmentviewer.ImageLoaderTarget
import im.vector.lib.attachmentviewer.VideoLoaderTarget
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.matrix.android.sdk.api.session.events.model.isVideoMessage
@ -44,6 +44,7 @@ abstract class BaseAttachmentProvider<Type>(
private val attachments: List<Type>,
private val imageContentRenderer: ImageContentRenderer,
protected val fileService: FileService,
private val coroutineScope: CoroutineScope,
private val dateFormatter: VectorDateFormatter,
private val stringProvider: StringProvider
) : AttachmentSourceProvider {
@ -155,7 +156,7 @@ abstract class BaseAttachmentProvider<Type>(
target.onVideoURLReady(info.uid, data.url)
} else {
target.onVideoFileLoading(info.uid)
GlobalScope.launch(Dispatchers.Main) {
coroutineScope.launch(Dispatchers.IO) {
val result = runCatching {
fileService.downloadFile(
fileName = data.filename,

View file

@ -19,6 +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.CoroutineScope
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
@ -32,8 +33,16 @@ class DataAttachmentRoomProvider(
imageContentRenderer: ImageContentRenderer,
dateFormatter: VectorDateFormatter,
fileService: FileService,
coroutineScope: CoroutineScope,
stringProvider: StringProvider
) : BaseAttachmentProvider<AttachmentData>(attachments, imageContentRenderer, fileService, dateFormatter, stringProvider) {
) : BaseAttachmentProvider<AttachmentData>(
attachments = attachments,
imageContentRenderer = imageContentRenderer,
fileService = fileService,
coroutineScope = coroutineScope,
dateFormatter = dateFormatter,
stringProvider = stringProvider
) {
override fun getAttachmentInfoAt(position: Int): AttachmentInfo {
return getItem(position).let {

View file

@ -19,6 +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.CoroutineScope
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
@ -38,8 +39,16 @@ class RoomEventsAttachmentProvider(
imageContentRenderer: ImageContentRenderer,
dateFormatter: VectorDateFormatter,
fileService: FileService,
coroutineScope: CoroutineScope,
stringProvider: StringProvider
) : BaseAttachmentProvider<TimelineEvent>(attachments, imageContentRenderer, fileService, dateFormatter, stringProvider) {
) : BaseAttachmentProvider<TimelineEvent>(
attachments = attachments,
imageContentRenderer = imageContentRenderer,
fileService = fileService,
coroutineScope = coroutineScope,
dateFormatter = dateFormatter,
stringProvider = stringProvider
) {
override fun getAttachmentInfoAt(position: Int): AttachmentInfo {
return getItem(position).let {

View file

@ -122,11 +122,11 @@ class VectorAttachmentViewerActivity : AttachmentViewerActivity(), BaseAttachmen
val inMemoryData = intent.getParcelableArrayListExtra<AttachmentData>(EXTRA_IN_MEMORY_DATA)
val sourceProvider = if (inMemoryData != null) {
initialIndex = inMemoryData.indexOfFirst { it.eventId == args.eventId }.coerceAtLeast(0)
dataSourceFactory.createProvider(inMemoryData, room)
dataSourceFactory.createProvider(inMemoryData, room, lifecycleScope)
} else {
val events = room?.getAttachmentMessages().orEmpty()
initialIndex = events.indexOfFirst { it.eventId == args.eventId }.coerceAtLeast(0)
dataSourceFactory.createProvider(events)
dataSourceFactory.createProvider(events, lifecycleScope)
}
sourceProvider.interactionListener = this
setSourceProvider(sourceProvider)