Untitled

 avatar
unknown
plain_text
2 years ago
1.2 kB
5
Indexable
@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)
}

Editor is loading...