add ModifyPsychomatrix UseCase and Interactor

This commit is contained in:
InsanusMokrassar 2018-09-05 20:31:41 +08:00
parent 527807b581
commit 7e7d00881f
2 changed files with 85 additions and 0 deletions

View File

@ -0,0 +1,31 @@
package com.github.insanusmokrassar.PsychomatrixBase.domain.UseCases
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>>
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 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

@ -0,0 +1,54 @@
package com.github.insanusmokrassar.PsychomatrixBase.domain.interactors
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.Operation
import com.github.insanusmokrassar.PsychomatrixBase.utils.extensions.SUBSCRIPTIONS_MEDIUM
import kotlinx.coroutines.experimental.Deferred
import kotlinx.coroutines.experimental.async
import kotlinx.coroutines.experimental.channels.BroadcastChannel
import kotlinx.coroutines.experimental.channels.ReceiveChannel
class ModifyPsychomatrixUseCaseInteractor : ModifyPsychomatrixUseCase {
private val currentPsychomatrixes: MutableList<MutablePsychomatrix> = ArrayList()
private val psychomatrixChangedBroadcastChannel = BroadcastChannel<PsychomatrixOperationIsConvert>(SUBSCRIPTIONS_MEDIUM)
override fun openPsychomatrixChangedSubscription(): ReceiveChannel<PsychomatrixOperationIsConvert> {
return psychomatrixChangedBroadcastChannel.openSubscription()
}
override suspend fun makeConvert(psychomatrix: Psychomatrix, operation: Operation): Deferred<Boolean> {
return async {
asMutablePsychomatrix(psychomatrix).applyConvert(operation)
}
}
override suspend fun makeInvert(psychomatrix: Psychomatrix, operation: Operation): Deferred<Boolean> {
return async {
asMutablePsychomatrix(psychomatrix).applyInvert(operation)
}
}
override suspend fun getConverts(psychomatrix: Psychomatrix): Deferred<List<Operation>> {
return asMutablePsychomatrix(psychomatrix).availableConverts
}
override suspend fun getInverts(psychomatrix: Psychomatrix): Deferred<List<Operation>> {
return asMutablePsychomatrix(psychomatrix).availableInverts
}
override suspend fun getPsychomatrixHistory(psychomatrix: Psychomatrix): Deferred<List<Operation>> {
return asMutablePsychomatrix(psychomatrix).operationsHistory
}
private fun asMutablePsychomatrix(psychomatrix: Psychomatrix): MutablePsychomatrix {
return currentPsychomatrixes.firstOrNull {
it == psychomatrix
} ?: MutablePsychomatrix(psychomatrix).also {
currentPsychomatrixes.add(it)
}
}
}