mirror of
https://github.com/zhigang1992/GitHawk.git
synced 2026-04-26 05:05:06 +08:00
45 lines
1.4 KiB
Swift
45 lines
1.4 KiB
Swift
//
|
|
// ImageCellHeightCache.swift
|
|
// Freetime
|
|
//
|
|
// Created by Ryan Nystrom on 7/5/17.
|
|
// Copyright © 2017 Ryan Nystrom. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import IGListKit
|
|
|
|
final class ImageCellHeightCache: IssueCommentImageHeightCellDelegate {
|
|
|
|
// width is unused since the full size of the image is stored
|
|
private static let cache = WidthCache<URL, CGSize>()
|
|
private weak var sectionController: ListSectionController?
|
|
|
|
init(sectionController: ListSectionController) {
|
|
self.sectionController = sectionController
|
|
}
|
|
|
|
// MARK: Public API
|
|
|
|
func height(model: IssueCommentImageModel, width: CGFloat) -> CGFloat {
|
|
guard let size = ImageCellHeightCache.cache.data(key: model.url, width: 0) else { return 200 }
|
|
return BoundedImageSize(originalSize: size, containerWidth: width).height
|
|
}
|
|
|
|
// MARK: IssueCommentImageHeightCellDelegate
|
|
|
|
func imageDidFinishLoad(cell: IssueCommentImageCell, url: URL, size: CGSize) {
|
|
guard let sectionController = self.sectionController,
|
|
sectionController.section != NSNotFound,
|
|
size != ImageCellHeightCache.cache.data(key: url, width: 0)
|
|
else { return }
|
|
|
|
ImageCellHeightCache.cache.set(data: size, key: url, width: 0)
|
|
|
|
UIView.performWithoutAnimation {
|
|
sectionController.collectionContext?.invalidateLayout(for: sectionController)
|
|
}
|
|
}
|
|
|
|
}
|