From ddb837891f5dccd2ea4584da888aa886da2c93e2 Mon Sep 17 00:00:00 2001 From: ST10195824 Date: Mon, 4 Nov 2024 13:00:04 +0200 Subject: [PATCH] Background shop is done! --- .../purrsonaltrainer/models/models.kt | 1 + .../screens/fragments/CatFragment.kt | 32 ++- .../screens/shop/BackgroundShopActivity.kt | 192 +++++++++++++++--- .../screens/shop/ItemShopActivity.kt | 1 - .../stores/BackgroundStore.kt | 14 ++ app/src/main/res/layout/fragment_cat.xml | 1 + 6 files changed, 209 insertions(+), 32 deletions(-) create mode 100644 app/src/main/java/za/co/varsitycollege/st10204902/purrsonaltrainer/stores/BackgroundStore.kt diff --git a/app/src/main/java/za/co/varsitycollege/st10204902/purrsonaltrainer/models/models.kt b/app/src/main/java/za/co/varsitycollege/st10204902/purrsonaltrainer/models/models.kt index ac91246..1c342ab 100644 --- a/app/src/main/java/za/co/varsitycollege/st10204902/purrsonaltrainer/models/models.kt +++ b/app/src/main/java/za/co/varsitycollege/st10204902/purrsonaltrainer/models/models.kt @@ -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 = "" ) diff --git a/app/src/main/java/za/co/varsitycollege/st10204902/purrsonaltrainer/screens/fragments/CatFragment.kt b/app/src/main/java/za/co/varsitycollege/st10204902/purrsonaltrainer/screens/fragments/CatFragment.kt index 6cfe241..af6f3f0 100644 --- a/app/src/main/java/za/co/varsitycollege/st10204902/purrsonaltrainer/screens/fragments/CatFragment.kt +++ b/app/src/main/java/za/co/varsitycollege/st10204902/purrsonaltrainer/screens/fragments/CatFragment.kt @@ -28,13 +28,38 @@ 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(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(R.id.cat_fragment) + layout.background = + ContextCompat.getDrawable(requireContext(), R.drawable.cat_background_1) + } + } // Setup xp bar and level val xpBar = view.findViewById(R.id.xp_bar) @@ -42,6 +67,7 @@ class CatFragment : Fragment() { val xpBarProgress = xpBar.findViewById(R.id.progressBarProgress) val progressbarContainer = xpBar.findViewById(R.id.progressBarContainer) val progressBarContent = xpBar.findViewById(R.id.progressBarContent) + val background = view.background // Setting cat level if (UserManager.user != null) @@ -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 @@ -105,4 +131,4 @@ class CatFragment : Fragment() { resources.displayMetrics ).toInt() } -} \ No newline at end of file +} diff --git a/app/src/main/java/za/co/varsitycollege/st10204902/purrsonaltrainer/screens/shop/BackgroundShopActivity.kt b/app/src/main/java/za/co/varsitycollege/st10204902/purrsonaltrainer/screens/shop/BackgroundShopActivity.kt index 989aabe..db75612 100644 --- a/app/src/main/java/za/co/varsitycollege/st10204902/purrsonaltrainer/screens/shop/BackgroundShopActivity.kt +++ b/app/src/main/java/za/co/varsitycollege/st10204902/purrsonaltrainer/screens/shop/BackgroundShopActivity.kt @@ -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}") + } + } } -} \ No newline at end of file +} diff --git a/app/src/main/java/za/co/varsitycollege/st10204902/purrsonaltrainer/screens/shop/ItemShopActivity.kt b/app/src/main/java/za/co/varsitycollege/st10204902/purrsonaltrainer/screens/shop/ItemShopActivity.kt index c3a8dc8..4c023d4 100644 --- a/app/src/main/java/za/co/varsitycollege/st10204902/purrsonaltrainer/screens/shop/ItemShopActivity.kt +++ b/app/src/main/java/za/co/varsitycollege/st10204902/purrsonaltrainer/screens/shop/ItemShopActivity.kt @@ -195,5 +195,4 @@ class ItemShopActivity : AppCompatActivity() { } } } - } diff --git a/app/src/main/java/za/co/varsitycollege/st10204902/purrsonaltrainer/stores/BackgroundStore.kt b/app/src/main/java/za/co/varsitycollege/st10204902/purrsonaltrainer/stores/BackgroundStore.kt new file mode 100644 index 0000000..dacf1e0 --- /dev/null +++ b/app/src/main/java/za/co/varsitycollege/st10204902/purrsonaltrainer/stores/BackgroundStore.kt @@ -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") + ) + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_cat.xml b/app/src/main/res/layout/fragment_cat.xml index d4cf124..7d52190 100644 --- a/app/src/main/res/layout/fragment_cat.xml +++ b/app/src/main/res/layout/fragment_cat.xml @@ -1,5 +1,6 @@