Skip to content

Commit

Permalink
사이즈 캐싱 로직 구현 및 usesCachedViewSize 피쳐플래그 연동해요.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaxtynSong committed Jan 24, 2025
1 parent b4ef008 commit b597f56
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Copyright (c) 2025 Danggeun Market Inc.
//

import UIKit

extension UIView {

func shouldInvalidateContentSize(
previousTraitCollection: UITraitCollection?
) -> Bool {
if traitCollection.preferredContentSizeCategory != previousTraitCollection?.preferredContentSizeCategory {
return true
}

if traitCollection.legibilityWeight != previousTraitCollection?.legibilityWeight {
return true
}

if traitCollection.horizontalSizeClass != previousTraitCollection?.horizontalSizeClass ||
traitCollection.verticalSizeClass != previousTraitCollection?.verticalSizeClass {
return true
}

return false
}
}
31 changes: 31 additions & 0 deletions Sources/KarrotListKit/View/UICollectionComponentReusableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ final class UICollectionComponentReusableView: UICollectionReusableView, Compone

var onSizeChanged: ((CGSize) -> Void)?

private var previousBounds: CGSize = .zero

// MARK: - Initializing

@available(*, unavailable)
Expand All @@ -29,6 +31,30 @@ final class UICollectionComponentReusableView: UICollectionReusableView, Compone

// MARK: - Override Methods

public override func traitCollectionDidChange(
_ previousTraitCollection: UITraitCollection?
) {
super.traitCollectionDidChange(previousTraitCollection)

if shouldInvalidateContentSize(
previousTraitCollection: previousTraitCollection
) {
previousBounds = .zero
}
}

public override func prepareForReuse() {
super.prepareForReuse()

previousBounds = .zero
}

public override func layoutSubviews() {
super.layoutSubviews()

previousBounds = bounds.size
}

override func preferredLayoutAttributesFitting(
_ layoutAttributes: UICollectionViewLayoutAttributes
) -> UICollectionViewLayoutAttributes {
Expand All @@ -38,6 +64,11 @@ final class UICollectionComponentReusableView: UICollectionReusableView, Compone
return attributes
}

if KarrotListKitFeatureFlag.provider.isEnabled(for: .usesCachedViewSize),
previousBounds == attributes.size {
return attributes
}

let size = renderedContent.sizeThatFits(bounds.size)

if renderedComponent != nil {
Expand Down
26 changes: 26 additions & 0 deletions Sources/KarrotListKit/View/UICollectionViewComponentCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public final class UICollectionViewComponentCell: UICollectionViewCell, Componen

var onSizeChanged: ((CGSize) -> Void)?

private var previousBounds: CGSize = .zero

// MARK: - Initializing

@available(*, unavailable)
Expand All @@ -37,12 +39,31 @@ public final class UICollectionViewComponentCell: UICollectionViewCell, Componen

// MARK: - Override Methods

public override func traitCollectionDidChange(
_ previousTraitCollection: UITraitCollection?
) {
super.traitCollectionDidChange(previousTraitCollection)

if shouldInvalidateContentSize(
previousTraitCollection: previousTraitCollection
) {
previousBounds = .zero
}
}

public override func prepareForReuse() {
super.prepareForReuse()

previousBounds = .zero
cancellables?.forEach { $0.cancel() }
}

public override func layoutSubviews() {
super.layoutSubviews()

previousBounds = bounds.size
}

public override func preferredLayoutAttributesFitting(
_ layoutAttributes: UICollectionViewLayoutAttributes
) -> UICollectionViewLayoutAttributes {
Expand All @@ -52,6 +73,11 @@ public final class UICollectionViewComponentCell: UICollectionViewCell, Componen
return attributes
}

if KarrotListKitFeatureFlag.provider.isEnabled(for: .usesCachedViewSize),
previousBounds == attributes.size {
return attributes
}

let size = renderedContent.sizeThatFits(contentView.bounds.size)

if renderedComponent != nil {
Expand Down

0 comments on commit b597f56

Please sign in to comment.