From a7cfaba83724121bc3082d1c598e199ab6eb3acd Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 6 Sep 2018 11:04:07 +0800 Subject: [PATCH] add ModifyPsychomatrixPresenter and it's implementation, fix GrowCustom fields accesses --- .../domain/entities/operations/Operation.kt | 2 +- .../ModifyPsychomatrixPresenterImpl.kt | 119 ++++++++++++++++++ .../presenters/ModifyPsychomatrixPresenter.kt | 31 +++++ 3 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/presentation/presenters/DefaultRealisations/ModifyPsychomatrixPresenterImpl.kt create mode 100644 src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/presentation/presenters/ModifyPsychomatrixPresenter.kt diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/operations/Operation.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/operations/Operation.kt index 2742e5a..5a90927 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/operations/Operation.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/operations/Operation.kt @@ -239,7 +239,7 @@ object NineGrowFive : Operation() { } } -private class GrowCustom(private val number: Byte) : Operation() { +class GrowCustom internal constructor(val number: Byte) : Operation() { override suspend fun canConvert(numbers: List, changesHistory: List): Boolean { return changesHistory.canGrowSimpleWay && changesHistory.firstOrNull { it is GrowCustom && it.number == number } == null diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/presentation/presenters/DefaultRealisations/ModifyPsychomatrixPresenterImpl.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/presentation/presenters/DefaultRealisations/ModifyPsychomatrixPresenterImpl.kt new file mode 100644 index 0000000..1e75a81 --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/presentation/presenters/DefaultRealisations/ModifyPsychomatrixPresenterImpl.kt @@ -0,0 +1,119 @@ +package com.github.insanusmokrassar.PsychomatrixBase.presentation.presenters.DefaultRealisations + +import com.github.insanusmokrassar.PsychomatrixBase.domain.UseCases.ModifyPsychomatrixUseCase +import com.github.insanusmokrassar.PsychomatrixBase.domain.UseCases.PsychomatrixOperationIsConvert +import com.github.insanusmokrassar.PsychomatrixBase.domain.entities.Psychomatrix +import com.github.insanusmokrassar.PsychomatrixBase.domain.entities.operations.* +import com.github.insanusmokrassar.PsychomatrixBase.presentation.presenters.ModifyPsychomatrixPresenter +import com.github.insanusmokrassar.PsychomatrixBase.utils.extensions.subscribe +import kotlinx.coroutines.experimental.Deferred +import kotlinx.coroutines.experimental.Job +import kotlinx.coroutines.experimental.async +import kotlinx.coroutines.experimental.channels.ReceiveChannel +import kotlinx.coroutines.experimental.launch + +class ModifyPsychomatrixPresenterImpl( + private val modifyPsychomatrixUseCase: ModifyPsychomatrixUseCase +) : ModifyPsychomatrixPresenter { + + private val availableConverts = HashMap>() + private val availableInverts = HashMap>() + + init { + openPsychomatrixChangedSubscription().subscribe { + if (it.second.second) { + updateConvertsOfPsychomatrix(it.first) + } else { + updateInvertsOfPsychomatrix(it.first) + } + } + } + + override fun openPsychomatrixChangedSubscription(): ReceiveChannel { + return modifyPsychomatrixUseCase.openPsychomatrixChangedSubscription() + } + + override suspend fun twoGrowFourAvailable(psychomatrix: Psychomatrix): Deferred { + return async { + (availableConverts[psychomatrix] ?: updateConvertsOfPsychomatrix(psychomatrix)).contains(TwoGrowFour) + } + } + + override suspend fun fourGrowTwoAvailable(psychomatrix: Psychomatrix): Deferred { + return async { + (availableConverts[psychomatrix] ?: updateConvertsOfPsychomatrix(psychomatrix)).contains(FourGrowTwo) + } + } + + override suspend fun oneGrowEightAvailable(psychomatrix: Psychomatrix): Deferred { + return async { + (availableConverts[psychomatrix] ?: updateConvertsOfPsychomatrix(psychomatrix)).contains(OneGrowEight) + } + } + + override suspend fun eightGrowOneAvailable(psychomatrix: Psychomatrix): Deferred { + return async { + (availableConverts[psychomatrix] ?: updateConvertsOfPsychomatrix(psychomatrix)).contains(EightGrowOne) + } + } + + override suspend fun sixGrowSevenAvailable(psychomatrix: Psychomatrix): Deferred { + return async { + (availableConverts[psychomatrix] ?: updateConvertsOfPsychomatrix(psychomatrix)).contains(SixGrowSeven) + } + } + + override suspend fun sevenGrowSixAvailable(psychomatrix: Psychomatrix): Deferred { + return async { + (availableConverts[psychomatrix] ?: updateConvertsOfPsychomatrix(psychomatrix)).contains(SevenGrowSix) + } + } + + override suspend fun fiveGrowNineAvailable(psychomatrix: Psychomatrix): Deferred { + return async { + (availableConverts[psychomatrix] ?: updateConvertsOfPsychomatrix(psychomatrix)).contains(FiveGrowNine) + } + } + + override suspend fun nineGrowFiveAvailable(psychomatrix: Psychomatrix): Deferred { + return async { + (availableConverts[psychomatrix] ?: updateConvertsOfPsychomatrix(psychomatrix)).contains(NineGrowFive) + } + } + + override suspend fun customGrowAvailable(psychomatrix: Psychomatrix, number: Byte): Deferred { + return async { + (availableConverts[psychomatrix] ?: updateConvertsOfPsychomatrix(psychomatrix)).firstOrNull { + it is GrowCustom && it.number == number + } != null + } + } + + override suspend fun rollback(psychomatrix: Psychomatrix, operations: Int): Job { + return launch { + history(psychomatrix).await().let { + it.subList(it.size - operations, it.size).asReversed() + }.forEach { + modifyPsychomatrixUseCase.makeInvert(psychomatrix, it) + } + } + } + + override suspend fun history(psychomatrix: Psychomatrix): Deferred> { + return async { + availableInverts[psychomatrix] ?: updateInvertsOfPsychomatrix(psychomatrix) + } + } + + private suspend fun updateConvertsOfPsychomatrix(psychomatrix: Psychomatrix): List { + return modifyPsychomatrixUseCase.getConverts(psychomatrix).await().also { + availableConverts[psychomatrix] = it + } + } + + private suspend fun updateInvertsOfPsychomatrix(psychomatrix: Psychomatrix): List { + return modifyPsychomatrixUseCase.getInverts(psychomatrix).await().also { + availableInverts[psychomatrix] = it + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/presentation/presenters/ModifyPsychomatrixPresenter.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/presentation/presenters/ModifyPsychomatrixPresenter.kt new file mode 100644 index 0000000..34b37ca --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/presentation/presenters/ModifyPsychomatrixPresenter.kt @@ -0,0 +1,31 @@ +package com.github.insanusmokrassar.PsychomatrixBase.presentation.presenters + +import com.github.insanusmokrassar.PsychomatrixBase.domain.UseCases.PsychomatrixOperationIsConvert +import com.github.insanusmokrassar.PsychomatrixBase.domain.entities.Psychomatrix +import com.github.insanusmokrassar.PsychomatrixBase.domain.entities.operations.Operation +import kotlinx.coroutines.experimental.Deferred +import kotlinx.coroutines.experimental.Job +import kotlinx.coroutines.experimental.channels.ReceiveChannel + +interface ModifyPsychomatrixPresenter { + + fun openPsychomatrixChangedSubscription(): ReceiveChannel + + suspend fun twoGrowFourAvailable(psychomatrix: Psychomatrix): Deferred + suspend fun fourGrowTwoAvailable(psychomatrix: Psychomatrix): Deferred + + suspend fun oneGrowEightAvailable(psychomatrix: Psychomatrix): Deferred + suspend fun eightGrowOneAvailable(psychomatrix: Psychomatrix): Deferred + + suspend fun sixGrowSevenAvailable(psychomatrix: Psychomatrix): Deferred + suspend fun sevenGrowSixAvailable(psychomatrix: Psychomatrix): Deferred + + suspend fun fiveGrowNineAvailable(psychomatrix: Psychomatrix): Deferred + suspend fun nineGrowFiveAvailable(psychomatrix: Psychomatrix): Deferred + + suspend fun customGrowAvailable(psychomatrix: Psychomatrix, number: Byte): Deferred + + suspend fun rollback(psychomatrix: Psychomatrix, operations: Int): Job + + suspend fun history(psychomatrix: Psychomatrix): Deferred> +} \ No newline at end of file