Skip to content

Commit

Permalink
Merge pull request #36 from Architect-Coders/feature/view_improvements
Browse files Browse the repository at this point in the history
Feature/view improvements

Signed-off-by: Pablo Jiménez Casado <[email protected]>
  • Loading branch information
PabloJC committed Mar 16, 2020
2 parents f2348f7 + aca740a commit 72e9d47
Show file tree
Hide file tree
Showing 35 changed files with 517 additions and 250 deletions.
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 @@ -22,7 +22,7 @@ import org.koin.android.viewmodel.ext.android.viewModel
class BarcodeReaderActivity : AppCompatActivity(), OnRequestPermissionsResultCallback {

private val cameraPermissionRequester = PermissionRequester(this, Manifest.permission.CAMERA)
private lateinit var adapter: ProductListAdapter
private lateinit var listAdapter: ProductListAdapter

private val viewModel: BarcodeReaderViewModel by currentScope.viewModel(this)

Expand Down Expand Up @@ -56,7 +56,7 @@ class BarcodeReaderActivity : AppCompatActivity(), OnRequestPermissionsResultCal
ProductListAdapter { product ->
viewModel.onProductClicked(product)
}.apply {
adapter = this
listAdapter = this
}
it.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, true)
}
Expand All @@ -73,7 +73,7 @@ class BarcodeReaderActivity : AppCompatActivity(), OnRequestPermissionsResultCal

private fun setProductList(list: List<ItemProduct>) {
rv_product_list.visible()
adapter.productList = list
listAdapter.productList = list
}

override fun onResume() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.pabji.myfridge.ui.common.adapters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import coil.api.load
import com.pabji.myfridge.R
import com.pabji.myfridge.model.ItemProduct
import com.pabji.myfridge.ui.common.extensions.notifyChanges
import kotlinx.android.synthetic.main.item_product_list.view.*
import kotlin.properties.Delegates

class ProductListAdapter(
private val recyclerType: RecyclerType = RecyclerType.LIST,
private val onProductClicked: (ItemProduct) -> Unit
) :
RecyclerView.Adapter<ProductListAdapter.ProductViewHolder>() {

enum class RecyclerType { GRID, LIST }

var productList: List<ItemProduct> by Delegates.observable(emptyList()) { _, oldList, newList ->
notifyChanges(oldList, newList) { o, n -> o.name == n.name }
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
val view = LayoutInflater.from(parent.context).inflate(
when (recyclerType) {
RecyclerType.GRID -> R.layout.item_product_grid
RecyclerType.LIST -> R.layout.item_product_list
}, parent, false
)

return ProductViewHolder(
view,
onProductClicked
)
}

override fun getItemCount() = productList.size

override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {
holder.bind(productList[position])
}

class ProductViewHolder(view: View, private val onProductClicked: (ItemProduct) -> Unit) :
RecyclerView.ViewHolder(view) {

fun bind(product: ItemProduct) {
itemView.run {
tv_product_name.text = product.name
iv_product_picture.load(product.previewUrl) {
crossfade(true)
error(R.mipmap.ic_launcher)
}
card_view.setCardBackgroundColor(
if (product.existInFridge) {
ContextCompat.getColor(context, R.color.lightgreen)
} else {
ContextCompat.getColor(context, R.color.white)
}
)
setOnClickListener { onProductClicked(product) }
}
}
}
}

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
Loading

0 comments on commit 72e9d47

Please sign in to comment.