Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Update InteractorExtensions.kt #50

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dk.nodes.template.presentation.extensions

import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import dk.nodes.template.domain.interactors.BaseAsyncInteractor
import dk.nodes.template.domain.interactors.CompleteResult
Expand All @@ -12,6 +13,7 @@ import dk.nodes.template.domain.interactors.Uninitialized
import io.reactivex.BackpressureStrategy
import io.reactivex.Flowable
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.Subject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
Expand Down Expand Up @@ -59,6 +61,31 @@ private class LiveDataInteractorImpl<T>(private val interactor: BaseAsyncInterac
}
}

abstract class LiveDataInteractor2<I, T> {
private val mediatorLiveData = MediatorLiveData<T>()
abstract fun createLiveData(input: I): LiveData<T>
private var previousSource: LiveData<T>? = null
fun invoke(input: I) {
previousSource?.let(mediatorLiveData::removeSource)
val source = createLiveData(input)
mediatorLiveData.addSource(source, mediatorLiveData::setValue)
previousSource = source
}

val liveData: LiveData<T> = mediatorLiveData
}

abstract class FlowableInteractor<I : Any, T> {
private val subject: Subject<I> = BehaviorSubject.create()
val flowable = subject.switchMap {
createObservable(it).toObservable()
}.toFlowable(BackpressureStrategy.LATEST)!!

operator fun invoke(input: I) = subject.onNext(input)

protected abstract fun createObservable(input: I): Flowable<T>
}

@ExperimentalCoroutinesApi
private class ChannelInteractorImpl<T>(private val interactor: BaseAsyncInteractor<out T>) :
ChannelInteractor<T> {
Expand Down