This file contains hidden or 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
/~\ | |
|oo ) This isn't the blogpost | |
_\=/_ you're looking for! | |
___ # / _ \ | |
/ ()\ \\//|/.\|\\ | |
_|_____|_ \/ \_/ || | |
| | === | | |\ /| || | |
|_| O |_| \_ _/ # | |
|| O || | | | |
This file contains hidden or 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
/~\ | |
|oo ) This isn't the blogpost | |
_\=/_ you're looking for! | |
___ # / _ \ | |
/ ()\ \\//|/.\|\\ | |
_|_____|_ \/ \_/ || | |
| | === | | |\ /| || | |
|_| O |_| \_ _/ # | |
|| O || | | | |
This file contains hidden or 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
((SomeApplicationBaseType) getContext().getApplicationContext()) | |
.getApplicationComponent() | |
.newActivityComponentBuilder() | |
.activity(this) | |
.build() | |
.inject(this); |
This file contains hidden or 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 DaggerComponentProvider { | |
val component: ApplicationComponent | |
} | |
val Activity.injector get() = (application as DaggerComponentProvider).component |
This file contains hidden or 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 ViewModelFactory<VM : ViewModel> @Inject constructor( | |
private val viewModel: Lazy<VM> | |
) : ViewModelProvider.Factory { | |
@Suppress("UNCHECKED_CAST") | |
override fun <T : ViewModel> create(modelClass: Class<T>) = viewModel.get() as T | |
} |
This file contains hidden or 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 BestPostActivity : AppCompatActivity() { | |
private val viewModel by lazy { | |
ViewModelProviders | |
.of(this, injector.bestPostViewModelFactory()) | |
.get(BestPostViewModel::class.java) | |
} | |
... | |
} |
This file contains hidden or 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
@Reusable | |
class BestPostFinder @Inject constructor() { | |
... | |
} |
This file contains hidden or 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
@Module | |
object PostModule { | |
@JvmStatic @Provides @Reusable | |
fun provideBestPostFinder() = BestPostFinder() | |
} |
This file contains hidden or 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
@Singleton | |
@Component(modules = [ApplicationModule::class, YourModule::class, ThatOtherModule::class]) | |
interface ApplicationComponent | |
@Module | |
class ApplicationModule(private val applicationContext: Context) { | |
@Provides fun provideApplicationContext(): Context = applicationContext | |
} |
This file contains hidden or 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
val Fragment.injector | |
get() = (requireActivity().application as DaggerComponentProvider).component |