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
|
|
|
|
|
|
|
2023-03-01 14:36:52 -05:00
|
|
|
|
import RoxasUIKit
|
2023-04-02 02:28:12 -04:00
|
|
|
|
import OSLog
|
|
|
|
|
|
#if canImport(Logging)
|
|
|
|
|
|
import Logging
|
|
|
|
|
|
#endif
|
2019-07-16 14:25:09 -07:00
|
|
|
|
|
2019-08-20 19:06:03 -05:00
|
|
|
|
import Nuke
|
|
|
|
|
|
|
2023-03-01 00:48:36 -05:00
|
|
|
|
@objc final class BrowseCollectionViewCell: UICollectionViewCell {
|
2019-08-20 19:06:03 -05:00
|
|
|
|
var imageURLs: [URL] = [] {
|
2019-07-16 14:25:09 -07:00
|
|
|
|
didSet {
|
2023-03-01 00:48:36 -05:00
|
|
|
|
dataSource.items = imageURLs as [NSURL]
|
2019-07-16 14:25:09 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-03-01 00:48:36 -05:00
|
|
|
|
|
2019-07-16 14:25:09 -07: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() {
|
2019-07-16 14:25:09 -07:00
|
|
|
|
super.awakeFromNib()
|
2023-03-01 00:48:36 -05:00
|
|
|
|
|
|
|
|
|
|
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 🤷♂️.
|
2023-03-01 00:48:36 -05:00
|
|
|
|
screenshotsCollectionView.register(ScreenshotCollectionViewCell.self, forCellWithReuseIdentifier: RSTCellContentGenericCellIdentifier)
|
|
|
|
|
|
|
|
|
|
|
|
screenshotsCollectionView.delegate = self
|
|
|
|
|
|
screenshotsCollectionView.dataSource = dataSource
|
|
|
|
|
|
screenshotsCollectionView.prefetchDataSource = dataSource
|
2019-07-16 14:25:09 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-01 00:48:36 -05:00
|
|
|
|
private extension BrowseCollectionViewCell {
|
|
|
|
|
|
func makeDataSource() -> RSTArrayCollectionViewPrefetchingDataSource<NSURL, UIImage> {
|
2019-08-20 19:06:03 -05:00
|
|
|
|
let dataSource = RSTArrayCollectionViewPrefetchingDataSource<NSURL, UIImage>(items: [])
|
2023-03-01 00:48:36 -05:00
|
|
|
|
dataSource.cellConfigurationHandler = { cell, _, _ in
|
2019-07-16 14:25:09 -07:00
|
|
|
|
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
|
|
|
|
|
|
}
|
2023-03-01 00:48:36 -05:00
|
|
|
|
dataSource.prefetchHandler = { imageURL, _, completionHandler in
|
|
|
|
|
|
RSTAsyncBlockOperation { operation in
|
2022-04-11 11:59:56 -07:00
|
|
|
|
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
|
2019-08-27 16:00:59 -07:00
|
|
|
|
guard !operation.isCancelled else { return operation.finish() }
|
2023-03-01 00:48:36 -05:00
|
|
|
|
|
|
|
|
|
|
if let image = response?.image {
|
2019-08-20 19:06:03 -05:00
|
|
|
|
completionHandler(image, nil)
|
2023-03-01 00:48:36 -05:00
|
|
|
|
} else {
|
2019-08-20 19:06:03 -05:00
|
|
|
|
completionHandler(nil, error)
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
2019-07-16 14:25:09 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-03-01 00:48:36 -05:00
|
|
|
|
dataSource.prefetchCompletionHandler = { cell, image, _, error in
|
2019-07-16 14:25:09 -07:00
|
|
|
|
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)
|
2019-08-20 19:06:03 -05:00
|
|
|
|
}
|
2019-07-16 14:25:09 -07:00
|
|
|
|
}
|
2023-03-01 00:48:36 -05:00
|
|
|
|
|
2019-07-16 14:25:09 -07:00
|
|
|
|
return dataSource
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-01 00:48:36 -05:00
|
|
|
|
extension BrowseCollectionViewCell: UICollectionViewDelegateFlowLayout {
|
|
|
|
|
|
func collectionView(_ collectionView: UICollectionView, layout _: UICollectionViewLayout, sizeForItemAt _: 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
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|