Skip to content

Move Toast invocations out of Camera/VideoEdit ViewModels. #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.google.android.samples.socialite.ui.camera
import android.Manifest
import android.annotation.SuppressLint
import android.view.Surface
import android.widget.Toast
import androidx.camera.core.CameraSelector
import androidx.camera.core.Preview
import androidx.camera.view.RotationProvider
Expand Down Expand Up @@ -91,6 +92,20 @@ fun Camera(
val lifecycleOwner = LocalLifecycleOwner.current
val context = LocalContext.current

LaunchedEffect(lifecycleOwner, context) {
viewModel.imageCaptureState.collect { state ->
when (state) {
ImageCaptureState.IMAGE_CAPTURE_SUCCESS ->
Toast.makeText(context, "Photo saved.", Toast.LENGTH_SHORT).show()
ImageCaptureState.IMAGE_CAPTURE_FAIL ->
Toast.makeText(context, "Photo capture failed.", Toast.LENGTH_SHORT).show()
else -> {
/* no-op */
}
}
}
}

var isLayoutUnfolded by remember { mutableStateOf<Boolean?>(null) }

LaunchedEffect(lifecycleOwner, context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import android.content.Context
import android.os.Build
import android.provider.MediaStore
import android.view.Display
import android.widget.Toast
import androidx.annotation.RequiresPermission
import androidx.camera.core.AspectRatio
import androidx.camera.core.Camera
Expand Down Expand Up @@ -58,6 +57,7 @@ import java.text.SimpleDateFormat
import java.util.Locale
import javax.inject.Inject
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.launch

@HiltViewModel
Expand All @@ -72,6 +72,8 @@ class CameraViewModel @Inject constructor(

val chatId: Long? = savedStateHandle.get("chatId")
var viewFinderState = MutableStateFlow(ViewFinderState())
private var _imageCaptureState = MutableStateFlow(ImageCaptureState.PENDING)
val imageCaptureState: SharedFlow<ImageCaptureState> = _imageCaptureState

val aspectRatioStrategy =
AspectRatioStrategy(AspectRatio.RATIO_16_9, AspectRatioStrategy.FALLBACK_RULE_NONE)
Expand Down Expand Up @@ -189,18 +191,23 @@ class CameraViewModel @Inject constructor(
ContextCompat.getMainExecutor(context),
object : ImageCapture.OnImageSavedCallback {
override fun onError(exc: ImageCaptureException) {
val msg = "Photo capture failed."
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
viewModelScope.launch {
_imageCaptureState.emit(ImageCaptureState.IMAGE_CAPTURE_FAIL)
}
}

override fun onImageSaved(output: ImageCapture.OutputFileResults) {
var state = ImageCaptureState.PENDING
val savedUri = output.savedUri
if (savedUri != null) {
state = ImageCaptureState.IMAGE_CAPTURE_SUCCESS
sendPhotoMessage(savedUri.toString())
onMediaCaptured(Media(savedUri, MediaType.PHOTO))
} else {
val msg = "Photo capture failed."
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
state = ImageCaptureState.IMAGE_CAPTURE_FAIL
}
viewModelScope.launch {
_imageCaptureState.emit(state)
}
}
},
Expand Down Expand Up @@ -310,6 +317,12 @@ data class ViewFinderState(
val lensFacing: Int = CameraSelector.LENS_FACING_BACK,
)

enum class ImageCaptureState {
PENDING,
IMAGE_CAPTURE_SUCCESS,
IMAGE_CAPTURE_FAIL,
}

/**
* Defines the current state of the camera.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.google.android.samples.socialite.ui.videoedit
import android.media.MediaMetadataRetriever
import android.net.Uri
import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
Expand Down Expand Up @@ -59,6 +60,7 @@ import androidx.compose.material3.TextFieldDefaults
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -71,6 +73,7 @@ import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalInspectionMode
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
Expand All @@ -92,6 +95,7 @@ fun VideoEditScreen(
onCloseButtonClicked: () -> Unit,
navController: NavController,
) {
val lifecycleOwner = LocalLifecycleOwner.current
val context = LocalContext.current

val viewModel: VideoEditScreenViewModel = hiltViewModel()
Expand All @@ -104,6 +108,21 @@ fun VideoEditScreen(

val isProcessing = viewModel.isProcessing.collectAsState()

LaunchedEffect(lifecycleOwner, context) {
viewModel.videoSaveState.collect { state ->
when (state) {
VideoSaveState.VIDEO_SAVE_SUCCESS ->
Toast.makeText(context, "Edited video saved", Toast.LENGTH_LONG).show()
VideoSaveState.VIDEO_SAVE_FAIL ->
Toast.makeText(context, "Error applying edits on video", Toast.LENGTH_LONG)
.show()
else -> {
/* no-op */
}
}
}
}

var removeAudioEnabled by rememberSaveable { mutableStateOf(false) }
var overlayText by rememberSaveable { mutableStateOf("") }
var redOverlayTextEnabled by rememberSaveable { mutableStateOf(false) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import android.text.style.RelativeSizeSpan
import android.widget.Toast
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.media3.common.MediaItem
Expand All @@ -42,19 +41,18 @@ import com.google.android.samples.socialite.repository.ChatRepository
import com.google.android.samples.socialite.ui.camera.CameraViewModel
import com.google.common.collect.ImmutableList
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import java.io.File
import java.io.IOException
import java.text.SimpleDateFormat
import java.util.Locale
import javax.inject.Inject
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

@HiltViewModel
class VideoEditScreenViewModel @Inject constructor(
@ApplicationContext private val application: Context,
private val repository: ChatRepository,
) : ViewModel() {

Expand All @@ -67,14 +65,19 @@ class VideoEditScreenViewModel @Inject constructor(
private val _isProcessing = MutableStateFlow(false)
val isProcessing: StateFlow<Boolean> = _isProcessing

private var _videoSaveState = MutableStateFlow(VideoSaveState.PENDING)
val videoSaveState: SharedFlow<VideoSaveState> = _videoSaveState

fun setChatId(chatId: Long) {
this.chatId.value = chatId
}

private val transformerListener: Transformer.Listener =
@UnstableApi object : Transformer.Listener {
override fun onCompleted(composition: Composition, exportResult: ExportResult) {
Toast.makeText(application, "Edited video saved", Toast.LENGTH_LONG).show()
viewModelScope.launch {
_videoSaveState.emit(VideoSaveState.VIDEO_SAVE_SUCCESS)
}

sendVideo()

Expand All @@ -88,8 +91,9 @@ class VideoEditScreenViewModel @Inject constructor(
exportException: ExportException,
) {
exportException.printStackTrace()
Toast.makeText(application, "Error applying edits on video", Toast.LENGTH_LONG)
.show()
viewModelScope.launch {
_videoSaveState.emit(VideoSaveState.VIDEO_SAVE_FAIL)
}
_isProcessing.value = false
}
}
Expand Down Expand Up @@ -183,3 +187,9 @@ class VideoEditScreenViewModel @Inject constructor(
}
}
}

enum class VideoSaveState {
PENDING,
VIDEO_SAVE_SUCCESS,
VIDEO_SAVE_FAIL,
}