diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/data/repository/PsychomatrixCeilDescriptionRepository.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/data/repository/PsychomatrixCeilDescriptionRepository.kt new file mode 100644 index 0000000..c13cb42 --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/data/repository/PsychomatrixCeilDescriptionRepository.kt @@ -0,0 +1,8 @@ +package com.github.insanusmokrassar.PsychomatrixBase.data.repository + +import com.github.insanusmokrassar.PsychomatrixBase.domain.entities.CeilDescription +import com.github.insanusmokrassar.PsychomatrixBase.domain.entities.PsychomatrixCeilInfo + +interface PsychomatrixCeilDescriptionRepository { + fun getCeilDescription(psychomatrixCeilInfo: PsychomatrixCeilInfo): CeilDescription +} diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/data/repository/realisations/PsychomatrixCeilDescriptionRepositoryImpl.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/data/repository/realisations/PsychomatrixCeilDescriptionRepositoryImpl.kt new file mode 100644 index 0000000..7a0554e --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/data/repository/realisations/PsychomatrixCeilDescriptionRepositoryImpl.kt @@ -0,0 +1,20 @@ +package com.github.insanusmokrassar.PsychomatrixBase.data.repository.realisations + +import com.github.insanusmokrassar.PsychomatrixBase.data.repository.PsychomatrixCeilDescriptionRepository +import com.github.insanusmokrassar.PsychomatrixBase.domain.UseCases.CeilDescriptionUseCase +import com.github.insanusmokrassar.PsychomatrixBase.utils.extensions.subscribe + +abstract class PsychomatrixCeilDescriptionRepositoryImpl( + ceilDescriptionUseCase: CeilDescriptionUseCase +) : PsychomatrixCeilDescriptionRepository { + init { + ceilDescriptionUseCase.openCeilDescriptionRequestedSubscription().subscribe { + getCeilDescription(it).also { + description -> + ceilDescriptionUseCase.descriptionReady( + it, description + ) + } + } + } +} \ 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 ae472d7..2e25b82 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/di/PresentationLayerDI.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/di/PresentationLayerDI.kt @@ -1,14 +1,15 @@ 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.ModifyPsychomatrixPresenter +import com.github.insanusmokrassar.PsychomatrixBase.data.repository.PsychomatrixCeilDescriptionRepository +import com.github.insanusmokrassar.PsychomatrixBase.presentation.presenters.* interface PresentationLayerDI : UseCasesDI { val datePickerPresenter: DatePickerPresenter + val modifyPsychomatrixPresenter: ModifyPsychomatrixPresenter + val ceilDescriptionPresenter: CeilDescriptionPresenter val historyDatesRepository: HistoryDatesRepository - - val modifyPsychomatrixPresenter: ModifyPsychomatrixPresenter + val psychomatrixCeilDescriptionRepository: PsychomatrixCeilDescriptionRepository } \ No newline at end of file diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/di/UseCasesDI.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/di/UseCasesDI.kt index e3a4fb1..debfdf5 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/di/UseCasesDI.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/di/UseCasesDI.kt @@ -1,9 +1,9 @@ package com.github.insanusmokrassar.PsychomatrixBase.di -import com.github.insanusmokrassar.PsychomatrixBase.domain.UseCases.CalculatePsychomatrixByDateUseCase -import com.github.insanusmokrassar.PsychomatrixBase.domain.UseCases.ModifyPsychomatrixUseCase +import com.github.insanusmokrassar.PsychomatrixBase.domain.UseCases.* interface UseCasesDI : EntitiesDI { val calculatePsychomatrixByDateUseCase: CalculatePsychomatrixByDateUseCase val modifyPsychomatrixUseCase: ModifyPsychomatrixUseCase + val ceilDescriptionUseCase: CeilDescriptionUseCase } \ 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 8c37b5e..f574b34 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 @@ -2,10 +2,8 @@ package com.github.insanusmokrassar.PsychomatrixBase.di.realisations import com.github.insanusmokrassar.PsychomatrixBase.di.PresentationLayerDI import com.github.insanusmokrassar.PsychomatrixBase.di.UseCasesDI -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.ModifyPsychomatrixPresenterImpl -import com.github.insanusmokrassar.PsychomatrixBase.presentation.presenters.ModifyPsychomatrixPresenter +import com.github.insanusmokrassar.PsychomatrixBase.presentation.presenters.* +import com.github.insanusmokrassar.PsychomatrixBase.presentation.presenters.DefaultRealisations.* abstract class PresentationLayerDIImpl( useCasesDI: UseCasesDI @@ -17,7 +15,15 @@ abstract class PresentationLayerDIImpl( ) } - override val modifyPsychomatrixPresenter: ModifyPsychomatrixPresenter = ModifyPsychomatrixPresenterImpl( - modifyPsychomatrixUseCase - ) + override val modifyPsychomatrixPresenter: ModifyPsychomatrixPresenter by lazy { + ModifyPsychomatrixPresenterImpl( + modifyPsychomatrixUseCase + ) + } + + override val ceilDescriptionPresenter: CeilDescriptionPresenter by lazy { + CeilDescriptionPresenterImpl( + ceilDescriptionUseCase + ) + } } \ No newline at end of file diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/di/realisations/UseCasesDIImpl.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/di/realisations/UseCasesDIImpl.kt index c7b5128..93b1a54 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/di/realisations/UseCasesDIImpl.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/di/realisations/UseCasesDIImpl.kt @@ -2,10 +2,8 @@ package com.github.insanusmokrassar.PsychomatrixBase.di.realisations import com.github.insanusmokrassar.PsychomatrixBase.di.EntitiesDI import com.github.insanusmokrassar.PsychomatrixBase.di.UseCasesDI -import com.github.insanusmokrassar.PsychomatrixBase.domain.UseCases.CalculatePsychomatrixByDateUseCase -import com.github.insanusmokrassar.PsychomatrixBase.domain.UseCases.ModifyPsychomatrixUseCase -import com.github.insanusmokrassar.PsychomatrixBase.domain.interactors.CalculatePsychomatrixByDateUseCaseInteractor -import com.github.insanusmokrassar.PsychomatrixBase.domain.interactors.ModifyPsychomatrixUseCaseInteractor +import com.github.insanusmokrassar.PsychomatrixBase.domain.UseCases.* +import com.github.insanusmokrassar.PsychomatrixBase.domain.interactors.* open class UseCasesDIImpl( entitiesDI: EntitiesDI @@ -17,4 +15,8 @@ open class UseCasesDIImpl( override val modifyPsychomatrixUseCase: ModifyPsychomatrixUseCase by lazy { ModifyPsychomatrixUseCaseInteractor() } + + override val ceilDescriptionUseCase: CeilDescriptionUseCase by lazy { + CeilDescriptionInteractor() + } } diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/UseCases/CeilDescriptionUseCase.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/UseCases/CeilDescriptionUseCase.kt new file mode 100644 index 0000000..ca18674 --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/UseCases/CeilDescriptionUseCase.kt @@ -0,0 +1,17 @@ +package com.github.insanusmokrassar.PsychomatrixBase.domain.UseCases + +import com.github.insanusmokrassar.PsychomatrixBase.domain.entities.CeilDescription +import com.github.insanusmokrassar.PsychomatrixBase.domain.entities.PsychomatrixCeilInfo +import kotlinx.coroutines.experimental.channels.ReceiveChannel + +typealias CeilDescriptionReady = Pair + +interface CeilDescriptionUseCase { + fun openCeilDescriptionReadySubscription(): ReceiveChannel + + fun openCeilDescriptionRequestedSubscription(): ReceiveChannel + + fun descriptionReady(psychomatrixCeilInfo: PsychomatrixCeilInfo, ceilDescription: CeilDescription) + + fun requestDescription(psychomatrixCeilInfo: PsychomatrixCeilInfo) +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/CeilDescription.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/CeilDescription.kt new file mode 100644 index 0000000..aafd069 --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/CeilDescription.kt @@ -0,0 +1,3 @@ +package com.github.insanusmokrassar.PsychomatrixBase.domain.entities + +typealias CeilDescription = String \ No newline at end of file diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/interactors/CeilDescriptionInteractor.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/interactors/CeilDescriptionInteractor.kt new file mode 100644 index 0000000..1da31a0 --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/interactors/CeilDescriptionInteractor.kt @@ -0,0 +1,42 @@ +package com.github.insanusmokrassar.PsychomatrixBase.domain.interactors + +import com.github.insanusmokrassar.PsychomatrixBase.domain.UseCases.CeilDescriptionReady +import com.github.insanusmokrassar.PsychomatrixBase.domain.UseCases.CeilDescriptionUseCase +import com.github.insanusmokrassar.PsychomatrixBase.domain.entities.CeilDescription +import com.github.insanusmokrassar.PsychomatrixBase.domain.entities.PsychomatrixCeilInfo +import com.github.insanusmokrassar.PsychomatrixBase.utils.extensions.SUBSCRIPTIONS_EXTRA_SMALL +import com.github.insanusmokrassar.PsychomatrixBase.utils.extensions.SUBSCRIPTIONS_SMALL +import kotlinx.coroutines.experimental.channels.BroadcastChannel +import kotlinx.coroutines.experimental.channels.ReceiveChannel +import kotlinx.coroutines.experimental.launch + +class CeilDescriptionInteractor : CeilDescriptionUseCase { + private val ceilDescriptionReadyBroadcastChannel = BroadcastChannel( + SUBSCRIPTIONS_SMALL + ) + private val ceilDescriptionRequestedBroadcastChannel = BroadcastChannel( + SUBSCRIPTIONS_EXTRA_SMALL + ) + + override fun openCeilDescriptionReadySubscription(): ReceiveChannel { + return ceilDescriptionReadyBroadcastChannel.openSubscription() + } + + override fun openCeilDescriptionRequestedSubscription(): ReceiveChannel { + return ceilDescriptionRequestedBroadcastChannel.openSubscription() + } + + override fun descriptionReady(psychomatrixCeilInfo: PsychomatrixCeilInfo, ceilDescription: CeilDescription) { + launch { + ceilDescriptionReadyBroadcastChannel.send(psychomatrixCeilInfo to ceilDescription) + } + } + + override fun requestDescription(psychomatrixCeilInfo: PsychomatrixCeilInfo) { + launch { + ceilDescriptionRequestedBroadcastChannel.send( + psychomatrixCeilInfo + ) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/presentation/presenters/CeilDescriptionPresenter.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/presentation/presenters/CeilDescriptionPresenter.kt new file mode 100644 index 0000000..256b147 --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/presentation/presenters/CeilDescriptionPresenter.kt @@ -0,0 +1,9 @@ +package com.github.insanusmokrassar.PsychomatrixBase.presentation.presenters + +import com.github.insanusmokrassar.PsychomatrixBase.domain.entities.CeilDescription +import com.github.insanusmokrassar.PsychomatrixBase.domain.entities.PsychomatrixCeilInfo +import kotlinx.coroutines.experimental.Deferred + +interface CeilDescriptionPresenter { + fun onUserChooseCeil(ceilInfo: PsychomatrixCeilInfo): Deferred +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/presentation/presenters/DefaultRealisations/CeilDescriptionPresenterImpl.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/presentation/presenters/DefaultRealisations/CeilDescriptionPresenterImpl.kt new file mode 100644 index 0000000..df5c69d --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/presentation/presenters/DefaultRealisations/CeilDescriptionPresenterImpl.kt @@ -0,0 +1,37 @@ +package com.github.insanusmokrassar.PsychomatrixBase.presentation.presenters.DefaultRealisations + +import com.github.insanusmokrassar.PsychomatrixBase.domain.UseCases.CeilDescriptionUseCase +import com.github.insanusmokrassar.PsychomatrixBase.domain.entities.CeilDescription +import com.github.insanusmokrassar.PsychomatrixBase.domain.entities.PsychomatrixCeilInfo +import com.github.insanusmokrassar.PsychomatrixBase.presentation.presenters.CeilDescriptionPresenter +import com.github.insanusmokrassar.PsychomatrixBase.utils.Container +import com.github.insanusmokrassar.PsychomatrixBase.utils.extensions.subscribeChecking +import kotlinx.coroutines.experimental.* + +class CeilDescriptionPresenterImpl( + private val ceilDescriptionUseCase: CeilDescriptionUseCase +) : CeilDescriptionPresenter { + override fun onUserChooseCeil(ceilInfo: PsychomatrixCeilInfo): Deferred { + return async { + val container = Container() + val subscription = ceilDescriptionUseCase.openCeilDescriptionReadySubscription().subscribeChecking( + { + container.throwable = it + false + } + ) { + if (it.first == ceilInfo) { + container.value = it.second + false + } else { + true + } + } + + ceilDescriptionUseCase.requestDescription(ceilInfo) + + subscription.join() + container.value ?: throw container.throwable ?: throw IllegalStateException("Strange state - must be set value or error") + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/utils/Container.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/utils/Container.kt new file mode 100644 index 0000000..6f9383a --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/utils/Container.kt @@ -0,0 +1,6 @@ +package com.github.insanusmokrassar.PsychomatrixBase.utils + +class Container( + var value: T? = null, + var throwable: Throwable? = null +) \ No newline at end of file