Skip to content

Commit

Permalink
Move Toast invocations out of Camera/VideoEdit ViewModels.
Browse files Browse the repository at this point in the history
  • Loading branch information
donovanfm committed Mar 20, 2024
1 parent f81a4bd commit 6cc3d37
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 9 deletions.
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 @@ -64,6 +65,7 @@ import androidx.window.layout.FoldingFeature
import androidx.window.layout.WindowInfoTracker
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.rememberMultiplePermissionsState
import kotlin.coroutines.coroutineContext
import kotlin.reflect.KFunction1
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.asExecutor
Expand Down Expand Up @@ -91,6 +93,21 @@ 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 @@ -49,6 +48,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.flow.StateFlow
import kotlinx.coroutines.launch

Expand All @@ -67,14 +67,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 +93,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 +189,9 @@ class VideoEditScreenViewModel @Inject constructor(
}
}
}

enum class VideoSaveState {
PENDING,
VIDEO_SAVE_SUCCESS,
VIDEO_SAVE_FAIL
}

0 comments on commit 6cc3d37

Please sign in to comment.