Skip to content

Instantly share code, notes, and snippets.

View Raiden18's full-sized avatar
🎯
Focusing

Paul Karpukhin Raiden18

🎯
Focusing
  • Limassol, Cyprus
View GitHub Profile
@Raiden18
Raiden18 / Cart.kt
Last active May 10, 2022 12:19
Information Expert. Domain Classes
data class Cart(
val products: List<Product>
)
@Raiden18
Raiden18 / CartInteractor.kt
Last active May 10, 2022 14:29
Information Expert. Interactor
class CartInteractor(
private val cartRepository: CartRepository
) {
fun getCart() : Cart {
return cartRepository.getCart()
}
}
@Raiden18
Raiden18 / CartViewModel.kt
Last active May 10, 2022 14:28
Information Expert. ViewModel
class CartViewModel(
private val cartInteractor: CartInteractor
): ViewModel() {
private val approximatePriceOfProductsLiveData = MutableLiveData<String>()
init {
val cart = cartInteractor.getCart()
val approximatePriceOfProducts : BigDecimal = TODO("Must be calculated")
val approximatePriceOfProductsLiveData = approximatePriceOfProducts.toString()
@Raiden18
Raiden18 / Cart.kt
Last active May 10, 2022 12:20
Information Expert. Cart with method.
data class Cart(
val products: List<Product>
) {
val productsApproximatePrice : BigDecimal
get() = products.sumOf { it.price }
}
@Raiden18
Raiden18 / CartInteractor.kt
Last active May 10, 2022 14:28
InformationExpert. Interactor with method
class CartInteractor(
private val cartRepository: CartRepository
) {
fun getCart() : Cart {
return cartRepository.getCart()
}
fun getApproximatePriceOfProducts(cart: Cart): BigDecimal {
return cart.products.sumOf{ it.price }
@Raiden18
Raiden18 / AnotherInteractor.kt
Created May 10, 2022 13:15
Information Expert. Interactor And Model class compare
class AnotherInteractor(
private val cartInteractor: CartInteractor,
private val anotherRepository: AnotherRepository
){
fun doSomething() {
val cart = cartInteractor.getCart()
anotherRepository.doSomething(cart.productsApproximatePrice)
// Rest of the code
}
data class Product(
val title: String,
val price: BigDecimal,
val discount: BigDecimal // Value from 0.00% to 1.00%
)
data class Wishlist(
val products: List<Product>
){
@Raiden18
Raiden18 / WithListInformationExpert.kt
Last active May 16, 2022 11:05
WithListInformation
data class Product(
val title: String,
val price: BigDecimal,
val discount: BigDecimal // Value from 0.00% to 1.00%
){
val productPriceWithDiscount : BigDecimal
get() {
val discount = price * discount
return price - discount
@Raiden18
Raiden18 / Button.kt
Last active June 2, 2022 10:12
Button without polymophism
sealed class ButtonSize {
object Small : ButtonSize()
object Medium : ButtonSize()
object Large : ButtonSize()
object Huge : ButtonSize()
data class Custom(val heightDpInt: Int) : ButtonSize()
}
@Composable
fun RenderButton(
@Raiden18
Raiden18 / ButtonSize.kt
Last active June 2, 2022 10:18
Closer look
private fun ButtonSize.getButtonHeight(): Dp {
return when (this) { // Useless checking type
ButtonSize.Small -> 16.dp // code duplication
ButtonSize.Medium -> 24.dp // code duplication
ButtonSize.Large -> 32.dp // code duplication
ButtonSize.Huge -> 40.dp // code duplication
is ButtonSize.Custom -> this.heightDpInt.dp // code duplication
}
}