Media: add more tests
This commit is contained in:
parent
0a268dc27f
commit
ced60c672e
15 changed files with 354 additions and 53 deletions
|
|
@ -18,10 +18,12 @@ package io.element.android.features.messages.impl.media.local
|
|||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import androidx.core.net.toUri
|
||||
import com.squareup.anvil.annotations.ContributesBinding
|
||||
import io.element.android.libraries.core.mimetype.MimeTypes
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import io.element.android.libraries.di.ApplicationContext
|
||||
import io.element.android.libraries.matrix.api.media.MediaFile
|
||||
import javax.inject.Inject
|
||||
|
||||
@ContributesBinding(AppScope::class)
|
||||
|
|
@ -29,8 +31,13 @@ class AndroidLocalMediaFactory @Inject constructor(
|
|||
@ApplicationContext private val context: Context
|
||||
) : LocalMediaFactory {
|
||||
|
||||
override fun createFromUri(uri: Uri?, mimeType: String?): LocalMedia? {
|
||||
if (uri == null) return null
|
||||
override fun createFromMediaFile(mediaFile: MediaFile, mimeType: String?): LocalMedia {
|
||||
val resolvedMimeType = mimeType ?: MimeTypes.OctetStream
|
||||
val uri = mediaFile.path().toUri()
|
||||
return LocalMedia(uri, resolvedMimeType)
|
||||
}
|
||||
|
||||
override fun createFromUri(uri: Uri, mimeType: String?): LocalMedia {
|
||||
val resolvedMimeType = mimeType ?: context.contentResolver.getType(uri) ?: MimeTypes.OctetStream
|
||||
return LocalMedia(uri, resolvedMimeType)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,30 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2023 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.element.android.features.messages.impl.media.local
|
||||
|
||||
import android.net.Uri
|
||||
import io.element.android.libraries.core.mimetype.MimeTypes
|
||||
|
||||
class FakeLocalMediaFactory() : LocalMediaFactory {
|
||||
|
||||
var fallbackMimeType: String = MimeTypes.OctetStream
|
||||
|
||||
override fun createFromUri(uri: Uri?, mimeType: String?): LocalMedia? {
|
||||
if (uri == null) return null
|
||||
return LocalMedia(uri, mimeType ?: fallbackMimeType)
|
||||
}
|
||||
}
|
||||
|
|
@ -17,12 +17,20 @@
|
|||
package io.element.android.features.messages.impl.media.local
|
||||
|
||||
import android.net.Uri
|
||||
import io.element.android.libraries.matrix.api.media.MediaFile
|
||||
|
||||
interface LocalMediaFactory {
|
||||
|
||||
/**
|
||||
* This method will create a [LocalMedia] with the given [MediaFile] and [mimeType]
|
||||
*
|
||||
*/
|
||||
fun createFromMediaFile(mediaFile: MediaFile, mimeType: String?): LocalMedia
|
||||
|
||||
/**
|
||||
* This method will create a [LocalMedia] with the given [uri] and [mimeType]
|
||||
* If the [mimeType] is null, it'll try to read it from the content.
|
||||
*
|
||||
*/
|
||||
fun createFromUri(uri: Uri?, mimeType: String?): LocalMedia?
|
||||
fun createFromUri(uri: Uri, mimeType: String?): LocalMedia
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,7 +24,6 @@ import androidx.compose.runtime.mutableStateOf
|
|||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.core.net.toUri
|
||||
import dagger.assisted.Assisted
|
||||
import dagger.assisted.AssistedFactory
|
||||
import dagger.assisted.AssistedInject
|
||||
|
|
@ -32,7 +31,7 @@ import io.element.android.features.messages.impl.media.local.LocalMedia
|
|||
import io.element.android.features.messages.impl.media.local.LocalMediaFactory
|
||||
import io.element.android.libraries.architecture.Async
|
||||
import io.element.android.libraries.architecture.Presenter
|
||||
import io.element.android.libraries.matrix.api.MatrixClient
|
||||
import io.element.android.libraries.matrix.api.media.MatrixMediaLoader
|
||||
import io.element.android.libraries.matrix.api.media.MediaFile
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
|
|
@ -40,7 +39,7 @@ import kotlinx.coroutines.launch
|
|||
class MediaViewerPresenter @AssistedInject constructor(
|
||||
@Assisted private val inputs: MediaViewerNode.Inputs,
|
||||
private val localMediaFactory: LocalMediaFactory,
|
||||
private val client: MatrixClient,
|
||||
private val mediaLoader: MatrixMediaLoader,
|
||||
) : Presenter<MediaViewerState> {
|
||||
|
||||
@AssistedFactory
|
||||
|
|
@ -79,14 +78,12 @@ class MediaViewerPresenter @AssistedInject constructor(
|
|||
}
|
||||
|
||||
private fun CoroutineScope.loadMedia(mediaFile: MutableState<MediaFile?>, localMedia: MutableState<Async<LocalMedia>>) = launch {
|
||||
mediaFile.value = null
|
||||
localMedia.value = Async.Loading()
|
||||
client.mediaLoader.loadMediaFile(inputs.mediaSource, inputs.mimeType)
|
||||
mediaLoader.loadMediaFile(inputs.mediaSource, inputs.mimeType)
|
||||
.onSuccess {
|
||||
mediaFile.value = it
|
||||
}.mapCatching {
|
||||
val uri = it.path().toUri()
|
||||
localMediaFactory.createFromUri(uri, inputs.mimeType)!!
|
||||
localMediaFactory.createFromMediaFile(it, inputs.mimeType)
|
||||
}.onSuccess {
|
||||
localMedia.value = Async.Success(it)
|
||||
}.onFailure {
|
||||
|
|
|
|||
|
|
@ -206,22 +206,22 @@ class MessageComposerPresenter @Inject constructor(
|
|||
mimeType: String? = null,
|
||||
compressIfPossible: Boolean = true,
|
||||
) {
|
||||
if (uri == null) {
|
||||
attachmentsState.value = AttachmentsState.None
|
||||
return
|
||||
}
|
||||
val localMedia = localMediaFactory.createFromUri(uri, mimeType)
|
||||
attachmentsState.value = if (localMedia == null) {
|
||||
AttachmentsState.None
|
||||
val mediaAttachment = Attachment.Media(localMedia, compressIfPossible)
|
||||
val isPreviewable = when {
|
||||
MimeTypes.isImage(localMedia.mimeType) -> true
|
||||
MimeTypes.isVideo(localMedia.mimeType) -> true
|
||||
MimeTypes.isAudio(localMedia.mimeType) -> true
|
||||
else -> false
|
||||
}
|
||||
attachmentsState.value = if (isPreviewable) {
|
||||
AttachmentsState.Previewing(persistentListOf(mediaAttachment))
|
||||
} else {
|
||||
val mediaAttachment = Attachment.Media(localMedia, compressIfPossible)
|
||||
val isPreviewable = when {
|
||||
MimeTypes.isImage(localMedia.mimeType) -> true
|
||||
MimeTypes.isVideo(localMedia.mimeType) -> true
|
||||
MimeTypes.isAudio(localMedia.mimeType) -> true
|
||||
else -> false
|
||||
}
|
||||
if (isPreviewable) {
|
||||
AttachmentsState.Previewing(persistentListOf(mediaAttachment))
|
||||
} else {
|
||||
AttachmentsState.Sending(persistentListOf(mediaAttachment))
|
||||
}
|
||||
AttachmentsState.Sending(persistentListOf(mediaAttachment))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue