Skip to content

Instantly share code, notes, and snippets.

@tfcporciuncula
tfcporciuncula / nope
Last active January 15, 2019 09:53
nope
/~\
|oo ) This isn't the blogpost
_\=/_ you're looking for!
___ # / _ \
/ ()\ \\//|/.\|\\
_|_____|_ \/ \_/ ||
| | === | | |\ /| ||
|_| O |_| \_ _/ #
|| O || | | |
@tfcporciuncula
tfcporciuncula / nope
Last active January 15, 2019 09:53
nope
/~\
|oo ) This isn't the blogpost
_\=/_ you're looking for!
___ # / _ \
/ ()\ \\//|/.\|\\
_|_____|_ \/ \_/ ||
| | === | | |\ /| ||
|_| O |_| \_ _/ #
|| O || | | |
@tfcporciuncula
tfcporciuncula / FrombulationActivity.java
Created August 31, 2018 08:29
Dagger injection in activities according to the docs
((SomeApplicationBaseType) getContext().getApplicationContext())
.getApplicationComponent()
.newActivityComponentBuilder()
.activity(this)
.build()
.inject(this);
@tfcporciuncula
tfcporciuncula / DaggerComponentProvider.kt
Created August 31, 2018 08:31
DaggerComponentProvider
interface DaggerComponentProvider {
val component: ApplicationComponent
}
val Activity.injector get() = (application as DaggerComponentProvider).component
@tfcporciuncula
tfcporciuncula / ViewModelFactory.kt
Created November 29, 2018 22:42
ViewModelFactory
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
}
@tfcporciuncula
tfcporciuncula / BestPostActivity.kt
Created November 29, 2018 23:04
BestPostActivity
class BestPostActivity : AppCompatActivity() {
private val viewModel by lazy {
ViewModelProviders
.of(this, injector.bestPostViewModelFactory())
.get(BestPostViewModel::class.java)
}
...
}
@tfcporciuncula
tfcporciuncula / BestPostFinder.kt
Created November 30, 2018 08:18
BestPostFinder
@Reusable
class BestPostFinder @Inject constructor() {
...
}
@Module
object PostModule {
@JvmStatic @Provides @Reusable
fun provideBestPostFinder() = BestPostFinder()
}
@tfcporciuncula
tfcporciuncula / ApplicationInjection.kt
Created December 14, 2018 20:09
ApplicationComponent-with-module
@Singleton
@Component(modules = [ApplicationModule::class, YourModule::class, ThatOtherModule::class])
interface ApplicationComponent
@Module
class ApplicationModule(private val applicationContext: Context) {
@Provides fun provideApplicationContext(): Context = applicationContext
}
@tfcporciuncula
tfcporciuncula / DaggerComponentProvider.kt
Created December 15, 2018 15:28
Fragment injector extension
val Fragment.injector
get() = (requireActivity().application as DaggerComponentProvider).component