Moving APIs in PreviewViewModel to Controller classes#482
Moving APIs in PreviewViewModel to Controller classes#482davidjiagoogle wants to merge 9 commits intomainfrom
Conversation
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
|
/gemini review |
There was a problem hiding this comment.
Code Review
The pull request successfully refactors the PreviewViewModel by extracting various UI event handling logic into dedicated callback data classes: QuickSettingsCallbacks, CaptureCallbacks, SnackBarCallbacks, and DebugCallbacks. This significantly simplifies the PreviewViewModel and improves modularity, making the code easier to understand and maintain. The changes are well-implemented, and the new callback structures enhance code organization and reusability. The test file PreviewViewModelTest.kt has also been updated to reflect these changes, ensuring continued test coverage.
feature/preview/src/main/java/com/google/jetpackcamera/feature/preview/PreviewViewModel.kt
Outdated
Show resolved
Hide resolved
feature/preview/src/main/java/com/google/jetpackcamera/feature/preview/PreviewViewModel.kt
Outdated
Show resolved
Hide resolved
feature/preview/src/main/java/com/google/jetpackcamera/feature/preview/PreviewViewModel.kt
Outdated
Show resolved
Hide resolved
...nts/capture/src/main/java/com/google/jetpackcamera/ui/components/capture/CaptureCallbacks.kt
Outdated
Show resolved
Hide resolved
...ts/capture/src/main/java/com/google/jetpackcamera/ui/components/capture/SnackBarCallbacks.kt
Outdated
Show resolved
Hide resolved
...capture/src/main/java/com/google/jetpackcamera/ui/components/capture/debug/DebugCallbacks.kt
Outdated
Show resolved
Hide resolved
.../java/com/google/jetpackcamera/ui/components/capture/quicksettings/QuickSettingsCallbacks.kt
Outdated
Show resolved
Hide resolved
|
/gemini review |
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request is a significant and well-executed refactoring that decomposes the large PreviewViewModel into several smaller, single-responsibility controller classes. This greatly improves the architecture by enhancing modularity, testability, and overall code clarity. The changes are consistently applied across the UI components, ViewModels, and tests. My review includes a few suggestions: fixing a minor copy-paste bug in CaptureControllerImpl, adding KDoc to the new controller APIs to improve documentation, and a couple of minor stylistic improvements in PreviewScreen.kt to make the code more idiomatic.
...main/java/com/google/jetpackcamera/ui/components/capture/controller/CaptureControllerImpl.kt
Outdated
Show resolved
Hide resolved
feature/preview/src/main/java/com/google/jetpackcamera/feature/preview/PreviewScreen.kt
Outdated
Show resolved
Hide resolved
feature/preview/src/main/java/com/google/jetpackcamera/feature/preview/PreviewScreen.kt
Outdated
Show resolved
Hide resolved
...src/main/java/com/google/jetpackcamera/ui/components/capture/controller/CaptureController.kt
Show resolved
Hide resolved
temcguir
left a comment
There was a problem hiding this comment.
This is a good start. There are definitely a few things that could be cleaned up later (such as the zoom controller). If you can address the comments I've added we could merge this and make smaller clean-up PRs to address those later.
| } | ||
| } | ||
|
|
||
| class MockQuickSettingsController : QuickSettingsController { |
There was a problem hiding this comment.
nit: Call this "NoOpQuickSettingsController". We should follow this naming convention for any of the @Preview composables..
| fun postCurrentMediaToMediaRepository( | ||
| viewModelScope: CoroutineScope, |
There was a problem hiding this comment.
Instead of passing in a viewModelScope, you should just make this a suspend function and have the viewModelScope.launch within the ViewModel wrapping this suspend function.
| /** | ||
| * Pauses or resumes video recording. | ||
| * | ||
| * @param shouldBePaused Whether the recording should be paused. | ||
| */ | ||
| fun setPaused(shouldBePaused: Boolean) |
There was a problem hiding this comment.
This should be in CaptureController.
| /** | ||
| * Enables or disables audio recording. | ||
| * | ||
| * @param shouldEnableAudio Whether audio should be enabled. | ||
| */ | ||
| fun setAudioEnabled(shouldEnableAudio: Boolean) |
There was a problem hiding this comment.
This should be in CaptureController.
|
|
||
| /** | ||
| * Updates the UI with the last captured media. | ||
| */ | ||
| fun updateLastCapturedMedia() | ||
|
|
||
| /** | ||
| * Transfers the media from the image well to the repository. | ||
| */ | ||
| fun imageWellToRepository() |
There was a problem hiding this comment.
Can these be moved to an ImageWellController?
| private val saveMode: SaveMode, | ||
| private val externalCaptureMode: ExternalCaptureMode, | ||
| private val externalCapturesCallback: () -> Pair<SaveLocation, IntProgress?>, | ||
| private val captureEvents: Channel<CaptureEvent>, |
There was a problem hiding this comment.
Does this need to be an input to the constructor? Can it just be a val on CaptureController? (likely as a ReceiveChannel in the interface).
| */ | ||
| class CaptureControllerImpl( | ||
| private val trackedCaptureUiState: MutableStateFlow<TrackedCaptureUiState>, | ||
| private val viewModelScope: CoroutineScope, |
There was a problem hiding this comment.
It's generally considered bad practice to pass in CoroutineScope to constructors. At a minimum this should be a CoroutineContext from which we create a scope within the controller. See go/android-api-guidelines-async#coroutines-constructor-context for more info.
| /** | ||
| * Sets the display rotation. | ||
| * | ||
| * @param deviceRotation The device rotation to set. | ||
| */ | ||
| fun setDisplayRotation(deviceRotation: DeviceRotation) | ||
|
|
||
| /** | ||
| * Initiates a tap-to-focus action at the given coordinates. | ||
| * | ||
| * @param x The x-coordinate of the tap. | ||
| * @param y The y-coordinate of the tap. | ||
| */ | ||
| fun tapToFocus(x: Float, y: Float) |
There was a problem hiding this comment.
These can be moved into CameraController.
| private val trackedCaptureUiState: MutableStateFlow<TrackedCaptureUiState>, | ||
| private val viewModelScope: CoroutineScope, | ||
| private val cameraSystem: CameraSystem, | ||
| private val mediaRepository: MediaRepository, |
There was a problem hiding this comment.
It would be good if we could remove the dependency on MediaRepository here. We should only have a MediaRepository dependency in the ViewModel. Maybe this can be replaced with a callback that can be supplied at creation time which will call the MediaRepository within the ViewModel instead of within the controller.
Interfaces QuickSettingsController, CaptureController, CaptureScreenController, SnackBarController, ZoomController, and DebugController are created. They contain APIs that were originally implemented in the view models. A default implementation of their APIs is made for each interface so that view model can be simplified
PreviewViewModel now only contains instances of these controller and has no APIs itself.