Skip to content

Instantly share code, notes, and snippets.

@enginebai
Created April 21, 2019 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enginebai/188f639b7f2d88bd966417264d952774 to your computer and use it in GitHub Desktop.
Save enginebai/188f639b7f2d88bd966417264d952774 to your computer and use it in GitHub Desktop.
class FeedViewModel : ViewModel(), KoinComponent {
private val repo: PostRepository by inject()
fun getFeeds(): Observable<PagedList<Post>> = repo.getFeeds()
}
class FeedActivity : BaseActivity() {
private lateinit var list: RecyclerView
private val adapter = FeedAdapter()
private val viewModel by viewModel<FeedViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
list.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
list.adapter = adapter
viewModel.getFeeds()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
adapter.submitList(it)
}.apply { addDisposable(this) }
}
class FeedAdapter : PagedListAdapter<Post, RecyclerView.ViewHolder>(PostDiffUtils) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
// create view holder as same as you do in RecyclerView.Adapter
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
// bind data as same as you do in RecyclerView.Adapter
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment