Skip to content

Commit

Permalink
Add: RxJava2 and RxAndroid
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikhil Bansal committed Aug 30, 2018
1 parent 80e695f commit 0cee210
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
5 changes: 5 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ dependencies {
kapt "com.google.dagger:dagger-android-processor:$dagger_version"
kapt "com.google.dagger:dagger-compiler:$dagger_version"

// RxJava and RxAndroid
implementation "io.reactivex.rxjava2:rxjava:$rxjava_version"
implementation "io.reactivex.rxjava2:rxandroid:$rxandroid_version"
implementation "com.jakewharton.retrofit:retrofit2-rxjava2-adapter:$retrofit_adapter"

testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.android.nikhil.worldnow.di.module

import com.android.nikhil.worldnow.network.NewsInterface
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import dagger.Module
import dagger.Provides
import retrofit2.Retrofit
Expand All @@ -16,6 +17,7 @@ class AppModule {
return Retrofit.Builder()
.baseUrl("https://content.guardianapis.com")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.android.nikhil.worldnow.network

import com.android.nikhil.worldnow.utils.MainResponse
import io.reactivex.Observable
import retrofit2.http.GET
import retrofit2.Call
import retrofit2.http.Query

interface NewsInterface {

@GET("search")
fun getNews(@Query("api-key") apiKey: String): Call<MainResponse>
fun getNews(@Query("api-key") apiKey: String): Observable<MainResponse>

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.android.nikhil.worldnow.BuildConfig
import com.android.nikhil.worldnow.network.NewsInterface
import com.android.nikhil.worldnow.utils.MainResponse
import com.android.nikhil.worldnow.utils.Result
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import retrofit2.Call
import retrofit2.Callback
import javax.inject.Inject
Expand All @@ -16,20 +18,13 @@ class NewsViewModal @Inject constructor(): ViewModel() {
private var newsLiveData = MutableLiveData<ArrayList<Result>>()
@Inject lateinit var newsApi: NewsInterface

fun getNewsData(): MutableLiveData<ArrayList<Result>> = newsLiveData

fun getNews() {
val callNews = newsApi.getNews(BuildConfig.ApiKey)
callNews.enqueue(object: Callback<MainResponse> {
override fun onResponse(call: Call<MainResponse>?, mainResponse: retrofit2.Response<MainResponse>?) {
val newsResponse = mainResponse?.body()
val body = newsResponse?.response
if (body?.results != null) newsLiveData.postValue(body.results)
else Log.d("", "results are null")
}
override fun onFailure(call: Call<MainResponse>?, t: Throwable?) {
Log.d("", t?.localizedMessage)
}
})
newsApi.getNews(BuildConfig.ApiKey)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.map { mainRes -> mainRes.response.results }
.subscribe({ value -> newsLiveData.postValue(value) }, { e -> e.printStackTrace() })
}

fun getNewsData() = newsLiveData
}
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ ext {
gson_convertor_version = "2.4.0"
arch_lifecycle_version = "1.1.1"
dagger_version = "2.16"
rxjava_version = "2.2.1"
rxandroid_version = "2.1.0"
retrofit_adapter = "1.0.0"
}

task clean(type: Delete) {
Expand Down

0 comments on commit 0cee210

Please sign in to comment.