hi all, is this right way to declare dependency fo...
# android
a
hi all, is this right way to declare dependency for coroutine dispatcher using KOIN , i'm worried about this as Dispatcher.IO and Dispatcher.Default are of same type while Main is of different type. This might be ambiguous
Copy code
val diCoroutineModule = module {
    single { <http://Dispatchers.IO|Dispatchers.IO>  }
    single { Dispatchers.Main }
    single { Dispatchers.Default }
}
Something similar ins HILT
Copy code
@InstallIn(SingletonComponent::class)
@Module
object CoroutinesModule {

    @DefaultDispatcher
    @Provides
    fun providesDefaultDispatcher(): CoroutineDispatcher = Dispatchers.Default

    @IoDispatcher
    @Provides
    fun providesIoDispatcher(): CoroutineDispatcher = <http://Dispatchers.IO|Dispatchers.IO>

    @MainDispatcher
    @Provides
    fun providesMainDispatcher(): CoroutineDispatcher = Dispatchers.Main

    @MainImmediateDispatcher
    @Provides
    fun providesMainImmediateDispatcher(): CoroutineDispatcher = Dispatchers.Main.immediate
}
Copy code
val diUseCaseModule = module {
    factory { LoginUseCase(get(), dispatcher = <http://Dispatchers.IO|Dispatchers.IO>) }
}
else i will have to be explicit about the dispatcher in the UseCaseModule
b
im a fan of the following:
Copy code
data class AppCoroutineDispatchers(
    val io: CoroutineDispatcher,
    val computation: CoroutineDispatcher,
    val main: CoroutineDispatcher
)
☝️ 2
Copy code
@Singleton
@Provides
fun provideCoroutineDispatchers() = AppCoroutineDispatchers(
    io = <http://Dispatchers.IO|Dispatchers.IO>,
    computation = Dispatchers.Default,
    main = Dispatchers.Main
)
a
Sorry for my ignorance, @brandonmcansh, the code you gave, is it KOIN ?
b
its Dagger, but could be applied the same i'd think. the core concept should be translatable
single { AppCoroutineDispatchers(io = .., computation = ..., main = ....) }
a
in Koin it is little bit different however .
Copy code
val diUseCaseModule = module {
    factory { LoginUseCase(get(), dispatcher = get()) }
}
dependency get auto resolved just be this code
b
ah true, been a minute since I used koin
a
when i define this
Copy code
val diCoroutineModule = module {
    single { <http://Dispatchers.IO|Dispatchers.IO>  }
    single { Dispatchers.Main }
    single { Dispatchers.Default }
}
it grammar is ambiguous as get() can resolve to IO and Default that is what i'm confused about
Login use case appears like this
Copy code
class LoginUseCase(private val authRepository: AuthRepository, private val dispatcher: CoroutineDispatcher)
b
could you
interface IODispatcher: CoroutineDispatcher
and couple it w/ a factory
to create IODispatcher
a
hm, let me try, main intent here is id don't want to couple the nature of the dispatcher to the loginusecase constructor, it should be able to accept any CoroutineDispatcher. Only way to solve this is to declare the dependency explicitly, like this.
Copy code
val diUseCaseModule = module {
    factory { LoginUseCase(get(), dispatcher = <http://Dispatchers.IO|Dispatchers.IO>) }
}
b
yeah I see where you're going
if you go the data class route and inject that, then your usecase can just do
withContext(<http://dispatchers.io|dispatchers.io>) { }
a
i see
k
Copy code
val coroutines = module {
    single(named("Background")) { Dispatchers.Default}
    single(named("IO")) { <http://Dispatchers.IO|Dispatchers.IO>}
    single<CoroutineDispatcher> (named("Main")) { Dispatchers.Main}
}
👍 2
Copy code
val mainDispatcher: CoroutineDispatcher by inject(named("Main"))
    val defaultDispatcher: CoroutineDispatcher by inject(named("Background"))
b
Ah nice didn't know koin had named support