mirror of
https://github.com/SideStore/SideStore.git
synced 2026-02-09 06:43:25 +01:00
146 lines
5.4 KiB
Swift
146 lines
5.4 KiB
Swift
//
|
|
// AppScreenshotsViewController.swift
|
|
// AltStore
|
|
//
|
|
// Created by Riley Testut on 9/18/23.
|
|
// Copyright © 2023 Riley Testut. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
import AltStoreCore
|
|
import Roxas
|
|
|
|
import Nuke
|
|
|
|
class AppScreenshotsViewController: UICollectionViewController
|
|
{
|
|
let app: StoreApp
|
|
|
|
private lazy var dataSource = self.makeDataSource()
|
|
|
|
init?(app: StoreApp, coder: NSCoder)
|
|
{
|
|
self.app = app
|
|
|
|
super.init(coder: coder)
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
override func viewDidLoad()
|
|
{
|
|
super.viewDidLoad()
|
|
|
|
self.collectionView.showsHorizontalScrollIndicator = false
|
|
|
|
// Allow parent background color to show through.
|
|
self.collectionView.backgroundColor = nil
|
|
|
|
// Match the parent table view margins.
|
|
self.collectionView.directionalLayoutMargins.top = 0
|
|
self.collectionView.directionalLayoutMargins.bottom = 0
|
|
self.collectionView.directionalLayoutMargins.leading = 20
|
|
self.collectionView.directionalLayoutMargins.trailing = 20
|
|
|
|
let collectionViewLayout = self.makeLayout()
|
|
self.collectionView.collectionViewLayout = collectionViewLayout
|
|
|
|
self.collectionView.register(AppScreenshotCollectionViewCell.self, forCellWithReuseIdentifier: RSTCellContentGenericCellIdentifier)
|
|
|
|
self.collectionView.dataSource = self.dataSource
|
|
self.collectionView.prefetchDataSource = self.dataSource
|
|
}
|
|
}
|
|
|
|
private extension AppScreenshotsViewController
|
|
{
|
|
func makeLayout() -> UICollectionViewCompositionalLayout
|
|
{
|
|
let layoutConfig = UICollectionViewCompositionalLayoutConfiguration()
|
|
layoutConfig.contentInsetsReference = .layoutMargins
|
|
|
|
let preferredHeight = 400.0
|
|
let estimatedWidth = preferredHeight * (AppScreenshot.defaultAspectRatio.width / AppScreenshot.defaultAspectRatio.height)
|
|
|
|
let layout = UICollectionViewCompositionalLayout(sectionProvider: { (sectionIndex, layoutEnvironment) -> NSCollectionLayoutSection? in
|
|
|
|
let itemSize = NSCollectionLayoutSize(widthDimension: .estimated(estimatedWidth), heightDimension: .fractionalHeight(1.0))
|
|
let item = NSCollectionLayoutItem(layoutSize: itemSize)
|
|
|
|
let groupSize = NSCollectionLayoutSize(widthDimension: .estimated(estimatedWidth), heightDimension: .absolute(preferredHeight))
|
|
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
|
|
|
|
let layoutSection = NSCollectionLayoutSection(group: group)
|
|
layoutSection.interGroupSpacing = 10
|
|
layoutSection.orthogonalScrollingBehavior = .groupPaging
|
|
|
|
return layoutSection
|
|
}, configuration: layoutConfig)
|
|
|
|
return layout
|
|
}
|
|
|
|
func makeDataSource() -> RSTArrayCollectionViewPrefetchingDataSource<AppScreenshot, UIImage>
|
|
{
|
|
let dataSource = RSTArrayCollectionViewPrefetchingDataSource<AppScreenshot, UIImage>(items: self.app.screenshots)
|
|
dataSource.cellConfigurationHandler = { (cell, screenshot, indexPath) in
|
|
let cell = cell as! AppScreenshotCollectionViewCell
|
|
cell.imageView.image = nil
|
|
cell.imageView.isIndicatingActivity = true
|
|
|
|
var aspectRatio = screenshot.size ?? AppScreenshot.defaultAspectRatio
|
|
if aspectRatio.width > aspectRatio.height
|
|
{
|
|
aspectRatio = CGSize(width: aspectRatio.height, height: aspectRatio.width)
|
|
}
|
|
|
|
cell.aspectRatio = aspectRatio
|
|
}
|
|
dataSource.prefetchHandler = { (screenshot, indexPath, completionHandler) in
|
|
let imageURL = screenshot.imageURL
|
|
return RSTAsyncBlockOperation() { (operation) in
|
|
let request = ImageRequest(url: imageURL, processors: [.screenshot])
|
|
ImagePipeline.shared.loadImage(with: request, progress: nil) { result in
|
|
guard !operation.isCancelled else { return operation.finish() }
|
|
|
|
switch result
|
|
{
|
|
case .success(let response): completionHandler(response.image, nil)
|
|
case .failure(let error): completionHandler(nil, error)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
dataSource.prefetchCompletionHandler = { (cell, image, indexPath, error) in
|
|
let cell = cell as! AppScreenshotCollectionViewCell
|
|
cell.imageView.isIndicatingActivity = false
|
|
cell.imageView.image = image
|
|
|
|
if let error = error
|
|
{
|
|
print("Error loading image:", error)
|
|
}
|
|
}
|
|
|
|
return dataSource
|
|
}
|
|
}
|
|
|
|
@available(iOS 17, *)
|
|
#Preview(traits: .portrait) {
|
|
DatabaseManager.shared.startForPreview()
|
|
|
|
let fetchRequest = StoreApp.fetchRequest()
|
|
let storeApp = try! DatabaseManager.shared.viewContext.fetch(fetchRequest).first!
|
|
|
|
let storyboard = UIStoryboard(name: "Main", bundle: .main)
|
|
let appViewConttroller = storyboard.instantiateViewController(withIdentifier: "appViewController") as! AppViewController
|
|
appViewConttroller.app = storeApp
|
|
|
|
let navigationController = UINavigationController(rootViewController: appViewConttroller)
|
|
return navigationController
|
|
}
|