From d3b9f88985587dfd386b913a101b7058a134ed1c Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 4 Sep 2018 10:29:25 +0800 Subject: [PATCH] add UserCanCalculatePsychomatrixByDateInteractor --- ...anCalculatePsychomatrixByDateInteractor.kt | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/interactors/UserCanCalculatePsychomatrixByDateInteractor.kt diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/interactors/UserCanCalculatePsychomatrixByDateInteractor.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/interactors/UserCanCalculatePsychomatrixByDateInteractor.kt new file mode 100644 index 0000000..47a5bf3 --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/interactors/UserCanCalculatePsychomatrixByDateInteractor.kt @@ -0,0 +1,35 @@ +package com.github.insanusmokrassar.PsychomatrixBase.domain.interactors + +import com.github.insanusmokrassar.PsychomatrixBase.domain.UseCases.UserCanCalculatePsychomatrixByDate +import com.github.insanusmokrassar.PsychomatrixBase.domain.entities.Psychomatrix +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 +import org.joda.time.DateTime +import java.util.* + +class UserCanCalculatePsychomatrixByDateInteractor : UserCanCalculatePsychomatrixByDate { + private val psychomatrixCreatedBroadcast = BroadcastChannel(SUBSCRIPTIONS_MEDIUM) + + override suspend fun calculate(date: Long): Deferred { + return calculate(DateTime(date)) + } + + override suspend fun calculate(date: DateTime): Deferred { + return async { + Psychomatrix(date).also { + psychomatrixCreatedBroadcast.send(it) + } + } + } + + override suspend fun calculate(date: Date): Deferred { + return calculate(DateTime(date)) + } + + override suspend fun openPsychomatrixCreatedSubscription(): ReceiveChannel { + return psychomatrixCreatedBroadcast.openSubscription() + } +} \ No newline at end of file