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

fix: wrong number of products for lists + analytics events #4648

Merged
merged 6 commits into from
Apr 9, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ sealed class AnalyticsEvent(val category: String, val action: String, val name:

object ShoppingListCreated : AnalyticsEvent("shopping-lists", "created", null, null)

data class ShoppingListProductAdded(val barcode: String) : AnalyticsEvent("shopping-lists", "add_product", barcode, null)

data class ShoppingListProductRemoved(val barcode: String) : AnalyticsEvent("shopping-lists", "remove_product", barcode, null)

object ShoppingListShared : AnalyticsEvent("shopping-lists", "shared", null, null)

object ShoppingListExported : AnalyticsEvent("shopping-lists", "exported", null, null)

data class IngredientAnalysisEnabled(val type: String) : AnalyticsEvent("ingredient-analysis", "enabled", type, null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import openfoodfacts.github.scrachx.openfood.images.ProductImage
import openfoodfacts.github.scrachx.openfood.models.*
import openfoodfacts.github.scrachx.openfood.models.entities.ListedProduct
import openfoodfacts.github.scrachx.openfood.models.entities.ListedProductDao
import openfoodfacts.github.scrachx.openfood.models.entities.ProductLists
import openfoodfacts.github.scrachx.openfood.models.entities.additive.AdditiveName
import openfoodfacts.github.scrachx.openfood.models.entities.allergen.AllergenHelper
import openfoodfacts.github.scrachx.openfood.models.entities.allergen.AllergenName
Expand Down Expand Up @@ -883,20 +884,29 @@ class SummaryProductFragment : BaseFragment(), ISummaryProductPresenter.View {
it.productDetails = product.getProductBrandsQuantityDetails()
it.imageUrl = product.getImageSmallUrl(localeManager.getLanguage())
}
daoSession.listedProductDao.insertOrReplace(product)
addListedProductToDatabase(product, list)
matomoAnalytics.trackEvent(AnalyticsEvent.ShoppingListProductAdded(product.barcode))
dialog.dismiss()
onRefresh()
VaiTon marked this conversation as resolved.
Show resolved Hide resolved
}

// Add listener to text view
val addToNewList = dialog.findViewById<TextView>(R.id.tvAddToNewList)!!
addToNewList.setOnClickListener {
context.startActivity(Intent(context, ProductListsActivity::class.java).apply {
putExtra("product", product)
})
ProductListsActivity.start(context, productToAdd = product)
}
}

private fun addListedProductToDatabase(
product: ListedProduct,
list: ProductLists
) {
daoSession.listedProductDao.insertOrReplace(product)
daoSession.productListsDao.update(list.apply {
products.add(product)
numOfProducts++
})
}

private fun takeMorePicture() {
sendOther = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,16 +372,34 @@ class ProductListActivity : BaseActivity(), SwipeController.Actions {
putExtra(Intent.EXTRA_TEXT, shareUrl)
type = "text/plain"
}, null))

matomoAnalytics.trackEvent(AnalyticsEvent.ShoppingListShared)
}

override fun onRightClicked(position: Int) {
if (adapter.products.isEmpty()) return

val productToRemove = adapter.products[position]
daoSession.listedProductDao.delete(productToRemove)
removeListedProductFromDatabase(productToRemove)

matomoAnalytics.trackEvent(AnalyticsEvent.ShoppingListProductRemoved(
barcode = productToRemove.barcode
))
adapter.remove(productToRemove)
}

private fun removeListedProductFromDatabase(productToRemove: ListedProduct) {
daoSession.listedProductDao.delete(productToRemove)

productList.apply {
numOfProducts -= 1
products.remove(productToRemove)
}

daoSession.listedProductDao.delete(productToRemove)
daoSession.productListsDao.update(productList)
}

