Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ramzijabali/learning-rxkotlin
Cook book for RXKtolin
https://github.com/ramzijabali/learning-rxkotlin
Last synced: 2 days ago
JSON representation
Cook book for RXKtolin
- Host: GitHub
- URL: https://github.com/ramzijabali/learning-rxkotlin
- Owner: RamziJabali
- Created: 2021-07-09T23:25:46.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-07-10T22:18:12.000Z (over 3 years ago)
- Last Synced: 2023-08-01T22:48:45.483Z (over 1 year ago)
- Size: 5.86 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# learning-rxKotlin
Cook book for RXKotlin## Creating a single Observable
If you have a List of items that you want use you can use a for each loop in the `onNext()` or you can use `just()` which acomodates for collection.
```
var stringy = "Hi"var x: Observable = Observable.create(ObservableOnSubscribe { emitter -> //Lambda
if(!emitter.isDisposed){
emitter.onNext(stringy)
emitter.onComplete()
}})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())x.subscribe(object : Observer {
override fun onSubscribe(d: Disposable?) {}
override fun onNext(t: String?) {
Log.d(TAG, "onNext: $t")
}override fun onError(e: Throwable?) {
Log.d(TAG, "onError: ${e.toString()}")
}override fun onComplete() {
Log.d(TAG, "Complete")
}})
```## Creating an instance of retro fit
### This allows us to convert a retrofit Call object to a Observable or a Flowable
```
retrofit = Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build()
```## Creating our Json place holder API
### Retrofit fills out the methods in the interface for us```
jsonWeatherAPI = retrofit.create(JsonWeatherAPI::class.java)
```## API Endpoint manager
```
@GET("/api/location/{whereOnEarthId}")
fun getWeatherForWhereOnEarthId(@Path("whereOnEarthId") whereOnEarthId: Int): Flowable
```## Making the call and setting our Views
```
jsonWeatherAPI.getWeatherForWhereOnEarthId(44418)
.toObservable()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { weatherForLocation ->
cityTextView.text = weatherForLocation.cityTitle
stateTextView.text = weatherForLocation.parentRegion.title
temperatureTextView.text = weatherForLocation.consolidatedWeather[0].theTemp.toString()
pressureValueTextView.text = weatherForLocation.consolidatedWeather[0].airPressure.toString()
humidityValueTextView.text = weatherForLocation.consolidatedWeather[0].humidity.toString()
windSpeedValueTextView.text = weatherForLocation.consolidatedWeather[0].windSpeed.toString()
}
```