mirror of
https://github.com/Myzel394/Alibi.git
synced 2025-06-18 23:05:26 +02:00
refactor: Use optimistic updates for torch
Signed-off-by: Myzel394 <50424412+Myzel394@users.noreply.github.com>
This commit is contained in:
parent
590f3f2d13
commit
f426cfe287
@ -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
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
}
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user