From 884350457358cfe4f24988b9f436ecf41c3f9b17 Mon Sep 17 00:00:00 2001 From: Aleksandar Ilic Date: Thu, 11 Apr 2024 20:29:53 +0200 Subject: [PATCH] Implement media saved user message per media type --- .../gallery/MediaGalleryContract.kt | 5 +++-- .../attachments/gallery/MediaGalleryScreen.kt | 18 ++++++++++-------- .../gallery/MediaGalleryViewModel.kt | 9 +++++---- app/src/main/res/values/strings.xml | 2 ++ 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/app/src/main/kotlin/net/primal/android/attachments/gallery/MediaGalleryContract.kt b/app/src/main/kotlin/net/primal/android/attachments/gallery/MediaGalleryContract.kt index ac3bae80e..2c43193de 100644 --- a/app/src/main/kotlin/net/primal/android/attachments/gallery/MediaGalleryContract.kt +++ b/app/src/main/kotlin/net/primal/android/attachments/gallery/MediaGalleryContract.kt @@ -1,5 +1,6 @@ package net.primal.android.attachments.gallery +import net.primal.android.attachments.domain.NoteAttachmentType import net.primal.android.core.compose.attachment.model.NoteAttachmentUi interface MediaGalleryContract { @@ -17,11 +18,11 @@ interface MediaGalleryContract { } sealed class UiEvent { - data class SaveMedia(val remoteUrl: String) : UiEvent() + data class SaveMedia(val attachment: NoteAttachmentUi) : UiEvent() data object DismissError : UiEvent() } sealed class SideEffect { - data object MediaSaved : SideEffect() + data class MediaSaved(val type: NoteAttachmentType) : SideEffect() } } diff --git a/app/src/main/kotlin/net/primal/android/attachments/gallery/MediaGalleryScreen.kt b/app/src/main/kotlin/net/primal/android/attachments/gallery/MediaGalleryScreen.kt index 986e1de8f..a8148c1e3 100644 --- a/app/src/main/kotlin/net/primal/android/attachments/gallery/MediaGalleryScreen.kt +++ b/app/src/main/kotlin/net/primal/android/attachments/gallery/MediaGalleryScreen.kt @@ -82,12 +82,13 @@ fun MediaGalleryScreen(viewModel: MediaGalleryViewModel, onClose: () -> Unit) { LaunchedEffect(viewModel) { viewModel.effects.collect { when (it) { - MediaGalleryContract.SideEffect.MediaSaved -> uiScope.launch { - Toast.makeText( - context, - context.getString(R.string.media_gallery_toast_photo_saved), - Toast.LENGTH_SHORT, - ).show() + is MediaGalleryContract.SideEffect.MediaSaved -> uiScope.launch { + val message = when (it.type) { + NoteAttachmentType.Image -> context.getString(R.string.media_gallery_toast_photo_saved) + NoteAttachmentType.Video -> context.getString(R.string.media_gallery_toast_video_saved) + else -> context.getString(R.string.media_gallery_toast_file_saved) + } + Toast.makeText(context, message, Toast.LENGTH_SHORT).show() } } } @@ -126,7 +127,7 @@ fun MediaGalleryScreen( }, actionLabel = stringResource(id = R.string.media_gallery_retry_save), onErrorDismiss = { eventPublisher(MediaGalleryContract.UiEvent.DismissError) }, - onActionPerformed = { currentImage()?.let { eventPublisher(MediaGalleryContract.UiEvent.SaveMedia(it.url)) } }, + onActionPerformed = { currentImage()?.let { eventPublisher(MediaGalleryContract.UiEvent.SaveMedia(it)) } }, ) val containerColor = AppTheme.colorScheme.surface.copy(alpha = 0.21f) @@ -150,7 +151,7 @@ fun MediaGalleryScreen( actions = { GalleryDropdownMenu( onSaveClick = { - currentImage()?.let { eventPublisher(MediaGalleryContract.UiEvent.SaveMedia(it.url)) } + currentImage()?.let { eventPublisher(MediaGalleryContract.UiEvent.SaveMedia(it)) } }, ) }, @@ -286,6 +287,7 @@ private fun AttachmentsHorizontalPager( NoteAttachmentType.Image -> { ImageScreen(attachment = attachment) } + NoteAttachmentType.Video -> { VideoScreen( positionMs = initialPositionMs, diff --git a/app/src/main/kotlin/net/primal/android/attachments/gallery/MediaGalleryViewModel.kt b/app/src/main/kotlin/net/primal/android/attachments/gallery/MediaGalleryViewModel.kt index 415437e81..d36581c7b 100644 --- a/app/src/main/kotlin/net/primal/android/attachments/gallery/MediaGalleryViewModel.kt +++ b/app/src/main/kotlin/net/primal/android/attachments/gallery/MediaGalleryViewModel.kt @@ -17,6 +17,7 @@ import net.primal.android.attachments.domain.NoteAttachmentType import net.primal.android.attachments.gallery.MediaGalleryContract.UiEvent import net.primal.android.attachments.gallery.MediaGalleryContract.UiState import net.primal.android.attachments.repository.AttachmentsRepository +import net.primal.android.core.compose.attachment.model.NoteAttachmentUi import net.primal.android.core.compose.attachment.model.asNoteAttachmentUi import net.primal.android.core.coroutines.CoroutineDispatcherProvider import net.primal.android.core.files.MediaDownloader @@ -59,7 +60,7 @@ class MediaGalleryViewModel @Inject constructor( viewModelScope.launch { events.collect { when (it) { - is UiEvent.SaveMedia -> saveMedia(remoteUrl = it.remoteUrl) + is UiEvent.SaveMedia -> saveMedia(attachment = it.attachment) UiEvent.DismissError -> setState { copy(error = null) } } } @@ -83,12 +84,12 @@ class MediaGalleryViewModel @Inject constructor( } } - private fun saveMedia(remoteUrl: String) = + private fun saveMedia(attachment: NoteAttachmentUi) = viewModelScope.launch { withContext(dispatcherProvider.io()) { try { - mediaDownloader.downloadToMediaGallery(url = remoteUrl) - setEffect(MediaGalleryContract.SideEffect.MediaSaved) + mediaDownloader.downloadToMediaGallery(url = attachment.url) + setEffect(MediaGalleryContract.SideEffect.MediaSaved(type = attachment.type)) } catch (error: UnsuccessfulFileDownload) { Timber.w(error) setState { copy(error = UiState.MediaGalleryError.FailedToSaveMedia(error)) } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e60add698..18fea0365 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -225,6 +225,8 @@ Save Photo saved. + Video saved. + File saved. Retry Unable to save photo. Please try again.