add UserCanCalculatePsychomatrixByDateInteractor

This commit is contained in:
InsanusMokrassar 2018-09-04 10:29:25 +08:00
parent fac3d8f856
commit d3b9f88985

View File

@ -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<Psychomatrix>(SUBSCRIPTIONS_MEDIUM)
override suspend fun calculate(date: Long): Deferred<Psychomatrix> {
return calculate(DateTime(date))
}
override suspend fun calculate(date: DateTime): Deferred<Psychomatrix> {
return async {
Psychomatrix(date).also {
psychomatrixCreatedBroadcast.send(it)
}
}
}
override suspend fun calculate(date: Date): Deferred<Psychomatrix> {
return calculate(DateTime(date))
}
override suspend fun openPsychomatrixCreatedSubscription(): ReceiveChannel<Psychomatrix> {
return psychomatrixCreatedBroadcast.openSubscription()
}
}