Skip to content

Commit d517f40

Browse files
authored
feat: add in cart total text to bottom nav button (#38)
1 parent 9adefa6 commit d517f40

File tree

5 files changed

+60
-4
lines changed

5 files changed

+60
-4
lines changed

core-sdk-samples/higgs-shop-sample-app/app/src/main/kotlin/com/mparticle/example/higgsshopsampleapp/activities/MainActivity.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.mparticle.example.higgsshopsampleapp.activities
22

33
import android.content.Intent
44
import android.os.Bundle
5+
import android.view.MenuItem
56
import androidx.activity.result.ActivityResultLauncher
67
import androidx.activity.result.contract.ActivityResultContracts
78
import androidx.appcompat.app.AppCompatActivity
@@ -15,6 +16,7 @@ import com.mparticle.MParticle
1516
import com.mparticle.example.higgsshopsampleapp.R
1617
import com.mparticle.example.higgsshopsampleapp.databinding.ActivityMainBinding
1718
import com.mparticle.example.higgsshopsampleapp.utils.Constants
19+
import com.mparticle.example.higgsshopsampleapp.viewmodels.CartViewModel
1820

1921

2022
class MainActivity : AppCompatActivity() {
@@ -83,4 +85,13 @@ class MainActivity : AppCompatActivity() {
8385
fun setActionBarTitle(title: String?) {
8486
getSupportActionBar()?.setTitle(title)
8587
}
88+
89+
fun updateBottomNavCartButtonText(size: Int) {
90+
val bottomNavigation: BottomNavigationView = findViewById(R.id.bottom_nav)
91+
val item: MenuItem = bottomNavigation.menu.findItem(R.id.navigation_cart)
92+
when(size) {
93+
0 -> item.title = getString(R.string.nav_cart)
94+
else -> item.title = getString(R.string.nav_cart) + " (${size})"
95+
}
96+
}
8697
}

core-sdk-samples/higgs-shop-sample-app/app/src/main/kotlin/com/mparticle/example/higgsshopsampleapp/fragments/CartFragment.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ class CartFragment : Fragment() {
5252
_binding?.tvCartSubtotalPrice?.text = "$${subTotalPrice}"
5353
})
5454

55+
cartViewModel.cartTotalLiveData.observe(
56+
viewLifecycleOwner,
57+
{ total ->
58+
(activity as MainActivity).updateBottomNavCartButtonText(total)
59+
})
60+
5561
cartViewModel.cartResponseLiveData.observe(viewLifecycleOwner, Observer { items ->
5662
Log.d(TAG, "Size: " + items?.size)
5763

@@ -61,13 +67,15 @@ class CartFragment : Fragment() {
6167
_binding?.tvCartSubtotalPrice?.text = getString(R.string.detail_price)
6268
btnCTA.isClickable = false
6369
btnCTA.alpha = 0.3F
70+
(activity as MainActivity).updateBottomNavCartButtonText(0)
6471
return@Observer
6572
}
6673
btnCTA.setBackgroundResource(R.drawable.rounded_button)
6774
_binding?.tvCart0?.visibility = View.GONE
6875
_binding?.rvCartList?.visibility = View.VISIBLE
6976
val adapter = CartItemsAdapter()
7077
adapter.list = items.toMutableList()
78+
cartViewModel.getTotalCartItems(this.requireContext())
7179

7280
_binding?.rvCartList?.let { listView ->
7381
if (listView.adapter == null) {

core-sdk-samples/higgs-shop-sample-app/app/src/main/kotlin/com/mparticle/example/higgsshopsampleapp/fragments/ShopFragment.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,20 @@ class ShopFragment : Fragment() {
7878
}
7979
})
8080
shopViewModel.getProducts(this.requireContext())
81+
82+
shopViewModel.cartTotalSizeResponseLiveData.observe(viewLifecycleOwner, Observer { total ->
83+
Log.d(TAG, "Total: " + total)
84+
85+
if (total == null || total == 0) {
86+
(activity as MainActivity).updateBottomNavCartButtonText(0)
87+
return@Observer
88+
}
89+
(activity as MainActivity).updateBottomNavCartButtonText(total)
90+
})
91+
}
92+
93+
override fun onResume() {
94+
super.onResume()
95+
shopViewModel.getTotalCartItems(this.requireContext())
8196
}
8297
}

core-sdk-samples/higgs-shop-sample-app/app/src/main/kotlin/com/mparticle/example/higgsshopsampleapp/viewmodels/CartViewModel.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import java.math.BigDecimal
1818
class CartViewModel : ViewModel() {
1919
val cartResponseLiveData = MutableLiveData<List<CartItemEntity>>()
2020
val cartSubtotalPriceLiveData = MutableLiveData<BigDecimal>()
21+
val cartTotalLiveData = MutableLiveData<Int>()
2122
private val cartRepository = CartRepository()
2223
private val TAG = "CartViewModel"
2324

@@ -27,6 +28,17 @@ class CartViewModel : ViewModel() {
2728
}
2829
}
2930

31+
fun getTotalCartItems(context: Context) {
32+
viewModelScope.launch {
33+
val items = cartRepository.getCartItems(context)
34+
var total = 0
35+
items.forEach {
36+
total += it.quantity
37+
}
38+
cartTotalLiveData.value = total
39+
}
40+
}
41+
3042
fun getSubtotalPrice(context: Context) {
3143
viewModelScope.launch {
3244
val items = cartRepository.getCartItems(context)

core-sdk-samples/higgs-shop-sample-app/app/src/main/kotlin/com/mparticle/example/higgsshopsampleapp/viewmodels/ShopViewModel.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ import android.content.Context
44
import androidx.lifecycle.MutableLiveData
55
import androidx.lifecycle.ViewModel
66
import androidx.lifecycle.viewModelScope
7+
import com.mparticle.example.higgsshopsampleapp.repositories.CartRepository
78
import com.mparticle.example.higgsshopsampleapp.repositories.ProductsRepository
89
import com.mparticle.example.higgsshopsampleapp.repositories.network.models.Product
910
import kotlinx.coroutines.launch
1011

1112
class ShopViewModel() : ViewModel() {
12-
val inventoryResponseLiveData = MutableLiveData<List<Product>>()
1313
private val TAG = "ProductsViewModel"
1414

15+
val inventoryResponseLiveData = MutableLiveData<List<Product>>()
16+
val cartTotalSizeResponseLiveData = MutableLiveData<Int>()
17+
18+
private val cartRepository = CartRepository()
1519
private val repository = ProductsRepository()
1620

1721
fun getProducts (context: Context) {
@@ -20,8 +24,14 @@ class ShopViewModel() : ViewModel() {
2024
}
2125
}
2226

23-
override fun onCleared() {
24-
super.onCleared()
25-
//repository.cancelCoroutinesJob()
27+
fun getTotalCartItems(context: Context) {
28+
viewModelScope.launch {
29+
val items = cartRepository.getCartItems(context)
30+
var total = 0
31+
items.forEach {
32+
total += it.quantity
33+
}
34+
cartTotalSizeResponseLiveData.value = total
35+
}
2636
}
2737
}

0 commit comments

Comments
 (0)