Skip to content

Commit

Permalink
Single source of truth for camera settings (#122)
Browse files Browse the repository at this point in the history
* Update CameraUseCase to reactive model

 CameraUseCase (and CameraXCameraUseCase implementation) will now hold on
 to CameraAppSettings and react to changes in those settings which are
 initiated through the CameraUseCase API.

* PreviewViewModel should not edit CameraAppState

 CameraAppState is now owned by CameraUseCase. It will have the single source of
 truth, and PreviewViewModel will only communicate changes through PreviewUiState,
 it will not edit CameraAppState directly.

* Setting aspect ratio and flipping camera no longer restarts camera job

 The camera job is started once by runCamera(), any changes that restart
 the camera will happen contained inside this coroutine. We no longer need
 to explicitly stop the camera before applying changes that would normally
 restart the camera.

* Fix tests to work with API changes

 Also changes FakeCameraUseCase to use a similar reactive model
 to CameraXCameraUseCase

* Fix more tests
  • Loading branch information
temcguir authored Feb 29, 2024
1 parent 600eeab commit 3ef520a
Show file tree
Hide file tree
Showing 10 changed files with 424 additions and 373 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ data class CameraAppSettings(
val videoCaptureStabilization: Stabilization = Stabilization.UNDEFINED,
val supportedStabilizationModes: List<SupportedStabilizationMode> = emptyList(),
val dynamicRange: DynamicRange = DynamicRange.SDR,
val supportedDynamicRanges: List<DynamicRange> = listOf(DynamicRange.SDR)
val supportedDynamicRanges: List<DynamicRange> = listOf(DynamicRange.SDR),
val zoomScale: Float = 1f
)

val DEFAULT_CAMERA_APP_SETTINGS = CameraAppSettings()
5 changes: 3 additions & 2 deletions domain/camera/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ android {
dependencies {
// Testing
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
testImplementation(libs.truth)
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3")
testImplementation("org.mockito:mockito-core:5.2.0")
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)

// Futures
implementation(libs.futures.ktx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ interface CameraUseCase {
*
* @return list of available lenses.
*/
suspend fun initialize(currentCameraSettings: CameraAppSettings): List<Int>
suspend fun initialize()

/**
* Starts the camera with given [CameraAppSettings].
* Starts the camera.
*
* This will start to configure the camera, but frames won't stream until a [SurfaceRequest]
* from [getSurfaceRequest] has been fulfilled.
*
* The camera will run until the calling coroutine is cancelled.
*/
suspend fun runCamera(currentCameraSettings: CameraAppSettings)
suspend fun runCamera()

suspend fun takePicture()

Expand All @@ -64,13 +64,15 @@ interface CameraUseCase {

fun getScreenFlashEvents(): SharedFlow<ScreenFlashEvent>

fun setFlashMode(flashMode: SettingsFlashMode, isFrontFacing: Boolean)
fun getCurrentSettings(): StateFlow<CameraAppSettings?>

fun setFlashMode(flashMode: SettingsFlashMode)

fun isScreenFlashEnabled(): Boolean

suspend fun setAspectRatio(aspectRatio: SettingsAspectRatio, isFrontFacing: Boolean)
suspend fun setAspectRatio(aspectRatio: SettingsAspectRatio)

suspend fun flipCamera(isFrontFacing: Boolean, flashMode: SettingsFlashMode)
suspend fun flipCamera(isFrontFacing: Boolean)

fun tapToFocus(display: Display, surfaceWidth: Int, surfaceHeight: Int, x: Float, y: Float)

Expand Down
Loading

0 comments on commit 3ef520a

Please sign in to comment.