mirror of
https://github.com/InsanusMokrassar/PsychomatrixBase.git
synced 2024-11-14 20:33:57 +00:00
add ModifyPsychomatrix UseCase and Interactor
This commit is contained in:
parent
527807b581
commit
7e7d00881f
@ -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>>
|
||||
}
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user