Implement bulk purchasing items. Fixes #1288

This commit is contained in:
Phillip Thelen 2020-06-11 17:07:46 +02:00
parent 1956df6f66
commit b1c393b168
5 changed files with 40 additions and 12 deletions

View file

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
@ -31,6 +32,14 @@
android:textColor="@color/black_50_alpha"
tools:text="These are the notes"
android:gravity="center"/>
<com.habitrpg.android.habitica.ui.views.tasks.form.StepperValueFormView
android:id="@+id/stepper_view"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_marginTop="@dimen/spacing_large"
app:defaultValue="1"
app:minValue="1" />
<TextView
android:id="@+id/amount_error_label"
android:layout_height="wrap_content"

View file

@ -1015,7 +1015,6 @@
<string name="read_more">Read More</string>
<string name="purchase_amount_error">You are unable to buy that amount.</string>
<string name="still_questions">Still have a question?</string>
<string name="delete_checklist_entry">Delete Checklist entry</string>
<string name="pet_ownership_fraction">%1$d/%2$d</string>
<string name="task_display">Task list display</string>
<string name="onboarding_tasks">Onboarding Tasks</string>

View file

@ -68,10 +68,13 @@ open class ShopItem : RealmObject() {
val isTypeAnimal: Boolean
get() = "pets" == purchaseType || "mounts" == purchaseType
val canPurchaseBulk: Boolean
get() = "eggs" == purchaseType || "hatchingPotions" == purchaseType || "food" == purchaseType
fun canAfford(user: User?, quantity: Int): Boolean = when(currency) {
"gold" -> (value * quantity) <= user?.stats?.gp ?: 0.0
"gems" -> true
"hourglasses" -> true
"gems" -> (value * quantity) <= user?.gemCount?.toDouble() ?: 0.0
"hourglasses" -> (value * quantity) <= user?.hourglassCount?.toDouble() ?: 0.0
else -> false
}

View file

@ -93,17 +93,29 @@ class PurchaseDialog(context: Context, component: UserComponent?, val item: Shop
val contentView: PurchaseDialogContent
when {
shopItem.isTypeItem -> contentView = PurchaseDialogItemContent(context)
shopItem.isTypeQuest -> {
contentView = PurchaseDialogQuestContent(context)
inventoryRepository.getQuestContent(shopItem.key).firstElement().subscribe(Consumer<QuestContent> { contentView.setQuestContent(it) }, RxErrorHandler.handleEmptyError())
shopItem.isTypeItem -> {
val itemContent = PurchaseDialogItemContent(context)
if (shopItem.canPurchaseBulk) {
itemContent.stepperView.visibility = View.VISIBLE
itemContent.stepperView.onValueChanged = {
purchaseQuantity = it.toInt()
updatePurchaseTotal()
}
} else {
itemContent.stepperView.visibility = View.GONE
}
contentView = itemContent
}
shopItem.isTypeGear -> {
shopItem.isTypeQuest -> {
contentView = PurchaseDialogQuestContent(context)
inventoryRepository.getQuestContent(shopItem.key).firstElement().subscribe(Consumer { contentView.setQuestContent(it) }, RxErrorHandler.handleEmptyError())
}
shopItem.isTypeGear -> {
contentView = PurchaseDialogGearContent(context)
inventoryRepository.getEquipment(shopItem.key).firstElement().subscribe(Consumer<Equipment> { contentView.setEquipment(it) }, RxErrorHandler.handleEmptyError())
inventoryRepository.getEquipment(shopItem.key).firstElement().subscribe(Consumer { contentView.setEquipment(it) }, RxErrorHandler.handleEmptyError())
checkGearClass()
}
"gems" == shopItem.purchaseType -> {
"gems" == shopItem.purchaseType -> {
val gemContent = PurchaseDialogGemsContent(context)
gemContent.stepperView.onValueChanged = {
purchaseQuantity = it.toInt()
@ -111,7 +123,7 @@ class PurchaseDialog(context: Context, component: UserComponent?, val item: Shop
}
contentView = gemContent
}
else -> contentView = PurchaseDialogBaseContent(context)
else -> contentView = PurchaseDialogBaseContent(context)
}
amountErrorLabel = contentView.findViewById(R.id.amount_error_label)
@ -123,7 +135,7 @@ class PurchaseDialog(context: Context, component: UserComponent?, val item: Shop
private fun updatePurchaseTotal() {
priceLabel.value = shopItem.value.toDouble() * purchaseQuantity
if (shopItem.canAfford(user, purchaseQuantity) && !shopItem.locked && purchaseQuantity >= 1) {
if ((shopItem.currency != "gold" || shopItem.canAfford(user, purchaseQuantity)) && !shopItem.locked && purchaseQuantity >= 1) {
buyButton.background = context.getDrawable(R.drawable.button_background_primary)
priceLabel.setTextColor(ContextCompat.getColor(context, R.color.white))
buyLabel.setTextColor(ContextCompat.getColor(context, R.color.white))

View file

@ -4,12 +4,16 @@ import android.content.Context
import android.util.AttributeSet
import android.widget.TextView
import com.habitrpg.android.habitica.R
import com.habitrpg.android.habitica.extensions.asDrawable
import com.habitrpg.android.habitica.ui.helpers.bindView
import com.habitrpg.android.habitica.models.shops.ShopItem
import com.habitrpg.android.habitica.ui.views.HabiticaIconsHelper
import com.habitrpg.android.habitica.ui.views.tasks.form.StepperValueFormView
class PurchaseDialogItemContent : PurchaseDialogContent {
internal val notesTextView: TextView by bindView(R.id.notesTextView)
val stepperView: StepperValueFormView by bindView(R.id.stepper_view)
override val viewId: Int
get() = R.layout.dialog_purchase_content_item
@ -21,5 +25,6 @@ class PurchaseDialogItemContent : PurchaseDialogContent {
override fun setItem(item: ShopItem) {
super.setItem(item)
notesTextView.text = item.notes
stepperView.iconDrawable = null
}
}