From 003886605cf1b571825687e63d4ac3db292f35bc Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 4 Sep 2018 17:29:10 +0800 Subject: [PATCH] add HistoryDatesRepository --- .../data/repository/HistoryDatesRepository.kt | 15 ++++++ .../HistoryDatesRepositoryImpl.kt | 51 +++++++++++++++++++ .../di/PresentationLayerDI.kt | 2 + .../realisations/PresentationLayerDIImpl.kt | 2 +- 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/data/repository/HistoryDatesRepository.kt create mode 100644 src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/data/repository/realisations/HistoryDatesRepositoryImpl.kt diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/data/repository/HistoryDatesRepository.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/data/repository/HistoryDatesRepository.kt new file mode 100644 index 0000000..06bb863 --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/data/repository/HistoryDatesRepository.kt @@ -0,0 +1,15 @@ +package com.github.insanusmokrassar.PsychomatrixBase.data.repository + +import kotlinx.coroutines.experimental.Deferred +import kotlinx.coroutines.experimental.channels.ReceiveChannel +import org.joda.time.DateTime + +interface HistoryDatesRepository { + + suspend fun openDateAddedSubscription(): ReceiveChannel + suspend fun openDateRemovedSubscription(): ReceiveChannel + + suspend fun getDates(from: DateTime? = null, to: DateTime? = null): Deferred> + suspend fun removeDate(date: DateTime): Deferred + +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/data/repository/realisations/HistoryDatesRepositoryImpl.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/data/repository/realisations/HistoryDatesRepositoryImpl.kt new file mode 100644 index 0000000..439c96a --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/data/repository/realisations/HistoryDatesRepositoryImpl.kt @@ -0,0 +1,51 @@ +package com.github.insanusmokrassar.PsychomatrixBase.data.repository.realisations + +import com.github.insanusmokrassar.PsychomatrixBase.data.repository.HistoryDatesRepository +import com.github.insanusmokrassar.PsychomatrixBase.domain.UseCases.CalculatePsychomatrixByDate +import com.github.insanusmokrassar.PsychomatrixBase.utils.extensions.SUBSCRIPTIONS_EXTRA_SMALL +import com.github.insanusmokrassar.PsychomatrixBase.utils.extensions.subscribe +import kotlinx.coroutines.experimental.* +import kotlinx.coroutines.experimental.channels.BroadcastChannel +import kotlinx.coroutines.experimental.channels.ReceiveChannel +import org.joda.time.DateTime + +abstract class HistoryDatesRepositoryImpl( + calculatePsychomatrixByDate: CalculatePsychomatrixByDate +) : HistoryDatesRepository { + private val dateAddedBroadcast = BroadcastChannel(SUBSCRIPTIONS_EXTRA_SMALL) + private val dateRemovedBroadcast = BroadcastChannel(SUBSCRIPTIONS_EXTRA_SMALL) + + init { + runBlocking { + calculatePsychomatrixByDate.openPsychomatrixCreatedSubscription().subscribe { + it.date.also { + date -> + onDateCalculated(date) + dateAddedBroadcast.send(date) + } + } + } + } + + override suspend fun openDateAddedSubscription(): ReceiveChannel { + return dateAddedBroadcast.openSubscription() + } + + override suspend fun openDateRemovedSubscription(): ReceiveChannel { + return dateRemovedBroadcast.openSubscription() + } + + protected abstract fun onDateCalculated(dateTime: DateTime) + + override suspend fun removeDate(date: DateTime): Deferred { + return async { + internalRemoveDate(date).also { + if (it) { + dateRemovedBroadcast.send(date) + } + } + } + } + + protected abstract fun internalRemoveDate(date: DateTime): Boolean +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/di/PresentationLayerDI.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/di/PresentationLayerDI.kt index e456218..ac263af 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/di/PresentationLayerDI.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/di/PresentationLayerDI.kt @@ -1,9 +1,11 @@ package com.github.insanusmokrassar.PsychomatrixBase.di +import com.github.insanusmokrassar.PsychomatrixBase.data.repository.HistoryDatesRepository import com.github.insanusmokrassar.PsychomatrixBase.presentation.presenters.DatePickerPresenter interface PresentationLayerDI : UseCasesDI { val datePickerPresenter: DatePickerPresenter + val historyDatesRepository: HistoryDatesRepository } \ No newline at end of file diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/di/realisations/PresentationLayerDIImpl.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/di/realisations/PresentationLayerDIImpl.kt index f20bb47..4cf19d6 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/di/realisations/PresentationLayerDIImpl.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/di/realisations/PresentationLayerDIImpl.kt @@ -5,7 +5,7 @@ import com.github.insanusmokrassar.PsychomatrixBase.di.UseCasesDI import com.github.insanusmokrassar.PsychomatrixBase.presentation.presenters.DatePickerPresenter import com.github.insanusmokrassar.PsychomatrixBase.presentation.presenters.DefaultRealisations.DatePickerPresenterImpl -open class PresentationLayerDIImpl( +abstract class PresentationLayerDIImpl( useCasesDI: UseCasesDI ) : PresentationLayerDI, UseCasesDI by useCasesDI {