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 SomeApplication : Application() { | |
private lateinit var appComponent: AppComponent | |
private var featureComponent: FeatureComponent? = null | |
override fun onCreate() { | |
super.onCreate() | |
appComponent = DaggerAppComponent.builder() | |
.someModule(SomeModule()) |
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
interface IHasComponent { | |
fun getComponent(): Any | |
fun getComponentKey(): String = javaClass.toString() | |
} |
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
internal class ComponentsStore { | |
private val componentsForView = mutableMapOf<String, Any>() | |
fun isExist(key: String): Boolean = | |
componentsForView.containsKey(key) | |
fun add(key: String, component: Any) { | |
componentsForView[key] = component | |
} |
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 ComponentNotFoundException : Throwable("Component was not found") |
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 InjectionManager { | |
companion object { | |
@JvmStatic val instance = InjectionManager() | |
} | |
private val componentsStore = ComponentsStore() | |
fun init(app: Application) { | |
app.registerActivityLifecycleCallbacks(ActivityLifecycleHelper(componentsStore)) | |
} |
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
internal class ActivityLifecycleHelper( | |
private val componentsStore: ComponentsStore | |
) : Application.ActivityLifecycleCallbacks { | |
override fun onActivityPaused(activity: Activity) {} | |
override fun onActivityResumed(activity: Activity) {} | |
override fun onActivityStarted(activity: Activity) {} | |
override fun onActivityDestroyed(activity: Activity) { |
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
internal class FragmentLifecycleHelper( | |
private val componentsStore: ComponentsStore | |
) : FragmentManager.FragmentLifecycleCallbacks() { | |
private var isInSaveState = false | |
override fun onFragmentStarted(fm: FragmentManager, f: Fragment) { | |
super.onFragmentStarted(fm, f) | |
isInSaveState = false | |
} |
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 SimpleApplication : Application() { | |
override fun onCreate() { | |
super.onCreate() | |
InjectionManager.instance.init(this) | |
} | |
} |
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 FirstFragment : Fragment(), IHasComponent { | |
//code... | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
InjectionManager.instance.bindComponent<FirstFeatureComponent>(this).inject(this) | |
} | |
override fun getComponent(): FirstFeatureComponent = | |
DaggerFirstFeatureComponent.builder() |
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 SecondFragment : Fragment() { | |
//code... | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
InjectionManager.instance.findComponent<AppComponent>().inject(this) | |
} | |
} |
OlderNewer