Extracted Step

This commit is contained in:
dankito 2020-08-13 15:11:38 +02:00
parent e0c5a72524
commit 8adab38d54
7 changed files with 69 additions and 51 deletions

View File

@ -26,8 +26,12 @@ open class ChipTanFlickerCodeStripeView @JvmOverloads constructor(
}
open fun showStripe(showStripe: Bit) {
if (showStripe == Bit.High) {
open fun setStripeVisibility(stripe: Bit) {
setStripeVisibility(stripe.isHigh)
}
open fun setStripeVisibility(showStripe: Boolean) {
if (showStripe) {
drawWhite()
}
else {

View File

@ -11,10 +11,9 @@ import kotlinx.android.synthetic.main.view_tan_image_size_controls.view.*
import net.dankito.banking.ui.android.R
import net.dankito.banking.ui.model.tan.FlickerCode
import net.dankito.banking.ui.util.FlickerCodeAnimator
import net.dankito.banking.ui.util.Bit
import net.dankito.banking.ui.model.settings.ITanView
import net.dankito.banking.ui.model.settings.TanProcedureSettings
import net.dankito.utils.android.extensions.asActivity
import net.dankito.banking.ui.util.Step
open class ChipTanFlickerCodeView @JvmOverloads constructor(
@ -227,19 +226,17 @@ open class ChipTanFlickerCodeView @JvmOverloads constructor(
this.didTanProcedureSettingsChange = false
animator.animateFlickerCode(flickerCode) { step ->
context.asActivity()?.runOnUiThread {
showStepOnUiThread(step)
}
showStepOnUiThread(step)
}
}
protected open fun showStepOnUiThread(step: Array<Bit>) {
protected open fun showStepOnUiThread(step: Step) {
stripe1.showStripe(step[0])
stripe2.showStripe(step[1])
stripe3.showStripe(step[2])
stripe4.showStripe(step[3])
stripe5.showStripe(step[4])
stripe1.setStripeVisibility(step.bit1)
stripe2.setStripeVisibility(step.bit2)
stripe3.setStripeVisibility(step.bit3)
stripe4.setStripeVisibility(step.bit4)
stripe5.setStripeVisibility(step.bit5)
}
}

View File

@ -11,6 +11,7 @@ import net.dankito.banking.javafx.dialogs.tan.controls.ChipTanFlickerCodeStripeV
import net.dankito.banking.javafx.dialogs.tan.controls.TanGeneratorMarkerView
import net.dankito.banking.ui.model.settings.ITanView
import net.dankito.banking.ui.model.settings.TanProcedureSettings
import net.dankito.banking.ui.util.Step
import net.dankito.utils.javafx.ui.extensions.fixedHeight
import net.dankito.utils.javafx.ui.extensions.fixedWidth
import net.dankito.utils.javafx.ui.extensions.setBackgroundToColor
@ -171,19 +172,17 @@ open class ChipTanFlickerCodeView(
updateMinAndMaxFrequencyReached()
animator.animateFlickerCode(flickerCode) { step ->
runLater {
paintFlickerCode(step)
}
paintFlickerCode(step)
}
}
protected open fun paintFlickerCode(step: Array<Bit>) {
stripe1.set(step[0] == Bit.High)
stripe2.set(step[1] == Bit.High)
stripe3.set(step[2] == Bit.High)
stripe4.set(step[3] == Bit.High)
stripe5.set(step[4] == Bit.High)
protected open fun paintFlickerCode(step: Step) {
stripe1.set(step.bit1.isHigh)
stripe2.set(step.bit2.isHigh)
stripe3.set(step.bit3.isHigh)
stripe4.set(step.bit4.isHigh)
stripe5.set(step.bit5.isHigh)
}
protected open fun setLeftMarkerPosition(component: UIComponent) {

View File

@ -8,6 +8,11 @@ enum class Bit(val value: Int) {
High(1);
val isHigh: Boolean
get() = this == High
val isLow: Boolean = !!! this.isHigh
fun invert(): Bit {
if (this == High) {
return Low

View File

@ -27,11 +27,11 @@ open class FlickerCodeAnimator {
open fun animateFlickerCode(flickerCode: FlickerCode, showStep: (Array<Bit>) -> Unit) {
open fun animateFlickerCode(flickerCode: FlickerCode, showStep: (Step) -> Unit) {
animateFlickerCode(flickerCode, DefaultFrequency, showStep)
}
open fun animateFlickerCode(flickerCode: FlickerCode, frequency: Int, showStep: (Array<Bit>) -> Unit) {
open fun animateFlickerCode(flickerCode: FlickerCode, frequency: Int, showStep: (Step) -> Unit) {
stop() // stop may still running previous animation
currentFrequency = frequency
@ -43,7 +43,7 @@ open class FlickerCodeAnimator {
}
}
protected open suspend fun calculateAnimation(steps: List<Array<Bit>>, showStep: (Array<Bit>) -> Unit) {
protected open suspend fun calculateAnimation(steps: List<Step>, showStep: (Step) -> Unit) {
var currentStepIndex = 0
while (true) {

View File

@ -7,36 +7,36 @@ open class FlickerCodeStepsCalculator {
companion object {
val bits = mutableMapOf<Char, List<Bit>>()
val bits = mutableMapOf<Char, Step>()
init {
/* bitfield: clock, bits 2^1, 2^2, 2^3, 2^4 */
bits['0'] = listOf(Bit.Low, Bit.Low, Bit.Low, Bit.Low, Bit.Low)
bits['1'] = listOf(Bit.Low, Bit.High, Bit.Low, Bit.Low, Bit.Low)
bits['2'] = listOf(Bit.Low, Bit.Low, Bit.High, Bit.Low, Bit.Low)
bits['3'] = listOf(Bit.Low, Bit.High, Bit.High, Bit.Low, Bit.Low)
bits['4'] = listOf(Bit.Low, Bit.Low, Bit.Low, Bit.High, Bit.Low)
bits['5'] = listOf(Bit.Low, Bit.High, Bit.Low, Bit.High, Bit.Low)
bits['6'] = listOf(Bit.Low, Bit.Low, Bit.High, Bit.High, Bit.Low)
bits['7'] = listOf(Bit.Low, Bit.High, Bit.High, Bit.High, Bit.Low)
bits['8'] = listOf(Bit.Low, Bit.Low, Bit.Low, Bit.Low, Bit.High)
bits['9'] = listOf(Bit.Low, Bit.High, Bit.Low, Bit.Low, Bit.High)
bits['A'] = listOf(Bit.Low, Bit.Low, Bit.High, Bit.Low, Bit.High)
bits['B'] = listOf(Bit.Low, Bit.High, Bit.High, Bit.Low, Bit.High)
bits['C'] = listOf(Bit.Low, Bit.Low, Bit.Low, Bit.High, Bit.High)
bits['D'] = listOf(Bit.Low, Bit.High, Bit.Low, Bit.High, Bit.High)
bits['E'] = listOf(Bit.Low, Bit.Low, Bit.High, Bit.High, Bit.High)
bits['F'] = listOf(Bit.Low, Bit.High, Bit.High, Bit.High, Bit.High)
bits['0'] = Step(Bit.Low, Bit.Low, Bit.Low, Bit.Low, Bit.Low)
bits['1'] = Step(Bit.Low, Bit.High, Bit.Low, Bit.Low, Bit.Low)
bits['2'] = Step(Bit.Low, Bit.Low, Bit.High, Bit.Low, Bit.Low)
bits['3'] = Step(Bit.Low, Bit.High, Bit.High, Bit.Low, Bit.Low)
bits['4'] = Step(Bit.Low, Bit.Low, Bit.Low, Bit.High, Bit.Low)
bits['5'] = Step(Bit.Low, Bit.High, Bit.Low, Bit.High, Bit.Low)
bits['6'] = Step(Bit.Low, Bit.Low, Bit.High, Bit.High, Bit.Low)
bits['7'] = Step(Bit.Low, Bit.High, Bit.High, Bit.High, Bit.Low)
bits['8'] = Step(Bit.Low, Bit.Low, Bit.Low, Bit.Low, Bit.High)
bits['9'] = Step(Bit.Low, Bit.High, Bit.Low, Bit.Low, Bit.High)
bits['A'] = Step(Bit.Low, Bit.Low, Bit.High, Bit.Low, Bit.High)
bits['B'] = Step(Bit.Low, Bit.High, Bit.High, Bit.Low, Bit.High)
bits['C'] = Step(Bit.Low, Bit.Low, Bit.Low, Bit.High, Bit.High)
bits['D'] = Step(Bit.Low, Bit.High, Bit.Low, Bit.High, Bit.High)
bits['E'] = Step(Bit.Low, Bit.Low, Bit.High, Bit.High, Bit.High)
bits['F'] = Step(Bit.Low, Bit.High, Bit.High, Bit.High, Bit.High)
}
}
open fun calculateSteps(flickerCode: String): List<Array<Bit>> {
open fun calculateSteps(flickerCode: String): List<Step> {
val halfbyteid = ObjectHolder(0)
val clock = ObjectHolder(Bit.High)
val bitarray = mutableListOf<MutableList<Bit>>()
val bitarray = mutableListOf<Step>()
/* prepend synchronization identifier */
@ -46,11 +46,11 @@ open class FlickerCodeStepsCalculator {
}
for (i in 0 until code.length step 2) {
bits[code[i + 1]]?.let { bitarray.add(mutableListOf(*it.toTypedArray())) }
bits[code[i]]?.let { bitarray.add(mutableListOf(*it.toTypedArray())) }
bits[code[i + 1]]?.let { bitarray.add(it) }
bits[code[i]]?.let { bitarray.add(it) }
}
val steps = mutableListOf<Array<Bit>>()
val steps = mutableListOf<Step>()
do {
steps.add(calculateStep(halfbyteid, clock, bitarray))
@ -59,10 +59,9 @@ open class FlickerCodeStepsCalculator {
return steps
}
protected open fun calculateStep(halfbyteid: ObjectHolder<Int>, clock: ObjectHolder<Bit>, bitarray: MutableList<MutableList<Bit>>): Array<Bit> {
bitarray[halfbyteid.value][0] = clock.value
protected open fun calculateStep(halfbyteid: ObjectHolder<Int>, clock: ObjectHolder<Bit>, bitarray: List<Step>): Step {
val step = Step(clock.value, bitarray[halfbyteid.value])
val stepBits = Array(5) { index -> bitarray[halfbyteid.value][index] }
clock.value = clock.value.invert()
@ -75,7 +74,7 @@ open class FlickerCodeStepsCalculator {
}
return stepBits
return step
}
}

View File

@ -0,0 +1,14 @@
package net.dankito.banking.ui.util
open class Step(
val bit1: Bit,
val bit2: Bit,
val bit3: Bit,
val bit4: Bit,
val bit5: Bit
) {
constructor(clockBit: Bit, step: Step) : this(clockBit, step.bit2, step.bit3, step.bit4, step.bit5)
}