change of modification

This commit is contained in:
InsanusMokrassar 2018-09-07 22:11:54 +08:00
parent c2a4f76130
commit 4a84f602a9
4 changed files with 26 additions and 119 deletions

View File

@ -13,11 +13,11 @@ interface ModifyPsychomatrixUseCase {
fun openPsychomatrixChangedSubscription(): ReceiveChannel<PsychomatrixOperationIsConvert> fun openPsychomatrixChangedSubscription(): ReceiveChannel<PsychomatrixOperationIsConvert>
suspend fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean> fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean>
suspend fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean> fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean>
suspend fun getConverts(psychomatrix: Psychomatrix): Deferred<List<Operation>> fun getConverts(psychomatrix: Psychomatrix): Deferred<List<Operation>>
suspend fun getInverts(psychomatrix: Psychomatrix): Deferred<List<Operation>> fun getInverts(psychomatrix: Psychomatrix): Deferred<List<Operation>>
suspend fun getPsychomatrixHistory(psychomatrix: Psychomatrix): Deferred<List<Operation>> fun getPsychomatrixHistory(psychomatrix: Psychomatrix): Deferred<List<Operation>>
} }

View File

@ -20,34 +20,34 @@ class ModifyPsychomatrixUseCaseInteractor : ModifyPsychomatrixUseCase {
return psychomatrixChangedBroadcastChannel.openSubscription() return psychomatrixChangedBroadcastChannel.openSubscription()
} }
override suspend fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean> { override fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean> {
return async { return async {
asMutablePsychomatrix(psychomatrix).applyConvert(operation) asMutablePsychomatrix(psychomatrix).applyConvert(operation)
} }
} }
override suspend fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean> { override fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean> {
return async { return async {
asMutablePsychomatrix(psychomatrix).applyInvert(operation) asMutablePsychomatrix(psychomatrix).applyInvert(operation)
} }
} }
override suspend fun getConverts(psychomatrix: Psychomatrix): Deferred<List<Operation>> { override fun getConverts(psychomatrix: Psychomatrix): Deferred<List<Operation>> {
return asMutablePsychomatrix(psychomatrix).availableConverts return asMutablePsychomatrix(psychomatrix).availableConverts
} }
override suspend fun getInverts(psychomatrix: Psychomatrix): Deferred<List<Operation>> { override fun getInverts(psychomatrix: Psychomatrix): Deferred<List<Operation>> {
return asMutablePsychomatrix(psychomatrix).availableInverts return asMutablePsychomatrix(psychomatrix).availableInverts
} }
override suspend fun getPsychomatrixHistory(psychomatrix: Psychomatrix): Deferred<List<Operation>> { override fun getPsychomatrixHistory(psychomatrix: Psychomatrix): Deferred<List<Operation>> {
return asMutablePsychomatrix(psychomatrix).operationsHistory return asMutablePsychomatrix(psychomatrix).operationsHistory
} }
private fun asMutablePsychomatrix(psychomatrix: Psychomatrix): MutablePsychomatrix { private fun asMutablePsychomatrix(psychomatrix: Psychomatrix): MutablePsychomatrix {
return currentPsychomatrixes.firstOrNull { return currentPsychomatrixes.firstOrNull {
it.date == psychomatrix.date it == psychomatrix
} ?: MutablePsychomatrix(psychomatrix).also { } ?: (psychomatrix as? MutablePsychomatrix) ?: MutablePsychomatrix(psychomatrix).also {
currentPsychomatrixes.add(it) currentPsychomatrixes.add(it)
} }
} }

View File

