From cda5321a4e3bb7e9f84528f6e61487bf6dae5737 Mon Sep 17 00:00:00 2001 From: HyunWoo Lee Date: Thu, 15 Feb 2024 18:58:15 +0900 Subject: [PATCH 1/3] Fix: Render Error at VideoEditScreen due to initialization of viewmodel in screen composable --- .../socialite/ui/videoedit/VideoEditScreen.kt | 104 ++++++++++++------ 1 file changed, 68 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/com/google/android/samples/socialite/ui/videoedit/VideoEditScreen.kt b/app/src/main/java/com/google/android/samples/socialite/ui/videoedit/VideoEditScreen.kt index ef6db537..84e64936 100644 --- a/app/src/main/java/com/google/android/samples/socialite/ui/videoedit/VideoEditScreen.kt +++ b/app/src/main/java/com/google/android/samples/socialite/ui/videoedit/VideoEditScreen.kt @@ -59,7 +59,6 @@ import androidx.compose.material3.TextFieldDefaults import androidx.compose.material3.TopAppBar import androidx.compose.material3.TopAppBarDefaults import androidx.compose.runtime.Composable -import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.saveable.rememberSaveable @@ -79,7 +78,6 @@ import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.media3.common.util.UnstableApi import androidx.navigation.NavController -import androidx.navigation.compose.rememberNavController import com.google.android.samples.socialite.R private const val TAG = "VideoEditScreen" @@ -91,37 +89,67 @@ fun VideoEditScreen( uri: String, onCloseButtonClicked: () -> Unit, navController: NavController, + viewModel: VideoEditScreenViewModel = hiltViewModel(), ) { - val context = LocalContext.current - - val viewModel: VideoEditScreenViewModel = hiltViewModel() viewModel.setChatId(chatId) - - val isFinishedEditing = viewModel.isFinishedEditing.collectAsStateWithLifecycle() - if (isFinishedEditing.value) { - navController.popBackStack("chat/$chatId", false) - } - - val isProcessing = viewModel.isProcessing.collectAsState() - + val isFinishedEditing by viewModel.isFinishedEditing.collectAsStateWithLifecycle() + val isProcessing by viewModel.isProcessing.collectAsStateWithLifecycle() var removeAudioEnabled by rememberSaveable { mutableStateOf(false) } var overlayText by rememberSaveable { mutableStateOf("") } var redOverlayTextEnabled by rememberSaveable { mutableStateOf(false) } var largeOverlayTextEnabled by rememberSaveable { mutableStateOf(false) } + val context = LocalContext.current + if (isFinishedEditing) { + navController.popBackStack("chat/$chatId", false) + } + VideoEditScreen( + uri = uri, + isProcessing = isProcessing, + filterChipSelected = removeAudioEnabled, + onCloseButtonClicked = onCloseButtonClicked, + buttonOverlayText = overlayText, + redOverlayTextEnabled = redOverlayTextEnabled, + largeOverlayTextEnabled = largeOverlayTextEnabled, + onSendButtonClicked = { + viewModel.applyVideoTransformation( + context = context, + videoUri = uri, + removeAudio = removeAudioEnabled, + textOverlayText = overlayText, + textOverlayRedSelected = redOverlayTextEnabled, + textOverlayLargeSelected = largeOverlayTextEnabled, + ) + }, + onFilterChipPressed = { removeAudioEnabled = !removeAudioEnabled }, + onButtonOverlayTextChanged = { overlayText = it }, + onRedOverlayTextCheckedStateChanged = { redOverlayTextEnabled = !redOverlayTextEnabled }, + onLargeOverlayTextCheckedStateChanged = { + largeOverlayTextEnabled = !largeOverlayTextEnabled + }, + ) +} + +@androidx.annotation.OptIn(UnstableApi::class) +@Composable +fun VideoEditScreen( + uri: String, + isProcessing: Boolean, + filterChipSelected: Boolean, + buttonOverlayText: String, + redOverlayTextEnabled: Boolean, + largeOverlayTextEnabled: Boolean, + onCloseButtonClicked: () -> Unit, + onSendButtonClicked: () -> Unit, + onFilterChipPressed: () -> Unit, + onButtonOverlayTextChanged: (String) -> Unit, + onRedOverlayTextCheckedStateChanged: () -> Unit, + onLargeOverlayTextCheckedStateChanged: () -> Unit, +) { Scaffold( topBar = { VideoEditTopAppBar( - onSendButtonClicked = { - viewModel.applyVideoTransformation( - context = context, - videoUri = uri, - removeAudio = removeAudioEnabled, - textOverlayText = overlayText, - textOverlayRedSelected = redOverlayTextEnabled, - textOverlayLargeSelected = largeOverlayTextEnabled, - ) - }, + onSendButtonClicked = onSendButtonClicked, onCloseButtonClicked = onCloseButtonClicked, ) }, @@ -136,7 +164,7 @@ fun VideoEditScreen( horizontalAlignment = Alignment.CenterHorizontally, ) { Spacer(modifier = Modifier.height(50.dp)) - VideoMessagePreview(uri, isProcessing.value) + VideoMessagePreview(uri, isProcessing) Spacer(modifier = Modifier.height(20.dp)) Column( @@ -150,27 +178,23 @@ fun VideoEditScreen( ) { VideoEditFilterChip( icon = Icons.Filled.VolumeMute, - selected = removeAudioEnabled, - onClick = { removeAudioEnabled = !removeAudioEnabled }, + selected = filterChipSelected, + onClick = onFilterChipPressed, label = stringResource(id = R.string.remove_audio), ) Spacer(modifier = Modifier.height(10.dp)) TextOverlayOption( - inputtedText = overlayText, + inputtedText = buttonOverlayText, inputtedTextChange = { // Limit character count to 20 if (it.length <= 20) { - overlayText = it + onButtonOverlayTextChanged(it) } }, redTextCheckedState = redOverlayTextEnabled, - redTextCheckedStateChange = { - redOverlayTextEnabled = !redOverlayTextEnabled - }, + redTextCheckedStateChange = onRedOverlayTextCheckedStateChanged, largeTextCheckedState = largeOverlayTextEnabled, - largeTextCheckedStateChange = { - largeOverlayTextEnabled = !largeOverlayTextEnabled - }, + largeTextCheckedStateChange = onLargeOverlayTextCheckedStateChanged, ) } } @@ -343,9 +367,17 @@ private fun VideoEditFilterChip( @Preview fun VideoEditScreenPreview() { VideoEditScreen( - chatId = 0L, uri = "", + isProcessing = true, + buttonOverlayText = "", + filterChipSelected = true, + redOverlayTextEnabled = false, + largeOverlayTextEnabled = false, onCloseButtonClicked = {}, - navController = rememberNavController(), + onSendButtonClicked = {}, + onFilterChipPressed = {}, + onButtonOverlayTextChanged = {}, + onRedOverlayTextCheckedStateChanged = {}, + onLargeOverlayTextCheckedStateChanged = {}, ) } From 7313f1b99bdb6260cd06d8432158378e3d56ee87 Mon Sep 17 00:00:00 2001 From: HyunWoo Lee Date: Thu, 15 Feb 2024 18:59:19 +0900 Subject: [PATCH 2/3] Fix: replace deprecated icon --- .../android/samples/socialite/ui/videoedit/VideoEditScreen.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/google/android/samples/socialite/ui/videoedit/VideoEditScreen.kt b/app/src/main/java/com/google/android/samples/socialite/ui/videoedit/VideoEditScreen.kt index 84e64936..72925d77 100644 --- a/app/src/main/java/com/google/android/samples/socialite/ui/videoedit/VideoEditScreen.kt +++ b/app/src/main/java/com/google/android/samples/socialite/ui/videoedit/VideoEditScreen.kt @@ -38,11 +38,11 @@ import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.verticalScroll import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.automirrored.filled.VolumeMute import androidx.compose.material.icons.filled.Close import androidx.compose.material.icons.filled.DonutLarge import androidx.compose.material.icons.filled.FormatSize import androidx.compose.material.icons.filled.Movie -import androidx.compose.material.icons.filled.VolumeMute import androidx.compose.material3.Button import androidx.compose.material3.ButtonDefaults import androidx.compose.material3.CircularProgressIndicator @@ -177,7 +177,7 @@ fun VideoEditScreen( .padding(15.dp), ) { VideoEditFilterChip( - icon = Icons.Filled.VolumeMute, + icon = Icons.AutoMirrored.Filled.VolumeMute, selected = filterChipSelected, onClick = onFilterChipPressed, label = stringResource(id = R.string.remove_audio), From 427dc25422a48649242af8c2595900d123d15252 Mon Sep 17 00:00:00 2001 From: HyunWoo Lee Date: Thu, 15 Feb 2024 18:59:38 +0900 Subject: [PATCH 3/3] Remove redundant optin api --- .../android/samples/socialite/ui/videoedit/VideoEditScreen.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/main/java/com/google/android/samples/socialite/ui/videoedit/VideoEditScreen.kt b/app/src/main/java/com/google/android/samples/socialite/ui/videoedit/VideoEditScreen.kt index 72925d77..47db5280 100644 --- a/app/src/main/java/com/google/android/samples/socialite/ui/videoedit/VideoEditScreen.kt +++ b/app/src/main/java/com/google/android/samples/socialite/ui/videoedit/VideoEditScreen.kt @@ -332,7 +332,6 @@ fun TextOverlayOption( } } -@OptIn(ExperimentalMaterial3Api::class) @Composable private fun VideoEditFilterChip( icon: ImageVector,