refactor: Use optimistic updates for torch

Signed-off-by: Myzel394 <50424412+Myzel394@users.noreply.github.com>
This commit is contained in:
Myzel394 2024-02-23 22:46:13 +01:00
parent 590f3f2d13
commit f426cfe287
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
2 changed files with 22 additions and 6 deletions

View File

@ -194,7 +194,9 @@ class VideoRecorderService :
selectedCamera,
videoCapture
)
cameraControl = CameraControl(camera!!)
cameraControl = CameraControl(camera!!).also {
it.init()
}
onCameraControlAvailable()
_cameraAvailableListener.complete(Unit)
@ -309,17 +311,25 @@ class VideoRecorderService :
}
class CameraControl(
val camera: Camera
val camera: Camera,
// Save state for optimistic updates
var torchEnabled: Boolean = false,
) {
fun init() {
torchEnabled = camera.cameraInfo.torchState.value == TorchState.ON
}
fun enableTorch() {
torchEnabled = true
camera.cameraControl.enableTorch(true)
}
fun disableTorch() {
torchEnabled = false
camera.cameraControl.enableTorch(false)
}
fun isTorchEnabled(): Boolean {
fun isHardwareTorchReallyEnabled(): Boolean {
return camera.cameraInfo.torchState.value == TorchState.ON
}

View File

@ -19,7 +19,11 @@ import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
@ -224,14 +228,16 @@ fun _VideoControls(videoRecorder: VideoRecorderModel) {
if (!videoRecorder.isStartingRecording) {
val cameraControl = videoRecorder.recorderService!!.cameraControl!!
if (cameraControl.hasTorchAvailable()) {
val isTorchEnabled = cameraControl.isTorchEnabled()
var torchEnabled by rememberSaveable { mutableStateOf(cameraControl.torchEnabled) }
TorchStatus(
enabled = isTorchEnabled,
enabled = torchEnabled,
onChange = {
if (isTorchEnabled) {
if (torchEnabled) {
torchEnabled = false
cameraControl.disableTorch()
} else {
torchEnabled = true
cameraControl.enableTorch()
}
},