Skip to content

Commit

Permalink
implemented get cart api
Browse files Browse the repository at this point in the history
  • Loading branch information
sneh-saraff committed Feb 16, 2025
1 parent ff56b9b commit a872c2f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package org.hackillinois.android.view.shop

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.launch
import org.hackillinois.android.App
import org.hackillinois.android.R
import org.hackillinois.android.database.entity.Cart
import org.hackillinois.android.database.entity.ShopItem

class CartFragment : Fragment() {
Expand Down Expand Up @@ -51,11 +56,32 @@ class CartFragment : Fragment() {
}

private fun fetchCartData() {
// random example to see the layout
cartItems = listOf(
Pair(ShopItem("1", "T-Shirt", 10, false, 1, "https://example.com/tshirt.png"), 2),
Pair(ShopItem("2", "Sticker", 5, false, 1, "https://example.com/sticker.png"), 3)
)
cartAdapter.updateCart(cartItems)
lifecycleScope.launch {
try {
// First, get all available shop items
val shopItems: List<ShopItem> = App.getAPI().shop()

// Next, get the cart from the API
val cart: Cart = App.getAPI().getCart()
val items = mutableListOf<Pair<ShopItem, Int>>()

// Iterate over the map of itemId -> quantity from the cart.
for ((itemId, quantity) in cart.items) {
// Look up the ShopItem from the shopItems list using itemId
val shopItem = shopItems.find { it.itemId == itemId }
if (shopItem != null) {
items.add(Pair(shopItem, quantity))
} else {
Log.e("CartFragment", "ShopItem not found for itemId: $itemId")
}
}
cartItems = items
cartAdapter.updateCart(cartItems)
} catch (e: Exception) {
Log.e("CartFragment", "Error fetching cart items", e)
}
}
}


}
4 changes: 2 additions & 2 deletions app/src/main/res/layout/fragment_point_shop_cart.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:spanCount="2"
app:layout_constraintVertical_bias="0.1"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintHorizontal_bias="0.5"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:layout_constraintBottom_toTopOf="@id/redeemButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/title_textview_cart" />
app:layout_constraintTop_toBottomOf="@id/backButton" />



Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/layout/point_shop_cart_tile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@

<TextView
android:id="@+id/text_view_sticker"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:scrollHorizontally="false"
android:fontFamily="@font/montserrat_medium"
android:text="@string/sticker"
android:textSize="16sp"
Expand Down

0 comments on commit a872c2f

Please sign in to comment.