diff --git a/app/src/main/java/dev/niro/cameraremote/ui/UserInputController.kt b/app/src/main/java/dev/niro/cameraremote/ui/UserInputController.kt index 28917a8..dc96d9c 100644 --- a/app/src/main/java/dev/niro/cameraremote/ui/UserInputController.kt +++ b/app/src/main/java/dev/niro/cameraremote/ui/UserInputController.kt @@ -3,11 +3,11 @@ package dev.niro.cameraremote.ui import android.content.Context import dev.niro.cameraremote.bluetooth.BluetoothController import dev.niro.cameraremote.interfaces.IUserInterfaceTimerCallback +import dev.niro.cameraremote.utils.Clock import dev.niro.cameraremote.utils.Vibrator import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job -import kotlinx.coroutines.delay import kotlinx.coroutines.launch object UserInputController { @@ -35,36 +35,40 @@ object UserInputController { } triggerCoroutine = CoroutineScope(Dispatchers.Default).launch { - var firstPicture = true + Vibrator.tick(context) + do { - val configuredTimerDelay = timerDelay + runTimerProcess(context) + } while (autoTriggerEnabled) + } + } - for (waitCounter in 0.. 0) { - Vibrator.tick(context) - } + private suspend fun runTimerProcess(context: Context) { + val configuredTimerDelay = timerDelay - val tickFPS = if (uiCallback?.isAmbientModeActive() == true) { 1 } else { 40 } + for (waitCounter in 0.. 0) { + Vibrator.tick(context) + } - for (subTick in 1..tickFPS) { - delay(1000L / tickFPS) + val uiFPS = if (uiCallback?.isAmbientModeActive() == false) { 40 } else { 1 } - val subTickProgress = subTick / tickFPS.toFloat() - val progress = (waitCounter + subTickProgress) / configuredTimerDelay + for (subTick in 1..uiFPS) { + Clock.sleep(1000L / uiFPS) - uiCallback?.changeProgressIndicatorState(progress) - } - } + val subTickProgress = subTick / uiFPS.toFloat() + val progress = (waitCounter + subTickProgress) / configuredTimerDelay - if (configuredTimerDelay == 0) { - uiCallback?.changeProgressIndicatorState(1f) - } + uiCallback?.changeProgressIndicatorState(progress) + } + } - Vibrator.shoot(context) - BluetoothController.takePicture() - firstPicture = false - } while (autoTriggerEnabled) + if (configuredTimerDelay == 0) { + uiCallback?.changeProgressIndicatorState(1f) } + + Vibrator.shoot(context) + BluetoothController.takePicture() } fun toggleTimer() { diff --git a/app/src/main/java/dev/niro/cameraremote/utils/Clock.kt b/app/src/main/java/dev/niro/cameraremote/utils/Clock.kt new file mode 100644 index 0000000..c93b8e3 --- /dev/null +++ b/app/src/main/java/dev/niro/cameraremote/utils/Clock.kt @@ -0,0 +1,26 @@ +package dev.niro.cameraremote.utils + +import kotlinx.coroutines.delay + +object Clock { + + suspend fun sleep(milliseconds: Long) { + val startTime = System.currentTimeMillis() + + while (true) { + val elapsedTime = System.currentTimeMillis() - startTime + val remainingTime = milliseconds - elapsedTime + + if (remainingTime <= 0) { + break + } + + if (remainingTime < 10) { + delay(remainingTime) + } else { + delay(remainingTime / 2) + } + } + } + +} \ No newline at end of file