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 / 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 / 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 / ProductRepositoryImpl.kt
Created September 3, 2019 23:01
Repository Pattern Repository getWishlist
class ProductRepositoryImpl(
private val productApiService: ProductApiService,
private val productDataMapper: Mapper<DataProduct, Product>,
private val productPreferences: ProductPreferences
) : ProductRepository {
override fun getWishlist(): Single<Result<List<Product>>> {
return productApiService.getWishlist(productPreferences.getFavourites()).map {
when (it) {
is Result.Success -> Result.Success(mapWishlist(it.value))
@DenisBronx
DenisBronx / TransactionsViewModel.kt
Last active October 15, 2019 13:28
ViewModel without usecases
class TransactionsViewModelImpl(
private val userRepository: UserRepository,
private val transactionRepository: TransactionRepository
) : TransactionsViewModel, ViewModel() {
private val compositeDisposable = CompositeDisposable()
override val transactions = MutableLiveData<List<Transaction>>()
override val showProgress = MutableLiveData<Boolean>()
override val showError = MutableLiveData<Boolean>()
@DenisBronx
DenisBronx / Dependencies.kt
Created October 15, 2019 13:34
Dependencies for with usecase vs without usecase
// 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>()
}
// The models (simplified)
@DenisBronx
DenisBronx / TransactionsViewModelWIthUseCase.kt
Created October 15, 2019 13:50
ViewModel with usecases
class TransactionsViewModelImpl(
private val getUserTransactionsUseCase: GetUserTransactionsUseCase
) : TransactionsViewModel, ViewModel() {
private val compositeDisposable = CompositeDisposable()
override val transactions = MutableLiveData<List<Transaction>>()
override val showProgress = MutableLiveData<Boolean>()
override val showError = MutableLiveData<Boolean>()
override val showContent = MutableLiveData<Boolean>()
@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 / 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()
}
}