totaly rewriten modify branch

This commit is contained in:
InsanusMokrassar 2018-09-07 21:54:19 +08:00
parent fdfd55cdb7
commit c2a4f76130
6 changed files with 16 additions and 35 deletions

View File

@ -1,13 +1,4 @@
package com.github.insanusmokrassar.PsychomatrixBase
import com.github.insanusmokrassar.PsychomatrixBase.domain.entities.MutablePsychomatrix
import com.github.insanusmokrassar.PsychomatrixBase.domain.entities.operations.TwoGrowFour
import kotlinx.coroutines.experimental.runBlocking
import org.joda.time.DateTime
fun main(args: Array<String>) {
val psychomatrix = MutablePsychomatrix(DateTime.now().withDate(2022, 12, 22))
runBlocking {
println(psychomatrix.availableOperations.await())
}
}

View File

@ -1,31 +1,23 @@
package com.github.insanusmokrassar.PsychomatrixBase.domain.UseCases
import com.github.insanusmokrassar.PsychomatrixBase.domain.entities.MutablePsychomatrix
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.async
import kotlinx.coroutines.experimental.channels.ReceiveChannel
typealias PsychomatrixOperationIsConvert = Pair<Psychomatrix, Pair<Operation, Boolean>>
typealias PsychomatrixOperationIsConvert = Pair<MutablePsychomatrix, Pair<Operation, Boolean>>
interface ModifyPsychomatrixUseCase {
fun openPsychomatrixChangedSubscription(): ReceiveChannel<PsychomatrixOperationIsConvert>
suspend fun makeConvert(psychomatrix: Psychomatrix, operation: Operation): Deferred<Boolean>
suspend fun makeInvert(psychomatrix: Psychomatrix, operation: Operation): Deferred<Boolean>
suspend fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean>
suspend fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean>
suspend fun getConverts(psychomatrix: Psychomatrix): Deferred<List<Operation>>
suspend fun getInverts(psychomatrix: Psychomatrix): Deferred<List<Operation>>
suspend fun getOperations(psychomatrix: Psychomatrix): Deferred<List<Operation>> = async {
listOf(
getConverts(psychomatrix),
getInverts(psychomatrix)
).flatMap {
it.await()
}
}
suspend fun getPsychomatrixHistory(psychomatrix: Psychomatrix): Deferred<List<Operation>>
}

View File

@ -186,8 +186,4 @@ open class Psychomatrix(val date: DateTime) {
getDownDiagSum()
)
}
override fun equals(other: Any?): Boolean {
return super.equals(other) || (other as? Psychomatrix) ?.date == date
}
}

View File

