Skip to content

Instantly share code, notes, and snippets.

View tizisdeepan's full-sized avatar
🎯
Focusing

Deepan tizisdeepan

🎯
Focusing
View GitHub Profile
@tizisdeepan
tizisdeepan / ExampleActivity.kt
Created July 29, 2020 14:39
MVVM with MediatorLiveData
onCreate() {
val viewModel = ... //initialize your view model
viewModel.data.observe(this, Observer {
//do operations based on the value
})
viewModel.getData()
}
@tizisdeepan
tizisdeepan / ExampleActivity.kt
Created July 29, 2020 14:11
MVVM with Kotlin Flow
onCreate() {
val viewModel = ... //initialize your view model
viewModel.getData()
viewModel.data.observe(this, Observer {
//do operations based on the value
})
}
@tizisdeepan
tizisdeepan / ExampleActivity.kt
Last active July 29, 2020 14:14
MVVM with LiveData
onCreate() {
val viewModel = ... //initialize your view model
viewModel.getData()
viewModel.data.observe(this, Observer {
//do operations based on the value
})
}
@HiltAndroidTest
class HiltTest {
@get:Rule val hiltRule = HiltAndroidRule(this)
@Inject lateinit var apiService: ApiService
@Test
fun testFoo() {
hiltRule.inject()
}
class MainViewModel @ViewModelInject constructor(private val useCase: UseCase) : ViewModel()
@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 ...
@Module
@InstallIn(ApplicationComponent::class) // Installs FooModule in the generate ApplicationComponent.
object AppModule {
@Provides
fun provideApiService(): ApiService {...}
}
@Singleton
@Component(modules = [
ActivityModule::class,
ViewModelModule::class,
NetworkModule::class])
interface AppComponent : AndroidInjector<DaggerApplication> {
@Component.Factory
interface Factory {
fun create(@BindsInstance application: Application): AppComponent
@HiltAndroidApp
class App : Application()
class App : DaggerApplication() {
override fun applicationInjector() = DaggerAppComponent.factory().create(this)
}