mirror of
https://github.com/InsanusMokrassar/PsychomatrixBase.git
synced 2024-06-01 23:45:15 +00:00
50 lines
2.4 KiB
Kotlin
50 lines
2.4 KiB
Kotlin
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 fun makeConvert(psychomatrix: MutablePsychomatrix, operation: Operation): Boolean {
|
|
return asMutablePsychomatrix(psychomatrix).applyConvert(operation)
|
|
}
|
|
|
|
override fun makeInvert(psychomatrix: MutablePsychomatrix, operation: Operation): Boolean {
|
|
return asMutablePsychomatrix(psychomatrix).applyInvert(operation)
|
|
}
|
|
|
|
override fun getConverts(psychomatrix: Psychomatrix): List<Operation> {
|
|
return asMutablePsychomatrix(psychomatrix).availableConverts
|
|
}
|
|
|
|
override fun getInverts(psychomatrix: Psychomatrix): List<Operation> {
|
|
return asMutablePsychomatrix(psychomatrix).availableInverts
|
|
}
|
|
|
|
override fun getPsychomatrixHistory(psychomatrix: Psychomatrix): List<Operation> {
|
|
return asMutablePsychomatrix(psychomatrix).operationsHistory
|
|
}
|
|
|
|
private fun asMutablePsychomatrix(psychomatrix: Psychomatrix): MutablePsychomatrix {
|
|
return currentPsychomatrixes.firstOrNull {
|
|
it == psychomatrix
|
|
} ?: (psychomatrix as? MutablePsychomatrix) ?: MutablePsychomatrix(psychomatrix).also {
|
|
currentPsychomatrixes.add(it)
|
|
}
|
|
}
|
|
} |