Skip to content

Commit

Permalink
merch/raffle tabs working to show appropriate items
Browse files Browse the repository at this point in the history
  • Loading branch information
leesunny790 committed Feb 4, 2024
1 parent 962649c commit 382e74c
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,6 @@ class ShopAdapter(private var itemList: List<ShopItem>) :
val imageURL = item.imageURL
val shopItemImageView: ImageView = itemView.findViewById(R.id.shopItemImageView)
Glide.with(context).load(imageURL).into(shopItemImageView)

// Declaring executor to parse the URL
// val executor = Executors.newSingleThreadExecutor()
// Once the executor parses the URL and receives the image, handler will load it
// in the ImageView
// val handler = Handler(Looper.getMainLooper())
// Initializing the image
// var image: Bitmap? = null
// val imageURL = item.imageURL
// executor.execute() {
// try {
// val `in` = java.net.URL(imageURL).openStream()
// image = BitmapFactory.decodeStream(`in`)

// Only for making changes in UI
// handler.post {
// shopItemImageView.setImageBitmap(image)
// }
// }
// If the URL doesnot point to image or any other kind of failure
// catch (e: Exception) {
// e.printStackTrace()
// }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,59 @@ class ShopFragment : Fragment() {
fun newInstance() = ShopFragment()
}

private lateinit var viewModel: ShopViewModel
private lateinit var shopViewModel: ShopViewModel
private var shop: List<ShopItem> = listOf()
private lateinit var recyclerView: RecyclerView
private lateinit var mLayoutManager: LinearLayoutManager
private lateinit var mAdapter: ShopAdapter

private lateinit var merchButton: TextView
private lateinit var raffleButton: TextView

private lateinit var merchItems: List<ShopItem>
private lateinit var raffleItems: List<ShopItem>

// Merch tab is default selected
private var showingMerch: Boolean = true
private var showingRaffle: Boolean = false

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

viewModel = ViewModelProvider(this).get(ShopViewModel::class.java)
shopViewModel = ViewModelProvider(this).get(ShopViewModel::class.java)

// pass whether the user is an attendee to the viewmodel
if (hasLoggedIn() && isAttendee()) {
viewModel.init(true)
shopViewModel.init(true)
} else {
viewModel.init(false)
shopViewModel.init(false)
}

// Observes value of showMerch and updates showingMerch value accordingly
shopViewModel.showMerch.observe(
this,
Observer {
showingMerch = it ?: false
updateShopUI()
},
)

// Observes value of showMerch and updates showingRaffle value accordingly
shopViewModel.showRaffle.observe(
this,
Observer {
showingRaffle = it ?: false
updateShopUI()
},
)
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_point_shop, container, false)

merchButton = view.findViewById(R.id.merchButton)
raffleButton = view.findViewById(R.id.raffleButton)

mAdapter = ShopAdapter(shop)

recyclerView = view.recyclerview_point_shop.apply {
Expand All @@ -60,13 +91,17 @@ class ShopFragment : Fragment() {
addDividers()
}

viewModel.shopLiveData.observe(
shopViewModel.shopLiveData.observe(
viewLifecycleOwner,
Observer {
// will split shop items into Merch or Raffle category
updateShop(it)
},
)

merchButton.setOnClickListener(merchClickListener)
raffleButton.setOnClickListener(raffleClickListener)

if (hasLoggedIn() && isAttendee()) {
// set coin views visible for attendee
val coinBg: TextView = view.findViewById(R.id.total_coin_view)
Expand All @@ -76,7 +111,7 @@ class ShopFragment : Fragment() {
coinText.visibility = View.VISIBLE
coinImg.visibility = View.VISIBLE

viewModel.profileLiveData.observe(
shopViewModel.profileLiveData.observe(
viewLifecycleOwner,
Observer {
updateCoinTotalUI(it)
Expand Down Expand Up @@ -119,8 +154,52 @@ class ShopFragment : Fragment() {
addItemDecoration(DividerItemDecorator(context))
}

// private fun updateShop(newShop: List<ShopItem>) {
// mAdapter.updateShop(newShop)
// }

// Called in onCreateView within shopLiveData.observe
private fun updateShop(newShop: List<ShopItem>) {
mAdapter.updateShop(newShop)
// Split the shop items into Merch and Raffle items
merchItems = newShop.filter { !it.isRaffle }
raffleItems = newShop.filter { it.isRaffle }

// Update the UI based on the selected button
updateShopUI()
}

private fun updateShopUI() {
// if showingMerch variable is True based on selected button, show merch items. else, show raffle
val itemsToShow = if (showingMerch) merchItems else raffleItems
mAdapter.updateShop(itemsToShow)
}

// update merch ViewModel on click
private val merchClickListener = View.OnClickListener {
if (!merchButton.isSelected) {
merchButton.isSelected = true
merchButton.background = this.context?.let { it1 -> ContextCompat.getDrawable(it1, R.drawable.shop_selected_tab) }
raffleButton.isSelected = false
raffleButton.background = this.context?.let { it1 -> ContextCompat.getDrawable(it1, R.drawable.shop_unselected_tab) }
shopViewModel.showMerch.postValue(true)
shopViewModel.showRaffle.postValue(false)
showingMerch = true
showingRaffle = false
}
}

// update raffle ViewModel on click
private val raffleClickListener = View.OnClickListener {
if (!raffleButton.isSelected) {
raffleButton.isSelected = true
raffleButton.background = this.context?.let { it1 -> ContextCompat.getDrawable(it1, R.drawable.shop_selected_tab) }
merchButton.isSelected = false
merchButton.background = this.context?.let { it1 -> ContextCompat.getDrawable(it1, R.drawable.shop_unselected_tab) }
shopViewModel.showRaffle.postValue(true)
shopViewModel.showMerch.postValue(false)
showingRaffle = true
showingMerch = false
}
}

private fun updateCoinTotalUI(newProfile: Profile?) {
Expand All @@ -140,7 +219,3 @@ class ShopFragment : Fragment() {
return context.getSharedPreferences(prefString, Context.MODE_PRIVATE).getString("provider", "") ?: "" == "github"
}
}

// var itemDecoration = DividerItemDecoration(this.context, DividerItemDecoration.VERTICAL)
// itemDecoration.setDrawable(getDrawable(this.context, R.drawable.leaderboard_divider)!!)
// addItemDecoration(itemDecoration)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.hackillinois.android.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import org.hackillinois.android.database.entity.Profile
import org.hackillinois.android.database.entity.ShopItem
Expand All @@ -18,7 +19,8 @@ class ShopViewModel : ViewModel() {

lateinit var timerObj: Timer

// var shopTabClicked by Delegates.notNull<Boolean>()
var showMerch: MutableLiveData<Boolean> = MutableLiveData()
var showRaffle: MutableLiveData<Boolean> = MutableLiveData()

fun init(isAttendee: Boolean) {
// initial fetch
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/shop_unselected_tab.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00FFFFFF" />
<corners android:radius="1dp"
android:topLeftRadius="6dp"
android:topRightRadius="6dp"
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"/>
</shape>
1 change: 1 addition & 0 deletions app/src/main/res/layout/fragment_point_shop.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
android:layout_height="wrap_content"
android:layout_marginHorizontal="5dp"
android:layout_marginTop="12dp"
android:background="@drawable/shop_unselected_tab"
android:fontFamily="@font/montserrat_bold"
android:gravity="center"
android:paddingVertical="10dp"
Expand Down

0 comments on commit 382e74c

Please sign in to comment.