1

I have written a Kotlin FirebaseRecyclerAdapter that works just fine as part of my MainActivity. However, I would like to have this code in a separate MainAdapter.kt file/class. How can I do this?

var query = FirebaseDatabase.getInstance()
        .reference
        .child("").child("categories")
        .limitToLast(50)

val options = FirebaseRecyclerOptions.Builder<Category>()
        .setQuery(query, Category::class.java)
        .setLifecycleOwner(this)
        .build()

val adapter = object : FirebaseRecyclerAdapter<Category, CategoryHolder>(options) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryHolder {
        return CategoryHolder(LayoutInflater.from(parent.context)
                .inflate(R.layout.category_row, parent, false))
    }

    protected override fun onBindViewHolder(holder: CategoryHolder, position: Int, model: Category) {
        holder.bind(model)
    }

    override fun onDataChanged() {
        // Called each time there is a new data snapshot. You may want to use this method
        // to hide a loading spinner or check for the "no documents" state and update your UI.
        // ...
    }
}

class CategoryHolder(val customView: View, var category: Category? = null) : RecyclerView.ViewHolder(customView) {

    fun bind(category: Category) {
        with(category) {
            customView.textView_name?.text = category.name
            customView.textView_description?.text = category.description
        }
    }
}
CEO tech4lifeapps
  • 885
  • 1
  • 12
  • 31

1 Answers1

1

Given your code you could do something like this :

class MainAdapter(lifecycleOwner: LifecycleOwner) : FirebaseRecyclerAdapter<Category, CategoryHolder>(buildOptions(lifecycleOwner)) {

    companion object {
        private fun buildQuery() = FirebaseDatabase.getInstance()
                .reference
                .child("").child("categories")
                .limitToLast(50)

        private fun buildOptions(lifecycleOwner:LifecycleOwner) = FirebaseRecyclerOptions.Builder<Category>()
                .setQuery(buildQuery(), Category::class.java)
                .setLifecycleOwner(lifecycleOwner)
                .build()

    }

    class CategoryHolder(val customView: View, var category: Category? = null) : RecyclerView.ViewHolder(customView) {

        fun bind(category: Category) {
            with(category) {
                customView.textView_name?.text = category.name
                customView.textView_description?.text = category.description
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryHolder {
        return CategoryHolder(LayoutInflater.from(parent.context)
                .inflate(R.layout.category_row, parent, false))
    }

    protected override fun onBindViewHolder(holder: CategoryHolder, position: Int, model: Category) {
        holder.bind(model)
    }

    override fun onDataChanged() {
        // Called each time there is a new data snapshot. You may want to use this method
        // to hide a loading spinner or check for the "no documents" state and update your UI.
        // ...
    }
}

There are many other ways to handle this problem, this is just an encapsulated version of yours.

Lionel Briand
  • 1,732
  • 2
  • 13
  • 21
  • Any recommendations for tutorials or other examples on how to handle this? I got really stuck trying to find the right approach to solve this problem. – CEO tech4lifeapps Apr 09 '18 at 15:27
  • Search for encapsulation. Take a look at [this question](https://stackoverflow.com/q/16014290/4681367) – Lionel Briand Apr 10 '18 at 08:10
  • I generally think of a box and ask to myself basic questions. In your example : What can this box do? (recycles views automatically) What parameters are expected to this box ? (nothing but LifecycleOwner is required so.. I have to put it in parameters) Why this box have to exist ? (separate my recycler from my activity for more readability) Can I reuse this box over time if I do it like this ? (probably with minors modifications) What auto-process this box have to do ? (create query and options, theses are always the same) – Lionel Briand Apr 10 '18 at 08:17
  • You might be able to answer this [new question](https://stackoverflow.com/questions/50072368/how-to-encapsulate-a-custom-mediarecorder-in-android-kotlin) Your expertise and help is highly appreciated! – CEO tech4lifeapps Apr 28 '18 at 08:51