Skip to content

Instantly share code, notes, and snippets.

@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 / FakePreferences.kt
Last active September 19, 2022 20:45
Fake Shared Preferences class that uses an in-memory implementation
class FakePreferences : SharedPreferences {
private val map: MutableMap<String, Any?> = mutableMapOf()
private val listeners: MutableList<SharedPreferences.OnSharedPreferenceChangeListener> = mutableListOf()
override fun getAll(): MutableMap<String, *> = map
override fun getString(key: String, default: String?): String? = map[key] as String? ?: default
@Suppress("UNCHECKED_CAST")
override fun getStringSet(key: String, default: MutableSet<String>?): MutableSet<String>? =
@DenisBronx
DenisBronx / Approach4.kt
Created August 23, 2022 11:08
Avoid Use Case Boilerplate Approach 4
data class SomeModel(val id: String, val name: String)
interface SomeModelRepository {
suspend fun getSomeModels(): Answer<List<SomeModel>, String>
}
fun interface GetSomeModelUseCase : suspend () -> Answer<List<SomeModel>, String>
class SomeViewModel(private val getSomeModelUseCase: GetSomeModelUseCase) : ViewModel() {
fun load() {
@DenisBronx
DenisBronx / Approach3.kt
Created August 23, 2022 10:56
Avoid Use Case Boilerplate Approach 3
data class SomeModel(val id: String, val name: String)
interface SomeModelRepository {
suspend fun getSomeModels(): Answer<List<SomeModel>, String>
}
typealias GetSomeModelUseCase = suspend () -> Answer<List<SomeModel>, String>
class SomeViewModel(private val getSomeModelUseCase: GetSomeModelUseCase) : ViewModel() {
fun load() {
@DenisBronx
DenisBronx / Approach2.kt
Created August 23, 2022 10:53
Avoid Use Case Boilerplate Approach 2
data class SomeModel(val id: String, val name: String)
interface SomeModelRepository {
suspend fun getSomeModels(): Answer<List<SomeModel>, String>
}
interface GetSomeModelUseCase {
suspend operator fun invoke(): Answer<List<SomeModel>, String>
}
@DenisBronx
DenisBronx / Approach1.kt
Created August 23, 2022 10:51
Avoid Use Case Boilerplate Approach 1
data class SomeModel(val id: String, val name: String)
interface SomeModelRepository {
suspend fun getSomeModels(): Answer<List<SomeModel>, String>
}
class SomeViewModel(private val someModelRepository: SomeModelRepository) : ViewModel() {
fun load() {
viewModelScope.launch {
val result = someModelRepository.getSomeModels()
@DenisBronx
DenisBronx / BrokenLSP.kt
Last active August 23, 2022 10:50
LSP violated
abstract class Employee {
abstract fun getSalary(): String
abstract fun getLineManager(): Employee
}
class SeniorAndroidDeveloper(
private val leadAndroidDeveloper: LeadAndroidDeveloper
class FPGetTransactionUseCaseFactory(
private val userRepository: UserRepository,
private val transactionRepository: TransactionRepository
) {
fun makeGetTransactionsUseCase(): GetTransactionsUseCase {
return {
getTransactions(
userRepository::getUser,
transactionRepository
class MyDaggerModule {
@Provides
fun provideGetTransactionsUseCase(
private val userRepository: UserRepository,
private val transactionRepository: TransactionRepository
): GetTransactionsUseCase {
return {
getTransactions(
userRepository::getUser,
interface GetTransactionsUseCase {
operator fun invoke(): Single<SimpleResult<List<Transaction>>>
}