Skip to content

Commit 16768ff

Browse files
author
Isaac
committed
Prevent media gallery from opening multiple times
1 parent 99d210e commit 16768ff

File tree

5 files changed

+56
-15
lines changed

5 files changed

+56
-15
lines changed

‎submodules/AccountContext/Sources/OpenChatMessage.swift

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public final class OpenChatMessageParams {
4848
public let gallerySource: GalleryControllerItemSource?
4949
public let centralItemUpdated: ((MessageId) -> Void)?
5050
public let getSourceRect: (() -> CGRect?)?
51+
public let blockInteraction: Promise<Bool>
5152

5253
public init(
5354
context: AccountContext,
@@ -109,5 +110,6 @@ public final class OpenChatMessageParams {
109110
self.gallerySource = gallerySource
110111
self.centralItemUpdated = centralItemUpdated
111112
self.getSourceRect = getSourceRect
113+
self.blockInteraction = Promise()
112114
}
113115
}

‎submodules/GalleryUI/Sources/Items/UniversalVideoGalleryItem.swift

+19-14
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,6 @@ final class UniversalVideoGalleryItemNode: ZoomableContentGalleryItemNode {
10761076

10771077
private var customUnembedWhenPortrait: ((OverlayMediaItemNode) -> Bool)?
10781078

1079-
private var pictureInPictureContent: AnyObject?
10801079
private var nativePictureInPictureContent: AnyObject?
10811080

10821081
private var activePictureInPictureNavigationController: NavigationController?
@@ -1544,10 +1543,6 @@ final class UniversalVideoGalleryItemNode: ZoomableContentGalleryItemNode {
15441543
strongSelf.videoNode?.setBaseRate(playbackRate)
15451544
}
15461545
}
1547-
1548-
if strongSelf.nativePictureInPictureContent == nil {
1549-
strongSelf.setupNativePictureInPicture()
1550-
}
15511546
}
15521547
}
15531548
self.videoNode = videoNode
@@ -2963,19 +2958,29 @@ final class UniversalVideoGalleryItemNode: ZoomableContentGalleryItemNode {
29632958
}
29642959

29652960
@objc func pictureInPictureButtonPressed() {
2966-
if let currentPictureInPictureNode = self.context.sharedContext.mediaManager.currentPictureInPictureNode as? UniversalVideoGalleryItemNode, let currentItem = currentPictureInPictureNode.item, case let .message(currentMessage, _) = currentItem.contentInfo, case let .message(message, _) = self.item?.contentInfo, currentMessage.id == message.id {
2967-
if let controller = self.galleryController() as? GalleryController {
2968-
controller.dismiss(forceAway: true)
2969-
}
2970-
return
2961+
if self.nativePictureInPictureContent == nil {
2962+
self.setupNativePictureInPicture()
29712963
}
29722964

2973-
if #available(iOS 15.0, *) {
2974-
if let nativePictureInPictureContent = self.nativePictureInPictureContent as? NativePictureInPictureContentImpl {
2975-
addAppLogEvent(postbox: self.context.account.postbox, type: "pip_btn", peerId: self.context.account.peerId)
2976-
nativePictureInPictureContent.beginPictureInPicture()
2965+
DispatchQueue.main.async { [weak self] in
2966+
guard let self else {
2967+
return
2968+
}
2969+
2970+
if let currentPictureInPictureNode = self.context.sharedContext.mediaManager.currentPictureInPictureNode as? UniversalVideoGalleryItemNode, let currentItem = currentPictureInPictureNode.item, case let .message(currentMessage, _) = currentItem.contentInfo, case let .message(message, _) = self.item?.contentInfo, currentMessage.id == message.id {
2971+
if let controller = self.galleryController() as? GalleryController {
2972+
controller.dismiss(forceAway: true)
2973+
}
29772974
return
29782975
}
2976+
2977+
if #available(iOS 15.0, *) {
2978+
if let nativePictureInPictureContent = self.nativePictureInPictureContent as? NativePictureInPictureContentImpl {
2979+
addAppLogEvent(postbox: self.context.account.postbox, type: "pip_btn", peerId: self.context.account.peerId)
2980+
nativePictureInPictureContent.beginPictureInPicture()
2981+
return
2982+
}
2983+
}
29792984
}
29802985
}
29812986

‎submodules/TelegramUI/Components/Chat/ChatMessageMediaBubbleContentNode/Sources/ChatMessageMediaBubbleContentNode.swift

+4-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ public class ChatMessageMediaBubbleContentNode: ChatMessageBubbleContentNode {
5858
openChatMessageMode = .automaticPlayback
5959
}
6060

61-
let _ = item.controllerInteraction.openMessage(item.message, OpenMessageParams(mode: openChatMessageMode, mediaIndex: self.mediaIndex, progress: self.itemNode?.makeProgress()))
61+
if !item.controllerInteraction.isOpeningMedia {
62+
let params = OpenMessageParams(mode: openChatMessageMode, mediaIndex: self.mediaIndex, progress: self.itemNode?.makeProgress())
63+
let _ = item.controllerInteraction.openMessage(item.message, params)
64+
}
6265
}
6366

6467
self.interactiveImageNode.activateAgeRestrictedMedia = { [weak self] in

‎submodules/TelegramUI/Components/ChatControllerInteraction/Sources/ChatControllerInteraction.swift

+27
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,29 @@ public final class ChatControllerInteraction: ChatControllerInteractionProtocol
303303
public var chatIsRotated: Bool = true
304304
public var canReadHistory: Bool = false
305305

306+
private var isOpeningMediaValue: Bool = false
307+
public var isOpeningMedia: Bool {
308+
return self.isOpeningMediaValue
309+
}
310+
private var isOpeningMediaDisposable: Disposable?
311+
public var isOpeningMediaSignal: Signal<Bool, NoError>? {
312+
didSet {
313+
self.isOpeningMediaDisposable?.dispose()
314+
self.isOpeningMediaDisposable = nil
315+
self.isOpeningMediaValue = false
316+
317+
if let isOpeningMediaSignal = self.isOpeningMediaSignal {
318+
self.isOpeningMediaValue = true
319+
self.isOpeningMediaDisposable = (isOpeningMediaSignal |> filter { !$0 } |> take(1) |> timeout(1.0, queue: .mainQueue(), alternate: .single(false)) |> deliverOnMainQueue).startStrict(next: { [weak self] _ in
320+
guard let self else {
321+
return
322+
}
323+
self.isOpeningMediaValue = false
324+
})
325+
}
326+
}
327+
}
328+
306329
public init(
307330
openMessage: @escaping (Message, OpenMessageParams) -> Bool,
308331
openPeer: @escaping (EnginePeer, ChatControllerInteractionNavigateToPeer, MessageReference?, OpenPeerSource) -> Void,
@@ -538,4 +561,8 @@ public final class ChatControllerInteraction: ChatControllerInteractionProtocol
538561

539562
self.presentationContext = presentationContext
540563
}
564+
565+
deinit {
566+
self.isOpeningMediaDisposable?.dispose()
567+
}
541568
}

‎submodules/TelegramUI/Sources/OpenChatMessage.swift

+4
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,12 @@ func openChatMessageImpl(_ params: OpenChatMessageParams) -> Bool {
323323
return true
324324
}
325325

326+
params.blockInteraction.set(.single(true))
327+
326328
let _ = (gallery
327329
|> deliverOnMainQueue).startStandalone(next: { gallery in
330+
params.blockInteraction.set(.single(false))
331+
328332
gallery.centralItemUpdated = { messageId in
329333
params.centralItemUpdated?(messageId)
330334
}

0 commit comments

Comments
 (0)