Shared media improvements

This commit is contained in:
Ali 2021-10-25 18:31:11 +04:00
parent 80fe21681e
commit d6f420a34b
3 changed files with 46 additions and 37 deletions

View File

@ -329,6 +329,7 @@ public final class SparseItemGrid: ASDisplayNode {
final class VisibleItem: SparseItemGridDisplayItem { final class VisibleItem: SparseItemGridDisplayItem {
let layer: SparseItemGridLayer? let layer: SparseItemGridLayer?
let view: SparseItemGridView? let view: SparseItemGridView?
var shimmerLayer: SparseItemGridShimmerLayer?
init(layer: SparseItemGridLayer?, view: SparseItemGridView?) { init(layer: SparseItemGridLayer?, view: SparseItemGridView?) {
self.layer = layer self.layer = layer
@ -470,12 +471,12 @@ public final class SparseItemGrid: ASDisplayNode {
self.view.addSubview(self.scrollView) self.view.addSubview(self.scrollView)
} }
func update(containerLayout: ContainerLayout, items: Items, restoreScrollPosition: (y: CGFloat, index: Int)?) { func update(containerLayout: ContainerLayout, items: Items, restoreScrollPosition: (y: CGFloat, index: Int)?, synchronous: Bool) {
if self.layout?.containerLayout != containerLayout || self.items !== items { if self.layout?.containerLayout != containerLayout || self.items !== items {
self.layout = Layout(containerLayout: containerLayout, zoomLevel: self.zoomLevel) self.layout = Layout(containerLayout: containerLayout, zoomLevel: self.zoomLevel)
self.items = items self.items = items
self.updateVisibleItems(resetScrolling: true, synchronous: false, restoreScrollPosition: restoreScrollPosition) self.updateVisibleItems(resetScrolling: true, synchronous: synchronous, restoreScrollPosition: restoreScrollPosition)
} }
} }
@ -691,10 +692,6 @@ public final class SparseItemGrid: ASDisplayNode {
self.scrollView.setContentOffset(self.scrollView.contentOffset, animated: false) self.scrollView.setContentOffset(self.scrollView.contentOffset, animated: false)
} }
func updateShimmerLayers() {
self.updateVisibleItems(resetScrolling: false, synchronous: false, restoreScrollPosition: nil)
}
private func updateVisibleItems(resetScrolling: Bool, synchronous: Bool, restoreScrollPosition: (y: CGFloat, index: Int)?) { private func updateVisibleItems(resetScrolling: Bool, synchronous: Bool, restoreScrollPosition: (y: CGFloat, index: Int)?) {
guard let layout = self.layout, let items = self.items else { guard let layout = self.layout, let items = self.items else {
return return
@ -770,19 +767,21 @@ public final class SparseItemGrid: ASDisplayNode {
if itemLayer.needsShimmer { if itemLayer.needsShimmer {
let placeholderLayer: SparseItemGridShimmerLayer let placeholderLayer: SparseItemGridShimmerLayer
if self.visiblePlaceholders.count > usedPlaceholderCount { if let current = itemLayer.shimmerLayer {
placeholderLayer = self.visiblePlaceholders[usedPlaceholderCount] placeholderLayer = current
} else { } else {
placeholderLayer = items.itemBinding.createShimmerLayer() ?? Shimmer.Layer() placeholderLayer = items.itemBinding.createShimmerLayer() ?? Shimmer.Layer()
self.scrollView.layer.insertSublayer(placeholderLayer, at: 0) self.scrollView.layer.insertSublayer(placeholderLayer, at: 0)
self.visiblePlaceholders.append(placeholderLayer) itemLayer.shimmerLayer = placeholderLayer
} }
let itemFrame = layout.frame(at: index) let itemFrame = layout.frame(at: index)
placeholderLayer.frame = itemFrame placeholderLayer.frame = itemFrame
self.shimmer.update(colors: shimmerColors, layer: placeholderLayer, containerSize: layout.containerLayout.size, frame: itemFrame.offsetBy(dx: 0.0, dy: -visibleBounds.minY)) self.shimmer.update(colors: shimmerColors, layer: placeholderLayer, containerSize: layout.containerLayout.size, frame: itemFrame.offsetBy(dx: 0.0, dy: -visibleBounds.minY))
placeholderLayer.update(size: itemFrame.size) placeholderLayer.update(size: itemFrame.size)
usedPlaceholderCount += 1 } else if let placeholderLayer = itemLayer.shimmerLayer {
itemLayer.shimmerLayer = nil
placeholderLayer.removeFromSuperlayer()
} }
validIds.insert(item.id) validIds.insert(item.id)
@ -1185,7 +1184,7 @@ public final class SparseItemGrid: ASDisplayNode {
}) })
nextViewport.frame = CGRect(origin: CGPoint(), size: containerLayout.size) nextViewport.frame = CGRect(origin: CGPoint(), size: containerLayout.size)
nextViewport.update(containerLayout: containerLayout, items: items, restoreScrollPosition: restoreScrollPosition) nextViewport.update(containerLayout: containerLayout, items: items, restoreScrollPosition: restoreScrollPosition, synchronous: false)
self.currentViewportTransition?.removeFromSupernode() self.currentViewportTransition?.removeFromSupernode()
@ -1235,7 +1234,7 @@ public final class SparseItemGrid: ASDisplayNode {
}) })
nextViewport.frame = CGRect(origin: CGPoint(), size: containerLayout.size) nextViewport.frame = CGRect(origin: CGPoint(), size: containerLayout.size)
nextViewport.update(containerLayout: containerLayout, items: items, restoreScrollPosition: restoreScrollPosition) nextViewport.update(containerLayout: containerLayout, items: items, restoreScrollPosition: restoreScrollPosition, synchronous: false)
let currentViewportTransition = ViewportTransition(interactiveState: interactiveState, layout: containerLayout, anchorItemIndex: anchorItemIndex, from: previousViewport, to: nextViewport, coveringOffsetUpdated: { [weak self] transition in let currentViewportTransition = ViewportTransition(interactiveState: interactiveState, layout: containerLayout, anchorItemIndex: anchorItemIndex, from: previousViewport, to: nextViewport, coveringOffsetUpdated: { [weak self] transition in
self?.transitionCoveringOffsetUpdated(transition: transition) self?.transitionCoveringOffsetUpdated(transition: transition)
@ -1272,7 +1271,7 @@ public final class SparseItemGrid: ASDisplayNode {
strongSelf.scrollingArea.frame = CGRect(origin: CGPoint(), size: containerLayout.size) strongSelf.scrollingArea.frame = CGRect(origin: CGPoint(), size: containerLayout.size)
currentViewport.setScrollingArea(scrollingArea: strongSelf.scrollingArea) currentViewport.setScrollingArea(scrollingArea: strongSelf.scrollingArea)
currentViewport.frame = CGRect(origin: CGPoint(), size: containerLayout.size) currentViewport.frame = CGRect(origin: CGPoint(), size: containerLayout.size)
currentViewport.update(containerLayout: containerLayout, items: items, restoreScrollPosition: nil) currentViewport.update(containerLayout: containerLayout, items: items, restoreScrollPosition: nil, synchronous: false)
} }
strongSelf.currentViewportTransition = nil strongSelf.currentViewportTransition = nil
@ -1284,7 +1283,7 @@ public final class SparseItemGrid: ASDisplayNode {
} }
} }
public func update(size: CGSize, insets: UIEdgeInsets, scrollIndicatorInsets: UIEdgeInsets, lockScrollingAtTop: Bool, fixedItemHeight: CGFloat?, items: Items) { public func update(size: CGSize, insets: UIEdgeInsets, scrollIndicatorInsets: UIEdgeInsets, lockScrollingAtTop: Bool, fixedItemHeight: CGFloat?, items: Items, synchronous: Bool) {
let containerLayout = ContainerLayout(size: size, insets: insets, scrollIndicatorInsets: scrollIndicatorInsets, lockScrollingAtTop: lockScrollingAtTop, fixedItemHeight: fixedItemHeight) let containerLayout = ContainerLayout(size: size, insets: insets, scrollIndicatorInsets: scrollIndicatorInsets, lockScrollingAtTop: lockScrollingAtTop, fixedItemHeight: fixedItemHeight)
self.containerLayout = containerLayout self.containerLayout = containerLayout
self.items = items self.items = items
@ -1312,7 +1311,7 @@ public final class SparseItemGrid: ASDisplayNode {
} else if let currentViewport = self.currentViewport { } else if let currentViewport = self.currentViewport {
self.scrollingArea.frame = CGRect(origin: CGPoint(), size: size) self.scrollingArea.frame = CGRect(origin: CGPoint(), size: size)
currentViewport.frame = CGRect(origin: CGPoint(), size: size) currentViewport.frame = CGRect(origin: CGPoint(), size: size)
currentViewport.update(containerLayout: containerLayout, items: items, restoreScrollPosition: nil) currentViewport.update(containerLayout: containerLayout, items: items, restoreScrollPosition: nil, synchronous: synchronous)
} }
} }
@ -1395,7 +1394,7 @@ public final class SparseItemGrid: ASDisplayNode {
self.scrollingArea.frame = CGRect(origin: CGPoint(), size: containerLayout.size) self.scrollingArea.frame = CGRect(origin: CGPoint(), size: containerLayout.size)
currentViewport.frame = CGRect(origin: CGPoint(), size: containerLayout.size) currentViewport.frame = CGRect(origin: CGPoint(), size: containerLayout.size)
currentViewport.update(containerLayout: containerLayout, items: items, restoreScrollPosition: restoreScrollPosition) currentViewport.update(containerLayout: containerLayout, items: items, restoreScrollPosition: restoreScrollPosition, synchronous: false)
let currentViewportTransition = ViewportTransition(interactiveState: nil, layout: containerLayout, anchorItemIndex: anchorItemIndex, from: previousViewport, to: currentViewport, coveringOffsetUpdated: { [weak self] transition in let currentViewportTransition = ViewportTransition(interactiveState: nil, layout: containerLayout, anchorItemIndex: anchorItemIndex, from: previousViewport, to: currentViewport, coveringOffsetUpdated: { [weak self] transition in
self?.transitionCoveringOffsetUpdated(transition: transition) self?.transitionCoveringOffsetUpdated(transition: transition)
@ -1413,7 +1412,7 @@ public final class SparseItemGrid: ASDisplayNode {
strongSelf.insertSubnode(currentViewport, belowSubnode: strongSelf.scrollingArea) strongSelf.insertSubnode(currentViewport, belowSubnode: strongSelf.scrollingArea)
strongSelf.scrollingArea.frame = CGRect(origin: CGPoint(), size: containerLayout.size) strongSelf.scrollingArea.frame = CGRect(origin: CGPoint(), size: containerLayout.size)
currentViewport.frame = CGRect(origin: CGPoint(), size: containerLayout.size) currentViewport.frame = CGRect(origin: CGPoint(), size: containerLayout.size)
currentViewport.update(containerLayout: containerLayout, items: items, restoreScrollPosition: nil) currentViewport.update(containerLayout: containerLayout, items: items, restoreScrollPosition: nil, synchronous: false)
} }
strongSelf.currentViewport?.setScrollingArea(scrollingArea: strongSelf.scrollingArea) strongSelf.currentViewport?.setScrollingArea(scrollingArea: strongSelf.scrollingArea)
@ -1500,11 +1499,13 @@ public final class SparseItemGrid: ASDisplayNode {
self.scrollingArea.hideScroller() self.scrollingArea.hideScroller()
} }
public func updateShimmerLayers() { public func updateShimmerLayers(item: SparseItemGridDisplayItem) {
self.currentViewport?.updateShimmerLayers() guard let item = item as? Viewport.VisibleItem else {
if let currentViewportTransition = self.currentViewportTransition { return
currentViewportTransition.fromViewport.updateShimmerLayers() }
currentViewportTransition.toViewport.updateShimmerLayers() if let itemShimmerLayer = item.shimmerLayer, !item.needsShimmer {
item.shimmerLayer = nil
itemShimmerLayer.removeFromSuperlayer()
} }
} }
} }

View File

@ -169,11 +169,11 @@ public final class SparseMessageList {
private func resetTopSection() { private func resetTopSection() {
let count: Int let count: Int
#if DEBUG /*#if DEBUG
count = 20 count = 20
#else #else*/
count = 200 count = 200
#endif //#endif
self.topItemsDisposable.set((self.account.postbox.aroundMessageHistoryViewForLocation(.peer(peerId), anchor: .upperBound, count: count, fixedCombinedReadStates: nil, topTaggedMessageIdNamespaces: Set(), tagMask: self.messageTag, appendMessagesFromTheSameGroup: false, namespaces: .not(Set(Namespaces.Message.allScheduled)), orderStatistics: []) self.topItemsDisposable.set((self.account.postbox.aroundMessageHistoryViewForLocation(.peer(peerId), anchor: .upperBound, count: count, fixedCombinedReadStates: nil, topTaggedMessageIdNamespaces: Set(), tagMask: self.messageTag, appendMessagesFromTheSameGroup: false, namespaces: .not(Set(Namespaces.Message.allScheduled)), orderStatistics: [])
|> deliverOn(self.queue)).start(next: { [weak self] view, updateType, _ in |> deliverOn(self.queue)).start(next: { [weak self] view, updateType, _ in
guard let strongSelf = self else { guard let strongSelf = self else {

View File

@ -979,7 +979,7 @@ private final class SparseItemGridBindingImpl: SparseItemGridBinding, ListShimme
var coveringInsetOffsetUpdatedImpl: ((ContainedViewLayoutTransition) -> Void)? var coveringInsetOffsetUpdatedImpl: ((ContainedViewLayoutTransition) -> Void)?
var onBeginFastScrollingImpl: (() -> Void)? var onBeginFastScrollingImpl: (() -> Void)?
var getShimmerColorsImpl: (() -> SparseItemGrid.ShimmerColors)? var getShimmerColorsImpl: (() -> SparseItemGrid.ShimmerColors)?
var updateShimmerLayersImpl: (() -> Void)? var updateShimmerLayersImpl: ((SparseItemGridDisplayItem) -> Void)?
private var shimmerImages: [CGFloat: UIImage] = [:] private var shimmerImages: [CGFloat: UIImage] = [:]
@ -1115,8 +1115,10 @@ private final class SparseItemGridBindingImpl: SparseItemGridBinding, ListShimme
continue continue
} }
let displayItem = layers[i]
if self.useListItems { if self.useListItems {
guard let view = layers[i].view as? ItemView else { guard let view = displayItem.view as? ItemView else {
continue continue
} }
view.bind( view.bind(
@ -1129,7 +1131,7 @@ private final class SparseItemGridBindingImpl: SparseItemGridBinding, ListShimme
size: view.bounds.size size: view.bounds.size
) )
} else { } else {
guard let layer = layers[i].layer as? ItemLayer else { guard let layer = displayItem.layer as? ItemLayer else {
continue continue
} }
if layer.bounds.isEmpty { if layer.bounds.isEmpty {
@ -1171,7 +1173,7 @@ private final class SparseItemGridBindingImpl: SparseItemGridBinding, ListShimme
} }
if let loadSignal = result.loadSignal { if let loadSignal = result.loadSignal {
layer.disposable = (loadSignal layer.disposable = (loadSignal
|> deliverOnMainQueue).start(next: { [weak self, weak layer] image in |> deliverOnMainQueue).start(next: { [weak self, weak layer, weak displayItem] image in
guard let layer = layer else { guard let layer = layer else {
return return
} }
@ -1193,7 +1195,9 @@ private final class SparseItemGridBindingImpl: SparseItemGridBinding, ListShimme
layer.animateAlpha(from: 0.0, to: 1.0, duration: 0.2) layer.animateAlpha(from: 0.0, to: 1.0, duration: 0.2)
self?.updateShimmerLayersImpl?() if let displayItem = displayItem {
self?.updateShimmerLayersImpl?(displayItem)
}
} }
}) })
} }
@ -1322,6 +1326,7 @@ final class PeerInfoVisualMediaPaneNode: ASDisplayNode, PeerInfoPaneNode, UIScro
private let itemGridBinding: SparseItemGridBindingImpl private let itemGridBinding: SparseItemGridBindingImpl
private let directMediaImageCache: DirectMediaImageCache private let directMediaImageCache: DirectMediaImageCache
private var items: SparseItemGrid.Items? private var items: SparseItemGrid.Items?
private var didUpdateItemsOnce: Bool = false
private var isDeceleratingAfterTracking = false private var isDeceleratingAfterTracking = false
@ -1534,8 +1539,8 @@ final class PeerInfoVisualMediaPaneNode: ASDisplayNode, PeerInfoPaneNode, UIScro
return SparseItemGrid.ShimmerColors(background: backgroundColor.argb, foreground: foregroundColor.argb) return SparseItemGrid.ShimmerColors(background: backgroundColor.argb, foreground: foregroundColor.argb)
} }
self.itemGridBinding.updateShimmerLayersImpl = { [weak self] in self.itemGridBinding.updateShimmerLayersImpl = { [weak self] layer in
self?.itemGrid.updateShimmerLayers() self?.itemGrid.updateShimmerLayers(item: layer)
} }
self.itemGrid.cancelExternalContentGestures = { [weak self] in self.itemGrid.cancelExternalContentGestures = { [weak self] in
@ -1646,14 +1651,15 @@ final class PeerInfoVisualMediaPaneNode: ASDisplayNode, PeerInfoPaneNode, UIScro
self.storedStateDisposable = (visualMediaStoredState(postbox: context.account.postbox, peerId: peerId, messageTag: self.stateTag) self.storedStateDisposable = (visualMediaStoredState(postbox: context.account.postbox, peerId: peerId, messageTag: self.stateTag)
|> deliverOnMainQueue).start(next: { [weak self] value in |> deliverOnMainQueue).start(next: { [weak self] value in
guard let strongSelf = self, let value = value else { guard let strongSelf = self else {
return return
} }
strongSelf.updateZoomLevel(level: ZoomLevel(rawValue: value.zoomLevel)) if let value = value {
strongSelf.updateZoomLevel(level: ZoomLevel(rawValue: value.zoomLevel))
}
strongSelf.requestHistoryAroundVisiblePosition(synchronous: false, reloadAtTop: false)
}) })
self.requestHistoryAroundVisiblePosition(synchronous: false, reloadAtTop: false)
self.hiddenMediaDisposable = context.sharedContext.mediaManager.galleryHiddenMediaManager.hiddenIds().start(next: { [weak self] ids in self.hiddenMediaDisposable = context.sharedContext.mediaManager.galleryHiddenMediaManager.hiddenIds().start(next: { [weak self] ids in
guard let strongSelf = self else { guard let strongSelf = self else {
return return
@ -2086,6 +2092,8 @@ final class PeerInfoVisualMediaPaneNode: ASDisplayNode, PeerInfoPaneNode, UIScro
transition.updateFrame(node: self.itemGrid, frame: CGRect(origin: CGPoint(x: 0.0, y: 0.0), size: CGSize(width: size.width, height: size.height))) transition.updateFrame(node: self.itemGrid, frame: CGRect(origin: CGPoint(x: 0.0, y: 0.0), size: CGSize(width: size.width, height: size.height)))
if let items = self.items { if let items = self.items {
let wasFirstTime = !self.didUpdateItemsOnce
self.didUpdateItemsOnce = true
let fixedItemHeight: CGFloat? let fixedItemHeight: CGFloat?
switch self.contentType { switch self.contentType {
case .files, .music, .voiceAndVideoMessages: case .files, .music, .voiceAndVideoMessages:
@ -2146,7 +2154,7 @@ final class PeerInfoVisualMediaPaneNode: ASDisplayNode, PeerInfoPaneNode, UIScro
fixedItemHeight = nil fixedItemHeight = nil
} }
self.itemGrid.update(size: size, insets: UIEdgeInsets(top: topInset, left: sideInset, bottom: bottomInset, right: sideInset), scrollIndicatorInsets: UIEdgeInsets(top: 0.0, left: sideInset, bottom: bottomInset, right: sideInset), lockScrollingAtTop: isScrollingLockedAtTop, fixedItemHeight: fixedItemHeight, items: items) self.itemGrid.update(size: size, insets: UIEdgeInsets(top: topInset, left: sideInset, bottom: bottomInset, right: sideInset), scrollIndicatorInsets: UIEdgeInsets(top: 0.0, left: sideInset, bottom: bottomInset, right: sideInset), lockScrollingAtTop: isScrollingLockedAtTop, fixedItemHeight: fixedItemHeight, items: items, synchronous: wasFirstTime)
} }
} }