Last active
January 16, 2019 02:54
-
-
Save vamsitallapudi/fffa70a6da4e6329dfdeef996ac6bf00 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.coderefer.rxandroidexamples.intro.operators.create | |
import android.support.v7.app.AppCompatActivity | |
import android.os.Bundle | |
import android.util.Log | |
import com.coderefer.rxandroidexamples.R | |
import io.reactivex.Observable | |
import io.reactivex.Observer | |
import io.reactivex.disposables.Disposable | |
private const val TAG = "CreateOperatorActivity" | |
/** | |
* Class to demonstrate create Operator | |
* */ | |
class CreateOperatorActivity : AppCompatActivity() { | |
private val numList = listOf(1, 2, 3, 4, 5) | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_create_operator) | |
val observable = initializeObservable() | |
val observer = initializeObserver() | |
observable.subscribe(observer) | |
} | |
private fun initializeObserver(): Observer<Int> { | |
return object : Observer<Int> { | |
override fun onComplete() { | |
Log.d(TAG, "onComplete") | |
} | |
override fun onSubscribe(d: Disposable) { | |
} | |
override fun onNext(t: Int) { | |
Log.d(TAG, t.toString()) | |
} | |
override fun onError(e: Throwable) { | |
} | |
} | |
} | |
private fun initializeObservable(): Observable<Int> { | |
// using create operator to create a new Observable | |
return Observable.create { | |
for (i in numList) | |
it.onNext(i) | |
it.onComplete() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment