From bd024971245ea723aa5a14937c8aa42928cb2e0a Mon Sep 17 00:00:00 2001 From: Eduard Bosch Bertran Date: Mon, 6 May 2019 22:09:29 +0200 Subject: [PATCH] fix: Skeleton new cells if container is skeletoned --- .../Collections/SkeletonCollectionDataSource.swift | 13 +++++++++++++ Sources/SkeletonView.swift | 9 ++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Sources/Collections/SkeletonCollectionDataSource.swift b/Sources/Collections/SkeletonCollectionDataSource.swift index 181aeea..01ea8d6 100644 --- a/Sources/Collections/SkeletonCollectionDataSource.swift +++ b/Sources/Collections/SkeletonCollectionDataSource.swift @@ -38,6 +38,7 @@ extension SkeletonCollectionDataSource: UITableViewDataSource { func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cellIdentifier = originalTableViewDataSource?.collectionSkeletonView(tableView, cellIdentifierForRowAt: indexPath) ?? "" let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) + skeletonCellIfContainerSkeletonIsActive(container: tableView, cell: cell) return cell } } @@ -56,6 +57,18 @@ extension SkeletonCollectionDataSource: UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cellIdentifier = originalCollectionViewDataSource?.collectionSkeletonView(collectionView, cellIdentifierForItemAt: indexPath) ?? "" let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) + skeletonCellIfContainerSkeletonIsActive(container: collectionView, cell: cell) return cell } } + +extension SkeletonCollectionDataSource { + private func skeletonCellIfContainerSkeletonIsActive(container: UIView, cell: UIView) { + guard container.isSkeletonActive, + let skeletonConfig = container.currentSkeletonConfig else { + return + } + + cell.showSkeleton(usingConfig: skeletonConfig) + } +} diff --git a/Sources/SkeletonView.swift b/Sources/SkeletonView.swift index 37523e9..c63eff8 100644 --- a/Sources/SkeletonView.swift +++ b/Sources/SkeletonView.swift @@ -20,6 +20,10 @@ public extension UIView { showSkeleton(withType: .gradient, usingColors: gradient.colors, animated: true, animation: animation) } + internal func showSkeleton(usingConfig config: SkeletonConfig) { + showSkeleton(withType: config.type, usingColors: config.colors, animated: config.animated, animation: config.animation) + } + func updateSkeleton(usingColor color: UIColor = SkeletonAppearance.default.tintColor) { updateSkeleton(withType: .solid, usingColors: [color]) } @@ -81,6 +85,8 @@ extension UIView { fileprivate func recursiveShowSkeleton(withType type: SkeletonType, usingColors colors: [UIColor], animated: Bool, animation: SkeletonLayerAnimation?, root: UIView? = nil) { layoutIfNeeded() + currentSkeletonConfig = SkeletonConfig(type: type, colors: colors, gradientDirection: nil, animated: animated, animation: animation) + addDummyDataSourceIfNeeded() subviewsSkeletonables.recursiveSearch(leafBlock: { showSkeletonIfNotActive(withType: type, usingColors: colors, animated: animated, animation: animation) @@ -96,6 +102,8 @@ extension UIView { fileprivate func recursiveUpdateSkeleton(withType type: SkeletonType, usingColors colors: [UIColor], animated: Bool, animation: SkeletonLayerAnimation?, root: UIView? = nil) { layoutIfNeeded() + currentSkeletonConfig = SkeletonConfig(type: type, colors: colors, gradientDirection: nil, animated: animated, animation: animation) + updateDummyDataSourceIfNeeded() subviewsSkeletonables.recursiveSearch(leafBlock: { guard isSkeletonActive else { return } @@ -195,7 +203,6 @@ extension UIView { layer.insertSublayer(skeletonLayer.contentLayer, at: UInt32.max) if animated { skeletonLayer.start(animation) } status = .on - currentSkeletonConfig = SkeletonConfig(type: type, colors: colors, gradientDirection: direction, animated: animated, animation: animation) } func updateSkeletonLayer(usingColors colors: [UIColor], gradientDirection direction: GradientDirection? = nil, animated: Bool, animation: SkeletonLayerAnimation? = nil) {