@ -20,13 +20,13 @@ class ModifyPsychomatrixUseCaseInteractor : ModifyPsychomatrixUseCase {
return psychomatrixChangedBroadcastChannel.openSubscription()
}
override suspend fun makeConvert(psychomatrix: Psychomatrix, operation: Operation): Deferred<Boolean> {
override suspend fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean> {
return async {
asMutablePsychomatrix(psychomatrix).applyConvert(operation)
}
}
override suspend fun makeInvert(psychomatrix: Psychomatrix, operation: Operation): Deferred<Boolean> {
override suspend fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean> {
return async {
asMutablePsychomatrix(psychomatrix).applyInvert(operation)
}

View File

@ -2,6 +2,7 @@ package com.github.insanusmokrassar.PsychomatrixBase.presentation.presenters.Def
import com.github.insanusmokrassar.PsychomatrixBase.domain.UseCases.ModifyPsychomatrixUseCase
import com.github.insanusmokrassar.PsychomatrixBase.domain.UseCases.PsychomatrixOperationIsConvert
import com.github.insanusmokrassar.PsychomatrixBase.domain.entities.MutablePsychomatrix
import com.github.insanusmokrassar.PsychomatrixBase.domain.entities.Psychomatrix
import com.github.insanusmokrassar.PsychomatrixBase.domain.entities.operations.*
import com.github.insanusmokrassar.PsychomatrixBase.presentation.presenters.ModifyPsychomatrixPresenter
@ -17,7 +18,7 @@ class ModifyPsychomatrixPresenterImpl(
) : ModifyPsychomatrixPresenter {
private val availableConverts = HashMap<Psychomatrix, List<Operation>>()
private val availableInverts = HashMap<Psychomatrix, List<Operation>>()
private val availableInverts = HashMap<MutablePsychomatrix, List<Operation>>()
init {
openPsychomatrixChangedSubscription().subscribe {
@ -33,7 +34,7 @@ class ModifyPsychomatrixPresenterImpl(
return modifyPsychomatrixUseCase.openPsychomatrixChangedSubscription()
}
override suspend fun tryToDoOperation(psychomatrix: Psychomatrix, operation: Operation): Deferred<Boolean> {
override suspend fun tryToDoOperation(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean> {
return modifyPsychomatrixUseCase.makeConvert(psychomatrix, operation)
}
@ -93,7 +94,7 @@ class ModifyPsychomatrixPresenterImpl(
}
}
override suspend fun rollback(psychomatrix: Psychomatrix, operations: Int): Job {
override suspend fun rollback(psychomatrix: MutablePsychomatrix, operations: Int): Job {
return launch {
history(psychomatrix).await().let {
it.subList(it.size - operations, it.size).asReversed()
@ -103,7 +104,7 @@ class ModifyPsychomatrixPresenterImpl(
}
}
override suspend fun history(psychomatrix: Psychomatrix): Deferred<List<Operation>> {
override suspend fun history(psychomatrix: MutablePsychomatrix): Deferred<List<Operation>> {
return async {
availableInverts[psychomatrix] ?: updateInvertsOfPsychomatrix(psychomatrix)
}
@ -115,7 +116,7 @@ class ModifyPsychomatrixPresenterImpl(
}
}
private suspend fun updateInvertsOfPsychomatrix(psychomatrix: Psychomatrix): List<Operation> {
private suspend fun updateInvertsOfPsychomatrix(psychomatrix: MutablePsychomatrix): List<Operation> {
return modifyPsychomatrixUseCase.getInverts(psychomatrix).await().also {
availableInverts[psychomatrix] = it
}

View File

@ -1,6 +1,7 @@
package com.github.insanusmokrassar.PsychomatrixBase.presentation.presenters
import com.github.insanusmokrassar.PsychomatrixBase.domain.UseCases.PsychomatrixOperationIsConvert
import com.github.insanusmokrassar.PsychomatrixBase.domain.entities.MutablePsychomatrix
import com.github.insanusmokrassar.PsychomatrixBase.domain.entities.Psychomatrix
import com.github.insanusmokrassar.PsychomatrixBase.domain.entities.operations.Operation
import kotlinx.coroutines.experimental.Deferred
@ -11,7 +12,7 @@ interface ModifyPsychomatrixPresenter {
fun openPsychomatrixChangedSubscription(): ReceiveChannel<PsychomatrixOperationIsConvert>
suspend fun tryToDoOperation(psychomatrix: Psychomatrix, operation: Operation): Deferred<Boolean>
suspend fun tryToDoOperation(psychomatrix: MutablePsychomatrix, operation: Operation): Deferred<Boolean>
suspend fun twoGrowFourAvailable(psychomatrix: Psychomatrix): Deferred<Boolean>
suspend fun fourGrowTwoAvailable(psychomatrix: Psychomatrix): Deferred<Boolean>
@ -27,7 +28,7 @@ interface ModifyPsychomatrixPresenter {
suspend fun customGrowAvailable(psychomatrix: Psychomatrix, number: Byte): Deferred<Boolean>
suspend fun rollback(psychomatrix: Psychomatrix, operations: Int): Job
suspend fun rollback(psychomatrix: MutablePsychomatrix, operations: Int): Job
suspend fun history(psychomatrix: Psychomatrix): Deferred<List<Operation>>
suspend fun history(psychomatrix: MutablePsychomatrix): Deferred<List<Operation>>
}