-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #193 from ST10204902/backgroundShop
Background shop is done!
- Loading branch information
Showing
6 changed files
with
209 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
192 changes: 164 additions & 28 deletions
192
...a/za/co/varsitycollege/st10204902/purrsonaltrainer/screens/shop/BackgroundShopActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -195,5 +195,4 @@ class ItemShopActivity : AppCompatActivity() { | |
} | ||
} | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
app/src/main/java/za/co/varsitycollege/st10204902/purrsonaltrainer/stores/BackgroundStore.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters