Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.