Various fixes

This commit is contained in:
Ilya Laktyushin 2025-07-27 23:36:37 +02:00
parent 7a93320a5c
commit cf3bc1edbd
5 changed files with 58 additions and 31 deletions

View File

@ -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..<endIndex])
} else {
balanceText = String(balanceText[balanceText.startIndex..<balanceText.endIndex])
if let maxDecimalPositions {
if let endIndex = balanceText.index(dotIndex.upperBound, offsetBy: maxDecimalPositions, limitedBy: balanceText.endIndex) {
balanceText = String(balanceText[balanceText.startIndex..<endIndex])
} else {
balanceText = String(balanceText[balanceText.startIndex..<balanceText.endIndex])
}
}
let integerPartString = balanceText[..<dotIndex.lowerBound]

View File

@ -421,7 +421,6 @@ private final class GiftViewSheetContent: CombinedComponent {
UndoOverlayController(
presentationData: presentationData,
content: .copy(text: presentationData.strings.Gift_View_CopiedAddress),
elevatedLayout: false,
position: .bottom,
action: { _ in return true }
),
@ -484,6 +483,7 @@ private final class GiftViewSheetContent: CombinedComponent {
undoText: presentationData.strings.Gift_Displayed_View,
customAction: nil
),
elevatedLayout: !(lastController is ChatController),
action: { [weak navigationController] action in
if case .undo = action, let navigationController, let giftsPeerId {
let _ = (self.context.engine.data.get(TelegramEngine.EngineData.Item.Peer.Peer(id: giftsPeerId))
@ -607,6 +607,7 @@ private final class GiftViewSheetContent: CombinedComponent {
customUndoText: nil,
timeout: nil
),
elevatedLayout: !(lastController is ChatController),
action: { _ in return true }
)
lastController.present(resultController, in: .current)
@ -797,6 +798,8 @@ private final class GiftViewSheetContent: CombinedComponent {
return
}
let isTablet = controller.validLayout?.metrics.isTablet ?? false
controller.dismissAllTooltips()
let presentationData = self.context.sharedContext.currentPresentationData.with { $0 }
@ -860,12 +863,12 @@ private final class GiftViewSheetContent: CombinedComponent {
),
position: .bottom,
animateInAsReplacement: false,
appearance: UndoOverlayController.Appearance(sideInset: 16.0, bottomInset: 62.0),
appearance: isTablet ? nil : UndoOverlayController.Appearance(sideInset: 16.0, bottomInset: 62.0),
action: { action in
return false
}
)
controller.present(tooltipController, in: .current)
controller.present(tooltipController, in: isTablet ? .current : .window(.root))
})
}),
TextAlertAction(type: .genericAction, title: presentationData.strings.Common_Cancel, action: {
@ -930,7 +933,7 @@ private final class GiftViewSheetContent: CombinedComponent {
case .stars:
priceString = presentationData.strings.Gift_View_Resale_Relist_Success_Stars(Int32(price.amount.value))
case .ton:
priceString = formatTonAmountText(price.amount.value, dateTimeFormat: presentationData.dateTimeFormat) + " TON"
priceString = formatTonAmountText(price.amount.value, dateTimeFormat: presentationData.dateTimeFormat, maxDecimalPositions: nil) + " TON"
}
text = presentationData.strings.Gift_View_Resale_Relist_Success(giftTitle, priceString).string
}
@ -947,12 +950,12 @@ private final class GiftViewSheetContent: CombinedComponent {
),
position: .bottom,
animateInAsReplacement: false,
appearance: UndoOverlayController.Appearance(sideInset: 16.0, bottomInset: 62.0),
appearance: isTablet ? nil : UndoOverlayController.Appearance(sideInset: 16.0, bottomInset: 62.0),
action: { action in
return false
}
)
controller.present(tooltipController, in: .current)
controller.present(tooltipController, in: isTablet ? .current : .window(.root))
})
})
controller.push(resellController)
@ -1010,7 +1013,7 @@ private final class GiftViewSheetContent: CombinedComponent {
let strings = presentationData.strings
if let _ = arguments.reference, case .unique = arguments.gift, let togglePinnedToTop = controller.togglePinnedToTop, let pinnedToTop = arguments.pinnedToTop {
items.append(.action(ContextMenuActionItem(text: pinnedToTop ? strings.PeerInfo_Gifts_Context_Unpin : strings.PeerInfo_Gifts_Context_Pin , icon: { theme in generateTintedImage(image: UIImage(bundleImageName: pinnedToTop ? "Chat/Context Menu/Unpin" : "Chat/Context Menu/Pin"), color: theme.contextMenu.primaryColor) }, action: { [weak self] c, f in
items.append(.action(ContextMenuActionItem(text: pinnedToTop ? strings.PeerInfo_Gifts_Context_Unpin : strings.PeerInfo_Gifts_Context_Pin , icon: { theme in generateTintedImage(image: UIImage(bundleImageName: pinnedToTop ? "Chat/Context Menu/Unpin" : "Chat/Context Menu/Pin"), color: theme.contextMenu.primaryColor) }, action: { [weak self] c, f in
c?.dismiss(completion: { [weak self, weak controller] in
guard let self, let controller else {
return
@ -1255,7 +1258,7 @@ private final class GiftViewSheetContent: CombinedComponent {
case .stars:
originalPriceString = presentationData.strings.Gift_Buy_ErrorPriceChanged_Text_Stars(Int32(resellAmount.amount.value))
case .ton:
originalPriceString = formatTonAmountText(resellAmount.amount.value, dateTimeFormat: presentationData.dateTimeFormat) + " TON"
originalPriceString = formatTonAmountText(resellAmount.amount.value, dateTimeFormat: presentationData.dateTimeFormat, maxDecimalPositions: nil) + " TON"
}
let newPriceString: String
@ -1265,7 +1268,7 @@ private final class GiftViewSheetContent: CombinedComponent {
newPriceString = presentationData.strings.Gift_Buy_ErrorPriceChanged_Text_Stars(Int32(newPrice.amount.value))
buttonText = presentationData.strings.Gift_Buy_Confirm_BuyFor(Int32(newPrice.amount.value))
case .ton:
let tonValueString = formatTonAmountText(newPrice.amount.value, dateTimeFormat: presentationData.dateTimeFormat)
let tonValueString = formatTonAmountText(newPrice.amount.value, dateTimeFormat: presentationData.dateTimeFormat, maxDecimalPositions: nil)
newPriceString = tonValueString + " TON"
buttonText = presentationData.strings.Gift_Buy_Confirm_BuyForTon(tonValueString).string
}
@ -1321,6 +1324,7 @@ private final class GiftViewSheetContent: CombinedComponent {
let resultController = UndoOverlayController(
presentationData: presentationData,
content: .sticker(context: context, file: animationFile, loop: false, title: presentationData.strings.Gift_View_Resale_SuccessYou_Title, text: presentationData.strings.Gift_View_Resale_SuccessYou_Text(giftTitle).string, undoText: nil, customAction: nil),
elevatedLayout: !(lastController is ChatController),
action: { _ in
return true
}
@ -1342,6 +1346,7 @@ private final class GiftViewSheetContent: CombinedComponent {
let resultController = UndoOverlayController(
presentationData: presentationData,
content: .sticker(context: context, file: animationFile, loop: false, title: presentationData.strings.Gift_View_Resale_Success_Title, text: presentationData.strings.Gift_View_Resale_Success_Text(peer.compactDisplayTitle).string, undoText: nil, customAction: nil),
elevatedLayout: !(lastController is ChatController),
action: { _ in
return true
}
@ -3379,6 +3384,8 @@ private final class GiftViewSheetContent: CombinedComponent {
HapticFeedback().impact(.light)
}))
} else {
let isTablet = environment.metrics.isTablet
let presentationData = context.sharedContext.currentPresentationData.with { $0 }
let text = strings.Gift_View_TooltipPremiumWearing
let tooltipController = UndoOverlayController(
@ -3386,7 +3393,7 @@ private final class GiftViewSheetContent: CombinedComponent {
content: .premiumPaywall(title: nil, text: text, customUndoText: nil, timeout: nil, linkAction: nil),
position: .bottom,
animateInAsReplacement: false,
appearance: UndoOverlayController.Appearance(sideInset: 16.0, bottomInset: 62.0),
appearance: isTablet ? nil : UndoOverlayController.Appearance(sideInset: 16.0, bottomInset: 62.0),
action: { [weak controller, weak state] action in
if case .info = action {
controller?.dismissAllTooltips()
@ -3400,7 +3407,7 @@ private final class GiftViewSheetContent: CombinedComponent {
return false
}
)
controller.present(tooltipController, in: .current)
controller.present(tooltipController, in: isTablet ? .current : .window(.root))
}
} else {
state.commitWear(uniqueGift)
@ -3536,7 +3543,7 @@ private final class GiftViewSheetContent: CombinedComponent {
currencyAmount = formatStarsAmountText(resellAmount.amount, dateTimeFormat: environment.dateTimeFormat)
case .ton:
currencySymbol = "$"
currencyAmount = formatTonAmountText(resellAmount.amount.value, dateTimeFormat: environment.dateTimeFormat)
currencyAmount = formatTonAmountText(resellAmount.amount.value, dateTimeFormat: environment.dateTimeFormat, maxDecimalPositions: nil)
if let starsAmount = uniqueGift?.resellAmounts?.first(where: { $0.currency == .stars }) {
//TODO:localize

View File

@ -45,7 +45,7 @@ public final class StarsBalanceOverlayComponent: Component {
public final class View: UIView {
private let backgroundView = BlurredBackgroundView(color: nil)
private let text = ComponentView<Empty>()
private var text = ComponentView<Empty>()
private let action = ComponentView<Empty>()
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 {

View File

@ -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",

View File

@ -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