diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/UseCases/ModifyPsychomatrixUseCase.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/UseCases/ModifyPsychomatrixUseCase.kt index 72f0d08..8092659 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/UseCases/ModifyPsychomatrixUseCase.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/UseCases/ModifyPsychomatrixUseCase.kt @@ -13,11 +13,11 @@ interface ModifyPsychomatrixUseCase { fun openPsychomatrixChangedSubscription(): ReceiveChannel - suspend fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred - suspend fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred + fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred + fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred - suspend fun getConverts(psychomatrix: Psychomatrix): Deferred> - suspend fun getInverts(psychomatrix: Psychomatrix): Deferred> + fun getConverts(psychomatrix: Psychomatrix): Deferred> + fun getInverts(psychomatrix: Psychomatrix): Deferred> - suspend fun getPsychomatrixHistory(psychomatrix: Psychomatrix): Deferred> + fun getPsychomatrixHistory(psychomatrix: Psychomatrix): Deferred> } \ No newline at end of file diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/interactors/ModifyPsychomatrixUseCaseInteractor.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/interactors/ModifyPsychomatrixUseCaseInteractor.kt index 2432126..eeb04a9 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/interactors/ModifyPsychomatrixUseCaseInteractor.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/interactors/ModifyPsychomatrixUseCaseInteractor.kt @@ -20,34 +20,34 @@ class ModifyPsychomatrixUseCaseInteractor : ModifyPsychomatrixUseCase { return psychomatrixChangedBroadcastChannel.openSubscription() } - override suspend fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred { + override fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred { return async { asMutablePsychomatrix(psychomatrix).applyConvert(operation) } } - override suspend fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred { + override fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred { return async { asMutablePsychomatrix(psychomatrix).applyInvert(operation) } } - override suspend fun getConverts(psychomatrix: Psychomatrix): Deferred> { + override fun getConverts(psychomatrix: Psychomatrix): Deferred> { return asMutablePsychomatrix(psychomatrix).availableConverts } - override suspend fun getInverts(psychomatrix: Psychomatrix): Deferred> { + override fun getInverts(psychomatrix: Psychomatrix): Deferred> { return asMutablePsychomatrix(psychomatrix).availableInverts } - override suspend fun getPsychomatrixHistory(psychomatrix: Psychomatrix): Deferred> { + override fun getPsychomatrixHistory(psychomatrix: Psychomatrix): Deferred> { return asMutablePsychomatrix(psychomatrix).operationsHistory } private fun asMutablePsychomatrix(psychomatrix: Psychomatrix): MutablePsychomatrix { return currentPsychomatrixes.firstOrNull { - it.date == psychomatrix.date - } ?: MutablePsychomatrix(psychomatrix).also { + it == psychomatrix + } ?: (psychomatrix as? MutablePsychomatrix) ?: MutablePsychomatrix(psychomatrix).also { currentPsychomatrixes.add(it) } } 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 index 596445f..45814b7 100644 --- 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 @@ -17,108 +17,27 @@ 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 tryToDoOperation(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred { + override fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred { return modifyPsychomatrixUseCase.makeConvert(psychomatrix, operation) } - override suspend fun twoGrowFourAvailable(psychomatrix: Psychomatrix): Deferred { - return async { - (availableConverts[psychomatrix] ?: updateConvertsOfPsychomatrix(psychomatrix)).contains(TwoGrowFour) - } + override fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred { + return modifyPsychomatrixUseCase.makeInvert(psychomatrix, operation) } - override suspend fun fourGrowTwoAvailable(psychomatrix: Psychomatrix): Deferred { - return async { - (availableConverts[psychomatrix] ?: updateConvertsOfPsychomatrix(psychomatrix)).contains(FourGrowTwo) - } + override fun getConverts(psychomatrix: Psychomatrix): Deferred> { + return modifyPsychomatrixUseCase.getConverts(psychomatrix) } - override suspend fun oneGrowEightAvailable(psychomatrix: Psychomatrix): Deferred { - return async { - (availableConverts[psychomatrix] ?: updateConvertsOfPsychomatrix(psychomatrix)).contains(OneGrowEight) - } + override fun getInverts(psychomatrix: Psychomatrix): Deferred> { + return modifyPsychomatrixUseCase.getInverts(psychomatrix) } - 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: MutablePsychomatrix, 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: MutablePsychomatrix): 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: MutablePsychomatrix): List { - return modifyPsychomatrixUseCase.getInverts(psychomatrix).await().also { - availableInverts[psychomatrix] = it - } + override fun getPsychomatrixHistory(psychomatrix: Psychomatrix): Deferred> { + return getInverts(psychomatrix) } } \ 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 index 658c4bc..f328cb2 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/presentation/presenters/ModifyPsychomatrixPresenter.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/presentation/presenters/ModifyPsychomatrixPresenter.kt @@ -12,23 +12,11 @@ interface ModifyPsychomatrixPresenter { fun openPsychomatrixChangedSubscription(): ReceiveChannel - suspend fun tryToDoOperation(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred + fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred + fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred - suspend fun twoGrowFourAvailable(psychomatrix: Psychomatrix): Deferred - suspend fun fourGrowTwoAvailable(psychomatrix: Psychomatrix): Deferred + fun getConverts(psychomatrix: Psychomatrix): Deferred> + fun getInverts(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: MutablePsychomatrix, operations: Int): Job - - suspend fun history(psychomatrix: MutablePsychomatrix): Deferred> + fun getPsychomatrixHistory(psychomatrix: Psychomatrix): Deferred> } \ No newline at end of file