Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/view improvements #36

Merged
merged 5 commits into from
Mar 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/src/androidTest/java/com/pabji/myfridge/ui/UITest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class UITest : KoinTest {
)
)

onView(withId(R.id.btn_add)).perform(click())

onView(isRoot()).perform(pressBack())

onView(withId(R.id.fab)).perform(click())
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@
</activity>
<activity
android:name="com.pabji.myfridge.ui.productDetail.ProductDetailActivity"
android:parentActivityName="com.pabji.myfridge.ui.main.MainActivity" />
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name="com.pabji.myfridge.ui.newProduct.NewProductActivity"
android:parentActivityName="com.pabji.myfridge.ui.main.MainActivity" />
<activity android:name=".ui.barcode.BarcodeReaderActivity" />
android:name=".ui.barcode.BarcodeReaderActivity"
android:theme="@style/AppTheme.NoActionBar" />
</application>

</manifest>
11 changes: 10 additions & 1 deletion app/src/main/java/com/pabji/myfridge/di/Modules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,17 @@ val scopesModule = module {
}

scope(named<ProductDetailActivity>()) {
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()) }
}

Expand Down
4 changes: 1 addition & 3 deletions app/src/main/java/com/pabji/myfridge/model/ItemProduct.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.pabji.domain.Product
import java.io.Serializable

data class ItemProduct(
val id: Long? = null,
val name: String = "",
val previewUrl: String = "",
val existInFridge: Boolean = false,
Expand All @@ -13,12 +12,11 @@ data class ItemProduct(

fun Product.toItemProduct(): ItemProduct =
ItemProduct(
id,
name,
previewUrl,
existInFridge,
barcode
)

fun ItemProduct.toProduct(): Product =
Product(id = id, name = name, barcode = barcode)
Product(name = name, barcode = barcode)
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ class RoomDataSource(database: RoomDatabase) : LocalDatasource {
} ?: Either.Left(DetailError)
}

override suspend fun getProductById(productId: Long) =
withContext(Dispatchers.IO) {
productDao.getProductById(productId)?.run {
Either.Right(toProduct())
} ?: Either.Left(DetailError)
}

override suspend fun getProductList() =
withContext(Dispatchers.IO) { productDao.getAll().map { it.toProduct() } }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ interface ProductDao {
@Query("SELECT * FROM products")
suspend fun getAll(): List<ProductEntity>

@Query("SELECT * FROM products WHERE id = :productId")
suspend fun getProductById(productId: Long): ProductEntity?

@Query("SELECT * FROM products WHERE barcode = :barcode")
suspend fun getProductByBarcode(barcode: String): ProductEntity?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import com.pabji.myfridge.model.common.extensions.getListByDelimit

@Entity(tableName = "products")
data class ProductEntity(
@PrimaryKey(autoGenerate = true) val id: Long?,
val barcode: String,
@PrimaryKey val barcode: String,
val name: String,
val previewUrl: String,
val imageUrl: String,
Expand All @@ -25,7 +24,6 @@ data class ProductEntity(

internal fun ProductEntity.toProduct() =
Product(
id,
barcode,
name,
previewUrl,
Expand All @@ -43,7 +41,6 @@ internal fun ProductEntity.toProduct() =
)

internal fun Product.toProductEntity() = ProductEntity(
id,
barcode,
name,
previewUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ 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.*
Expand All @@ -27,31 +27,58 @@ class ProductDetailActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_product_detail)
setTitle(R.string.product_detail_title)

setSupportActionBar(toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setDisplayShowHomeEnabled(true)

viewModel.model.observe(this, Observer(::updateUI))
btn_add.setOnClickListener { viewModel.onClickButtonAdd() }
btn_add.setOnClickListener { viewModel.onClickButton() }
}

override fun onSupportNavigateUp(): Boolean {
onBackPressed()
return true
}

private fun updateUI(viewState: UiModel?) {
when (viewState) {
UiModel.Loading -> {
}
is UiModel.Content -> showProduct(viewState.product)
is UiModel.BasicContent -> showBasicProduct(viewState.product)
is UiModel.FullContent -> showProduct(viewState.product)
is UiModel.ProductSaved -> showSaved(viewState.product)
is UiModel.ProductRemoved -> showRemoved(viewState.product)
UiModel.Error -> showError()
}
}

private fun showBasicProduct(product: ItemProduct?) {
product?.run {
title = name
setButton(existInFridge)
setProductImage(previewUrl)
setProductName(name)
}
}

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)
Expand All @@ -61,23 +88,42 @@ 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
ll_stores.visible()
} else {
ll_stores.gone()
}
}

private fun setCategories(categories: String) {
if (categories.isNotEmpty()) {
tv_categories.text = categories
ll_categories.visible()
} else {
ll_categories.gone()
}
}

private fun setIngredientsText(ingredientsText: String) {
tv_ingredients.text = ingredientsText
ll_ingredients.visible()
if (ingredientsText.isNotEmpty()) {
tv_ingredients.text = ingredientsText
ll_ingredients.visible()
} else {
ll_ingredients.gone()
}
}

private fun setGenericName(genericName: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<UiModel>()
val model: LiveData<UiModel>
get() {
Expand All @@ -27,31 +30,41 @@ class ProductDetailViewModel(
}

sealed class UiModel {
object Loading : UiModel()
data class Content(val product: Product) : UiModel()
data class BasicContent(val product: ItemProduct?) : UiModel()
data class FullContent(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 = UiModel.BasicContent(itemProduct)
_model.value = itemProduct?.run {
getProductDetail(toProduct()).fold({
UiModel.Error
}, {
UiModel.Content(it)
product = it
UiModel.FullContent(it)
})
} ?: UiModel.Error
}
}

fun onClickButtonAdd() {
fun onClickButton() {
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)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class SearchProductsFragment : BaseFragment() {
setHasOptionsMenu(true)
}

override fun onResume() {
super.onResume()
viewModel.onSearch()
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ class SearchProductsViewModel(
) : BaseViewModel(uiDispatcher) {

private val _model = MutableLiveData<UiModel>()
val model: LiveData<UiModel>
get() {
if (_model.value == null) onSearch()
return _model
}
val model: LiveData<UiModel> = _model

private val _navigation = MutableLiveData<Event<ItemProduct>>()
val navigation: LiveData<Event<ItemProduct>> = _navigation
Expand Down
Loading