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 MainActivity : AppCompatActivity() { | |
@Inject | |
lateinit var viewModelProvider: Provider<MyViewModel> | |
private val viewModel by viewModels { viewModelProvider } | |
override fun onCreate(savedInstanceState: Bundle?) { | |
(application as MyApp).component.inject(this) | |
super.onCreate(savedInstanceState) |
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
inline fun <reified VM : ViewModel> AppCompatActivity.viewModels( | |
crossinline producer: () -> Provider<VM> | |
): Lazy<VM> = | |
viewModels { | |
object : ViewModelProvider.Factory { | |
override fun <T : ViewModel> create(c: Class<T>) = | |
producer().get() as T | |
} | |
} |
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 MainActivity : AppCompatActivity() { | |
@Inject | |
lateinit var dependency: Dependency | |
override fun onCreate(savedInstanceState: Bundle?) { | |
(application as MyApp).component.inject(this) | |
super.onCreate(savedInstanceState) | |
//... | |
} |
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
@Module | |
object MyModule { | |
@Singleton | |
@Provides | |
fun provideCache( | |
databaseCache: Provider<DatabaseCache>, | |
inMemoryCache: Provider<InMemoryCache> | |
): Cache { | |
return if (useInMemoryCache()) | |
inMemoryCache.get() |
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
@Module | |
object MyModule { | |
@Singleton | |
@Provides | |
fun provideCache( | |
databaseCache: DatabaseCache, | |
inMemoryCache: InMemoryCache | |
): Cache { | |
return if (useInMemoryCache()) | |
inMemoryCache |
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
@Module | |
interface MyModule { | |
@Binds | |
fun bindsCache(impl: DatabaseCache): Cache | |
companion object { | |
@Provides | |
fun provideApi(): Api { | |
return createApiImplementationUsingRetrofit() | |
} |
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
@Module | |
object MyApiModule { | |
@Singleton | |
@Provides | |
fun provideApi(): Api { | |
return createApiImplementationUsingRetrofit() | |
} | |
} |
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
@Module | |
interface MyModule { | |
@Binds | |
fun bindsCache(impl: DatabaseCache): Cache | |
} |
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
@Module | |
object MyModule { | |
@Provides | |
fun provideCache(impl: DatabaseCache): Cache = impl | |
} |
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 DatabaseCache @Inject constructor( | |
private val database: Database | |
) : Cache { | |
override fun load(): String? = database.executeQuery() | |
} |