Skip to content

Commit

Permalink
make ReuseManager easier to use outside of UIComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
lkzhao committed Sep 15, 2021
1 parent 3272b73 commit 88404d0
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions Sources/UIComponent/Core/ComponentView/ReuseManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@
import UIKit

open class ReuseManager: NSObject {
static let shared = ReuseManager()
public static let shared = ReuseManager()

/// Time it takes for ReuseManager to
/// dump all reusableViews to save memory
public var lifeSpan: TimeInterval = 5.0

/// When `removeFromComponentViewWhenReuse` is enabled,
/// When `removeFromSuperviewWhenReuse` is enabled,
/// cells will always be removed from ComponentView during reuse.
/// This is slower but it doesn't influence the `isHidden` property
/// of individual cells.
public var removeFromComponentViewWhenReuse = true
public var removeFromSuperviewWhenReuse = true

var reusableViews: [String: [UIView]] = [:]
var cleanupTimer: Timer?

public func enqueue(identifier: String,
view: UIView) {

public func enqueue<T: UIView>(identifier: String = NSStringFromClass(T.self), view: T) {
view.ckContext.reuseIdentifier = nil
view.ckContext.reuseManager = nil
if removeFromComponentViewWhenReuse {
if removeFromSuperviewWhenReuse {
view.removeFromSuperview()
} else {
view.isHidden = true
Expand All @@ -44,7 +43,7 @@ open class ReuseManager: NSObject {
_ defaultView: @autoclosure () -> T) -> T {
let queuedView = reusableViews[identifier]?.popLast() as? T
let view = queuedView ?? defaultView()
if !removeFromComponentViewWhenReuse {
if !removeFromSuperviewWhenReuse {
view.isHidden = false
}
view.ckContext.reuseManager = self
Expand Down

0 comments on commit 88404d0

Please sign in to comment.