diff --git a/submodules/TelegramStringFormatting/Sources/TonFormat.swift b/submodules/TelegramStringFormatting/Sources/TonFormat.swift index b5a3151174..dd2d735a11 100644 --- a/submodules/TelegramStringFormatting/Sources/TonFormat.swift +++ b/submodules/TelegramStringFormatting/Sources/TonFormat.swift @@ -28,7 +28,7 @@ public func formatTonUsdValue(_ value: Int64, divide: Bool = true, rate: Double return "$\(formattedValue)" } -public func formatTonAmountText(_ value: Int64, dateTimeFormat: PresentationDateTimeFormat, showPlus: Bool = false, maxDecimalPositions: Int = 2) -> String { +public func formatTonAmountText(_ value: Int64, dateTimeFormat: PresentationDateTimeFormat, showPlus: Bool = false, maxDecimalPositions: Int? = 2) -> String { var balanceText = "\(abs(value))" while balanceText.count < 10 { balanceText.insert("0", at: balanceText.startIndex) @@ -49,10 +49,12 @@ public func formatTonAmountText(_ value: Int64, dateTimeFormat: PresentationDate } if let dotIndex = balanceText.range(of: dateTimeFormat.decimalSeparator) { - if let endIndex = balanceText.index(dotIndex.upperBound, offsetBy: maxDecimalPositions, limitedBy: balanceText.endIndex) { - balanceText = String(balanceText[balanceText.startIndex..() + private var text = ComponentView() private let action = ComponentView() private var component: StarsBalanceOverlayComponent? @@ -89,6 +89,7 @@ public final class StarsBalanceOverlayComponent: Component { defer { self.isUpdating = false } + let previousComponent = self.component self.component = component self.state = state @@ -169,6 +170,13 @@ public final class StarsBalanceOverlayComponent: Component { attributedText.addAttribute(.baselineOffset, value: 1.0, range: tonRange) } + if previousComponent?.currency != component.currency { + if let textView = self.text.view { + textView.removeFromSuperview() + } + self.text = ComponentView() + } + let textSize = self.text.update( transition: .immediate, component: AnyComponent( @@ -222,7 +230,6 @@ public final class StarsBalanceOverlayComponent: Component { actionView.removeFromSuperview() } } - if let textView = self.text.view { if textView.superview == nil { diff --git a/submodules/TelegramUI/Components/Stars/StarsTransactionScreen/Sources/StarsTransactionScreen.swift b/submodules/TelegramUI/Components/Stars/StarsTransactionScreen/Sources/StarsTransactionScreen.swift index 03ff9a4aee..f81d39c1cc 100644 --- a/submodules/TelegramUI/Components/Stars/StarsTransactionScreen/Sources/StarsTransactionScreen.swift +++ b/submodules/TelegramUI/Components/Stars/StarsTransactionScreen/Sources/StarsTransactionScreen.swift @@ -660,7 +660,7 @@ private final class StarsTransactionSheetContent: CombinedComponent { case .stars: formattedAmount = formatStarsAmountText(absCount, dateTimeFormat: dateTimeFormat) case .ton: - formattedAmount = formatTonAmountText(absCount.value, dateTimeFormat: dateTimeFormat) + formattedAmount = formatTonAmountText(absCount.value, dateTimeFormat: dateTimeFormat, maxDecimalPositions: nil) } let countColor: UIColor var countFont: UIFont = isSubscription || isSubscriber ? Font.regular(17.0) : Font.semibold(17.0) @@ -1207,14 +1207,25 @@ private final class StarsTransactionSheetContent: CombinedComponent { if transaction.flags.contains(.isPaidMessage) || transaction.flags.contains(.isStarGiftResale) { var totalStars = transaction.count if let starrefCount = transaction.starrefAmount { - totalStars = CurrencyAmount(amount: totalStars.amount + starrefCount, currency: .stars) + totalStars = CurrencyAmount(amount: totalStars.amount + starrefCount, currency: totalStars.currency) + } + var valueString = formatCurrencyAmountText(totalStars, dateTimeFormat: dateTimeFormat) + switch totalStars.currency { + case .stars: + valueString = "\(valueString)⭐️" + case .ton: + valueString = "💎\(valueString)" } - let valueString = "\(presentationStringsFormattedNumber(abs(Int32(totalStars.amount.value)), dateTimeFormat.groupingSeparator))⭐️" let valueAttributedString = NSMutableAttributedString(string: valueString, font: tableBoldFont, textColor: theme.list.itemDisclosureActions.constructive.fillColor) - let range = (valueAttributedString.string as NSString).range(of: "⭐️") - if range.location != NSNotFound { - valueAttributedString.addAttribute(ChatTextInputAttributes.customEmoji, value: ChatTextInputTextCustomEmojiAttribute(interactivelySelectedFromPackId: nil, fileId: 0, file: nil, custom: .stars(tinted: false)), range: range) - valueAttributedString.addAttribute(.baselineOffset, value: 1.0, range: range) + let starRange = (valueAttributedString.string as NSString).range(of: "⭐️") + if starRange.location != NSNotFound { + valueAttributedString.addAttribute(ChatTextInputAttributes.customEmoji, value: ChatTextInputTextCustomEmojiAttribute(interactivelySelectedFromPackId: nil, fileId: 0, file: nil, custom: .stars(tinted: false)), range: starRange) + valueAttributedString.addAttribute(.baselineOffset, value: 1.0, range: starRange) + } + let tonRange = (valueAttributedString.string as NSString).range(of: "💎") + if tonRange.location != NSNotFound { + valueAttributedString.addAttribute(ChatTextInputAttributes.customEmoji, value: ChatTextInputTextCustomEmojiAttribute(interactivelySelectedFromPackId: nil, fileId: 0, file: nil, custom: .ton(tinted: true)), range: tonRange) + valueAttributedString.addAttribute(.baselineOffset, value: 1.0, range: tonRange) } tableItems.append(.init( id: "paid", diff --git a/submodules/TelegramUI/Components/Stars/StarsWithdrawalScreen/Sources/StarsWithdrawalScreen.swift b/submodules/TelegramUI/Components/Stars/StarsWithdrawalScreen/Sources/StarsWithdrawalScreen.swift index aae750f048..4b1c4a94be 100644 --- a/submodules/TelegramUI/Components/Stars/StarsWithdrawalScreen/Sources/StarsWithdrawalScreen.swift +++ b/submodules/TelegramUI/Components/Stars/StarsWithdrawalScreen/Sources/StarsWithdrawalScreen.swift @@ -467,7 +467,7 @@ private final class SheetContent: CombinedComponent { case .ton: if let value = state.amount?.value, value > 0 { let tonValue = Int64(Float(value) * Float(resaleConfiguration.starGiftCommissionTonPermille) / 1000.0) - let tonString = formatTonAmountText(tonValue, dateTimeFormat: environment.dateTimeFormat) + " TON" + let tonString = formatTonAmountText(tonValue, dateTimeFormat: environment.dateTimeFormat, maxDecimalPositions: nil) + " TON" amountInfoString = NSAttributedString(attributedString: parseMarkdownIntoAttributedString(environment.strings.Stars_SellGift_AmountInfo(tonString).string, attributes: amountMarkdownAttributes, textAlignment: .natural)) if let tonUsdRate = withdrawConfiguration.tonUsdRate { @@ -778,7 +778,7 @@ private final class SheetContent: CombinedComponent { currencyAmount = presentationStringsFormattedNumber(amount, environment.dateTimeFormat.groupingSeparator) case .ton: currencySymbol = "$" - currencyAmount = formatTonAmountText(amount.value, dateTimeFormat: environment.dateTimeFormat) + currencyAmount = formatTonAmountText(amount.value, dateTimeFormat: environment.dateTimeFormat, maxDecimalPositions: nil) } buttonString = "\(environment.strings.Stars_SellGift_SellFor) \(currencySymbol) \(currencyAmount)" } else { @@ -798,7 +798,7 @@ private final class SheetContent: CombinedComponent { currencyAmount = presentationStringsFormattedNumber(amount, environment.dateTimeFormat.groupingSeparator) case .ton: currencySymbol = "$" - currencyAmount = formatTonAmountText(amount.value, dateTimeFormat: environment.dateTimeFormat) + currencyAmount = formatTonAmountText(amount.value, dateTimeFormat: environment.dateTimeFormat, maxDecimalPositions: nil) } buttonString = environment.strings.Chat_PostSuggestion_Suggest_OfferButtonPrice("\(currencySymbol) \(currencyAmount)").string } else { @@ -1423,7 +1423,7 @@ private final class AmountFieldStarsFormatter: NSObject, UITextFieldDelegate { case .stars: textField.text = "\(self.maxValue)" case .ton: - textField.text = "\(formatTonAmountText(self.maxValue, dateTimeFormat: PresentationDateTimeFormat(timeFormat: self.dateTimeFormat.timeFormat, dateFormat: self.dateTimeFormat.dateFormat, dateSeparator: "", dateSuffix: "", requiresFullYear: false, decimalSeparator: ".", groupingSeparator: "")))" + textField.text = "\(formatTonAmountText(self.maxValue, dateTimeFormat: PresentationDateTimeFormat(timeFormat: self.dateTimeFormat.timeFormat, dateFormat: self.dateTimeFormat.dateFormat, dateSeparator: "", dateSuffix: "", requiresFullYear: false, decimalSeparator: ".", groupingSeparator: ""), maxDecimalPositions: nil))" } self.onTextChanged(text: self.textField.text ?? "") self.animateError() @@ -1592,7 +1592,7 @@ private final class AmountFieldComponent: Component { case .stars: text = "\(value)" case .ton: - text = "\(formatTonAmountText(value, dateTimeFormat: PresentationDateTimeFormat(timeFormat: component.dateTimeFormat.timeFormat, dateFormat: component.dateTimeFormat.dateFormat, dateSeparator: "", dateSuffix: "", requiresFullYear: false, decimalSeparator: ".", groupingSeparator: "")))" + text = "\(formatTonAmountText(value, dateTimeFormat: PresentationDateTimeFormat(timeFormat: component.dateTimeFormat.timeFormat, dateFormat: component.dateTimeFormat.dateFormat, dateSeparator: "", dateSuffix: "", requiresFullYear: false, decimalSeparator: ".", groupingSeparator: ""), maxDecimalPositions: nil))" } self.textField.text = text self.didSetValueOnce = true