gilgoldzweig / Gencycler

Gencycler is the fastest way to write RecyclerView adapters

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Don't pass Context in constuctor

consp1racy opened this issue · comments

We didn't actually need it 10 years ago, sure as hell don't need it now.

Don't give consumers the ability to pass in wrong context (typically applicationContext or this in an Activity). Maybe I'm using theme overlays in the app, maybe not, maybe I don't know what that is just yet.

Current behavior

abstract class GencyclerRecyclerAdapter<T : GencyclerModel, VH : GencyclerHolder>(
	context: Context,
	var elements: MutableList<T> = ArrayList(),
	var updateUi: Boolean = true) : RecyclerView.Adapter<VH>() {

    private val inflater: LayoutInflater = LayoutInflater.from(context)

    protected fun inflate(@LayoutRes layoutResId: Int, parent: ViewGroup): View =
		inflater.inflate(layoutResId, parent, false)

Expected behavior

Use the most relevant themed context:

abstract class GencyclerRecyclerAdapter<T : GencyclerModel, VH : GencyclerHolder>(
	var elements: MutableList<T> = ArrayList(),
	var updateUi: Boolean = true) : RecyclerView.Adapter<VH>() {

    protected fun inflate(@LayoutRes layoutResId: Int, parent: ViewGroup): View {
        val inflater = parent.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        return inflater.inflate(layoutResId, parent, false)
    }

Don't worry too much about performance. ContextThemeWrapper (which Activity extends) caches a LayoutInflater. Worry about correctness.

Hi, I found a couple of other issues, already reported. Two years passed should help bring a new perspective on things, too. Good luck with the update!