Files
SideStore/SideStoreApp/Sources/SideStoreUIKit/Browse/BrowseCollectionViewCell.swift

95 lines
3.2 KiB
Swift
Raw Normal View History

//
// BrowseCollectionViewCell.swift
// AltStore
//
// Created by Riley Testut on 7/15/19.
// Copyright © 2019 Riley Testut. All rights reserved.
//
import UIKit
2023-03-01 14:36:52 -05:00
import RoxasUIKit
import OSLog
#if canImport(Logging)
import Logging
#endif
import Nuke
2023-03-01 00:48:36 -05:00
@objc final class BrowseCollectionViewCell: UICollectionViewCell {
var imageURLs: [URL] = [] {
didSet {
2023-03-01 00:48:36 -05:00
dataSource.items = imageURLs as [NSURL]
}
}
2023-03-01 00:48:36 -05:00
private lazy var dataSource = self.makeDataSource()
2023-03-01 00:48:36 -05:00
2019-10-22 12:23:03 -07:00
@IBOutlet var bannerView: AppBannerView!
@IBOutlet var subtitleLabel: UILabel!
2023-03-01 00:48:36 -05:00
2019-10-22 12:23:03 -07:00
@IBOutlet private(set) var screenshotsCollectionView: UICollectionView!
2023-03-01 00:48:36 -05:00
override func awakeFromNib() {
super.awakeFromNib()
2023-03-01 00:48:36 -05:00
contentView.preservesSuperviewLayoutMargins = true
// Must be registered programmatically, not in BrowseCollectionViewCell.xib, or else it'll throw an exception 🤷.
2023-03-01 00:48:36 -05:00
screenshotsCollectionView.register(ScreenshotCollectionViewCell.self, forCellWithReuseIdentifier: RSTCellContentGenericCellIdentifier)
screenshotsCollectionView.delegate = self
screenshotsCollectionView.dataSource = dataSource
screenshotsCollectionView.prefetchDataSource = dataSource
}
}
2023-03-01 00:48:36 -05:00
private extension BrowseCollectionViewCell {
func makeDataSource() -> RSTArrayCollectionViewPrefetchingDataSource<NSURL, UIImage> {
let dataSource = RSTArrayCollectionViewPrefetchingDataSource<NSURL, UIImage>(items: [])
2023-03-01 00:48:36 -05:00
dataSource.cellConfigurationHandler = { cell, _, _ in
let cell = cell as! ScreenshotCollectionViewCell
cell.imageView.image = nil
cell.imageView.isIndicatingActivity = true
}
2023-03-01 00:48:36 -05:00
dataSource.prefetchHandler = { imageURL, _, completionHandler in
RSTAsyncBlockOperation { operation in
let request = ImageRequest(url: imageURL as URL, processor: .screenshot)
2023-03-01 00:48:36 -05:00
ImagePipeline.shared.loadImage(with: request, progress: nil, completion: { response, error in
guard !operation.isCancelled else { return operation.finish() }
2023-03-01 00:48:36 -05:00
if let image = response?.image {
completionHandler(image, nil)
2023-03-01 00:48:36 -05:00
} else {
completionHandler(nil, error)
}
})
}
}
2023-03-01 00:48:36 -05:00
dataSource.prefetchCompletionHandler = { cell, image, _, error in
let cell = cell as! ScreenshotCollectionViewCell
cell.imageView.isIndicatingActivity = false
cell.imageView.image = image
2023-03-01 00:48:36 -05:00
if let error = error {
2023-03-02 00:40:11 -05:00
os_log("Error loading image: %@", type: .error , error.localizedDescription)
}
}
2023-03-01 00:48:36 -05:00
return dataSource
}
}
2023-03-01 00:48:36 -05:00
extension BrowseCollectionViewCell: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout _: UICollectionViewLayout, sizeForItemAt _: IndexPath) -> CGSize {
// Assuming 9.0 / 16.0 ratio for now.
let aspectRatio: CGFloat = 9.0 / 16.0
let itemHeight = collectionView.bounds.height
let itemWidth = itemHeight * aspectRatio
let size = CGSize(width: itemWidth.rounded(.down), height: itemHeight.rounded(.down))
return size
}
}