Skip to content

Commit

Permalink
Load translated name for eggs and potions in hatching dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
phillipthelen committed Sep 25, 2024
1 parent 14e8328 commit 235618f
Showing 1 changed file with 43 additions and 36 deletions.
79 changes: 43 additions & 36 deletions HabitRPG/Views/PetHatchingDialog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,14 @@ class PetHatchingAlertController: HabiticaAlertController {
}()

private var gemCount = 0
private var eggCount = 0
private var potionCount = 0

// swiftlint:disable:next function_body_length
convenience init(item: PetStableItem, ownedEggs: OwnedItemProtocol?, ownedPotions: OwnedItemProtocol?) {
self.init()
let eggCount = ownedEggs?.numberOwned ?? 0
let potionCount = ownedPotions?.numberOwned ?? 0
eggCount = ownedEggs?.numberOwned ?? 0
potionCount = ownedPotions?.numberOwned ?? 0
eggCountView.text = String(eggCount)
eggCountView.isHidden = eggCount == 0
potionCountView.text = String(potionCount)
Expand All @@ -101,32 +103,12 @@ class PetHatchingAlertController: HabiticaAlertController {
userRepository.getUser().take(first: 1).on(value: { user in
self.gemCount = user.gemCount
}).start()
if item.canRaise || item.pet?.type == "special" {
title = L10n.hatchPet
petView.setImagewith(name: "stable_Pet-\(item.pet?.key ?? "")-outline")
if eggCount == 0 && potionCount == 0 {
descriptionlabel.text = L10n.suggestPetHatchMissingBoth(item.pet?.egg ?? "", item.pet?.potion ?? "")
} else if eggCount == 0 {
descriptionlabel.text = L10n.suggestPetHatchMissingEgg(item.pet?.egg ?? "")
} else if potionCount == 0 {
descriptionlabel.text = L10n.suggestPetHatchMissingPotion(item.pet?.potion ?? "")
} else {
descriptionlabel.text = L10n.canHatchPet(item.pet?.egg ?? "", item.pet?.potion ?? "")
}
} else {
title = L10n.hatchPetAgain
petView.setImagewith(name: "stable_Pet-\(item.pet?.key ?? "")")
petView.alpha = 0.5
if eggCount == 0 && potionCount == 0 {
descriptionlabel.text = L10n.suggestPetHatchAgainMissingBoth(item.pet?.egg ?? "", item.pet?.potion ?? "")
} else if eggCount == 0 {
descriptionlabel.text = L10n.suggestPetHatchAgainMissingEgg(item.pet?.egg ?? "")
} else if potionCount == 0 {
descriptionlabel.text = L10n.suggestPetHatchAgainMissingPotion(item.pet?.potion ?? "")
} else {
descriptionlabel.text = L10n.canHatchPet(item.pet?.egg ?? "", item.pet?.potion ?? "")
}
}
inventoryRepository.getItems(keys: [.eggs: [item.pet?.egg ?? ""], .hatchingPotions: [item.pet?.potion ?? ""]]).take(first: 1).on(value: { items in
let egg = items.eggs.value.first
let potion = items.hatchingPotions.value.first
self.setText(item: item, eggText: egg?.text ?? item.pet?.egg ?? "", potionText: potion?.text ?? item.pet?.potion ?? "")
}).start()
setText(item: item, eggText: item.pet?.egg ?? "", potionText: item.pet?.potion ?? "")

if eggCount > 0 && potionCount > 0 {
addAction(title: L10n.hatch, isMainAction: true) { _ in
Expand All @@ -145,8 +127,8 @@ class PetHatchingAlertController: HabiticaAlertController {
#endif
let egg = items.eggs.value.first
let potion = items.hatchingPotions.value.first
var hatchValue = eggCount > 0 ? 0 : Int(egg?.value ?? 0.0)
hatchValue += potionCount > 0 ? 0 : Int(potion?.value ?? 0.0)
var hatchValue = self.eggCount > 0 ? 0 : Int(egg?.value ?? 0.0)
hatchValue += self.potionCount > 0 ? 0 : Int(potion?.value ?? 0.0)

if hatchValue > 0 && (item.pet?.type == "drop" || (item.pet?.type == "quest" && ownedEggs != nil)) {
let attributedText = NSMutableAttributedString(string: L10n.hatch + " \(hatchValue) :gems:")
Expand All @@ -169,12 +151,12 @@ class PetHatchingAlertController: HabiticaAlertController {
observable.send(Signal.Event.value(nil))
observable.sendCompleted()
}
if eggCount == 0 {
if self.eggCount == 0 {
signal = signal.flatMap(.latest, { _ -> Signal<UserProtocol?, Never> in
return self.inventoryRepository.purchaseItem(purchaseType: "eggs", key: item.pet?.egg ?? "", value: 4, quantity: 1, text: (egg?.text ?? "") + " " + L10n.egg)
})
}
if potionCount == 0 {
if self.potionCount == 0 {
signal = signal.flatMap(.latest, { _ -> Signal<UserProtocol?, Never> in
return self.inventoryRepository.purchaseItem(purchaseType: "hatchingPotions", key: item.pet?.potion ?? "",
value: 4,
Expand All @@ -185,10 +167,6 @@ class PetHatchingAlertController: HabiticaAlertController {

signal.flatMap(.latest, { _ in
return self.inventoryRepository.hatchPet(egg: item.pet?.egg, potion: item.pet?.potion)
}).on(completed: {
guard let egg = items.eggs.value.first, let potion = items.hatchingPotions.value.first else {
return
}
}).start()
}
button.setAttributedTitle(attributedText, for: .normal)
Expand All @@ -198,6 +176,35 @@ class PetHatchingAlertController: HabiticaAlertController {

setupView()
}

private func setText(item: PetStableItem, eggText: String, potionText: String) {
if item.canRaise || item.pet?.type == "special" {
title = L10n.hatchPet
petView.setImagewith(name: "stable_Pet-\(item.pet?.key ?? "")-outline")
if eggCount == 0 && potionCount == 0 {
descriptionlabel.text = L10n.suggestPetHatchMissingBoth(eggText, potionText)
} else if eggCount == 0 {
descriptionlabel.text = L10n.suggestPetHatchMissingEgg(eggText)
} else if potionCount == 0 {
descriptionlabel.text = L10n.suggestPetHatchMissingPotion(potionText)
} else {
descriptionlabel.text = L10n.canHatchPet(eggText, potionText)
}
} else {
title = L10n.hatchPetAgain
petView.setImagewith(name: "stable_Pet-\(item.pet?.key ?? "")")
petView.alpha = 0.5
if eggCount == 0 && potionCount == 0 {
descriptionlabel.text = L10n.suggestPetHatchAgainMissingBoth(eggText, potionText)
} else if eggCount == 0 {
descriptionlabel.text = L10n.suggestPetHatchAgainMissingEgg(eggText)
} else if potionCount == 0 {
descriptionlabel.text = L10n.suggestPetHatchAgainMissingPotion(potionText)
} else {
descriptionlabel.text = L10n.canHatchPet(eggText, potionText)
}
}
}

private func setupView() {
contentView = stackView
Expand Down

0 comments on commit 235618f

Please sign in to comment.