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

Add basic Camera sample #382

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

Add basic Camera sample #382

wants to merge 4 commits into from

Conversation

JolandaVerhoef
Copy link
Contributor

No description provided.

Copy link

@temcguir temcguir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some changes to make in order to get it working with the current CoroutineLifecycleOwner code. I'll take another pass to get it working without CoroutineLifecycleOwner.

media/build.gradle.kts Show resolved Hide resolved
media/src/main/AndroidManifest.xml Show resolved Hide resolved
Comment on lines 69 to 70
fun CameraPreviewScreen(modifier: Modifier = Modifier) {
val viewModel = CameraPreviewViewModel(LocalContext.current.applicationContext)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ViewModel should be retrieved via androidx.lifecycle.viewmodel.compose.viewModel. Something like:

@Composable
fun CameraPreviewScreen(
    modifier: Modifier = Modifier,
    viewModel: CameraPreviewViewModel = viewModel()
) {
    val surfaceRequest by viewModel.surfaceRequest.collectAsStateWithLifecycle()

val surfaceRequest by viewModel.surfaceRequest.collectAsStateWithLifecycle()

LifecycleStartEffect(Unit) {
viewModel.startCamera()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can just pass the context to the camera here instead of through the ViewModel constructor. This is to align with the advice from https://developer.android.com/topic/architecture/recommendations#viewmodel where it says not to use AndroidViewModel. Though I wonder if there is still a better way to pass the context in.

    val context = LocalContext.current
    LifecycleStartEffect(Unit) {
        viewModel.startCamera(context)
        onStopOrDispose {
            viewModel.stopCamera()
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants