This file contains 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
class NewsListActivity : AppCompatActivity() { | |
private lateinit var viewModel: NewsListViewModel | |
private lateinit var newsListAdapter: NewsListAdapter | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_news_list) | |
viewModel = ViewModelProviders.of(this) |
This file contains 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
class NewsListAdapter(private val retry: () -> Unit) | |
: PagedListAdapter<News, RecyclerView.ViewHolder>(NewsDiffCallback) { | |
private val DATA_VIEW_TYPE = 1 | |
private val FOOTER_VIEW_TYPE = 2 | |
private var state = State.LOADING | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { | |
return if (viewType == DATA_VIEW_TYPE) NewsViewHolder.create(parent) else ListFooterViewHolder.create(retry, parent) |
This file contains 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
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<ProgressBar | |
android:id="@+id/progress_bar" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" |
This file contains 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
class NewsListViewModel : ViewModel() { | |
private val networkService = NetworkService.getService() | |
var newsList: LiveData<PagedList<News>> | |
private val compositeDisposable = CompositeDisposable() | |
private val pageSize = 5 | |
private val newsDataSourceFactory: NewsDataSourceFactory | |
init { | |
newsDataSourceFactory = NewsDataSourceFactory(compositeDisposable, networkService) |
This file contains 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
class NewsDataSourceFactory( | |
private val compositeDisposable: CompositeDisposable, | |
private val networkService: NetworkService) | |
: DataSource.Factory<Int, News>() { | |
val newsDataSourceLiveData = MutableLiveData<NewsDataSource>() | |
override fun create(): DataSource<Int, News> { | |
val newsDataSource = NewsDataSource(networkService, compositeDisposable) | |
newsDataSourceLiveData.postValue(newsDataSource) |
This file contains 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
class NewsDataSource( | |
private val networkService: NetworkService, | |
private val compositeDisposable: CompositeDisposable) | |
: PageKeyedDataSource<Int, News>() { | |
var state: MutableLiveData<State> = MutableLiveData() | |
private var retryCompletable: Completable? = null | |
override fun loadInitial(params: LoadInitialParams<Int>, callback: LoadInitialCallback<Int, News>) { |
This file contains 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
enum class State { | |
DONE, LOADING, ERROR | |
} |
This file contains 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
interface NetworkService { | |
@GET("/everything?q=sports&apiKey=aa67d8d98c8e4ad1b4f16dbd5f3be348") | |
fun getNews(@Query("page") page: Int, @Query("pageSize") pageSize: Int): Single<Response> | |
companion object { | |
fun getService(): NetworkService { | |
val retrofit = Retrofit.Builder() | |
.baseUrl("https://newsapi.org/v2/") | |
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) |
This file contains 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
data class Response( | |
@SerializedName("articles") val news: List<News> | |
) | |
data class News( | |
val title: String, | |
@SerializedName("urlToImage") val image: String | |
) |