Skip to content

Instantly share code, notes, and snippets.

@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
@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>? =
interface GetTransactionsUseCase {
fun execute(): Single<SimpleResult<List<Transaction>>>
}
interface GetTransactionsUseCase {
operator fun invoke(): Single<SimpleResult<List<Transaction>>>
}
class MyViewModel(
private val getTransactionUseCase: GetTransactionUseCase
) {
fun doSomething() {
//No need for invoke
getTransactionUseCase().subscribeBy {
class MyViewModel(
private val getTransactionUseCase: GetTransactionUseCase
) {
fun doSomething() {
getTransactionUseCase.execute().subscribeBy {
//do something with the result
}
}
}
interface GetTransactionsUseCase {
operator fun invoke(): Single<SimpleResult<List<Transaction>>>
}