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
View Approach4.kt
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
View Approach3.kt
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
View Approach2.kt
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
View Approach1.kt
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
View BrokenLSP.kt
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
View FakePreferences.kt
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>? =
View OldGetTransactionsUseCase.kt
interface GetTransactionsUseCase {
fun execute(): Single<SimpleResult<List<Transaction>>>
}
View Invoke.kt
interface GetTransactionsUseCase {
operator fun invoke(): Single<SimpleResult<List<Transaction>>>
}
class MyViewModel(
private val getTransactionUseCase: GetTransactionUseCase
) {
fun doSomething() {
//No need for invoke
getTransactionUseCase().subscribeBy {
View MyViewModel.kt
class MyViewModel(
private val getTransactionUseCase: GetTransactionUseCase
) {
fun doSomething() {
getTransactionUseCase.execute().subscribeBy {
//do something with the result
}
}
}
View GetTransactionsUseCase.kt
interface GetTransactionsUseCase {
operator fun invoke(): Single<SimpleResult<List<Transaction>>>
}