[TOC]
Ruby and Kotlin have the same magic candy function!
- apply (also)
| // There is a class with generic parameter. | |
| abstract class A<T> { | |
| // This is KClass version. | |
| val t: T by lazy { | |
| (this@A::class | |
| .supertypes.first() // KType of A class | |
| .arguments.first() // Data type of the generic T | |
| .type!!.classifier as KClass<*>) // The class of the generic T | |
| .primaryConstructor!!.call() as T // An object from the generic T | |
| } |
| override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { | |
| // Measure all of children's width & height. | |
| this.measureChildren(widthMeasureSpec, heightMeasureSpec) | |
| // Measure width & height of this view_group's layout(layout_width or layout_height will be `match_parent` | |
| // no matter what we set `wrap_content` or `match_patent` when we're using getDefaultSize). | |
| // We'll reset this method by another way for achieving `wrap_content`. | |
| val mini = minOf(getDefaultSize(suggestedMinimumWidth, widthMeasureSpec), getDefaultSize(suggestedMinimumHeight, heightMeasureSpec)) | |
| this.setMeasuredDimension(mini, mini) | |
| } |
| @Singleton | |
| @Component(modules = arrayOf(AppModule::class, | |
| RepositoryModule::class, | |
| BindingActivityModule::class, | |
| AndroidSupportInjectionModule::class)) | |
| interface AppComponent : AndroidInjector<App> { | |
| /** [AndroidInjector] Builder for using on this whole app. */ | |
| @Component.Builder | |
| abstract class Builder : AndroidInjector.Builder<App>() |
| @Module | |
| class AppModule { | |
| @Provides | |
| @Singleton | |
| fun provideApplication(app: App): Application = app | |
| @Provides | |
| @Singleton | |
| fun provideAppContext(app: App): Context = app.applicationContext | |
| } |
| @Module | |
| abstract class BindingActivityModule { | |
| /** | |
| * ContributesAndroidInjector is including fragment injector. Only putting FragmentBindModule in modules array, | |
| * the children fragment can obtain the parent's fragment injector. | |
| */ | |
| @PerActivity | |
| @ContributesAndroidInjector(modules = arrayOf(BindingFragmentModule::class)) | |
| abstract fun contributeMainActivityInjector(): MainActivity | |
| } |
| @Module | |
| abstract class BindingFragmentModule { | |
| @PerFragment | |
| @ContributesAndroidInjector(modules = arrayOf(FragmentMainModule::class)) | |
| abstract fun contributeMainFragmentInjector(): MainFragment | |
| } |
| @Module | |
| class FragmentMainModule { | |
| @Provides | |
| @PerFragment | |
| fun provideMainPresenter(usecase: CreateFakeUseCase): MainContract.Presenter = MainPresenter(usecase) | |
| @Provides | |
| @PerFragment | |
| fun provideUsecase(threadExecutor: ThreadExecutor, | |
| postExecutionThread: PostExecutionThread, |
| {"lastUpload":"2017-11-27T08:31:15.832Z","extensionVersion":"v2.8.6"} |
| class App: Application() { | |
| companion object { | |
| lateinit private var context: Context | |
| @JvmStatic fun appComponent(): AppComponent = (context as App).appComponent | |
| // Provide the global application context. | |
| @JvmStatic fun getAppContext(): Context = context | |
| } | |
| // Before dagger 2.11 |