Skip to content

Instantly share code, notes, and snippets.

View coroutineDispatcher's full-sized avatar
💻
Androiding

Stavro Xhardha coroutineDispatcher

💻
Androiding
View GitHub Profile
@Module(includes = [InterceptorModule::class])
class NetworkModule {
@Provides
@ApplicationScope
fun provideTreasureApi(retrofit: Retrofit): TreasureApi = retrofit.create(TreasureApi::class.java)
@Provides
@ApplicationScope
fun provideRetrofit(client: OkHttpClient): Retrofit = Retrofit.Builder()
@coroutineDispatcher
coroutineDispatcher / ApplicationScope.kt
Created May 1, 2019 13:58
DaggerBasicSetupAppScope
@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class ApplicationScope
@Module
class InterceptorModule {
@Provides
@ApplicationScope
fun provideOkHttpClient(interceptor: HttpLoggingInterceptor): OkHttpClient {
return OkHttpClient.Builder()
.addInterceptor(interceptor)
.build()
}
@coroutineDispatcher
coroutineDispatcher / AppComponent.kt
Last active May 1, 2019 16:09
Application Component
@ApplicationScope
@Component(modules = [NetworkModule::class, SchedulersModule::class])
interface PocketTreasureComponent {
fun getTreasureApi(): TreasureApi
fun getSchedulers(): CoroutineDispatcher
}
@coroutineDispatcher
coroutineDispatcher / PocketTreasureApplication.kt
Last active May 1, 2019 16:08
Application without Dagger implementation
class PocketTreasureApplication : Application() {
override fun onCreate() {
super.onCreate()
}
}
@coroutineDispatcher
coroutineDispatcher / PocketTreasureApplication.kt
Created May 1, 2019 14:26
Application Class After I created app dependencies
class PocketTreasureApplication : Application() {
private lateinit var pocketTreasureComponent: PocketTreasureComponent
override fun onCreate() {
super.onCreate()
pocketTreasureComponent = DaggerPocketTreasureComponent.builder().build()
}
fun getPocketTreasureComponent() = pocketTreasureComponent
}
@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class FragmentScope
@Module
class NamesFragmentModule(private val context: Context) {
@Provides
@FragmentScope
fun provideNamesFragmentContext(): Context = context
@Provides
@FragmentScope
fun provideViewModelFactory(
@coroutineDispatcher
coroutineDispatcher / NamesFragmentComponent.kt
Created May 1, 2019 14:50
Define component for the fragment
@FragmentScope
@Component(modules = [NamesFragmentModule::class], dependencies = [PocketTreasureComponent::class])
interface NamesFragmentComponent {
fun inject(namesFragment: NamesFragment)
}
@coroutineDispatcher
coroutineDispatcher / NamesViewModelProviderFactory.kt
Created May 1, 2019 15:20
Creating ViewModel provider factory for my fragment .
class NamesViewModelProviderFactory(
private val repository: NamesRepository,
private val coroutineDispatcher: CoroutineDispatcher
) : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T = NamesViewModel(repository, coroutineDispatcher) as T
}