2019-07-16 14:25:09 -07:00
|
|
|
|
//
|
|
|
|
|
|
// BrowseCollectionViewCell.swift
|
|
|
|
|
|
// AltStore
|
|
|
|
|
|
//
|
|
|
|
|
|
// Created by Riley Testut on 7/15/19.
|
|
|
|
|
|
// Copyright © 2019 Riley Testut. All rights reserved.
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
|
|
|
|
|
|
import Roxas
|
|
|
|
|
|
|
2019-08-20 19:06:03 -05:00
|
|
|
|
import Nuke
|
|
|
|
|
|
|
2023-01-04 09:52:12 -05:00
|
|
|
|
@objc final class BrowseCollectionViewCell: UICollectionViewCell
|
2019-07-16 14:25:09 -07:00
|
|
|
|
{
|
2019-08-20 19:06:03 -05:00
|
|
|
|
var imageURLs: [URL] = [] {
|
2019-07-16 14:25:09 -07:00
|
|
|
|
didSet {
|
2019-08-20 19:06:03 -05:00
|
|
|
|
self.dataSource.items = self.imageURLs as [NSURL]
|
2019-07-16 14:25:09 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
private lazy var dataSource = self.makeDataSource()
|
|
|
|
|
|
|
2019-10-22 12:23:03 -07:00
|
|
|
|
@IBOutlet var bannerView: AppBannerView!
|
|
|
|
|
|
@IBOutlet var subtitleLabel: UILabel!
|
2019-07-30 12:41:50 -07:00
|
|
|
|
|
2019-10-22 12:23:03 -07:00
|
|
|
|
@IBOutlet private(set) var screenshotsCollectionView: UICollectionView!
|
2019-07-16 14:25:09 -07:00
|
|
|
|
|
|
|
|
|
|
override func awakeFromNib()
|
|
|
|
|
|
{
|
|
|
|
|
|
super.awakeFromNib()
|
|
|
|
|
|
|
2019-10-22 12:23:03 -07:00
|
|
|
|
self.contentView.preservesSuperviewLayoutMargins = true
|
|
|
|
|
|
|
2019-07-30 12:41:50 -07:00
|
|
|
|
// Must be registered programmatically, not in BrowseCollectionViewCell.xib, or else it'll throw an exception 🤷♂️.
|
|
|
|
|
|
self.screenshotsCollectionView.register(ScreenshotCollectionViewCell.self, forCellWithReuseIdentifier: RSTCellContentGenericCellIdentifier)
|
|
|
|
|
|
|
2019-07-16 14:25:09 -07:00
|
|
|
|
self.screenshotsCollectionView.delegate = self
|
|
|
|
|
|
self.screenshotsCollectionView.dataSource = self.dataSource
|
|
|
|
|
|
self.screenshotsCollectionView.prefetchDataSource = self.dataSource
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private extension BrowseCollectionViewCell
|
|
|
|
|
|
{
|
2019-08-20 19:06:03 -05:00
|
|
|
|
func makeDataSource() -> RSTArrayCollectionViewPrefetchingDataSource<NSURL, UIImage>
|
2019-07-16 14:25:09 -07:00
|
|
|
|
{
|
2019-08-20 19:06:03 -05:00
|
|
|
|
let dataSource = RSTArrayCollectionViewPrefetchingDataSource<NSURL, UIImage>(items: [])
|
2019-07-16 14:25:09 -07:00
|
|
|
|
dataSource.cellConfigurationHandler = { (cell, screenshot, indexPath) in
|
|
|
|
|
|
let cell = cell as! ScreenshotCollectionViewCell
|
2019-07-30 12:41:50 -07:00
|
|
|
|
cell.imageView.image = nil
|
2019-07-16 14:25:09 -07:00
|
|
|
|
cell.imageView.isIndicatingActivity = true
|
|
|
|
|
|
}
|
2019-08-20 19:06:03 -05:00
|
|
|
|
dataSource.prefetchHandler = { (imageURL, indexPath, completionHandler) in
|
|
|
|
|
|
return RSTAsyncBlockOperation() { (operation) in
|
2022-04-11 11:59:56 -07:00
|
|
|
|
let request = ImageRequest(url: imageURL as URL, processor: .screenshot)
|
|
|
|
|
|
ImagePipeline.shared.loadImage(with: request, progress: nil, completion: { (response, error) in
|
2019-08-27 16:00:59 -07:00
|
|
|
|
guard !operation.isCancelled else { return operation.finish() }
|
2019-08-20 19:06:03 -05:00
|
|
|
|
|
|
|
|
|
|
if let image = response?.image
|
|
|
|
|
|
{
|
|
|
|
|
|
completionHandler(image, nil)
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
completionHandler(nil, error)
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
2019-07-16 14:25:09 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
dataSource.prefetchCompletionHandler = { (cell, image, indexPath, error) in
|
|
|
|
|
|
let cell = cell as! ScreenshotCollectionViewCell
|
|
|
|
|
|
cell.imageView.isIndicatingActivity = false
|
|
|
|
|
|
cell.imageView.image = image
|
2019-08-20 19:06:03 -05:00
|
|
|
|
|
|
|
|
|
|
if let error = error
|
|
|
|
|
|
{
|
|
|
|
|
|
print("Error loading image:", error)
|
|
|
|
|
|
}
|
2019-07-16 14:25:09 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return dataSource
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extension BrowseCollectionViewCell: UICollectionViewDelegateFlowLayout
|
|
|
|
|
|
{
|
|
|
|
|
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
|
|
|
|
|
|
{
|
2019-07-30 12:41:50 -07:00
|
|
|
|
// 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))
|
2019-07-16 14:25:09 -07:00
|
|
|
|
return size
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|