diff --git a/submodules/TelegramBaseController/Sources/TelegramBaseController.swift b/submodules/TelegramBaseController/Sources/TelegramBaseController.swift index 5c7dbc2da9..4e5390fe96 100644 --- a/submodules/TelegramBaseController/Sources/TelegramBaseController.swift +++ b/submodules/TelegramBaseController/Sources/TelegramBaseController.swift @@ -553,7 +553,7 @@ open class TelegramBaseController: ViewController, KeyShortcutResponder { guard let self else { return } - if !"".isEmpty, self.giftAuctionStates.count == 1, let gift = self.giftAuctionStates.first?.gift, case let .generic(gift) = gift { + if self.giftAuctionStates.count == 1, let gift = self.giftAuctionStates.first?.gift, case let .generic(gift) = gift { if let giftAuctionsManager = self.context.giftAuctionsManager { let _ = (giftAuctionsManager.auctionContext(for: .giftId(gift.id)) |> deliverOnMainQueue).start(next: { [weak self] auction in diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Payments/StarGiftsAuctions.swift b/submodules/TelegramCore/Sources/TelegramEngine/Payments/StarGiftsAuctions.swift index 1a7ffbdee5..a032c64bc5 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Payments/StarGiftsAuctions.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Payments/StarGiftsAuctions.swift @@ -448,17 +448,60 @@ public class GiftAuctionsManager { } public extension GiftAuctionContext.State { - var place: Int32? { - guard case let .ongoing(_, _, _, _, bidLevels, _, _, _, _, _) = self.auctionState, let myBid = self.myState.bidAmount, let myBidDate = self.myState.bidDate else { + func getPlace(myBid: Int64?, myBidDate: Int32?) -> Int32? { + guard case let .ongoing(_, _, _, _, bidLevels, _, _, _, _, _) = self.auctionState else { return nil } - var place: Int32 = 1 - for level in bidLevels { - if myBid < level.amount || (myBid == level.amount && myBidDate > level.date) { - place = level.position + 1 + guard let myBid = myBid ?? self.myState.bidAmount else { + return nil + } + let currentTime = Int32(CFAbsoluteTimeGetCurrent() + kCFAbsoluteTimeIntervalSince1970) + let myBidDate = self.myState.bidDate ?? currentTime + + let levels = bidLevels + guard !levels.isEmpty else { + return 1 + } + + func isWorse(than level: GiftAuctionContext.State.BidLevel) -> Bool { + if myBid < level.amount { + return true + } + if myBid == level.amount, myBidDate > level.date { + return true + } + return false + } + + var lowerIndex: Int = -1 + for (i, level) in levels.enumerated() { + if isWorse(than: level) { + lowerIndex = i + } else { + break } } - return place + if lowerIndex == -1 { + return 1 + } + + let lowerPosition = levels[lowerIndex].position + let nextPosition: Int32 + let nextIndex = lowerIndex + 1 + if nextIndex < levels.count { + nextPosition = levels[nextIndex].position + } else { + nextPosition = lowerPosition + } + if nextPosition == lowerPosition + 1 { + return lowerPosition + 1 + } else { + return nextPosition + } + } + + var place: Int32? { + return self.getPlace(myBid: nil, myBidDate: nil) } var startDate: Int32 { diff --git a/submodules/TelegramUI/Components/Gifts/GiftViewScreen/Sources/GiftAuctionBidScreen.swift b/submodules/TelegramUI/Components/Gifts/GiftViewScreen/Sources/GiftAuctionBidScreen.swift index fb4109cb2f..7545f87984 100644 --- a/submodules/TelegramUI/Components/Gifts/GiftViewScreen/Sources/GiftAuctionBidScreen.swift +++ b/submodules/TelegramUI/Components/Gifts/GiftViewScreen/Sources/GiftAuctionBidScreen.swift @@ -1781,6 +1781,8 @@ private final class GiftAuctionBidScreenComponent: Component { self.backgroundLayer.backgroundColor = environment.theme.actionSheet.opaqueItemBackgroundColor.cgColor } + let currentTime = Int32(CFAbsoluteTimeGetCurrent() + kCFAbsoluteTimeIntervalSince1970) + transition.setFrame(view: self.dimView, frame: CGRect(origin: CGPoint(), size: availableSize)) var contentHeight: CGFloat = 0.0 @@ -2055,7 +2057,6 @@ private final class GiftAuctionBidScreenComponent: Component { minBidAnimatedItems.append(AnimatedTextComponent.Item(id: "static", content: .text(minBidString))) } - let currentTime = Int32(CFAbsoluteTimeGetCurrent() + kCFAbsoluteTimeIntervalSince1970) let dropTimeout = max(0, nextDropDate - currentTime) let minutes = Int(dropTimeout / 60) @@ -2235,19 +2236,16 @@ private final class GiftAuctionBidScreenComponent: Component { var topBidsComponents: [(EnginePeer.Id, AnyComponent)] = [] if let giftAuctionState = self.giftAuctionState, case let .ongoing(_, _, _, _, bidLevels, topBidders, _, _, _, _) = giftAuctionState.auctionState { - if var myBidAmount = giftAuctionState.myState.bidAmount, let myBidDate = giftAuctionState.myState.bidDate, let peer = self.peersMap[component.context.account.peerId] { + if var myBidAmount = giftAuctionState.myState.bidAmount, var myBidDate = giftAuctionState.myState.bidDate, let peer = self.peersMap[component.context.account.peerId] { var place: Int32 = 1 var isBiddingUp = false if self.amount.realValue > myBidAmount { myBidAmount = Int64(self.amount.realValue) + myBidDate = currentTime isBiddingUp = true } - for level in bidLevels { - if myBidAmount < level.amount || (myBidAmount == level.amount && myBidDate > level.date) { - place = level.position + 1 - } - } - + place = giftAuctionState.getPlace(myBid: myBidAmount, myBidDate: myBidDate) ?? 1 + var bidTitle: String var bidTitleColor: UIColor var bidStatus: PeerComponent.Status?