add HistoryDatesRepository

This commit is contained in:
InsanusMokrassar 2018-09-04 17:29:10 +08:00
parent 108fd4276d
commit 003886605c
4 changed files with 69 additions and 1 deletions

View File

@ -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<DateTime>
suspend fun openDateRemovedSubscription(): ReceiveChannel<DateTime>
suspend fun getDates(from: DateTime? = null, to: DateTime? = null): Deferred<List<DateTime>>
suspend fun removeDate(date: DateTime): Deferred<Boolean>
}

View File

@ -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<DateTime>(SUBSCRIPTIONS_EXTRA_SMALL)
private val dateRemovedBroadcast = BroadcastChannel<DateTime>(SUBSCRIPTIONS_EXTRA_SMALL)
init {
runBlocking {
calculatePsychomatrixByDate.openPsychomatrixCreatedSubscription().subscribe {
it.date.also {
date ->
onDateCalculated(date)
dateAddedBroadcast.send(date)
}
}
}
}
override suspend fun openDateAddedSubscription(): ReceiveChannel<DateTime> {
return dateAddedBroadcast.openSubscription()
}
override suspend fun openDateRemovedSubscription(): ReceiveChannel<DateTime> {
return dateRemovedBroadcast.openSubscription()
}
protected abstract fun onDateCalculated(dateTime: DateTime)
override suspend fun removeDate(date: DateTime): Deferred<Boolean> {
return async {
internalRemoveDate(date).also {
if (it) {
dateRemovedBroadcast.send(date)
}
}
}
}
protected abstract fun internalRemoveDate(date: DateTime): Boolean
}

View File

@ -1,9 +1,11 @@
package com.github.insanusmokrassar.PsychomatrixBase.di package com.github.insanusmokrassar.PsychomatrixBase.di
import com.github.insanusmokrassar.PsychomatrixBase.data.repository.HistoryDatesRepository
import com.github.insanusmokrassar.PsychomatrixBase.presentation.presenters.DatePickerPresenter import com.github.insanusmokrassar.PsychomatrixBase.presentation.presenters.DatePickerPresenter
interface PresentationLayerDI : UseCasesDI { interface PresentationLayerDI : UseCasesDI {
val datePickerPresenter: DatePickerPresenter val datePickerPresenter: DatePickerPresenter
val historyDatesRepository: HistoryDatesRepository
} }

View File

@ -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.DatePickerPresenter
import com.github.insanusmokrassar.PsychomatrixBase.presentation.presenters.DefaultRealisations.DatePickerPresenterImpl import com.github.insanusmokrassar.PsychomatrixBase.presentation.presenters.DefaultRealisations.DatePickerPresenterImpl
open class PresentationLayerDIImpl( abstract class PresentationLayerDIImpl(
useCasesDI: UseCasesDI useCasesDI: UseCasesDI
) : PresentationLayerDI, UseCasesDI by useCasesDI { ) : PresentationLayerDI, UseCasesDI by useCasesDI {