Skip to content
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

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Clean up video/photo save state logic.
  • Loading branch information
donovanfm committed Mar 21, 2024
commit 2bc9213639974de06d55eac3e4a6d613a9a884c1
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ fun Camera(
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 */
}
ImageCaptureState.PENDING -> Unit
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +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.flow.StateFlow
import kotlinx.coroutines.launch

@HiltViewModel
Expand All @@ -73,7 +73,7 @@ class CameraViewModel @Inject constructor(
val chatId: Long? = savedStateHandle.get("chatId")
var viewFinderState = MutableStateFlow(ViewFinderState())
private var _imageCaptureState = MutableStateFlow(ImageCaptureState.PENDING)
donovanfm marked this conversation as resolved.
Show resolved Hide resolved
val imageCaptureState: SharedFlow<ImageCaptureState> = _imageCaptureState
val imageCaptureState: StateFlow<ImageCaptureState> = _imageCaptureState

val aspectRatioStrategy =
AspectRatioStrategy(AspectRatio.RATIO_16_9, AspectRatioStrategy.FALLBACK_RULE_NONE)
Expand Down Expand Up @@ -191,13 +191,11 @@ class CameraViewModel @Inject constructor(
ContextCompat.getMainExecutor(context),
object : ImageCapture.OnImageSavedCallback {
override fun onError(exc: ImageCaptureException) {
viewModelScope.launch {
_imageCaptureState.emit(ImageCaptureState.IMAGE_CAPTURE_FAIL)
}
_imageCaptureState.value = ImageCaptureState.IMAGE_CAPTURE_FAIL
}

override fun onImageSaved(output: ImageCapture.OutputFileResults) {
var state = ImageCaptureState.PENDING
var state: ImageCaptureState
donovanfm marked this conversation as resolved.
Show resolved Hide resolved
val savedUri = output.savedUri
if (savedUri != null) {
state = ImageCaptureState.IMAGE_CAPTURE_SUCCESS
Expand All @@ -206,9 +204,7 @@ class CameraViewModel @Inject constructor(
} else {
state = ImageCaptureState.IMAGE_CAPTURE_FAIL
}
viewModelScope.launch {
_imageCaptureState.emit(state)
}
_imageCaptureState.value = state
}
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@ fun VideoEditScreen(
VideoSaveState.VIDEO_SAVE_FAIL ->
Toast.makeText(context, "Error applying edits on video", Toast.LENGTH_LONG)
.show()
else -> {
/* no-op */
}
VideoSaveState.PENDING -> Unit
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ 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 @@ -66,7 +65,7 @@ class VideoEditScreenViewModel @Inject constructor(
val isProcessing: StateFlow<Boolean> = _isProcessing

private var _videoSaveState = MutableStateFlow(VideoSaveState.PENDING)
donovanfm marked this conversation as resolved.
Show resolved Hide resolved
val videoSaveState: SharedFlow<VideoSaveState> = _videoSaveState
val videoSaveState: StateFlow<VideoSaveState> = _videoSaveState

fun setChatId(chatId: Long) {
this.chatId.value = chatId
Expand All @@ -75,9 +74,7 @@ class VideoEditScreenViewModel @Inject constructor(
private val transformerListener: Transformer.Listener =
@UnstableApi object : Transformer.Listener {
override fun onCompleted(composition: Composition, exportResult: ExportResult) {
viewModelScope.launch {
_videoSaveState.emit(VideoSaveState.VIDEO_SAVE_SUCCESS)
}
_videoSaveState.value = VideoSaveState.VIDEO_SAVE_SUCCESS

sendVideo()

Expand All @@ -91,9 +88,7 @@ class VideoEditScreenViewModel @Inject constructor(
exportException: ExportException,
) {
exportException.printStackTrace()
viewModelScope.launch {
_videoSaveState.emit(VideoSaveState.VIDEO_SAVE_FAIL)
}
_videoSaveState.value = VideoSaveState.VIDEO_SAVE_FAIL
_isProcessing.value = false
}
}
Expand Down