Skip to content

Instantly share code, notes, and snippets.

@DenisBronx
DenisBronx / Components.kt
Last active September 5, 2020 06:10
Repository Pattern components
// 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 DataSource for the SharedPreferences
@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 / Mapper.kt
Created September 3, 2019 23:02
Repository Pattern Mapper
interface Mapper<I, O> {
fun map(input: I): O
}
@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 / Usecases.kt
Last active August 6, 2020 16:24
Usecases
// User
interface GetCurrentUserUseCase {
operator fun invoke(): Result<User>
}
class GetCurrentUserUseCaseImpl(
private val userRepository: UserRepository
) : GetCurrentUserUseCase {
override fun invoke(): Result<User> = userRepository.getUser()
}
@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 / ListMappersFP.kt
Last active July 26, 2020 10:41
ListMappers in FP
// Non-nullable to Non-nullable
inline fun <I, O> mapList(input: List<I>, mapSingle: (I) -> O): List<O> {
return input.map { mapSingle(it) }
}
// Nullable to Non-nullable
inline fun <I, O> mapNullInputList(input: List<I>?, mapSingle: (I) -> O): List<O> {
return input?.map { mapSingle(it) } ?: emptyList()
}
// Aggregate
data class Album(
val id: String,
val title: String,
val songs: List<Song>
)
// Entity
data class Song(
val id: String,
data class NetworkAlbum(
@SerializedName("id")
val id: String?,
@SerializedName("title")
val title: String?,
@SerializedName("songs")
val songs: List<NetworkSong>?
)
data class NetworkSong(