From 7e7d00881f314f41be9dd0c055d99e822685281e Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 5 Sep 2018 20:31:41 +0800 Subject: [PATCH] add ModifyPsychomatrix UseCase and Interactor --- .../UseCases/ModifyPsychomatrixUseCase.kt | 31 +++++++++++ .../ModifyPsychomatrixUseCaseInteractor.kt | 54 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/UseCases/ModifyPsychomatrixUseCase.kt create mode 100644 src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/interactors/ModifyPsychomatrixUseCaseInteractor.kt 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 new file mode 100644 index 0000000..89e70f2 --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/UseCases/ModifyPsychomatrixUseCase.kt @@ -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> + +interface ModifyPsychomatrixUseCase { + + fun openPsychomatrixChangedSubscription(): ReceiveChannel + + suspend fun makeConvert(psychomatrix: Psychomatrix, operation: Operation): Deferred + suspend fun makeInvert(psychomatrix: Psychomatrix, operation: Operation): Deferred + + suspend fun getConverts(psychomatrix: Psychomatrix): Deferred> + suspend fun getInverts(psychomatrix: Psychomatrix): Deferred> + + suspend fun getOperations(psychomatrix: Psychomatrix): Deferred> = async { + listOf( + getConverts(psychomatrix), + getInverts(psychomatrix) + ).flatMap { + it.await() + } + } + + suspend 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 new file mode 100644 index 0000000..05487d8 --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/interactors/ModifyPsychomatrixUseCaseInteractor.kt @@ -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 = ArrayList() + + private val psychomatrixChangedBroadcastChannel = BroadcastChannel(SUBSCRIPTIONS_MEDIUM) + + override fun openPsychomatrixChangedSubscription(): ReceiveChannel { + return psychomatrixChangedBroadcastChannel.openSubscription() + } + + override suspend fun makeConvert(psychomatrix: Psychomatrix, operation: Operation): Deferred { + return async { + asMutablePsychomatrix(psychomatrix).applyConvert(operation) + } + } + + override suspend fun makeInvert(psychomatrix: Psychomatrix, operation: Operation): Deferred { + return async { + asMutablePsychomatrix(psychomatrix).applyInvert(operation) + } + } + + override suspend fun getConverts(psychomatrix: Psychomatrix): Deferred> { + return asMutablePsychomatrix(psychomatrix).availableConverts + } + + override suspend fun getInverts(psychomatrix: Psychomatrix): Deferred> { + return asMutablePsychomatrix(psychomatrix).availableInverts + } + + override suspend fun getPsychomatrixHistory(psychomatrix: Psychomatrix): Deferred> { + return asMutablePsychomatrix(psychomatrix).operationsHistory + } + + private fun asMutablePsychomatrix(psychomatrix: Psychomatrix): MutablePsychomatrix { + return currentPsychomatrixes.firstOrNull { + it == psychomatrix + } ?: MutablePsychomatrix(psychomatrix).also { + currentPsychomatrixes.add(it) + } + } +} \ No newline at end of file