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

Set up Compose rules check #84

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ ij_kotlin_allow_trailing_comma = true
ij_kotlin_allow_trailing_comma_on_call_site = true
ktlint_code_style = intellij_idea
ktlint_function_naming_ignore_when_annotated_with = Composable
ktlint_standard_function-expression-body = disabled

[*.md]
trim_trailing_whitespace = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ class ChatRepository @Inject internal constructor(
) {
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")
private val enableChatbotKey = booleanPreferencesKey("enable_chatbot")
val isBotEnabled = appContext.dataStore.data.map {
preference ->
val isBotEnabled = appContext.dataStore.data.map { preference ->
preference[enableChatbotKey] ?: false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,22 @@ import androidx.compose.ui.Modifier
import com.google.android.samples.socialite.ui.chat.ChatScreen

@Composable
fun Bubble(chatId: Long) {
fun Bubble(
chatId: Long,
modifier: Modifier = Modifier,
) {
SocialTheme {
ChatScreen(
chatId = chatId,
foreground = false,
onBackPressed = null,
onBackPress = null,
// TODO (donovanfm): Hook up camera button in the Bubble composable
onCameraClick = {},
// TODO (jolandaverhoef): Hook up play video button in the Bubble composable
onVideoClick = {},
// TODO (mayurikhin): Hook up camera button in the Bubble composable
onPhotoPickerClick = {},
modifier = Modifier.fillMaxSize(),
modifier = modifier.fillMaxSize(),
)
}
}
21 changes: 12 additions & 9 deletions app/src/main/java/com/google/android/samples/socialite/ui/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,20 @@ import com.google.android.samples.socialite.ui.videoedit.VideoEditScreen
@Composable
fun Main(
shortcutParams: ShortcutParams?,
modifier: Modifier = Modifier,
) {
val modifier = Modifier.fillMaxSize()
SocialTheme {
MainNavigation(modifier, shortcutParams)
MainNavigation(
shortcutParams = shortcutParams,
modifier = modifier.fillMaxSize(),
)
}
}

@Composable
fun MainNavigation(
modifier: Modifier,
shortcutParams: ShortcutParams?,
modifier: Modifier = Modifier,
) {
val activity = LocalContext.current as Activity
val navController = rememberNavController()
Expand Down Expand Up @@ -95,7 +98,7 @@ fun MainNavigation(
) {
Home(
modifier = Modifier.fillMaxSize(),
onChatClicked = { chatId -> navController.navigate("chat/$chatId") },
onChatClick = { chatId -> navController.navigate("chat/$chatId") },
)
}
composable(
Expand All @@ -116,7 +119,7 @@ fun MainNavigation(
ChatScreen(
chatId = chatId,
foreground = true,
onBackPressed = { navController.popBackStack() },
onBackPress = { navController.popBackStack() },
onCameraClick = { navController.navigate("chat/$chatId/camera") },
onPhotoPickerClick = { navController.navigateToPhotoPicker(chatId) },
onVideoClick = { uri -> navController.navigate("videoPlayer?uri=$uri") },
Expand All @@ -132,7 +135,7 @@ fun MainNavigation(
) { backStackEntry ->
val chatId = backStackEntry.arguments?.getLong("chatId") ?: 0L
Camera(
onMediaCaptured = { capturedMedia: Media? ->
onMediaCapture = { capturedMedia: Media? ->
when (capturedMedia?.mediaType) {
MediaType.PHOTO -> {
navController.popBackStack()
Expand All @@ -155,7 +158,7 @@ fun MainNavigation(

// Invoke PhotoPicker to select photo or video from device gallery
photoPickerScreen(
onPhotoPicked = navController::popBackStack,
onPhotoPick = navController::popBackStack,
)

composable(
Expand All @@ -170,7 +173,7 @@ fun MainNavigation(
VideoEditScreen(
chatId = chatId,
uri = videoUri,
onCloseButtonClicked = { navController.popBackStack() },
onCloseButtonClick = { navController.popBackStack() },
navController = navController,
)
}
Expand All @@ -183,7 +186,7 @@ fun MainNavigation(
val videoUri = backStackEntry.arguments?.getString("videoUri") ?: ""
VideoPlayerScreen(
uri = videoUri,
onCloseButtonClicked = { navController.popBackStack() },
onCloseButtonClick = { navController.popBackStack() },
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ import kotlinx.coroutines.asExecutor
@Composable
fun Camera(
chatId: Long,
onMediaCaptured: (Media?) -> Unit,
onMediaCapture: (Media?) -> Unit,
modifier: Modifier = Modifier,
viewModel: CameraViewModel = hiltViewModel(),
) {
Expand Down Expand Up @@ -165,7 +165,7 @@ fun Camera(
@SuppressLint("MissingPermission")
fun onVideoRecordingStart() {
captureMode = CaptureMode.VIDEO_RECORDING
viewModel.startVideoCapture(onMediaCaptured)
viewModel.startVideoCapture(onMediaCapture)
}

fun onVideoRecordingFinish() {
Expand All @@ -184,7 +184,7 @@ fun Camera(
.height(50.dp),
) {
IconButton(onClick = {
onMediaCaptured(null)
onMediaCapture(null)
}) {
Icon(
imageVector = Icons.Default.ArrowBack,
Expand Down Expand Up @@ -227,7 +227,7 @@ fun Camera(
) {
ShutterButton(
captureMode,
{ viewModel.capturePhoto(onMediaCaptured) },
{ viewModel.capturePhoto(onMediaCapture) },
{ onVideoRecordingStart() },
{ onVideoRecordingFinish() },
)
Expand All @@ -248,9 +248,9 @@ fun Camera(
horizontalAlignment = Alignment.CenterHorizontally,
) {
ViewFinder(
viewFinderState.cameraState,
onPreviewSurfaceProviderReady,
viewModel::setZoomScale,
cameraState = viewFinderState.cameraState,
onSurfaceProviderReady = onPreviewSurfaceProviderReady,
onZoomChange = viewModel::setZoomScale,
)
}
}
Expand All @@ -261,9 +261,9 @@ fun Camera(
.weight(1f),
) {
ViewFinder(
viewFinderState.cameraState,
onPreviewSurfaceProviderReady,
viewModel::setZoomScale,
cameraState = viewFinderState.cameraState,
onSurfaceProviderReady = onPreviewSurfaceProviderReady,
onZoomChange = viewModel::setZoomScale,
)
}
Row(
Expand Down Expand Up @@ -293,7 +293,7 @@ fun Camera(
Spacer(modifier = Modifier.size(50.dp))
ShutterButton(
captureMode,
{ viewModel.capturePhoto(onMediaCaptured) },
{ viewModel.capturePhoto(onMediaCapture) },
{ onVideoRecordingStart() },
{ onVideoRecordingFinish() },
)
Expand All @@ -305,13 +305,18 @@ fun Camera(
}
} else {
CameraAndRecordAudioPermission(cameraAndRecordAudioPermissionState) {
onMediaCaptured(null)
onMediaCapture(null)
}
}
}

@Composable
fun CameraControls(captureMode: CaptureMode, onPhotoButtonClick: () -> Unit, onVideoButtonClick: () -> Unit) {
fun CameraControls(
captureMode: CaptureMode,
onPhotoButtonClick: () -> Unit,
onVideoButtonClick: () -> Unit,
modifier: Modifier = Modifier,
) {
val activeButtonColor =
ButtonDefaults.buttonColors(containerColor = MaterialTheme.colorScheme.primary)
val inactiveButtonColor =
Expand All @@ -335,8 +340,14 @@ fun CameraControls(captureMode: CaptureMode, onPhotoButtonClick: () -> Unit, onV
}

@Composable
fun ShutterButton(captureMode: CaptureMode, onPhotoCapture: () -> Unit, onVideoRecordingStart: () -> Unit, onVideoRecordingFinish: () -> Unit) {
Box(modifier = Modifier.padding(25.dp, 0.dp)) {
fun ShutterButton(
captureMode: CaptureMode,
onPhotoCapture: () -> Unit,
onVideoRecordingStart: () -> Unit,
onVideoRecordingFinish: () -> Unit,
modifier: Modifier = Modifier,
) {
Box(modifier = modifier.padding(25.dp, 0.dp)) {
if (captureMode == CaptureMode.PHOTO) {
Button(
onClick = onPhotoCapture,
Expand Down Expand Up @@ -370,15 +381,23 @@ fun ShutterButton(captureMode: CaptureMode, onPhotoCapture: () -> Unit, onVideoR
}

@Composable
fun CameraSwitcher(captureMode: CaptureMode, cameraSelector: CameraSelector, setCameraSelector: KFunction1<CameraSelector, Unit>) {
fun CameraSwitcher(
captureMode: CaptureMode,
cameraSelector: CameraSelector,
setCameraSelector: KFunction1<CameraSelector, Unit>,
modifier: Modifier = Modifier,
) {
if (captureMode != CaptureMode.VIDEO_RECORDING) {
IconButton(onClick = {
if (cameraSelector == CameraSelector.DEFAULT_BACK_CAMERA) {
setCameraSelector(CameraSelector.DEFAULT_FRONT_CAMERA)
} else {
setCameraSelector(CameraSelector.DEFAULT_BACK_CAMERA)
}
}) {
IconButton(
onClick = {
if (cameraSelector == CameraSelector.DEFAULT_BACK_CAMERA) {
setCameraSelector(CameraSelector.DEFAULT_FRONT_CAMERA)
} else {
setCameraSelector(CameraSelector.DEFAULT_BACK_CAMERA)
}
},
modifier = modifier,
) {
Icon(
imageVector = Icons.Default.Autorenew,
contentDescription = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ import com.google.android.samples.socialite.R
fun CameraAndRecordAudioPermission(
permissionsState: MultiplePermissionsState,
modifier: Modifier = Modifier,
onBackClicked: () -> Unit,
onBackClick: () -> Unit,
) {
var alreadyRequestedCameraPermissions by remember { mutableStateOf(false) }
fun onRequestPermissionsClicked() {
fun onRequestPermissionsClick() {
permissionsState.launchMultiplePermissionRequest()
alreadyRequestedCameraPermissions = true
}
Expand Down Expand Up @@ -92,7 +92,7 @@ fun CameraAndRecordAudioPermission(
} else {
if (permissionsState.shouldShowRationale) {
Row {
Button(onClick = { onRequestPermissionsClicked() }) {
Button(onClick = { onRequestPermissionsClick() }) {
Text(stringResource(R.string.grant_permission))
}
}
Expand All @@ -104,7 +104,7 @@ fun CameraAndRecordAudioPermission(
}
}

Button(onClick = { onBackClicked() }) {
Button(onClick = { onBackClick() }) {
Text(stringResource(R.string.back))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.produceState
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Modifier
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Runnable
Expand Down Expand Up @@ -58,9 +59,10 @@ fun CameraXViewfinder(
implementationMode: ImplementationMode = ImplementationMode.PERFORMANCE,
onSurfaceProviderReady: (Preview.SurfaceProvider) -> Unit = {},
) {
val latestOnSurfaceProviderReady by rememberUpdatedState(onSurfaceProviderReady)
val viewfinderArgs by produceState<ViewfinderArgs?>(initialValue = null, implementationMode) {
val requests = MutableStateFlow<SurfaceRequest?>(null)
onSurfaceProviderReady(
latestOnSurfaceProviderReady(
Preview.SurfaceProvider { request ->
requests.update { oldRequest ->
oldRequest?.willNotProvideSurface()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import androidx.compose.ui.graphics.Color
@Composable
fun ViewFinder(
cameraState: CameraState,
modifier: Modifier = Modifier,
onSurfaceProviderReady: (Preview.SurfaceProvider) -> Unit = {},
onZoomChange: (Float) -> Unit,
) {
Expand All @@ -40,7 +41,7 @@ fun ViewFinder(
},
)
Box(
Modifier
modifier
.background(Color.Black)
.fillMaxSize(),
contentAlignment = Alignment.Center,
Expand Down
Loading
Loading