Last active
October 22, 2021 09:43
-
-
Save thegarlynch/3ce33e438acbd82e26514d7fb6a41c50 to your computer and use it in GitHub Desktop.
Manual Dependency Injection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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