Skip to content

Commit

Permalink
feat: add in cart total text to bottom nav button (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
mchuangatmp authored Apr 7, 2022
1 parent 9adefa6 commit d517f40
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.mparticle.example.higgsshopsampleapp.activities

import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
Expand All @@ -15,6 +16,7 @@ import com.mparticle.MParticle
import com.mparticle.example.higgsshopsampleapp.R
import com.mparticle.example.higgsshopsampleapp.databinding.ActivityMainBinding
import com.mparticle.example.higgsshopsampleapp.utils.Constants
import com.mparticle.example.higgsshopsampleapp.viewmodels.CartViewModel


class MainActivity : AppCompatActivity() {
Expand Down Expand Up @@ -83,4 +85,13 @@ class MainActivity : AppCompatActivity() {
fun setActionBarTitle(title: String?) {
getSupportActionBar()?.setTitle(title)
}

fun updateBottomNavCartButtonText(size: Int) {
val bottomNavigation: BottomNavigationView = findViewById(R.id.bottom_nav)
val item: MenuItem = bottomNavigation.menu.findItem(R.id.navigation_cart)
when(size) {
0 -> item.title = getString(R.string.nav_cart)
else -> item.title = getString(R.string.nav_cart) + " (${size})"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ class CartFragment : Fragment() {
_binding?.tvCartSubtotalPrice?.text = "$${subTotalPrice}"
})

cartViewModel.cartTotalLiveData.observe(
viewLifecycleOwner,
{ total ->
(activity as MainActivity).updateBottomNavCartButtonText(total)
})

cartViewModel.cartResponseLiveData.observe(viewLifecycleOwner, Observer { items ->
Log.d(TAG, "Size: " + items?.size)

Expand All @@ -61,13 +67,15 @@ class CartFragment : Fragment() {
_binding?.tvCartSubtotalPrice?.text = getString(R.string.detail_price)
btnCTA.isClickable = false
btnCTA.alpha = 0.3F
(activity as MainActivity).updateBottomNavCartButtonText(0)
return@Observer
}
btnCTA.setBackgroundResource(R.drawable.rounded_button)
_binding?.tvCart0?.visibility = View.GONE
_binding?.rvCartList?.visibility = View.VISIBLE
val adapter = CartItemsAdapter()
adapter.list = items.toMutableList()
cartViewModel.getTotalCartItems(this.requireContext())

_binding?.rvCartList?.let { listView ->
if (listView.adapter == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,20 @@ class ShopFragment : Fragment() {
}
})
shopViewModel.getProducts(this.requireContext())

shopViewModel.cartTotalSizeResponseLiveData.observe(viewLifecycleOwner, Observer { total ->
Log.d(TAG, "Total: " + total)

if (total == null || total == 0) {
(activity as MainActivity).updateBottomNavCartButtonText(0)
return@Observer
}
(activity as MainActivity).updateBottomNavCartButtonText(total)
})
}

override fun onResume() {
super.onResume()
shopViewModel.getTotalCartItems(this.requireContext())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import java.math.BigDecimal
class CartViewModel : ViewModel() {
val cartResponseLiveData = MutableLiveData<List<CartItemEntity>>()
val cartSubtotalPriceLiveData = MutableLiveData<BigDecimal>()
val cartTotalLiveData = MutableLiveData<Int>()
private val cartRepository = CartRepository()
private val TAG = "CartViewModel"

Expand All @@ -27,6 +28,17 @@ class CartViewModel : ViewModel() {
}
}

fun getTotalCartItems(context: Context) {
viewModelScope.launch {
val items = cartRepository.getCartItems(context)
var total = 0
items.forEach {
total += it.quantity
}
cartTotalLiveData.value = total
}
}

fun getSubtotalPrice(context: Context) {
viewModelScope.launch {
val items = cartRepository.getCartItems(context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ import android.content.Context
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.mparticle.example.higgsshopsampleapp.repositories.CartRepository
import com.mparticle.example.higgsshopsampleapp.repositories.ProductsRepository
import com.mparticle.example.higgsshopsampleapp.repositories.network.models.Product
import kotlinx.coroutines.launch

class ShopViewModel() : ViewModel() {
val inventoryResponseLiveData = MutableLiveData<List<Product>>()
private val TAG = "ProductsViewModel"

val inventoryResponseLiveData = MutableLiveData<List<Product>>()
val cartTotalSizeResponseLiveData = MutableLiveData<Int>()

private val cartRepository = CartRepository()
private val repository = ProductsRepository()

fun getProducts (context: Context) {
Expand All @@ -20,8 +24,14 @@ class ShopViewModel() : ViewModel() {
}
}

override fun onCleared() {
super.onCleared()
//repository.cancelCoroutinesJob()
fun getTotalCartItems(context: Context) {
viewModelScope.launch {
val items = cartRepository.getCartItems(context)
var total = 0
items.forEach {
total += it.quantity
}
cartTotalSizeResponseLiveData.value = total
}
}
}

0 comments on commit d517f40

Please sign in to comment.