diff --git a/vector/src/main/java/im/vector/app/core/di/MavericksViewModelModule.kt b/vector/src/main/java/im/vector/app/core/di/MavericksViewModelModule.kt index 2cd7136ffc..33afcf1dfb 100644 --- a/vector/src/main/java/im/vector/app/core/di/MavericksViewModelModule.kt +++ b/vector/src/main/java/im/vector/app/core/di/MavericksViewModelModule.kt @@ -58,6 +58,7 @@ import im.vector.app.features.login.LoginViewModel import im.vector.app.features.login2.LoginViewModel2 import im.vector.app.features.login2.created.AccountCreatedViewModel import im.vector.app.features.matrixto.MatrixToBottomSheetViewModel +import im.vector.app.features.media.VectorAttachmentViewerViewModel import im.vector.app.features.onboarding.OnboardingViewModel import im.vector.app.features.poll.create.CreatePollViewModel import im.vector.app.features.qrcode.QrCodeScannerViewModel @@ -594,4 +595,9 @@ interface MavericksViewModelModule { @IntoMap @MavericksViewModelKey(LocationSharingViewModel::class) fun createLocationSharingViewModelFactory(factory: LocationSharingViewModel.Factory): MavericksAssistedViewModelFactory<*, *> + + @Binds + @IntoMap + @MavericksViewModelKey(VectorAttachmentViewerViewModel::class) + fun vectorAttachmentViewerViewModelFactory(factory: VectorAttachmentViewerViewModel.Factory): MavericksAssistedViewModelFactory<*, *> } diff --git a/vector/src/main/java/im/vector/app/features/media/VectorAttachmentViewerActivity.kt b/vector/src/main/java/im/vector/app/features/media/VectorAttachmentViewerActivity.kt index abcab12bd7..f051c63828 100644 --- a/vector/src/main/java/im/vector/app/features/media/VectorAttachmentViewerActivity.kt +++ b/vector/src/main/java/im/vector/app/features/media/VectorAttachmentViewerActivity.kt @@ -41,6 +41,8 @@ import im.vector.app.features.themes.ThemeUtils import im.vector.lib.attachmentviewer.AttachmentCommands import im.vector.lib.attachmentviewer.AttachmentViewerActivity import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import kotlinx.parcelize.Parcelize @@ -147,6 +149,8 @@ class VectorAttachmentViewerActivity : AttachmentViewerActivity(), AttachmentInt window.statusBarColor = ContextCompat.getColor(this, R.color.black_alpha) window.navigationBarColor = ContextCompat.getColor(this, R.color.black_alpha) + + observeViewEvents() } override fun onResume() { @@ -241,6 +245,23 @@ class VectorAttachmentViewerActivity : AttachmentViewerActivity(), AttachmentInt }) } + private fun observeViewEvents() { + viewModel.viewEvents + .stream() + .onEach(::handleViewEvents) + .launchIn(lifecycleScope) + } + + private fun handleViewEvents(event: VectorAttachmentViewerViewEvents) { + when (event) { + is VectorAttachmentViewerViewEvents.DownloadingMedia -> Unit // TODO show loader? + is VectorAttachmentViewerViewEvents.ErrorDownloadingMedia -> { + // TODO show snackbar + Timber.e("failure saving file: ${event.error}") + } + } + } + /* ========================================================================================== * Specialization AttachmentInteractionListener * ========================================================================================== */ @@ -273,13 +294,12 @@ class VectorAttachmentViewerActivity : AttachmentViewerActivity(), AttachmentInt override fun onDownload() { // TODO - // handle viewEvents // 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? + // add unit tests for usecase? what is the used mock library? lifecycleScope.launch(Dispatchers.IO) { val file = currentSourceProvider?.getFileForSharing(currentPosition) ?: return@launch viewModel.handle(VectorAttachmentViewerAction.DownloadMedia(file)) diff --git a/vector/src/main/java/im/vector/app/features/media/VectorAttachmentViewerViewEvents.kt b/vector/src/main/java/im/vector/app/features/media/VectorAttachmentViewerViewEvents.kt index 2b3266ee7e..6516da9dcc 100644 --- a/vector/src/main/java/im/vector/app/features/media/VectorAttachmentViewerViewEvents.kt +++ b/vector/src/main/java/im/vector/app/features/media/VectorAttachmentViewerViewEvents.kt @@ -20,5 +20,5 @@ import im.vector.app.core.platform.VectorViewEvents sealed class VectorAttachmentViewerViewEvents : VectorViewEvents { object DownloadingMedia : VectorAttachmentViewerViewEvents() - object ErrorDownloadingMedia : VectorAttachmentViewerViewEvents() + data class ErrorDownloadingMedia(val error: Throwable) : VectorAttachmentViewerViewEvents() } diff --git a/vector/src/main/java/im/vector/app/features/media/VectorAttachmentViewerViewModel.kt b/vector/src/main/java/im/vector/app/features/media/VectorAttachmentViewerViewModel.kt index 039977d573..9e311c6a1f 100644 --- a/vector/src/main/java/im/vector/app/features/media/VectorAttachmentViewerViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/media/VectorAttachmentViewerViewModel.kt @@ -17,10 +17,12 @@ package im.vector.app.features.media +import com.airbnb.mvrx.MavericksViewModelFactory import dagger.assisted.Assisted import dagger.assisted.AssistedFactory import dagger.assisted.AssistedInject import im.vector.app.core.di.MavericksAssistedViewModelFactory +import im.vector.app.core.di.hiltMavericksViewModelFactory import im.vector.app.core.platform.VectorDummyViewState import im.vector.app.core.platform.VectorViewModel import im.vector.app.features.media.domain.usecase.DownloadMediaUseCase @@ -40,6 +42,8 @@ class VectorAttachmentViewerViewModel @AssistedInject constructor( override fun create(initialState: VectorDummyViewState): VectorAttachmentViewerViewModel } + companion object : MavericksViewModelFactory by hiltMavericksViewModelFactory() + /* ========================================================================================== * Specialization * ========================================================================================== */ @@ -60,7 +64,7 @@ class VectorAttachmentViewerViewModel @AssistedInject constructor( // Success event is handled via a notification inside use case downloadMediaUseCase.execute(action.file) - .onFailure { _viewEvents.post(VectorAttachmentViewerViewEvents.ErrorDownloadingMedia) } + .onFailure { _viewEvents.post(VectorAttachmentViewerViewEvents.ErrorDownloadingMedia(it)) } } } }