https://kotlinlang.org logo
#android
Title
# android
c

Can Orhan

07/11/2018, 3:56 PM
I haven't tried it but I imagine you should be able to. Higher order functions are first class citizens, after all. Have you tried anything yet?
@jc I've found an issue that had been opened and answered for you https://github.com/google/dagger/issues/900
j

jc

07/11/2018, 4:20 PM
Thank you @Can Orhan !
👍 1
r

rkeazor

07/12/2018, 12:33 AM
I cant imagine a good use case for injecting a high order function . Why would you really want to
c

Can Orhan

07/12/2018, 8:04 AM
@rkeazor Perhaps an on click listener in Android that needs different behaviour based on build variant or something?
👍 2
r

rkeazor

07/12/2018, 1:00 PM
hmm I see your point , i guess it could come in handy
did it end up working for you in the long run
by adding the surpressedWildcard anontation?
c

Can Orhan

07/12/2018, 2:43 PM
I personally haven't tried it. @jc?
👍 1
j

jc

07/13/2018, 8:28 AM
In our app we have MVP and Dagger 2. We have also a lot of RecyclerView’s adapters which need Listener’s for click actions. We try to avoid using Listeners which activities or fragments have to implement it. Right now we can inject presenter’s function directly to adapter. Inject must look something like this: @Module object MyModule { @Provides @JvmStatic @JvmSuppressWildcards @jsingle fun provideClickFunction(presenter: MyContract.Presenter): Function1<@JvmWildcard MyModel, Unit> = presenter::onModelClick @Provides @JvmStatic @jsingle fun providePresenter(presenter: MyPresenter): MyContract.Presenter = presenter @Provides @JvmStatic @jsingle fun provideAdapter(adapter: MyAdapter): ListAdapter<*, *> = adapter } class MyAdapter @Inject constructor(private val clickFunction: (MyModel) -> Unit) : ListAdapter ... { inner class ItemViewHolder(view: View) : RecyclerView.ViewHolder(view) { fun bind(model: MyModel) = itemView.setOnClickListener { clickFunction(model) } } }
I hope this dummy code is clear for you 🙂
r

rkeazor

07/16/2018, 5:30 AM
What's wrong with the presenter not implementing the listener instead?
j

jc

07/19/2018, 8:20 AM
nothing it’s only a different approach 😉
4 Views