Skip to content

Instantly share code, notes, and snippets.

@wellbranding
Last active May 7, 2020 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wellbranding/40866861bdc749389a981ea1c51836ea to your computer and use it in GitHub Desktop.
Save wellbranding/40866861bdc749389a981ea1c51836ea to your computer and use it in GitHub Desktop.
class UseCase(private val userDetails:
UserDetails,
val apiService: APIService, val compositeDisposable: CompositeDisposable) {
val resultsHotObservable:BehaviorSubject<Model> = BehaviorSubject.create()
fun startRepeadedNetworkRequest(){
val observable = Observable.defer { startCheckingResults() }
.repeatWhen { observable -> observable.delay(5, TimeUnit.MILLISECONDS) }
.takeUntil { it.data = FINISHED) }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ response ->
resultsHotObservable.onNext(response)
}, {
})
compositeDisposable.add(observable)
}
private fun startCheckingResults():Observable<Model> {
return apiService.startReceivingData(AuthTokenRequest(
userDetails.token)
)
}
fun onStop(){
//is triggered from activity to clear current request and prevent network request from being executed in background
compositeDisposable.clear()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment