Hey in Android we have bunch of platforms classes ...
# kodein
i
Hey in Android we have bunch of platforms classes that are instantiated by Android. I introduced 4 “base injection classes” and made all of them
KodeinAware
(just to make separation of injection code cleaner), and in each of them I am overriding 4 properties in very similar way. Each class contains the almost identical code (generic types arguments and trigger call side differs), so I wonder is there an easy way to extract this common logic and somehow use composition or maybe interface with default values 🤔. Goal here is to remove this “duplicated” kodein related code?
Copy code
abstract class InjectionFragment : Fragment(), KodeinAware {

    final override val kodeinContext = kcontext<Fragment>(this)
    final override val kodein: Kodein by kodein()
    final override val kodeinTrigger: KodeinTrigger? // See description in InjectionActivity
        get() = if (BuildConfig.DEBUG) KodeinTrigger() else super.kodeinTrigger

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        kodeinTrigger?.trigger()
    }
}
Copy code
abstract class InjectionActivity : AppCompatActivity(), KodeinAware {

    private val parentKodein by kodein()

    final override val kodeinContext = kcontext<AppCompatActivity>(this)
    final override val kodein: Kodein by retainedKodein {
        extend(parentKodein)
    }

    final override val kodeinTrigger: KodeinTrigger?
        get() = if (BuildConfig.DEBUG) KodeinTrigger() else super.kodeinTrigger

    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
        kodeinTrigger?.trigger()
    }
}