Skip to content

Instantly share code, notes, and snippets.

@DenisBronx
DenisBronx / Components.kt
Last active September 3, 2019 22:44
Repository pattern gists
// A wrapper for handling failing requests
sealed class Result<T> {
data class Success<T>(val value: T) : Result<T>()
data class Failure<T>(val throwable: Throwable) : Result<T>()
}
// A DataStore for the SharedPreferences
interface ProductPreferences {
@DenisBronx
DenisBronx / Product.kt
Last active August 6, 2020 16:23
Repository Pattern Product gist
// Entity
data class Product(
val id: String,
val name: String,
val price: Price
) {
// Value object
data class Price(
val nowPrice: Double,
val wasPrice: Double
@DenisBronx
DenisBronx / NetworkProduct.kt
Created September 3, 2019 22:50
Repository Pattern Network DTO
// Network DTO
data class NetworkProduct(
@SerializedName("id")
val id: String?,
@SerializedName("name")
val name: String?,
@SerializedName("nowPrice")
val nowPrice: Double?,
@SerializedName("wasPrice")
val wasPrice: Double?
@DenisBronx
DenisBronx / DBProduct.kt
Created September 3, 2019 22:51
Repository Pattern DB DTO
// Database DTO
@Entity(tableName = "Product")
data class DBProduct(
@PrimaryKey
@ColumnInfo(name = "id")
val id: String,
@ColumnInfo(name = "name")
val name: String,
@ColumnInfo(name = "nowPrice")
val nowPrice: Double,
@DenisBronx
DenisBronx / ListMapper.kt
Last active November 8, 2023 20:08
Repository Pattern ListMapper
// Non-nullable to Non-nullable
interface ListMapper<I, O>: Mapper<List<I>, List<O>>
class ListMapperImpl<I, O>(
private val mapper: Mapper<I, O>
) : ListMapper<I, O> {
override fun map(input: List<I>): List<O> {
return input.map { mapper.map(it) }
}
}
@DenisBronx
DenisBronx / NullableInputListMapper.kt
Last active April 15, 2020 22:08
Repository Pattern NullableInputListMapper
// Nullable to Non-nullable
interface NullableInputListMapper<I, O>: Mapper<List<I>?, List<O>>
class NullableInputListMapperImpl<I, O>(
private val mapper: Mapper<I, O>
) : NullableInputListMapper<I, O> {
override fun map(input: List<I>?): List<O> {
return input?.map { mapper.map(it) }.orEmpty()
}
}
@DenisBronx
DenisBronx / NullableOutputListMapper.kt
Last active April 15, 2020 22:08
Repository Pattern NullableOutputListMapper
// Non-nullable to Nullable
interface NullableOutputListMapper<I, O>: Mapper<List<I>, List<O>?>
class NullableOutputListMapperImpl<I, O>(
private val mapper: Mapper<I, O>
) : NullableOutputListMapper<I, O> {
override fun map(input: List<I>): List<O>? {
return if (input.isEmpty()) null else input.map { mapper.map(it) }
}
}
@DenisBronx
DenisBronx / ProductDTO.kt
Created September 3, 2019 22:56
Repository Pattern Mixed DTO
@Entity(tableName = "Product")
data class ProductDTO(
@PrimaryKey
@ColumnInfo(name = "id")
@SerializedName("id")
val id: String?,
@ColumnInfo(name = "name")
@SerializedName("name")
val name: String?,
@ColumnInfo(name = "nowPrice")
@DenisBronx
DenisBronx / Product.kt
Created September 3, 2019 22:56
Repository Pattern Product with new field
// Entity
data class Product(
val id: String,
val name: String,
val price: Price,
val isFavourite: Boolean
) {
// Value object
data class Price(
val nowPrice: Double,
@DenisBronx
DenisBronx / ProductRepositoryImpl.kt
Created September 3, 2019 22:58
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))