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
|
|
|
|
|
|
|
2019-07-16 14:25:09 -07:00
|
|
|
|
@objc class BrowseCollectionViewCell: UICollectionViewCell
|
|
|
|
|
|
{
|
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-07-30 12:41:50 -07:00
|
|
|
|
|
2019-07-16 14:25:09 -07:00
|
|
|
|
@IBOutlet var nameLabel: UILabel!
|
|
|
|
|
|
@IBOutlet var developerLabel: UILabel!
|
|
|
|
|
|
@IBOutlet var appIconImageView: UIImageView!
|
2019-07-19 16:42:40 -07:00
|
|
|
|
@IBOutlet var actionButton: PillButton!
|
2019-07-16 14:25:09 -07:00
|
|
|
|
@IBOutlet var subtitleLabel: UILabel!
|
|
|
|
|
|
|
2019-07-30 12:41:50 -07:00
|
|
|
|
@IBOutlet var screenshotsCollectionView: UICollectionView!
|
2019-08-28 11:13:22 -07:00
|
|
|
|
@IBOutlet var betaBadgeView: UIImageView!
|
2019-07-30 12:41:50 -07:00
|
|
|
|
|
2019-07-16 14:25:09 -07:00
|
|
|
|
@IBOutlet private var screenshotsContentView: UIView!
|
|
|
|
|
|
|
|
|
|
|
|
override func awakeFromNib()
|
|
|
|
|
|
{
|
|
|
|
|
|
super.awakeFromNib()
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
self.screenshotsContentView.layer.cornerRadius = 20
|
|
|
|
|
|
self.screenshotsContentView.layer.masksToBounds = true
|
|
|
|
|
|
|
|
|
|
|
|
self.update()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override func tintColorDidChange()
|
|
|
|
|
|
{
|
|
|
|
|
|
super.tintColorDidChange()
|
|
|
|
|
|
|
|
|
|
|
|
self.update()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
ImagePipeline.shared.loadImage(with: imageURL as URL, 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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private func update()
|
|
|
|
|
|
{
|
|
|
|
|
|
self.subtitleLabel.textColor = self.tintColor
|
|
|
|
|
|
self.screenshotsContentView.backgroundColor = self.tintColor.withAlphaComponent(0.1)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|