mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2024-11-26 19:36:08 +03:00
Remove usage of GlobalScope
This commit is contained in:
parent
1f7482922d
commit
535d266cc2
3 changed files with 101 additions and 88 deletions
|
@ -43,8 +43,7 @@ import im.vector.app.R
|
|||
import im.vector.app.features.notifications.NotificationUtils
|
||||
import im.vector.app.features.themes.ThemeUtils
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import okio.buffer
|
||||
import okio.sink
|
||||
import okio.source
|
||||
|
@ -57,6 +56,7 @@ import timber.log.Timber
|
|||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.FileOutputStream
|
||||
import java.lang.IllegalStateException
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
|
@ -344,7 +344,8 @@ private fun appendTimeToFilename(name: String): String {
|
|||
return """${filename}_$dateExtension.$fileExtension"""
|
||||
}
|
||||
|
||||
fun saveMedia(context: Context, file: File, title: String, mediaMimeType: String?, notificationUtils: NotificationUtils) {
|
||||
suspend fun saveMedia(context: Context, file: File, title: String, mediaMimeType: String?, notificationUtils: NotificationUtils) {
|
||||
withContext(Dispatchers.IO) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
val filename = appendTimeToFilename(title)
|
||||
|
||||
|
@ -365,6 +366,7 @@ fun saveMedia(context: Context, file: File, title: String, mediaMimeType: String
|
|||
val uri = context.contentResolver.insert(externalContentUri, values)
|
||||
if (uri == null) {
|
||||
Toast.makeText(context, R.string.error_saving_media_file, Toast.LENGTH_LONG).show()
|
||||
throw IllegalStateException(context.getString(R.string.error_saving_media_file))
|
||||
} else {
|
||||
val source = file.inputStream().source().buffer()
|
||||
context.contentResolver.openOutputStream(uri)?.sink()?.buffer()?.let { sink ->
|
||||
|
@ -386,16 +388,19 @@ fun saveMedia(context: Context, file: File, title: String, mediaMimeType: String
|
|||
saveMediaLegacy(context, mediaMimeType, title, file)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
private fun saveMediaLegacy(context: Context, mediaMimeType: String?, title: String, file: File) {
|
||||
private fun saveMediaLegacy(context: Context,
|
||||
mediaMimeType: String?,
|
||||
title: String,
|
||||
file: File) {
|
||||
val state = Environment.getExternalStorageState()
|
||||
if (Environment.MEDIA_MOUNTED != state) {
|
||||
context.toast(context.getString(R.string.error_saving_media_file))
|
||||
return
|
||||
throw IllegalStateException(context.getString(R.string.error_saving_media_file))
|
||||
}
|
||||
|
||||
GlobalScope.launch(Dispatchers.IO) {
|
||||
val dest = when {
|
||||
mediaMimeType?.isMimeTypeImage() == true -> Environment.DIRECTORY_PICTURES
|
||||
mediaMimeType?.isMimeTypeVideo() == true -> Environment.DIRECTORY_MOVIES
|
||||
|
@ -424,10 +429,8 @@ private fun saveMediaLegacy(context: Context, mediaMimeType: String?, title: Str
|
|||
addToGallery(savedFile, mediaMimeType, context)
|
||||
}
|
||||
} catch (error: Throwable) {
|
||||
GlobalScope.launch(Dispatchers.Main) {
|
||||
context.toast(context.getString(R.string.error_saving_media_file))
|
||||
}
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1745,8 +1745,7 @@ class RoomDetailFragment @Inject constructor(
|
|||
session.coroutineScope.launch {
|
||||
val result = runCatching { session.fileService().downloadFile(messageContent = action.messageContent) }
|
||||
if (!isAdded) return@launch
|
||||
result.fold(
|
||||
{
|
||||
result.mapCatching {
|
||||
saveMedia(
|
||||
context = requireContext(),
|
||||
file = it,
|
||||
|
@ -1754,11 +1753,11 @@ class RoomDetailFragment @Inject constructor(
|
|||
mediaMimeType = action.messageContent.mimeType ?: getMimeTypeFromUri(requireContext(), it.toUri()),
|
||||
notificationUtils = notificationUtils
|
||||
)
|
||||
},
|
||||
{
|
||||
}
|
||||
.onFailure {
|
||||
if (!isAdded) return@onFailure
|
||||
showErrorInSnackbar(it)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@ import android.view.LayoutInflater
|
|||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.core.net.toUri
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.airbnb.mvrx.args
|
||||
import com.airbnb.mvrx.fragmentViewModel
|
||||
import com.airbnb.mvrx.withState
|
||||
|
@ -36,6 +37,7 @@ import im.vector.app.databinding.FragmentRoomUploadsBinding
|
|||
import im.vector.app.features.home.AvatarRenderer
|
||||
import im.vector.app.features.notifications.NotificationUtils
|
||||
import im.vector.app.features.roomprofile.RoomProfileArgs
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
import org.matrix.android.sdk.api.util.toMatrixItem
|
||||
import javax.inject.Inject
|
||||
|
@ -76,6 +78,8 @@ class RoomUploadsFragment @Inject constructor(
|
|||
shareMedia(requireContext(), it.file, getMimeTypeFromUri(requireContext(), it.file.toUri()))
|
||||
}
|
||||
is RoomUploadsViewEvents.FileReadyForSaving -> {
|
||||
lifecycleScope.launch {
|
||||
runCatching {
|
||||
saveMedia(
|
||||
context = requireContext(),
|
||||
file = it.file,
|
||||
|
@ -83,6 +87,13 @@ class RoomUploadsFragment @Inject constructor(
|
|||
mediaMimeType = getMimeTypeFromUri(requireContext(), it.file.toUri()),
|
||||
notificationUtils = notificationUtils
|
||||
)
|
||||
}.onFailure { failure ->
|
||||
if (!isAdded) return@onFailure
|
||||
showErrorInSnackbar(failure)
|
||||
}
|
||||
|
||||
}
|
||||
Unit
|
||||
}
|
||||
is RoomUploadsViewEvents.Failure -> showFailure(it.throwable)
|
||||
}.exhaustive
|
||||
|
|
Loading…
Reference in a new issue