Calling use case inside ViewModel

This commit is contained in:
Maxime Naturel 2022-02-24 09:31:26 +01:00
parent c6c46375d6
commit b17ce12c3d
3 changed files with 23 additions and 5 deletions

View File

@ -269,8 +269,6 @@ class VectorAttachmentViewerActivity : AttachmentViewerActivity(), AttachmentInt
}
}
// TODO add save feature for image => check it works for video as well,
// check if it is already possible to save from menu with long press on video
override fun onDownload() {
// TODO
// call usecase using viewModel into handle method
@ -279,6 +277,8 @@ class VectorAttachmentViewerActivity : AttachmentViewerActivity(), AttachmentInt
// show message on error event: see TimelineFragment
// check write file permissions: see TimelineFragment
// should we check if media is saveable?
// check if it is already possible to save from menu with long press on video
// check if it works for video or other media type as well
// add unit tests for usecase?
TODO("Not yet implemented")
}

View File

@ -20,6 +20,5 @@ import im.vector.app.core.platform.VectorViewEvents
sealed class VectorAttachmentViewerViewEvents : VectorViewEvents {
object DownloadingMedia : VectorAttachmentViewerViewEvents()
object MediaDownloaded : VectorAttachmentViewerViewEvents()
object ErrorDownloadingMedia : VectorAttachmentViewerViewEvents()
}

View File

@ -23,9 +23,12 @@ import dagger.assisted.AssistedInject
import im.vector.app.core.di.MavericksAssistedViewModelFactory
import im.vector.app.core.platform.VectorDummyViewState
import im.vector.app.core.platform.VectorViewModel
import im.vector.app.features.media.domain.usecase.DownloadMediaUseCase
import kotlinx.coroutines.launch
class VectorAttachmentViewerViewModel @AssistedInject constructor(
@Assisted initialState: VectorDummyViewState
@Assisted initialState: VectorDummyViewState,
private val downloadMediaUseCase: DownloadMediaUseCase
) : VectorViewModel<VectorDummyViewState, VectorAttachmentViewerAction, VectorAttachmentViewerViewEvents>(initialState) {
/* ==========================================================================================
@ -42,6 +45,22 @@ class VectorAttachmentViewerViewModel @AssistedInject constructor(
* ========================================================================================== */
override fun handle(action: VectorAttachmentViewerAction) {
TODO("Not yet implemented")
when (action) {
is VectorAttachmentViewerAction.DownloadMedia -> handleDownloadAction(action)
}
}
/* ==========================================================================================
* Private methods
* ========================================================================================== */
private fun handleDownloadAction(action: VectorAttachmentViewerAction.DownloadMedia) {
viewModelScope.launch {
_viewEvents.post(VectorAttachmentViewerViewEvents.DownloadingMedia)
// Success event is handled via a notification inside use case
downloadMediaUseCase.execute(action.file)
.onFailure { _viewEvents.post(VectorAttachmentViewerViewEvents.ErrorDownloadingMedia) }
}
}
}