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.
Thanks for the suggestion, I'm working on a new version now with this
already in mind.
I'm rewriting pretty much everything from the ground up. If you have any
additional suggestions, I'm happy to hear and collaborate
…On Wed, Feb 17, 2021, 19:04 Eugen Pechanec ***@***.***> wrote:
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 ***@***.*** 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 ***@***.*** 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.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#6>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AD563XFERO2WRPFUZ6O5OSDS7PZKVANCNFSM4XYW6ZKA>
.
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!