add ModifyPsychomatrixPresenter and it's implementation, fix GrowCustom fields accesses

This commit is contained in:
InsanusMokrassar 2018-09-06 11:04:07 +08:00
parent 5b0567beaf
commit a7cfaba837
3 changed files with 151 additions and 1 deletions

View File

@ -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<Byte>, changesHistory: List<Operation>): Boolean { override suspend fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return changesHistory.canGrowSimpleWay return changesHistory.canGrowSimpleWay
&& changesHistory.firstOrNull { it is GrowCustom && it.number == number } == null && changesHistory.firstOrNull { it is GrowCustom && it.number == number } == null

View File

@ -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<Psychomatrix, List<Operation>>()
private val availableInverts = HashMap<Psychomatrix, List<Operation>>()
init {
openPsychomatrixChangedSubscription().subscribe {
if (it.second.second) {
updateConvertsOfPsychomatrix(it.first)
} else {
updateInvertsOfPsychomatrix(it.first)
}
}
}
override fun openPsychomatrixChangedSubscription(): ReceiveChannel<PsychomatrixOperationIsConvert> {
return modifyPsychomatrixUseCase.openPsychomatrixChangedSubscription()
}
override suspend fun twoGrowFourAvailable(psychomatrix: Psychomatrix): Deferred<Boolean> {
return async {
(availableConverts[psychomatrix] ?: updateConvertsOfPsychomatrix(psychomatrix)).contains(TwoGrowFour)
}
}
override suspend fun fourGrowTwoAvailable(psychomatrix: Psychomatrix): Deferred<Boolean> {
return async {
(availableConverts[psychomatrix] ?: updateConvertsOfPsychomatrix(psychomatrix)).contains(FourGrowTwo)
}
}
override suspend fun oneGrowEightAvailable(psychomatrix: Psychomatrix): Deferred<Boolean> {
return async {
(availableConverts[psychomatrix] ?: updateConvertsOfPsychomatrix(psychomatrix)).contains(OneGrowEight)
}
}
override suspend fun eightGrowOneAvailable(psychomatrix: Psychomatrix): Deferred<Boolean> {
return async {
(availableConverts[psychomatrix] ?: updateConvertsOfPsychomatrix(psychomatrix)).contains(EightGrowOne)
}
}
override suspend fun sixGrowSevenAvailable(psychomatrix: Psychomatrix): Deferred<Boolean> {
return async {
(availableConverts[psychomatrix] ?: updateConvertsOfPsychomatrix(psychomatrix)).contains(SixGrowSeven)
}
}
override suspend fun sevenGrowSixAvailable(psychomatrix: Psychomatrix): Deferred<Boolean> {
return async {
(availableConverts[psychomatrix] ?: updateConvertsOfPsychomatrix(psychomatrix)).contains(SevenGrowSix)
}
}
override suspend fun fiveGrowNineAvailable(psychomatrix: Psychomatrix): Deferred<Boolean> {
return async {
(availableConverts[psychomatrix] ?: updateConvertsOfPsychomatrix(psychomatrix)).contains(FiveGrowNine)
}
}
override suspend fun nineGrowFiveAvailable(psychomatrix: Psychomatrix): Deferred<Boolean> {
return async {
(availableConverts[psychomatrix] ?: updateConvertsOfPsychomatrix(psychomatrix)).contains(NineGrowFive)
}
}
override suspend fun customGrowAvailable(psychomatrix: Psychomatrix, number: Byte): Deferred<Boolean> {
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<List<Operation>> {
return async {
availableInverts[psychomatrix] ?: updateInvertsOfPsychomatrix(psychomatrix)
}
}
private suspend fun updateConvertsOfPsychomatrix(psychomatrix: Psychomatrix): List<Operation> {
return modifyPsychomatrixUseCase.getConverts(psychomatrix).await().also {
availableConverts[psychomatrix] = it
}
}
private suspend fun updateInvertsOfPsychomatrix(psychomatrix: Psychomatrix): List<Operation> {
return modifyPsychomatrixUseCase.getInverts(psychomatrix).await().also {
availableInverts[psychomatrix] = it
}
}
}

View File

@ -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<PsychomatrixOperationIsConvert>
suspend fun twoGrowFourAvailable(psychomatrix: Psychomatrix): Deferred<Boolean>
suspend fun fourGrowTwoAvailable(psychomatrix: Psychomatrix): Deferred<Boolean>
suspend fun oneGrowEightAvailable(psychomatrix: Psychomatrix): Deferred<Boolean>
suspend fun eightGrowOneAvailable(psychomatrix: Psychomatrix): Deferred<Boolean>
suspend fun sixGrowSevenAvailable(psychomatrix: Psychomatrix): Deferred<Boolean>
suspend fun sevenGrowSixAvailable(psychomatrix: Psychomatrix): Deferred<Boolean>
suspend fun fiveGrowNineAvailable(psychomatrix: Psychomatrix): Deferred<Boolean>
suspend fun nineGrowFiveAvailable(psychomatrix: Psychomatrix): Deferred<Boolean>
suspend fun customGrowAvailable(psychomatrix: Psychomatrix, number: Byte): Deferred<Boolean>
suspend fun rollback(psychomatrix: Psychomatrix, operations: Int): Job
suspend fun history(psychomatrix: Psychomatrix): Deferred<List<Operation>>
}