Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.2 kB
2
Indexable
Never
@Module
@InstallIn(SingletonComponent::class)
object CategoryModule {

    @Provides
    @Singleton
    fun provideProductDataSource(productsService: ProductsService): CategoryRemoteDataSource =
        CategoryRemoteDataSourceImpl(productsService)

    @Provides
    @Singleton
    fun provideCategoryRepository(
        productDataSource: CategoryRemoteDataSource,
        @IODispatcher ioDispatcher: CoroutineDispatcher,
    ): CategoryRepository = CategoryRepositoryImpl(productDataSource, ioDispatcher)

}

class CategoryRemoteDataSourceImpl(private val productsService: ProductsService) : CategoryRemoteDataSource {
    override suspend fun getCategoriesList(page: Int, orderBy: String): List<CategoryItem> =
        productsService.getCategories().map { mapCategoryDTOToCategory(it) }
}

class CategoryRepositoryImpl(
    private val categoryRemoteDataSource: CategoryRemoteDataSource,
    private val dispatcher: CoroutineDispatcher,
) : CategoryRepository {
    override fun getTopRatedProducts(page: Int, orderBy: String): Flow<ResponseState<List<CategoryItem>>> =
        flow { emit(categoryRemoteDataSource.getCategoriesList(page, orderBy)) }.asResult().flowOn(dispatcher)
}