Skip to content

Commit

Permalink
fixed adding to cart API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaK2905 committed Feb 14, 2025
1 parent a637c06 commit cff102b
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 7 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/org/hackillinois/android/API.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.hackillinois.android.model.user.FavoritesResponse
import org.hackillinois.android.model.version.Version
import org.hackillinois.android.notifications.DeviceToken
import retrofit2.Call
import retrofit2.Response
import retrofit2.http.*

interface API {
Expand Down Expand Up @@ -70,7 +71,7 @@ interface API {
suspend fun buyShopItem(@Body body: ItemInstance): ShopItem

@POST("shop/cart/{itemId}")
fun addItemCart(@Path("itemId") itemId: String): Call<ResponseBody>
suspend fun addItemCart(@Path("itemId") itemId: String): Response<ResponseBody>

@GET("shop/cart/")
suspend fun getCart(): Cart
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/org/hackillinois/android/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ class App : Application() {
return if (apiInitialized) apiInternal else getAPI("")
}

Log.d("TOKEN", token)
Log.d("APPTOKEN", token)

val interceptor = { chain: Interceptor.Chain ->
val newRequest = chain.request().newBuilder()
.addHeader("Authorization", token)
.addHeader("Authorization", "Bearer $token")
.addHeader("Accept", "application/json")
.build()
chain.proceed(newRequest)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,27 @@ import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.findViewTreeLifecycleOwner
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.shop_tile.view.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import okhttp3.ResponseBody
import org.hackillinois.android.App
import org.hackillinois.android.R
import org.hackillinois.android.database.entity.ShopItem
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch

class ShopAdapter(private var itemList: List<ShopItem>) :
class ShopAdapter(private var itemList: List<ShopItem>, private val buyItemListener : OnBuyItemListener) :
RecyclerView.Adapter<ShopAdapter.ViewHolder>() {
private lateinit var context: Context
inner class ViewHolder(parent: View) : RecyclerView.ViewHolder(parent)



// onCreateViewHolder used to display scrollable list of items
// implemented as part of RecyclerView's adapter, responsible for creating new ViewHolder objects
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
Expand Down Expand Up @@ -59,9 +68,45 @@ class ShopAdapter(private var itemList: List<ShopItem>) :
Log.d("Shop Glide Error", e.message.toString())
}
val plusButton: ImageView = findViewById(R.id.plusButton)

// plusButton.setOnClickListener {
// Log.d("CartDebug", "Plus button clicked!")
// Log.d("Item ID: ", ""+item.itemId)
// App.getAPI().addItemCart(item.itemId).enqueue(object : Callback<ResponseBody> {
// override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
// if (response.isSuccessful) {
// Log.d("CartDebug", "Item added to cart successfully!: ${response.code()}, ${response.message()}, ${response.body().toString()}")
// } else {
// Log.e("CartDebug", "Failed to add item: ${response.code()}")
// }
// }
//
// override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
// Log.e("CartDebug", "Error adding item to cart", t)
// }
// })
// }
// plusButton.setOnClickListener {
// Log.d("CartDebug", "Plus button clicked!")
// Log.d("Item ID: ", ""+item.itemId)
// itemView.findViewTreeLifecycleOwner()?.lifecycleScope?.launch {
// try {
// val response = App.getAPI().addItemCart(item.itemId)
// if (response.isSuccessful) {
// val cartResponse = response.body()
// Log.d("CartDebug", "Item added: $cartResponse")
// } else {
// Log.e("CartDebug", "Failed to add item: ${response.code()}")
// }
// } catch (e: Exception) {
// Log.e("CartDebug", "Error adding item to cart", e)
// }
// } ?: Log.e("CartDebug", "No LifecycleOwner found for itemView")
// }
plusButton.setOnClickListener {
Log.d("CartDebug", "Plus button clicked!")
App.getAPI().addItemCart(item.itemId)
Log.d("Item ID: ", ""+item.itemId)
buyItemListener.onBuyItem(item)
}
}
}
Expand All @@ -70,4 +115,8 @@ class ShopAdapter(private var itemList: List<ShopItem>) :
this.itemList = shopItem
notifyDataSetChanged()
}

interface OnBuyItemListener {
fun onBuyItem(item : ShopItem)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@ import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.fragment_point_shop.number_of_coins_textview
import kotlinx.android.synthetic.main.fragment_point_shop.view.recyclerview_point_shop
import kotlinx.coroutines.launch
import org.hackillinois.android.App
import org.hackillinois.android.R
import org.hackillinois.android.common.JWTUtilities
import org.hackillinois.android.database.entity.Profile
import org.hackillinois.android.database.entity.ShopItem
import org.hackillinois.android.viewmodel.ShopViewModel

class ShopFragment : Fragment() {
class ShopFragment : Fragment(), ShopAdapter.OnBuyItemListener {

companion object {
fun newInstance() = ShopFragment()
Expand Down Expand Up @@ -76,7 +79,7 @@ class ShopFragment : Fragment() {
merchButton = view.findViewById(R.id.merchButton)
raffleButton = view.findViewById(R.id.raffleButton)

mAdapter = ShopAdapter(shop)
mAdapter = ShopAdapter(shop, this)

recyclerView = view.recyclerview_point_shop.apply {
mLayoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
Expand Down Expand Up @@ -203,4 +206,21 @@ class ShopFragment : Fragment() {
return context.getSharedPreferences(prefString, Context.MODE_PRIVATE)
.getString("provider", "") ?: "" == "github"
}

override fun onBuyItem(item: ShopItem) {
// Implement your buying logic here (e.g., make a network call)
lifecycleScope.launch {
try {
val response = App.getAPI().addItemCart(item.itemId)
if (response.isSuccessful) {
// Update UI or local data with the new cart state
Log.d("CartDebug", "Item added: ${response.body()}")
} else {
Log.e("CartDebug", "Failed to add item: ${response.code()}")
}
} catch (e: Exception) {
Log.e("CartDebug", "Error adding item to cart", e)
}
}
}
}

0 comments on commit cff102b

Please sign in to comment.