From 9d50f8749e2630e79d1c5512d346b326a0f85855 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Jime=CC=81nez=20Casado?= Date: Tue, 10 Mar 2020 23:01:17 +0100 Subject: [PATCH 1/5] Modify ProductDetailActivity MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Pablo Jiménez Casado --- .../java/com/pabji/myfridge/di/Modules.kt | 11 +- .../ui/productDetail/ProductDetailActivity.kt | 29 ++- .../productDetail/ProductDetailViewModel.kt | 25 ++- .../res/layout/activity_product_detail.xml | 175 +++++++++++++----- app/src/main/res/values/colors.xml | 3 + app/src/main/res/values/strings.xml | 1 + build.gradle | 1 + .../src/main/java/com/pabji/domain/Product.kt | 2 +- 8 files changed, 187 insertions(+), 60 deletions(-) diff --git a/app/src/main/java/com/pabji/myfridge/di/Modules.kt b/app/src/main/java/com/pabji/myfridge/di/Modules.kt index a9cb871..512609e 100644 --- a/app/src/main/java/com/pabji/myfridge/di/Modules.kt +++ b/app/src/main/java/com/pabji/myfridge/di/Modules.kt @@ -68,8 +68,17 @@ val scopesModule = module { } scope(named()) { - viewModel { (product: ItemProduct) -> ProductDetailViewModel(product, get(), get(), get()) } + viewModel { (product: ItemProduct) -> + ProductDetailViewModel( + product, + get(), + get(), + get(), + get() + ) + } scoped { GetProductDetail(get()) } + scoped { RemoveProduct(get()) } scoped { SaveProduct(get()) } } diff --git a/app/src/main/java/com/pabji/myfridge/ui/productDetail/ProductDetailActivity.kt b/app/src/main/java/com/pabji/myfridge/ui/productDetail/ProductDetailActivity.kt index 6925ac2..cad0936 100644 --- a/app/src/main/java/com/pabji/myfridge/ui/productDetail/ProductDetailActivity.kt +++ b/app/src/main/java/com/pabji/myfridge/ui/productDetail/ProductDetailActivity.kt @@ -3,14 +3,13 @@ package com.pabji.myfridge.ui.productDetail import android.os.Bundle import android.widget.Toast import androidx.appcompat.app.AppCompatActivity +import androidx.core.content.ContextCompat import androidx.lifecycle.Observer import coil.api.load import coil.size.Scale import com.pabji.domain.Product import com.pabji.myfridge.R import com.pabji.myfridge.model.ItemProduct -import com.pabji.myfridge.ui.common.extensions.gone -import com.pabji.myfridge.ui.common.extensions.setVisible import com.pabji.myfridge.ui.common.extensions.visible import com.pabji.myfridge.ui.productDetail.ProductDetailViewModel.UiModel import kotlinx.android.synthetic.main.activity_product_detail.* @@ -39,19 +38,30 @@ class ProductDetailActivity : AppCompatActivity() { } is UiModel.Content -> showProduct(viewState.product) is UiModel.ProductSaved -> showSaved(viewState.product) + is UiModel.ProductRemoved -> showRemoved(viewState.product) UiModel.Error -> showError() } } + private fun showRemoved(product: Product) { + Toast.makeText( + this, + "${product.name} has been removed from your fridge", + Toast.LENGTH_SHORT + ) + .show() + setButton(false) + } + private fun showSaved(product: Product) { Toast.makeText(this, "${product.name} has been saved in your fridge", Toast.LENGTH_SHORT) .show() - btn_add.gone() + setButton(true) } private fun showProduct(product: Product) { product.run { - btn_add.setVisible(!existInFridge) + setButton(existInFridge) setProductImage(imageUrl) setProductName(name) setGenericName(genericName) @@ -61,6 +71,17 @@ class ProductDetailActivity : AppCompatActivity() { } } + private fun setButton(existInFridge: Boolean) { + if (existInFridge) { + btn_add.setBackgroundColor(ContextCompat.getColor(this, R.color.colorAccent)) + btn_add.text = getString(R.string.remove_from_fridge) + } else { + btn_add.setBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimary)) + btn_add.text = getString(R.string.add_to_the_fridge) + } + btn_add.visible() + } + private fun setStores(stores: String) { if (stores.isNotEmpty()) { tv_stores.text = stores diff --git a/app/src/main/java/com/pabji/myfridge/ui/productDetail/ProductDetailViewModel.kt b/app/src/main/java/com/pabji/myfridge/ui/productDetail/ProductDetailViewModel.kt index 5fd4923..550fc61 100644 --- a/app/src/main/java/com/pabji/myfridge/ui/productDetail/ProductDetailViewModel.kt +++ b/app/src/main/java/com/pabji/myfridge/ui/productDetail/ProductDetailViewModel.kt @@ -8,6 +8,7 @@ import com.pabji.myfridge.model.ItemProduct import com.pabji.myfridge.model.toProduct import com.pabji.myfridge.ui.common.BaseViewModel import com.pabji.usecases.GetProductDetail +import com.pabji.usecases.RemoveProduct import com.pabji.usecases.SaveProduct import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.launch @@ -16,9 +17,11 @@ class ProductDetailViewModel( private var itemProduct: ItemProduct?, private val getProductDetail: GetProductDetail, private val saveProduct: SaveProduct, + private val removeProduct: RemoveProduct, uiDispatcher: CoroutineDispatcher ) : BaseViewModel(uiDispatcher) { + private var product: Product? = null private val _model = MutableLiveData() val model: LiveData get() { @@ -30,16 +33,18 @@ class ProductDetailViewModel( object Loading : UiModel() data class Content(val product: Product) : UiModel() data class ProductSaved(val product: Product) : UiModel() + data class ProductRemoved(val product: Product) : UiModel() object Error : UiModel() } - private fun loadProduct(product: ItemProduct?) { + private fun loadProduct(itemProduct: ItemProduct?) { launch { _model.value = UiModel.Loading - _model.value = product?.run { + _model.value = itemProduct?.run { getProductDetail(toProduct()).fold({ UiModel.Error }, { + product = it UiModel.Content(it) }) } ?: UiModel.Error @@ -48,10 +53,18 @@ class ProductDetailViewModel( fun onClickButtonAdd() { launch { - when (val value = _model.value) { - is UiModel.Content -> { - saveProduct(value.product) - _model.value = UiModel.ProductSaved(value.product) + product?.run { + _model.value = when { + existInFridge -> { + removeProduct(this) + existInFridge = false + UiModel.ProductRemoved(this) + } + else -> { + saveProduct(this) + existInFridge = true + UiModel.ProductSaved(this) + } } } } diff --git a/app/src/main/res/layout/activity_product_detail.xml b/app/src/main/res/layout/activity_product_detail.xml index 012e98c..941b9ad 100644 --- a/app/src/main/res/layout/activity_product_detail.xml +++ b/app/src/main/res/layout/activity_product_detail.xml @@ -3,52 +3,146 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" - android:layout_height="match_parent"> + android:layout_height="match_parent" + android:background="@color/lightgreen"> - + + - + android:layout_height="match_parent"> + + + + + + + + + - + - + + + + + + +