Skip to content

Commit

Permalink
Implement media saved user message per media type
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandarIlic committed Apr 11, 2024
1 parent 28faae5 commit 8843504
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
Expand Down Expand Up @@ -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)
Expand All @@ -150,7 +151,7 @@ fun MediaGalleryScreen(
actions = {
GalleryDropdownMenu(
onSaveClick = {
currentImage()?.let { eventPublisher(MediaGalleryContract.UiEvent.SaveMedia(it.url)) }
currentImage()?.let { eventPublisher(MediaGalleryContract.UiEvent.SaveMedia(it)) }
},
)
},
Expand Down Expand Up @@ -286,6 +287,7 @@ private fun AttachmentsHorizontalPager(
NoteAttachmentType.Image -> {
ImageScreen(attachment = attachment)
}

NoteAttachmentType.Video -> {
VideoScreen(
positionMs = initialPositionMs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) }
}
}
Expand All @@ -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)) }
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@

<string name="media_gallery_context_save">Save</string>
<string name="media_gallery_toast_photo_saved">Photo saved.</string>
<string name="media_gallery_toast_video_saved">Video saved.</string>
<string name="media_gallery_toast_file_saved">File saved.</string>
<string name="media_gallery_retry_save">Retry</string>
<string name="media_gallery_error_photo_not_saved">Unable to save photo. Please try again.</string>

Expand Down

0 comments on commit 8843504

Please sign in to comment.