Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Deepan tizisdeepan

🎯
Focusing
View GitHub Profile
@tizisdeepan
tizisdeepan / ExampleActivity.kt
Created Jul 29, 2020
MVVM with MediatorLiveData
View ExampleActivity.kt
onCreate() {
val viewModel = ... //initialize your view model
viewModel.data.observe(this, Observer {
//do operations based on the value
})
viewModel.getData()
}
View ExampleActivity.kt
onCreate() {
val viewModel = ... //initialize your view model
viewModel.getData()
viewModel.data.observe(this, Observer {
//do operations based on the value
})
}
View ExampleActivity.kt
onCreate() {
val viewModel = ... //initialize your view model
viewModel.getData()
viewModel.data.observe(this, Observer {
//do operations based on the value
})
}
View HiltTest.kt
@HiltAndroidTest
class HiltTest {
@get:Rule val hiltRule = HiltAndroidRule(this)
@Inject lateinit var apiService: ApiService
@Test
fun testFoo() {
hiltRule.inject()
}
View MainViewModel.kt
class MainViewModel @ViewModelInject constructor(private val useCase: UseCase) : ViewModel()
View MainActivity.kt
@AndroidEntryPoint
class MainActivity : MyBaseActivity() {
@Inject lateinit var apiService: ApiService
override fun onCreate(savedInstanceState: Bundle?) {
// Injection happens in super.onCreate().
super.onCreate()
// Do something with apiService ...
View AppModule.kt
@Module
@InstallIn(ApplicationComponent::class) // Installs FooModule in the generate ApplicationComponent.
object AppModule {
@Provides
fun provideApiService(): ApiService {...}
}
View AppComponent.kt
@Singleton
@Component(modules = [
ActivityModule::class,
ViewModelModule::class,
NetworkModule::class])
interface AppComponent : AndroidInjector<DaggerApplication> {
@Component.Factory
interface Factory {
fun create(@BindsInstance application: Application): AppComponent
View HiltApp.kt
@HiltAndroidApp
class App : Application()
View App.kt
class App : DaggerApplication() {
override fun applicationInjector() = DaggerAppComponent.factory().create(this)
}