Skip to content

Instantly share code, notes, and snippets.

@thegarlynch
Last active October 22, 2021 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thegarlynch/3ce33e438acbd82e26514d7fb6a41c50 to your computer and use it in GitHub Desktop.
Save thegarlynch/3ce33e438acbd82e26514d7fb6a41c50 to your computer and use it in GitHub Desktop.
Manual Dependency Injection
class TitanApplication : Application(), ApplicationComponentOwner {
override lateinit var component : ApplicationComponent
fun onCreate(){
component = ApplicationComponentImpl()
}
}
class ApplicationComponentImpl : ApplicationComponent {
override val mainActivityInterface : MainActivityInterface = MainActivityInterfaceImpl()
}
class MainActivityInterfaceImpl : MainActivityInterface {
override fun show(context : Context){
context.startActivity(intent, MainActivity::class)
}
}
abstract class BaseActivity : AppCompatActivity {
fun applicationComponent() : ApplicationComponent {
return (applicationContext as ApplicationComponentOwner).component
}
fun callMainActivity(){
applicationComponent().mainActivityInterface.show(context)
}
}
interface MainActivityInterface {
fun show(context : Context)
}
interface ApplicationComponent {
val mainActivityInterface : MainActivityInterface
}
interface ApplicationComponentOwner {
val component : ApplicationComponent
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment