Fixing DI + observing events

This commit is contained in:
Maxime Naturel 2022-02-24 11:12:38 +01:00
parent 7d7b1f305e
commit 73ac3f3fda
4 changed files with 34 additions and 4 deletions

View File

@ -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<*, *>
}

View File

@ -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))

View File

@ -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()
}

View File

@ -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<VectorAttachmentViewerViewModel, VectorDummyViewState> 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)) }
}
}
}