@ -17,108 +17,27 @@ class ModifyPsychomatrixPresenterImpl(
private val modifyPsychomatrixUseCase: ModifyPsychomatrixUseCase private val modifyPsychomatrixUseCase: ModifyPsychomatrixUseCase
) : ModifyPsychomatrixPresenter { ) : ModifyPsychomatrixPresenter {
private val availableConverts = HashMap<Psychomatrix, List<Operation>>()
private val availableInverts = HashMap<MutablePsychomatrix, List<Operation>>()
init {
openPsychomatrixChangedSubscription().subscribe {
if (it.second.second) {
updateConvertsOfPsychomatrix(it.first)
} else {
updateInvertsOfPsychomatrix(it.first)
}
}
}
override fun openPsychomatrixChangedSubscription(): ReceiveChannel<PsychomatrixOperationIsConvert> { override fun openPsychomatrixChangedSubscription(): ReceiveChannel<PsychomatrixOperationIsConvert> {
return modifyPsychomatrixUseCase.openPsychomatrixChangedSubscription() return modifyPsychomatrixUseCase.openPsychomatrixChangedSubscription()
} }
override suspend fun tryToDoOperation(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean> { override fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean> {
return modifyPsychomatrixUseCase.makeConvert(psychomatrix, operation) return modifyPsychomatrixUseCase.makeConvert(psychomatrix, operation)
} }
override suspend fun twoGrowFourAvailable(psychomatrix: Psychomatrix): Deferred<Boolean> { override fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean> {
return async { return modifyPsychomatrixUseCase.makeInvert(psychomatrix, operation)
(availableConverts[psychomatrix] ?: updateConvertsOfPsychomatrix(psychomatrix)).contains(TwoGrowFour)
}
} }
override suspend fun fourGrowTwoAvailable(psychomatrix: Psychomatrix): Deferred<Boolean> { override fun getConverts(psychomatrix: Psychomatrix): Deferred<List<Operation>> {
return async { return modifyPsychomatrixUseCase.getConverts(psychomatrix)
(availableConverts[psychomatrix] ?: updateConvertsOfPsychomatrix(psychomatrix)).contains(FourGrowTwo)
}
} }
override suspend fun oneGrowEightAvailable(psychomatrix: Psychomatrix): Deferred<Boolean> { override fun getInverts(psychomatrix: Psychomatrix): Deferred<List<Operation>> {
return async { return modifyPsychomatrixUseCase.getInverts(psychomatrix)
(availableConverts[psychomatrix] ?: updateConvertsOfPsychomatrix(psychomatrix)).contains(OneGrowEight)
}
} }
override suspend fun eightGrowOneAvailable(psychomatrix: Psychomatrix): Deferred<Boolean> { override fun getPsychomatrixHistory(psychomatrix: Psychomatrix): Deferred<List<Operation>> {
return async { return getInverts(psychomatrix)
(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: 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<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: MutablePsychomatrix): List<Operation> {
return modifyPsychomatrixUseCase.getInverts(psychomatrix).await().also {
availableInverts[psychomatrix] = it
}
} }
} }

View File

@ -12,23 +12,11 @@ interface ModifyPsychomatrixPresenter {
fun openPsychomatrixChangedSubscription(): ReceiveChannel<PsychomatrixOperationIsConvert> fun openPsychomatrixChangedSubscription(): ReceiveChannel<PsychomatrixOperationIsConvert>
suspend fun tryToDoOperation(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean> fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean>
fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean>
suspend fun twoGrowFourAvailable(psychomatrix: Psychomatrix): Deferred<Boolean> fun getConverts(psychomatrix: Psychomatrix): Deferred<List<Operation>>
suspend fun fourGrowTwoAvailable(psychomatrix: Psychomatrix): Deferred<Boolean> fun getInverts(psychomatrix: Psychomatrix): Deferred<List<Operation>>
suspend fun oneGrowEightAvailable(psychomatrix: Psychomatrix): Deferred<Boolean> fun getPsychomatrixHistory(psychomatrix: Psychomatrix): Deferred<List<Operation>>
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: MutablePsychomatrix, operations: Int): Job
suspend fun history(psychomatrix: MutablePsychomatrix): Deferred<List<Operation>>
} }