diff --git a/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/ObjectHolder.kt b/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/ObjectHolder.kt new file mode 100644 index 00000000..093646da --- /dev/null +++ b/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/ObjectHolder.kt @@ -0,0 +1,12 @@ +package net.dankito.utils.multiplatform + + +open class ObjectHolder( + var value: T +) { + + override fun toString(): String { + return value?.toString() ?: "Value is null" + } + +} \ No newline at end of file diff --git a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/tan/FlickerCodeStepsCalculator.kt b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/tan/FlickerCodeStepsCalculator.kt index 0aad576f..e174edcf 100644 --- a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/tan/FlickerCodeStepsCalculator.kt +++ b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/tan/FlickerCodeStepsCalculator.kt @@ -1,42 +1,46 @@ package net.dankito.banking.fints.tan - -open class FlickerCodeStepsCalculator(var code: String) { - - var halfbyteid = 0 - var clock = Bit.High - var bitarray = mutableListOf>() - - val steps: List> +import net.dankito.utils.multiplatform.ObjectHolder - fun reset() { - halfbyteid = 0 - clock = Bit.High +open class FlickerCodeStepsCalculator { + + companion object { + + val bits = mutableMapOf>() + + 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) + } + } - init { - val bits = mutableMapOf>() - /* 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) + + open fun calculateSteps(flickerCode: String): List> { + + val halfbyteid = ObjectHolder(0) + val clock = ObjectHolder(Bit.High) + val bitarray = mutableListOf>() + /* prepend synchronization identifier */ - code = "0FFF" + code + var code = "0FFF" + flickerCode if (code.length % 2 != 0) { code += "F" } @@ -49,24 +53,24 @@ open class FlickerCodeStepsCalculator(var code: String) { val steps = mutableListOf>() do { - steps.add(step()) - } while (halfbyteid > 0 || clock == Bit.Low) + steps.add(calculateStep(halfbyteid, clock, bitarray)) + } while (halfbyteid.value > 0 || clock.value == Bit.Low) - this.steps = steps + return steps } - fun step(): Array { - bitarray[halfbyteid][0] = clock + protected open fun calculateStep(halfbyteid: ObjectHolder, clock: ObjectHolder, bitarray: MutableList>): Array { + bitarray[halfbyteid.value][0] = clock.value - val stepBits = Array(5) { index -> bitarray[halfbyteid][index] } + val stepBits = Array(5) { index -> bitarray[halfbyteid.value][index] } - clock = clock.invert() + clock.value = clock.value.invert() - if (clock == Bit.High) { - halfbyteid++ + if (clock.value == Bit.High) { + halfbyteid.value++ - if (halfbyteid >= bitarray.size) { - halfbyteid = 0 + if (halfbyteid.value >= bitarray.size) { + halfbyteid.value = 0 } } 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 987d7c81..9a3fceb5 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 @@ -39,7 +39,7 @@ open class FlickerCodeAnimator { currentFrequency = frequency animationJob = GlobalScope.launch(Dispatchers.Default) { - val steps = FlickerCodeStepsCalculator(flickerCode.parsedDataSet).steps + val steps = FlickerCodeStepsCalculator().calculateSteps(flickerCode.parsedDataSet) calculateAnimation(steps, showStep) }