Skip to content

Commit

Permalink
Merge pull request #193 from ST10204902/backgroundShop
Browse files Browse the repository at this point in the history
Background shop is done!
  • Loading branch information
ST10195824 authored Nov 4, 2024
2 parents e0410c2 + ddb8378 commit a4b786d
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ data class Exercise(
data class UserBackground(
val backgroundID: String = CreateID.GenerateID(),
val name: String = "",
val cost: Int = 0,
val backgroundURI: String = ""
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,46 @@ import za.co.varsitycollege.st10204902.purrsonaltrainer.services.navigateTo
* create an instance of this fragment.
*/
class CatFragment : Fragment() {

private val currentUser get() = UserManager.user // Updated here
private val TAG = "CatFragment"
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View?
{ // Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_cat, container, false)
// set the background image
if (currentUser != null) {
val backgroundURI = currentUser!!.backgroundURI
if (backgroundURI != "") {
try {
val layout = view.findViewById<LinearLayout>(R.id.cat_fragment)

val resourceId = resources.getIdentifier(
backgroundURI,
"drawable", requireContext().packageName
)
val drawable = ContextCompat.getDrawable(requireContext(), resourceId)

layout.background = drawable

} catch (e: Exception) {
Log.e(TAG, "Failed to set background", e)
}
} else {
val layout = view.findViewById<LinearLayout>(R.id.cat_fragment)
layout.background =
ContextCompat.getDrawable(requireContext(), R.drawable.cat_background_1)
}
}

// Setup xp bar and level
val xpBar = view.findViewById<LinearLayout>(R.id.xp_bar)
val level = xpBar.findViewById<GradientTextView>(R.id.cat_level)
val xpBarProgress = xpBar.findViewById<ImageView>(R.id.progressBarProgress)
val progressbarContainer = xpBar.findViewById<FrameLayout>(R.id.progressBarContainer)
val progressBarContent = xpBar.findViewById<ConstraintLayout>(R.id.progressBarContent)
val background = view.background

// Setting cat level
if (UserManager.user != null)
Expand Down Expand Up @@ -92,7 +118,7 @@ class CatFragment : Fragment() {
}
catch (e: Exception)
{
Log.e("Cat Fragment", "Failed to set cat avatar", e)
Log.e(TAG, "Failed to set cat avatar", e)
}

return view
Expand All @@ -105,4 +131,4 @@ class CatFragment : Fragment() {
resources.displayMetrics
).toInt()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,45 +1,181 @@
package za.co.varsitycollege.st10204902.purrsonaltrainer.screens.shop

import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.Log
import android.view.Window
import android.view.WindowManager
import android.widget.Button
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.isVisible
import za.co.varsitycollege.st10204902.purrsonaltrainer.R
import za.co.varsitycollege.st10204902.purrsonaltrainer.backend.UserManager
import za.co.varsitycollege.st10204902.purrsonaltrainer.databinding.ActivityBackgroundShopBinding
import za.co.varsitycollege.st10204902.purrsonaltrainer.databinding.ComponentItemPopupBinding
import za.co.varsitycollege.st10204902.purrsonaltrainer.databinding.ComponentShopItemBinding
import za.co.varsitycollege.st10204902.purrsonaltrainer.models.UserBackground
import za.co.varsitycollege.st10204902.purrsonaltrainer.stores.BackgroundStore

class BackgroundShopActivity : AppCompatActivity()
{
class BackgroundShopActivity : AppCompatActivity() {
private lateinit var binding: ActivityBackgroundShopBinding
override fun onCreate(savedInstanceState: Bundle?)
{
private val TAG = "BackgroundShopActivity"
private val currentUser get() = UserManager.user

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
binding = ActivityBackgroundShopBinding.inflate(layoutInflater)
setContentView(binding.root)

// Setup of Items
val background1 = binding.shopItemBackground1
background1.shopItemName.text = "*BG Name Here"
background1.shopItemImage.setImageResource(R.drawable.shop_background_1)
background1.milkcoinsComponent.milkcoinsAmount.reInitialiseComponent(R.color.background_price_start, R.color.background_price_end)

val background2 = binding.shopItemBackground2
background2.shopItemName.text = "*BG Name Here"
background2.shopItemImage.setImageResource(R.drawable.shop_background_2)
background2.milkcoinsComponent.milkcoinsAmount.reInitialiseComponent(R.color.background_price_start, R.color.background_price_end)

val background3 = binding.shopItemBackground3
background3.shopItemName.text = "*BG Name Here"
background3.shopItemImage.setImageResource(R.drawable.shop_background_3)
background3.milkcoinsComponent.milkcoinsAmount.reInitialiseComponent(R.color.background_price_start, R.color.background_price_end)

val background4 = binding.shopItemBackground4
background4.shopItemName.text = "*BG Name Here"
background4.shopItemImage.setImageResource(R.drawable.shop_background_4)
background4.milkcoinsComponent.milkcoinsAmount.reInitialiseComponent(R.color.background_price_start, R.color.background_price_end)

// Setup Coins
binding.backgroundShopCoins.milkcoinsAmount.reInitialiseComponent(R.color.background_balance_start, R.color.background_balance_end)
setupShopBackgrounds()
setupClickListeners()
setupCoinsDisplay()
}

private fun setupShopBackgrounds() {
Log.d(TAG, "setupShopBackgrounds: Setting up shop backgrounds")
val backgrounds = BackgroundStore.globalBackgrounds

setupShopBackground(binding.shopItemBackground1, backgrounds.getOrNull(0),
R.drawable.shop_background_1)

setupShopBackground(binding.shopItemBackground2, backgrounds.getOrNull(1),
R.drawable.shop_background_2)

setupShopBackground(binding.shopItemBackground3, backgrounds.getOrNull(2),
R.drawable.shop_background_3)

setupShopBackground(binding.shopItemBackground4, backgrounds.getOrNull(3),
R.drawable.shop_background_4)
}

private fun setupShopBackground(
shopBackgroundBinding: ComponentShopItemBinding,
background: UserBackground?,
imageResource: Int
) {
Log.d(TAG, "setupShopBackground: Setting up background ${background?.name}")
background?.let {
shopBackgroundBinding.apply {
shopItemName.text = it.name
shopItemImage.setImageResource(imageResource)
milkcoinsComponent.milkcoinsAmount.reInitialiseComponent(
R.color.background_price_start,
R.color.background_price_end
)
milkcoinsComponent.milkcoinsImage.setImageResource(R.drawable.milkcoin)
milkcoinsComponent.milkcoinsAmount.text = it.cost.toString()

val user = currentUser
if (user != null) {
if (user.userBackgrounds.containsKey(it.backgroundID)) {
milkcoinsComponent.milkcoinsAmount.text = ""
milkcoinsComponent.milkcoinsImage.isVisible = false
}
}

root.setOnClickListener {
showBackgroundDetailsDialog(background)
}
}
}
}

private fun setupClickListeners() {
Log.d(TAG, "setupClickListeners: Setting up click listeners")
// Add any additional click listeners here if needed
}

private fun setupCoinsDisplay() {
Log.d(TAG, "setupCoinsDisplay: Displaying user coins")
binding.backgroundShopCoins.milkcoinsAmount.text = currentUser?.milkCoins.toString()
binding.backgroundShopCoins.milkcoinsAmount.reInitialiseComponent(
R.color.background_balance_start,
R.color.background_balance_end
)
}

private fun showBackgroundDetailsDialog(background: UserBackground) {
Log.d(TAG, "showBackgroundDetailsDialog: Showing details for background ${background.name}")
val dialog = Dialog(this)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)

val dialogBinding = ComponentItemPopupBinding.inflate(layoutInflater)
dialog.setContentView(dialogBinding.root)

dialog.window?.setLayout(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT
)
dialog.window?.apply {
setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
}

with(dialogBinding) {
itemName.text = background.name
tvPrice.text = background.cost.toString()
val user = currentUser
if (user != null) {
if (user.userBackgrounds.containsKey(background.backgroundID)) {
btnPurchase.text = getString(R.string.owned)
ivCoin.isVisible = false
tvPrice.text = ""
btnPurchase.background = resources.getDrawable(R.drawable.svg_purple_bblbtn)
if (user.backgroundURI == background.backgroundURI) {
btnPurchase.text = getString(R.string.equipped)
btnPurchase.background =
resources.getDrawable(R.drawable.svg_purple_bblbtn_clicked)
Log.d(TAG, "showBackgroundDetailsDialog: Background ${background.name} is equipped")
} else {
btnPurchase.text = getString(R.string.equip)
Log.d(TAG, "showBackgroundDetailsDialog: Background ${background.name} can be equipped")
}
} else {
btnPurchase.text = getString(R.string.buy)
if (user.milkCoins < background.cost) {
btnPurchase.isEnabled = false
btnPurchase.background = resources.getDrawable(R.drawable.svg_grey_bblbtn)
Log.d(TAG, "showBackgroundDetailsDialog: Not enough coins to buy ${background.name}")
}
}
}

btnPurchase.setOnClickListener {
Log.d(TAG, "showBackgroundDetailsDialog: btnPurchase clicked for ${background.name}")
onPurchaseBackground(background, dialog, btnPurchase)
}
}

dialog.show()
}

private fun onPurchaseBackground(background: UserBackground, dialog: Dialog, btnPurchase: Button) {
Log.d(TAG, "onPurchaseBackground: Attempting to purchase/equip background ${background.name}")
val user = currentUser
if (user != null) {
if (user.userBackgrounds.containsKey(background.backgroundID)) {
UserManager.updateBackgroundURI(background.backgroundURI)
btnPurchase.text = getString(R.string.equipped)
Log.d(TAG, "onPurchaseBackground: Background ${background.name} equipped")
setupShopBackgrounds()
dialog.dismiss()
} else if (user.milkCoins >= background.cost) {
val newBalance = user.milkCoins - background.cost
UserManager.updateMilkCoins(newBalance)
UserManager.addUserBackground(background)
Log.d(TAG, "onPurchaseBackground: Background ${background.name} purchased, new balance: $newBalance")
setupCoinsDisplay()
setupShopBackgrounds()
dialog.dismiss()
} else {
Log.d(TAG, "onPurchaseBackground: Not enough coins to purchase ${background.name}")
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,4 @@ class ItemShopActivity : AppCompatActivity() {
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package za.co.varsitycollege.st10204902.purrsonaltrainer.stores

import za.co.varsitycollege.st10204902.purrsonaltrainer.models.UserBackground

class BackgroundStore {
companion object {
val globalBackgrounds = listOf(
UserBackground("0", "Baggies Bottom",10, "shop_background_1"),
UserBackground("1", "Clear Skies", 14, "shop_background_2"),
UserBackground("2", "City Skyline", 16, "shop_background_3"),
UserBackground("3", "Alien World", 25, "shop_background_4")
)
}
}
1 change: 1 addition & 0 deletions app/src/main/res/layout/fragment_cat.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cat_fragment"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down

0 comments on commit a4b786d

Please sign in to comment.