Re-implemented FlickerCodeAnimator with coroutines, but it's not working on iOS though

This commit is contained in:
dankito 2020-08-13 14:31:03 +02:00
parent de81b227eb
commit cbbdf4bc73
1 changed files with 41 additions and 41 deletions

View File

@ -1,5 +1,6 @@
package net.dankito.banking.ui.util package net.dankito.banking.ui.util
import kotlinx.coroutines.*
import net.dankito.banking.ui.model.tan.FlickerCode import net.dankito.banking.ui.model.tan.FlickerCode
import net.dankito.banking.fints.tan.Bit import net.dankito.banking.fints.tan.Bit
import net.dankito.banking.fints.tan.FlickerCanvas import net.dankito.banking.fints.tan.FlickerCanvas
@ -19,50 +20,52 @@ open class FlickerCodeAnimator {
} }
@Volatile
protected var currentFrequency: Int = DefaultFrequency protected var currentFrequency: Int = DefaultFrequency
protected var currentStepIndex = 0
@Volatile @Volatile
protected var isPaused = false protected var isPaused = false
// protected var calculateAnimationThread: Thread? = null protected var animationJob: Job? = null
@JvmOverloads open fun animateFlickerCode(flickerCode: FlickerCode, showStep: (Array<Bit>) -> Unit) {
open fun animateFlickerCode(flickerCode: FlickerCode, frequency: Int = DefaultFrequency, showStep: (Array<Bit>) -> Unit) { animateFlickerCode(flickerCode, DefaultFrequency, showStep)
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()
} }
// protected open fun calculateAnimation(steps: List<Array<Bit>>, showStep: (Array<Bit>) -> Unit) { open fun animateFlickerCode(flickerCode: FlickerCode, frequency: Int, showStep: (Array<Bit>) -> Unit) {
// while (Thread.currentThread().isInterrupted == false) { stop() // stop may still running previous animation
// if (isPaused == false) {
// val nextStep = steps[currentStepIndex] currentFrequency = frequency
//
// showStep(nextStep) animationJob = GlobalScope.launch(Dispatchers.Default) {
// val steps = FlickerCanvas(flickerCode.parsedDataSet).steps
// currentStepIndex++
// if (currentStepIndex >= steps.size) { calculateAnimation(steps, showStep)
// currentStepIndex = 0 // all steps shown, start again from beginning }
// } }
// }
// protected open suspend fun calculateAnimation(steps: List<Array<Bit>>, showStep: (Array<Bit>) -> Unit) {
// try { var currentStepIndex = 0
// TimeUnit.MILLISECONDS.sleep(1000L / currentFrequency)
// } catch (ignored: Exception) { while (true) {
// Thread.currentThread().interrupt() 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() { open fun pause() {
this.isPaused = true this.isPaused = true
@ -74,14 +77,11 @@ open class FlickerCodeAnimator {
open fun stop() { open fun stop() {
try { try {
// if (calculateAnimationThread?.isInterrupted == false) { animationJob?.cancel()
// calculateAnimationThread?.interrupt()
// calculateAnimationThread?.join(500) animationJob = null
//
// calculateAnimationThread = null
// }
} catch (e: Exception) { } catch (e: Exception) {
log.warn(e) { "Could not stop calculateAnimationThread" } log.warn(e) { "Could not stop animation job" }
} }
} }