-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from daangn/using-cachedSize-for-performance
노출된 View 사이즈 캐시 로직을 구현해요.
- Loading branch information
Showing
9 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
Sources/KarrotListKit/Extension/UIView+TraitCollection.swift .swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
Sources/KarrotListKit/FeatureFlag/DefaultFeatureFlagProvider.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// | ||
// Copyright (c) 2025 Danggeun Market Inc. | ||
// | ||
|
||
import Foundation | ||
|
||
final class DefaultFeatureFlagProvider: FeatureFlagProviding { | ||
|
||
func featureFlags() -> [FeatureFlagItem] { | ||
[] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// Copyright (c) 2025 Danggeun Market Inc. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Representing a feature flag item. | ||
public struct FeatureFlagItem { | ||
|
||
/// The type of the feature flag. | ||
public let type: FeatureFlagType | ||
|
||
/// A Boolean value indicating whether the feature flag is enabled. | ||
public let isEnabled: Bool | ||
|
||
/// Initializes a new `FeatureFlagItem`. | ||
/// | ||
/// - Parameters: | ||
/// - type: The type of the feature flag. | ||
/// - isEnabled: A Boolean value indicating whether the feature flag is enabled. | ||
public init( | ||
type: FeatureFlagType, | ||
isEnabled: Bool | ||
) { | ||
self.type = type | ||
self.isEnabled = isEnabled | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
Sources/KarrotListKit/FeatureFlag/FeatureFlagProviding.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// Copyright (c) 2025 Danggeun Market Inc. | ||
// | ||
|
||
import Foundation | ||
|
||
/// A protocol for providing feature flags. | ||
public protocol FeatureFlagProviding { | ||
|
||
/// Returns an array of feature flags. | ||
/// | ||
/// - Returns: An array of `FeatureFlagItem`. | ||
func featureFlags() -> [FeatureFlagItem] | ||
} | ||
|
||
extension FeatureFlagProviding { | ||
|
||
func isEnabled(for type: FeatureFlagType) -> Bool { | ||
featureFlags() | ||
.first(where: { $0.type == type })? | ||
.isEnabled ?? false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// Copyright (c) 2025 Danggeun Market Inc. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Define the feature flags | ||
public enum FeatureFlagType: Equatable { | ||
|
||
/// Improve scrolling performance using calculated view size. | ||
/// You can find more information at https://developer.apple.com/documentation/uikit/building-high-performance-lists-and-collection-views | ||
case usesCachedViewSize | ||
} |
15 changes: 15 additions & 0 deletions
15
Sources/KarrotListKit/FeatureFlag/KarrotListKitFeatureFlag.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// Copyright (c) 2025 Danggeun Market Inc. | ||
// | ||
|
||
import Foundation | ||
|
||
/// An interface for injecting a feature flag provider. | ||
public enum KarrotListKitFeatureFlag { | ||
|
||
/// The feature flag provider used by `KarrotListKit`. | ||
/// | ||
/// By default, this is set to `DefaultFeatureFlagProvider`. | ||
/// You can replace it with a custom provider to change the feature flag behavior. | ||
public static var provider: FeatureFlagProviding = DefaultFeatureFlagProvider() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// Copyright (c) 2025 Danggeun Market Inc. | ||
// | ||
|
||
import Foundation | ||
import XCTest | ||
|
||
@testable import KarrotListKit | ||
|
||
final class FeatureFlagProviderTests: XCTestCase { | ||
|
||
final class FeatureFlagProviderStub: FeatureFlagProviding { | ||
|
||
var featureFlagsStub: [FeatureFlagItem] = [] | ||
|
||
func featureFlags() -> [FeatureFlagItem] { | ||
featureFlagsStub | ||
} | ||
} | ||
|
||
func test_default_featureFlags_is_empty() { | ||
// given | ||
let sut = KarrotListKitFeatureFlag.provider | ||
|
||
// when | ||
let featureFlags = sut.featureFlags() | ||
|
||
// then | ||
XCTAssertTrue(featureFlags.isEmpty) | ||
} | ||
|
||
func test_usesCachedViewSize_isEnabled() { | ||
[true, false].forEach { flag in | ||
// given | ||
let provider = FeatureFlagProviderStub() | ||
provider.featureFlagsStub = [.init(type: .usesCachedViewSize, isEnabled: flag)] | ||
KarrotListKitFeatureFlag.provider = provider | ||
|
||
// when | ||
let isEnabled = KarrotListKitFeatureFlag.provider.isEnabled(for: .usesCachedViewSize) | ||
|
||
// then | ||
XCTAssertEqual(isEnabled, flag) | ||
} | ||
} | ||
} |