We will fetch all the posts using the getAllPosts() function. This function will send a request to the server to get all the post list. In return, we will get the updated list named newPostList and pass using setItems(newPostList) to PostRecycleViewAdapter and notify with notifyDataSetChanged(). For the error handling, we have used toast. Here is the code of the getAllPosts() function:
private fun getAllPosts() {
APIClient.postAPICall(PrefUtils.getUsername(this)!!, PrefUtils.getPassword(this)!!)
.getPostList()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
newPostList ->
postRecycleViewAdapter.setItems(newPostList)
postRecycleViewAdapter.notifyDataSetChanged()
},{
error ->
Toast.makeText(applicationContext, error.message.toString(), Toast.LENGTH_SHORT).show()
})
}