Adding download media use case

This commit is contained in:
Maxime Naturel 2022-02-23 16:27:18 +01:00
parent e6995cbdd4
commit f64268efdb
4 changed files with 61 additions and 2 deletions

View File

@ -2111,6 +2111,7 @@ class TimelineFragment @Inject constructor(
} }
} }
// TODO mutualize permission checking creating activity extension or delegation interface?
private fun onSaveActionClicked(action: EventSharedAction.Save) { private fun onSaveActionClicked(action: EventSharedAction.Save) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q && if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q &&
!checkPermissions(PERMISSIONS_FOR_WRITING_FILES, requireActivity(), saveActionActivityResultLauncher)) { !checkPermissions(PERMISSIONS_FOR_WRITING_FILES, requireActivity(), saveActionActivityResultLauncher)) {

View File

@ -251,7 +251,7 @@ class MessageActionsViewModel @AssistedInject constructor(@Assisted
val msgType = messageContent?.msgType val msgType = messageContent?.msgType
return arrayListOf<EventSharedAction>().apply { return arrayListOf<EventSharedAction>().apply {
// TODO need to check all possible items and confirm order // TODO need to check all possible items and confirm order changes to apply
when { when {
timelineEvent.root.sendState.hasFailed() -> { timelineEvent.root.sendState.hasFailed() -> {
addActionsForFailedState(timelineEvent, actionPermissions, messageContent, msgType) addActionsForFailedState(timelineEvent, actionPermissions, messageContent, msgType)
@ -569,7 +569,7 @@ class MessageActionsViewModel @AssistedInject constructor(@Assisted
} }
} }
// TODO need to add this into Viewer screen to check if it is saveable? => create extension on MessageType? // TODO need to add this into Viewer screen to check if it is saveable? => create extension on MessageType or useCase?
private fun canSave(msgType: String?): Boolean { private fun canSave(msgType: String?): Boolean {
return when (msgType) { return when (msgType) {
MessageType.MSGTYPE_IMAGE, MessageType.MSGTYPE_IMAGE,

View File

@ -271,6 +271,13 @@ class VectorAttachmentViewerActivity : AttachmentViewerActivity(), AttachmentInt
} }
} }
// TODO create suspendable usecase: downloadMediaUseCase
// create interface for base use case
// create ViewModel with action downloadAction, events downloading, downloaded, error
// call usecase using viewModel
// launch coroutine in Activity using repeatOnLifeCycle extension
// add unit tests for usecase?
/* ========================================================================================== /* ==========================================================================================
* COMPANION * COMPANION
* ========================================================================================== */ * ========================================================================================== */

View File

@ -0,0 +1,51 @@
/*
* Copyright (c) 2022 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 im.vector.app.features.media.domain.usecase
import android.content.Context
import androidx.core.net.toUri
import dagger.hilt.android.qualifiers.ApplicationContext
import im.vector.app.core.intent.getMimeTypeFromUri
import im.vector.app.core.utils.saveMedia
import im.vector.app.features.notifications.NotificationUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.File
import javax.inject.Inject
class DownloadMediaUseCase @Inject constructor(
@ApplicationContext private val appContext: Context,
private val notificationUtils: NotificationUtils
) {
/* ==========================================================================================
* Public API
* ========================================================================================== */
// TODO find a way to provide Dispatchers via Interface to be able to unit tests
suspend fun execute(file: File): Result<Unit> = withContext(Dispatchers.IO) {
runCatching {
saveMedia(
context = appContext,
file = file,
title = file.name,
mediaMimeType = getMimeTypeFromUri(appContext, file.toUri()),
notificationUtils = notificationUtils
)
}
}
}