From cbbdf4bc73d709ba0ff38e3f826260b0ebb29fdc Mon Sep 17 00:00:00 2001 From: dankito Date: Thu, 13 Aug 2020 14:31:03 +0200 Subject: [PATCH] Re-implemented FlickerCodeAnimator with coroutines, but it's not working on iOS though --- .../banking/ui/util/FlickerCodeAnimator.kt | 82 +++++++++---------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/util/FlickerCodeAnimator.kt b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/util/FlickerCodeAnimator.kt index 9b3b01d2..4375fe3f 100644 --- a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/util/FlickerCodeAnimator.kt +++ b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/util/FlickerCodeAnimator.kt @@ -1,5 +1,6 @@ package net.dankito.banking.ui.util +import kotlinx.coroutines.* import net.dankito.banking.ui.model.tan.FlickerCode import net.dankito.banking.fints.tan.Bit import net.dankito.banking.fints.tan.FlickerCanvas @@ -19,50 +20,52 @@ open class FlickerCodeAnimator { } + @Volatile protected var currentFrequency: Int = DefaultFrequency - protected var currentStepIndex = 0 - @Volatile protected var isPaused = false -// protected var calculateAnimationThread: Thread? = null + protected var animationJob: Job? = null - @JvmOverloads - open fun animateFlickerCode(flickerCode: FlickerCode, frequency: Int = DefaultFrequency, showStep: (Array) -> Unit) { - currentFrequency = frequency - currentStepIndex = 0 - val steps = FlickerCanvas(flickerCode.parsedDataSet).steps - - stop() // stop may still running previous animation - -// calculateAnimationThread = Thread({ calculateAnimation(steps, showStep) }, "CalculateFlickerCodeAnimation") -// -// calculateAnimationThread?.start() + open fun animateFlickerCode(flickerCode: FlickerCode, showStep: (Array) -> Unit) { + animateFlickerCode(flickerCode, DefaultFrequency, showStep) } -// protected open fun calculateAnimation(steps: List>, showStep: (Array) -> Unit) { -// while (Thread.currentThread().isInterrupted == false) { -// if (isPaused == false) { -// val nextStep = steps[currentStepIndex] -// -// showStep(nextStep) -// -// currentStepIndex++ -// if (currentStepIndex >= steps.size) { -// currentStepIndex = 0 // all steps shown, start again from beginning -// } -// } -// -// try { -// TimeUnit.MILLISECONDS.sleep(1000L / currentFrequency) -// } catch (ignored: Exception) { -// Thread.currentThread().interrupt() -// } -// } -// } + open fun animateFlickerCode(flickerCode: FlickerCode, frequency: Int, showStep: (Array) -> Unit) { + stop() // stop may still running previous animation + + currentFrequency = frequency + + animationJob = GlobalScope.launch(Dispatchers.Default) { + val steps = FlickerCanvas(flickerCode.parsedDataSet).steps + + calculateAnimation(steps, showStep) + } + } + + protected open suspend fun calculateAnimation(steps: List>, showStep: (Array) -> Unit) { + var currentStepIndex = 0 + + while (true) { + if (isPaused == false) { + val nextStep = steps[currentStepIndex] + + withContext(Dispatchers.Main) { + showStep(nextStep) + } + + currentStepIndex++ + if (currentStepIndex >= steps.size) { + currentStepIndex = 0 // all steps shown, start again from beginning + } + } + + delay(1000L / currentFrequency) + } + } open fun pause() { this.isPaused = true @@ -74,14 +77,11 @@ open class FlickerCodeAnimator { open fun stop() { try { -// if (calculateAnimationThread?.isInterrupted == false) { -// calculateAnimationThread?.interrupt() -// calculateAnimationThread?.join(500) -// -// calculateAnimationThread = null -// } + animationJob?.cancel() + + animationJob = null } catch (e: Exception) { - log.warn(e) { "Could not stop calculateAnimationThread" } + log.warn(e) { "Could not stop animation job" } } }