override fun onBackPressed() {
setResult(RESULT_OK, Intent().apply { putExtra("update", true) })
super.onBackPressed()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ import java.io.InputStream
import java.io.InputStreamReader
import javax.inject.Inject


@AndroidEntryPoint
class ProductListsActivity : BaseActivity(), SwipeController.Actions {
private var _binding: ActivityProductListsBinding? = null
Expand Down Expand Up @@ -102,12 +101,7 @@ class ProductListsActivity : BaseActivity(), SwipeController.Actions {

binding.fabAdd.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_plus_blue_24, 0, 0, 0)

// FIXME: remove runBlocking
productListsDao = daoSession.productListsDao.defaultIfEmpty(this)
val productLists = productListsDao.loadAll().toMutableList()

adapter = ProductListsAdapter(this, productLists)

adapter = ProductListsAdapter(this, mutableListOf())

binding.productListsRecyclerView.layoutManager = LinearLayoutManager(this)
binding.productListsRecyclerView.adapter = adapter
Expand All @@ -122,6 +116,8 @@ class ProductListsActivity : BaseActivity(), SwipeController.Actions {

binding.productListsRecyclerView.addOnItemTouchListener(
RecyclerItemClickListener(this) { _, position ->
val productLists = adapter.lists

val id = productLists[position].id
val listName = productLists[position].listName
Intent(this, ProductListActivity::class.java).apply {
Expand All @@ -138,6 +134,12 @@ class ProductListsActivity : BaseActivity(), SwipeController.Actions {
binding.fabAdd.setOnClickListener { showCreateListDialog() }
}

private fun refreshLists() {
// FIXME: remove runBlocking
productListsDao = daoSession.productListsDao.defaultIfEmpty(this)
adapter.replaceWith(productListsDao.loadAll())
}

// On Android < 5, the drawableStart attribute in XML will cause a crash
// That's why, it's instead done here in the code
private fun fixFabIcon() {
Expand Down Expand Up @@ -181,7 +183,7 @@ class ProductListsActivity : BaseActivity(), SwipeController.Actions {
matomoAnalytics.trackEvent(AnalyticsEvent.ShoppingListCreated)
val productList = ProductLists(listName, if (productToAdd != null) 1 else 0)

adapter.lists.add(productList)
adapter.add(productList)
productListsDao.insert(productList)

adapter.notifyDataSetChanged()
Expand Down Expand Up @@ -261,6 +263,7 @@ class ProductListsActivity : BaseActivity(), SwipeController.Actions {

public override fun onResume() {
super.onResume()
refreshLists()
binding.bottomNavigation.bottomNavigation.selectNavigationItem(R.id.my_lists)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package openfoodfacts.github.scrachx.openfood.features.productlists
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import openfoodfacts.github.scrachx.openfood.databinding.YourProductListsItemBinding
import openfoodfacts.github.scrachx.openfood.models.entities.ProductLists

class ProductListsAdapter(
internal val context: Context,
val lists: MutableList<ProductLists>
internal val context: Context,
val lists: MutableList<ProductLists>
) : RecyclerView.Adapter<ProductListsViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductListsViewHolder {
val binding = YourProductListsItemBinding.inflate(LayoutInflater.from(context), parent, false)
Expand All @@ -27,12 +28,30 @@ class ProductListsAdapter(

override fun getItemCount() = lists.size

fun add(productList: ProductLists) {
lists.add(productList)
notifyItemInserted(lists.size - 1)
}

fun remove(data: ProductLists) {
val position = lists.indexOf(data)
lists.removeAt(position)
notifyItemRemoved(position)
}

fun replaceWith(newList: MutableList<ProductLists>) {
val oldList = lists.toList()
lists.clear()
lists.addAll(newList)

DiffUtil.calculateDiff(
ProductListsDiffCallback(
oldList,
newList
),
).dispatchUpdatesTo(this)
}

}

class ProductListsViewHolder(val binding: YourProductListsItemBinding) : RecyclerView.ViewHolder(binding.root)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package openfoodfacts.github.scrachx.openfood.features.productlists

import androidx.recyclerview.widget.DiffUtil
import openfoodfacts.github.scrachx.openfood.models.entities.ProductLists

class ProductListsDiffCallback(
private val oldItems: List<ProductLists>,
private val newItems: List<ProductLists>,
) : DiffUtil.Callback() {

override fun getOldListSize(): Int = oldItems.size

override fun getNewListSize(): Int = newItems.size

override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return oldItems[oldItemPosition].id == newItems[newItemPosition].id
}

override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return areItemsTheSame(oldItemPosition, newItemPosition)
}
}