mirror of
https://github.com/MessageKit/MessageKit.git
synced 2026-02-06 19:03:19 +00:00
Replace dictionary with NSCache #495
This commit is contained in:
@@ -57,13 +57,17 @@ open class MessagesCollectionViewFlowLayout: UICollectionViewFlowLayout {
|
||||
/// Determines the maximum number of `MessageCollectionViewCell` attributes to cache.
|
||||
///
|
||||
/// The default value of this property is 500.
|
||||
open var attributesCacheMaxSize: Int = 500
|
||||
open var attributesCacheMaxSize: Int = 500 {
|
||||
didSet {
|
||||
layoutContextCache.countLimit = attributesCacheMaxSize
|
||||
}
|
||||
}
|
||||
|
||||
typealias MessageID = String
|
||||
typealias MessageID = NSString
|
||||
|
||||
/// The cache for `MessageCellLayoutContext`.
|
||||
/// The key is the `messageId` of the `MessageType`.
|
||||
fileprivate var layoutContextCache: [MessageID: MessageCellLayoutContext] = [:]
|
||||
fileprivate var layoutContextCache = NSCache<MessageID, MessageCellLayoutContext>()
|
||||
|
||||
/// The `MessageCellLayoutContext` for the current cell.
|
||||
var currentLayoutContext: MessageCellLayoutContext!
|
||||
@@ -108,12 +112,12 @@ extension MessagesCollectionViewFlowLayout {
|
||||
/// - Parameters:
|
||||
/// - messageId: The `messageId` for the `MessageType` whose cached layout information is to be removed.
|
||||
public func removeCachedAttributes(for messageId: String) {
|
||||
layoutContextCache.removeValue(forKey: messageId)
|
||||
layoutContextCache.removeObject(forKey: messageId as NSString)
|
||||
}
|
||||
|
||||
/// Removes the cached layout information for all `MessageType`s.
|
||||
public func removeAllCachedAttributes() {
|
||||
layoutContextCache.removeAll()
|
||||
layoutContextCache.removeAllObjects()
|
||||
}
|
||||
|
||||
open override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
|
||||
@@ -207,12 +211,11 @@ extension MessagesCollectionViewFlowLayout {
|
||||
extension MessagesCollectionViewFlowLayout {
|
||||
|
||||
internal func cellLayoutContext(for message: MessageType, at indexPath: IndexPath) -> MessageCellLayoutContext {
|
||||
guard let cachedContext = layoutContextCache[message.messageId] else {
|
||||
guard let cachedContext = layoutContextCache.object(forKey: message.messageId as NSString) else {
|
||||
let newContext = newCellLayoutContext(for: message, at: indexPath)
|
||||
let shouldCache = messagesLayoutDelegate.shouldCacheLayoutAttributes(for: message)
|
||||
|
||||
if shouldCache && layoutContextCache.count < attributesCacheMaxSize {
|
||||
layoutContextCache[message.messageId] = newContext
|
||||
if messagesLayoutDelegate.shouldCacheLayoutAttributes(for: message) {
|
||||
layoutContextCache.setObject(newContext, forKey: message.messageId as NSString)
|
||||
}
|
||||
return newContext
|
||||
}
|
||||
|
||||
@@ -149,7 +149,7 @@ open class MessageCollectionViewCell: UICollectionViewCell, CollectionViewReusab
|
||||
open func layoutAvatarView(with attributes: MessagesCollectionViewLayoutAttributes) {
|
||||
guard attributes.avatarSize != .zero else { return }
|
||||
|
||||
var origin = CGPoint.zero
|
||||
var origin: CGPoint = .zero
|
||||
|
||||
switch attributes.avatarPosition.horizontal {
|
||||
case .cellLeading:
|
||||
@@ -201,7 +201,7 @@ open class MessageCollectionViewCell: UICollectionViewCell, CollectionViewReusab
|
||||
let topLabelPadding = topLabelAlignment.insets
|
||||
let topLabelSize = attributes.topLabelSize
|
||||
|
||||
var origin = CGPoint.zero
|
||||
var origin: CGPoint = .zero
|
||||
|
||||
origin.y = topLabelPadding.top
|
||||
|
||||
|
||||
Reference in New Issue
Block a user