Skip to content

Instantly share code, notes, and snippets.

@DenisBronx
Created September 3, 2019 22:58
Show Gist options
  • Save DenisBronx/d21890aef45030b91421bf8cad630414 to your computer and use it in GitHub Desktop.
Save DenisBronx/d21890aef45030b91421bf8cad630414 to your computer and use it in GitHub Desktop.
Repository Pattern Repository getProducts
class ProductRepositoryImpl(
private val productApiService: ProductApiService,
private val productDataMapper: Mapper<DataProduct, Product>,
private val productPreferences: ProductPreferences
) : ProductRepository {
override fun getProducts(): Single<Result<List<Product>>> {
return productApiService.getProducts().map {
when(it) {
is Result.Success -> Result.Success(mapProducts(it.value))
is Result.Failure -> Result.Failure<List<Product>>(it.throwable)
}
}
}
private fun mapProducts(networkProductList: List<NetworkProduct>): List<Product> {
return networkProductList.map {
productDataMapper.map(DataProduct(it, productPreferences.isFavourite(it.id)))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment