Skip to content

Commit

Permalink
chore: handle uri asset (#2827)
Browse files Browse the repository at this point in the history
  • Loading branch information
Garzas authored Mar 27, 2024
1 parent 23e02bf commit 94cb684
Show file tree
Hide file tree
Showing 13 changed files with 441 additions and 340 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ class ConversationMessagesViewModel @Inject constructor(
val assetContent = messageContent.value
assetDataPath(conversationId, messageId)?.let { (path, _) ->
messageId to AssetBundle(
key = assetContent.remoteData.assetId,
dataPath = path,
fileName = assetContent.name ?: DEFAULT_ASSET_NAME,
dataSize = assetContent.sizeInBytes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import okio.Path
* Represents a set of metadata information of an asset message
*/
data class AssetBundle(
val key: String,
val mimeType: String,
val dataPath: Path,
val dataSize: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

package com.wire.android.ui.home.conversations.sendmessage

import android.net.Uri
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
Expand All @@ -35,11 +34,11 @@ import com.wire.android.ui.home.conversations.ConversationSnackbarMessages
import com.wire.android.ui.home.conversations.SureAboutMessagingDialogState
import com.wire.android.ui.home.conversations.model.AssetBundle
import com.wire.android.ui.home.conversations.model.UriAsset
import com.wire.android.ui.home.conversations.usecase.HandleUriAssetUseCase
import com.wire.android.ui.home.messagecomposer.state.ComposableMessageBundle
import com.wire.android.ui.home.messagecomposer.state.MessageBundle
import com.wire.android.ui.home.messagecomposer.state.Ping
import com.wire.android.ui.navArgs
import com.wire.android.util.FileManager
import com.wire.android.util.ImageUtil
import com.wire.android.util.dispatchers.DispatcherProvider
import com.wire.android.util.getAudioLengthInMs
Expand All @@ -49,7 +48,6 @@ import com.wire.kalium.logic.data.asset.KaliumFileSystem
import com.wire.kalium.logic.data.conversation.Conversation.TypingIndicatorMode
import com.wire.kalium.logic.data.id.QualifiedID
import com.wire.kalium.logic.failure.LegalHoldEnabledForConversationFailure
import com.wire.kalium.logic.feature.asset.GetAssetSizeLimitUseCase
import com.wire.kalium.logic.feature.asset.ScheduleNewAssetMessageResult
import com.wire.kalium.logic.feature.asset.ScheduleNewAssetMessageUseCase
import com.wire.kalium.logic.feature.conversation.ObserveConversationUnderLegalHoldNotifiedUseCase
Expand Down Expand Up @@ -85,26 +83,19 @@ class SendMessageViewModel @Inject constructor(
private val retryFailedMessage: RetryFailedMessageUseCase,
private val dispatchers: DispatcherProvider,
private val kaliumFileSystem: KaliumFileSystem,
private val getAssetSizeLimit: GetAssetSizeLimitUseCase,
private val handleUriAsset: HandleUriAssetUseCase,
private val sendKnockUseCase: SendKnockUseCase,
private val sendTypingEvent: SendTypingEventUseCase,
private val pingRinger: PingRinger,
private val imageUtil: ImageUtil,
private val fileManager: FileManager,
private val setUserInformedAboutVerification: SetUserInformedAboutVerificationUseCase,
private val observeDegradedConversationNotified: ObserveDegradedConversationNotifiedUseCase,
private val setNotifiedAboutConversationUnderLegalHold: SetNotifiedAboutConversationUnderLegalHoldUseCase,
private val observeConversationUnderLegalHoldNotified: ObserveConversationUnderLegalHoldNotifiedUseCase,
private val sendLocation: SendLocationUseCase,
private val removeMessageDraft: RemoveMessageDraftUseCase
private val removeMessageDraft: RemoveMessageDraftUseCase,
) : SavedStateViewModel(savedStateHandle) {

var tempWritableVideoUri: Uri? = null
private set

var tempWritableImageUri: Uri? = null
private set

private val conversationNavArgs: ConversationNavArgs = savedStateHandle.navArgs()
val conversationId: QualifiedID = conversationNavArgs.conversationId

Expand All @@ -119,11 +110,6 @@ class SendMessageViewModel @Inject constructor(
SureAboutMessagingDialogState.Hidden
)

init {
initTempWritableVideoUri()
initTempWritableImageUri()
}

private fun onSnackbarMessage(type: SnackBarMessage) = viewModelScope.launch {
_infoMessage.emit(type)
}
Expand Down Expand Up @@ -208,47 +194,26 @@ class SendMessageViewModel @Inject constructor(
attachmentUri: UriAsset,
audioPath: Path? = null
) {
val tempCachePath = kaliumFileSystem.rootCachePath
val assetBundle = fileManager.getAssetBundleFromUri(
attachmentUri = attachmentUri.uri,
tempCachePath = tempCachePath,
when (val result = handleUriAsset.invoke(
uri = attachmentUri.uri,
saveToDeviceIfInvalid = attachmentUri.saveToDeviceIfInvalid,
audioPath = audioPath
)
if (assetBundle != null) {
// The max limit for sending assets changes between user and asset types.
// Check [GetAssetSizeLimitUseCase] class for more detailed information about the real limits.
val maxSizeLimitInBytes =
getAssetSizeLimit(isImage = assetBundle.assetType == AttachmentType.IMAGE)
handleBundle(assetBundle, maxSizeLimitInBytes, attachmentUri)
} else {
onSnackbarMessage(ConversationSnackbarMessages.ErrorPickingAttachment)
}
}
)) {
is HandleUriAssetUseCase.Result.Failure.AssetTooLarge -> {
assetTooLargeDialogState = AssetTooLargeDialogState.Visible(
assetType = result.assetBundle.assetType,
maxLimitInMB = result.maxLimitInMB,
savedToDevice = attachmentUri.saveToDeviceIfInvalid
)
}

private suspend fun handleBundle(
assetBundle: AssetBundle,
maxSizeLimitInBytes: Long,
attachmentUri: UriAsset
) {
if (assetBundle.dataSize <= maxSizeLimitInBytes) {
sendAttachment(assetBundle)
} else {
if (attachmentUri.saveToDeviceIfInvalid) {
with(assetBundle) {
fileManager.saveToExternalMediaStorage(
fileName,
dataPath,
dataSize,
mimeType,
dispatchers
)
}
HandleUriAssetUseCase.Result.Failure.Unknown -> {
onSnackbarMessage(ConversationSnackbarMessages.ErrorPickingAttachment)
}

is HandleUriAssetUseCase.Result.Success -> {
sendAttachment(result.assetBundle)

Check warning on line 215 in app/src/main/kotlin/com/wire/android/ui/home/conversations/sendmessage/SendMessageViewModel.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/ui/home/conversations/sendmessage/SendMessageViewModel.kt#L215

Added line #L215 was not covered by tests
}
assetTooLargeDialogState = AssetTooLargeDialogState.Visible(
assetType = assetBundle.assetType,
maxLimitInMB = maxSizeLimitInBytes.div(sizeOf1MB).toInt(),
savedToDevice = attachmentUri.saveToDeviceIfInvalid
)
}
}

Expand Down Expand Up @@ -323,20 +288,6 @@ class SendMessageViewModel @Inject constructor(
}
}

private fun initTempWritableVideoUri() {
viewModelScope.launch {
tempWritableVideoUri =
fileManager.getTempWritableVideoUri(kaliumFileSystem.rootCachePath)
}
}

private fun initTempWritableImageUri() {
viewModelScope.launch {
tempWritableImageUri =
fileManager.getTempWritableImageUri(kaliumFileSystem.rootCachePath)
}
}

fun hideAssetTooLargeError() {
assetTooLargeDialogState = AssetTooLargeDialogState.Hidden
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.android.ui.home.conversations.usecase

import android.net.Uri
import com.wire.android.ui.home.conversations.model.AssetBundle
import com.wire.android.util.FileManager
import com.wire.android.util.dispatchers.DispatcherProvider
import com.wire.kalium.logic.data.asset.AttachmentType
import com.wire.kalium.logic.data.asset.KaliumFileSystem
import com.wire.kalium.logic.feature.asset.GetAssetSizeLimitUseCase
import kotlinx.coroutines.withContext
import okio.Path
import javax.inject.Inject

class HandleUriAssetUseCase @Inject constructor(
private val getAssetSizeLimit: GetAssetSizeLimitUseCase,
private val fileManager: FileManager,
private val kaliumFileSystem: KaliumFileSystem,
private val dispatchers: DispatcherProvider
) {

suspend fun invoke(
uri: Uri,
saveToDeviceIfInvalid: Boolean = false,

Check warning on line 40 in app/src/main/kotlin/com/wire/android/ui/home/conversations/usecase/HandleUriAssetUseCase.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/ui/home/conversations/usecase/HandleUriAssetUseCase.kt#L40

Added line #L40 was not covered by tests
audioPath: Path? = null,
): Result = withContext(dispatchers.io()) {

val tempCachePath = kaliumFileSystem.rootCachePath
val assetBundle = fileManager.getAssetBundleFromUri(
attachmentUri = uri,
tempCachePath = tempCachePath,
audioPath = audioPath
)
if (assetBundle != null) {
// The max limit for sending assets changes between user and asset types.
// Check [GetAssetSizeLimitUseCase] class for more detailed information about the real limits.
val maxSizeLimitInBytes = getAssetSizeLimit(isImage = assetBundle.assetType == AttachmentType.IMAGE)

if (assetBundle.dataSize <= maxSizeLimitInBytes) {
return@withContext Result.Success(assetBundle)
} else {
if (saveToDeviceIfInvalid) {
with(assetBundle) {
fileManager.saveToExternalMediaStorage(
fileName,
dataPath,
dataSize,
mimeType,
dispatchers
)
}
}
return@withContext Result.Failure.AssetTooLarge(assetBundle, maxSizeLimitInBytes.div(sizeOf1MB).toInt())
}
} else {
return@withContext Result.Failure.Unknown
}
}

companion object {
private const val sizeOf1MB = 1024 * 1024
}

sealed class Result {
data class Success(val assetBundle: AssetBundle) : Result()
sealed class Failure : Result() {
data class AssetTooLarge(val assetBundle: AssetBundle, val maxLimitInMB: Int) : Failure()
data object Unknown : Failure()
}
}
}
Loading

0 comments on commit 94cb684

Please sign in to comment.