Files
SideStore/AltStore/News/NewsViewController.swift

565 lines
21 KiB
Swift
Raw Normal View History

2019-09-03 21:58:07 -07:00
//
// NewsViewController.swift
// AltStore
//
// Created by Riley Testut on 8/29/19.
// Copyright © 2019 Riley Testut. All rights reserved.
//
import UIKit
import SafariServices
import AltStoreCore
2019-09-03 21:58:07 -07:00
import Roxas
import Nuke
private final class AppBannerFooterView: UICollectionReusableView
2019-09-03 21:58:07 -07:00
{
let bannerView = AppBannerView(frame: .zero)
let tapGestureRecognizer = UITapGestureRecognizer(target: nil, action: nil)
override init(frame: CGRect)
{
super.init(frame: frame)
self.addGestureRecognizer(self.tapGestureRecognizer)
self.bannerView.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(self.bannerView)
NSLayoutConstraint.activate([
self.bannerView.topAnchor.constraint(equalTo: self.topAnchor),
self.bannerView.bottomAnchor.constraint(equalTo: self.bottomAnchor),
self.bannerView.leadingAnchor.constraint(equalTo: self.layoutMarginsGuide.leadingAnchor),
self.bannerView.trailingAnchor.constraint(equalTo: self.layoutMarginsGuide.trailingAnchor)
])
2019-09-03 21:58:07 -07:00
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
2023-03-02 15:48:33 -06:00
class NewsViewController: UICollectionViewController, PeekPopPreviewing
2019-09-03 21:58:07 -07:00
{
// Nil == Show news from all sources.
var source: Source?
2019-09-03 21:58:07 -07:00
private lazy var dataSource = self.makeDataSource()
private lazy var placeholderView = RSTPlaceholderView(frame: .zero)
2019-09-03 21:58:07 -07:00
private var prototypeCell: NewsCollectionViewCell!
private var loadingState: LoadingState = .loading {
didSet {
self.update()
}
}
2019-09-03 21:58:07 -07:00
// Cache
private var cachedCellSizes = [String: CGSize]()
init?(source: Source?, coder: NSCoder)
{
self.source = source
super.init(coder: coder)
self.initialize()
}
required init?(coder: NSCoder)
{
super.init(coder: coder)
self.initialize()
}
private func initialize()
{
NotificationCenter.default.addObserver(self, selector: #selector(NewsViewController.importApp(_:)), name: AppDelegate.importAppDeepLinkNotification, object: nil)
}
2019-09-03 21:58:07 -07:00
override func viewDidLoad()
{
super.viewDidLoad()
self.collectionView.backgroundColor = .altBackground
2019-09-03 21:58:07 -07:00
self.prototypeCell = NewsCollectionViewCell.instantiate(with: NewsCollectionViewCell.nib!)
self.prototypeCell.contentView.translatesAutoresizingMaskIntoConstraints = false
self.collectionView.dataSource = self.dataSource
self.collectionView.prefetchDataSource = self.dataSource
self.collectionView.register(NewsCollectionViewCell.nib, forCellWithReuseIdentifier: RSTCellContentGenericCellIdentifier)
self.collectionView.register(AppBannerFooterView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "AppBanner")
2023-03-02 15:48:33 -06:00
(self as PeekPopPreviewing).registerForPreviewing(with: self, sourceView: self.collectionView)
if let source = self.source
{
let tintColor = source.effectiveTintColor ?? .altPrimary
self.view.tintColor = tintColor
let appearance = NavigationBarAppearance()
appearance.configureWithTintColor(tintColor)
appearance.configureWithDefaultBackground()
let edgeAppearance = appearance.copy()
edgeAppearance.configureWithTransparentBackground()
self.navigationItem.standardAppearance = appearance
self.navigationItem.scrollEdgeAppearance = edgeAppearance
}
self.update()
2019-09-03 21:58:07 -07:00
}
override func viewWillAppear(_ animated: Bool)
{
super.viewWillAppear(animated)
self.fetchSource()
}
2019-10-03 12:30:53 -07:00
override func viewWillLayoutSubviews()
{
super.viewWillLayoutSubviews()
if self.collectionView.contentInset.bottom != 20
{
// Triggers collection view update in iOS 13, which crashes if we do it in viewDidLoad()
// since the database might not be loaded yet.
self.collectionView.contentInset.bottom = 20
}
}
2019-09-03 21:58:07 -07:00
}
private extension NewsViewController
{
func makeDataSource() -> RSTFetchedResultsCollectionViewPrefetchingDataSource<NewsItem, UIImage>
{
let fetchRequest = NewsItem.sortedFetchRequest(for: self.source)
let context = self.source?.managedObjectContext ?? DatabaseManager.shared.viewContext
2019-09-03 21:58:07 -07:00
// Use fetchedResultsController to split NewsItems up into sections.
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: #keyPath(NewsItem.objectID), cacheName: nil)
2019-09-03 21:58:07 -07:00
let dataSource = RSTFetchedResultsCollectionViewPrefetchingDataSource<NewsItem, UIImage>(fetchedResultsController: fetchedResultsController)
dataSource.proxy = self
dataSource.cellConfigurationHandler = { (cell, newsItem, indexPath) in
let cell = cell as! NewsCollectionViewCell
cell.layoutMargins.left = self.view.layoutMargins.left
cell.layoutMargins.right = self.view.layoutMargins.right
2019-09-03 21:58:07 -07:00
cell.titleLabel.text = newsItem.title
cell.captionLabel.text = newsItem.caption
cell.contentBackgroundView.backgroundColor = newsItem.tintColor
2019-09-03 21:58:07 -07:00
cell.imageView.image = nil
if newsItem.imageURL != nil
{
cell.imageView.isIndicatingActivity = true
cell.imageView.isHidden = false
}
else
{
cell.imageView.isIndicatingActivity = false
cell.imageView.isHidden = true
}
cell.isAccessibilityElement = true
cell.accessibilityLabel = (cell.titleLabel.text ?? "") + ". " + (cell.captionLabel.text ?? "")
if newsItem.storeApp != nil || newsItem.externalURL != nil
{
cell.accessibilityTraits.insert(.button)
}
else
{
cell.accessibilityTraits.remove(.button)
}
2019-09-03 21:58:07 -07:00
}
dataSource.prefetchHandler = { (newsItem, indexPath, completionHandler) in
guard let imageURL = newsItem.imageURL else { return nil }
return RSTAsyncBlockOperation() { (operation) in
ImagePipeline.shared.loadImage(with: imageURL, progress: nil) { result in
2019-09-03 21:58:07 -07:00
guard !operation.isCancelled else { return operation.finish() }
switch result
2019-09-03 21:58:07 -07:00
{
case .success(let response): completionHandler(response.image, nil)
case .failure(let error): completionHandler(nil, error)
2019-09-03 21:58:07 -07:00
}
}
2019-09-03 21:58:07 -07:00
}
}
dataSource.prefetchCompletionHandler = { (cell, image, indexPath, error) in
let cell = cell as! NewsCollectionViewCell
cell.imageView.isIndicatingActivity = false
cell.imageView.image = image
if let error = error
{
print("Error loading image:", error)
}
}
dataSource.placeholderView = self.placeholderView
2019-09-03 21:58:07 -07:00
return dataSource
}
2019-09-03 21:58:07 -07:00
func fetchSource()
{
self.loadingState = .loading
AppManager.shared.fetchSources() { (result) in
2019-09-03 21:58:07 -07:00
do
{
do
{
let (_, context) = try result.get()
try context.save()
DispatchQueue.main.async {
self.loadingState = .finished(.success(()))
}
}
catch let error as AppManager.FetchSourcesError
{
try error.managedObjectContext?.save()
throw error
}
catch let mergeError as MergeError
{
guard let sourceID = mergeError.sourceID else { throw mergeError }
let sanitizedError = (mergeError as NSError).sanitizedForSerialization()
DatabaseManager.shared.persistentContainer.performBackgroundTask { context in
do
{
guard let source = Source.first(satisfying: NSPredicate(format: "%K == %@", #keyPath(Source.identifier), sourceID), in: context) else { return }
source.error = sanitizedError
try context.save()
}
catch
{
print("[ALTLog] Failed to assign error \(sanitizedError.localizedErrorCode) to source \(sourceID).", error)
}
}
throw mergeError
}
2019-09-03 21:58:07 -07:00
}
catch var error as NSError
2019-09-03 21:58:07 -07:00
{
if error.localizedTitle == nil
{
error = error.withLocalizedTitle(NSLocalizedString("Unable to Refresh Store", comment: ""))
}
2019-09-03 21:58:07 -07:00
DispatchQueue.main.async {
if self.dataSource.itemCount > 0
{
let toastView = ToastView(error: error)
toastView.addTarget(nil, action: #selector(TabBarController.presentSources), for: .touchUpInside)
toastView.show(in: self)
}
self.loadingState = .finished(.failure(error))
2019-09-03 21:58:07 -07:00
}
}
}
}
func update()
{
switch self.loadingState
{
case .loading:
self.placeholderView.textLabel.isHidden = true
self.placeholderView.detailTextLabel.isHidden = false
self.placeholderView.detailTextLabel.text = NSLocalizedString("Loading...", comment: "")
self.placeholderView.activityIndicatorView.startAnimating()
case .finished(.failure(let error)):
self.placeholderView.textLabel.isHidden = false
self.placeholderView.detailTextLabel.isHidden = false
self.placeholderView.textLabel.text = NSLocalizedString("Unable to Fetch News", comment: "")
self.placeholderView.detailTextLabel.text = error.localizedDescription
self.placeholderView.activityIndicatorView.stopAnimating()
case .finished(.success):
self.placeholderView.textLabel.isHidden = true
self.placeholderView.detailTextLabel.isHidden = true
self.placeholderView.activityIndicatorView.stopAnimating()
}
}
2019-09-03 21:58:07 -07:00
}
private extension NewsViewController
{
@objc func handleTapGesture(_ gestureRecognizer: UITapGestureRecognizer)
{
guard let footerView = gestureRecognizer.view as? UICollectionReusableView else { return }
let indexPaths = self.collectionView.indexPathsForVisibleSupplementaryElements(ofKind: UICollectionView.elementKindSectionFooter)
guard let indexPath = indexPaths.first(where: { (indexPath) -> Bool in
let supplementaryView = self.collectionView.supplementaryView(forElementKind: UICollectionView.elementKindSectionFooter, at: indexPath)
return supplementaryView == footerView
}) else { return }
let item = self.dataSource.item(at: indexPath)
guard let storeApp = item.storeApp else { return }
let appViewController = AppViewController.makeAppViewController(app: storeApp)
self.navigationController?.pushViewController(appViewController, animated: true)
}
@objc func performAppAction(_ sender: PillButton)
{
let point = self.collectionView.convert(sender.center, from: sender.superview)
let indexPaths = self.collectionView.indexPathsForVisibleSupplementaryElements(ofKind: UICollectionView.elementKindSectionFooter)
guard let indexPath = indexPaths.first(where: { (indexPath) -> Bool in
let supplementaryView = self.collectionView.supplementaryView(forElementKind: UICollectionView.elementKindSectionFooter, at: indexPath)
return supplementaryView?.frame.contains(point) ?? false
}) else { return }
let app = self.dataSource.item(at: indexPath)
guard let storeApp = app.storeApp else { return }
if let installedApp = storeApp.installedApp, !installedApp.isUpdateAvailable
2019-09-03 21:58:07 -07:00
{
self.open(installedApp)
}
else
{
self.install(storeApp, at: indexPath)
}
}
@objc func install(_ storeApp: StoreApp, at indexPath: IndexPath)
{
let previousProgress = AppManager.shared.installationProgress(for: storeApp)
guard previousProgress == nil else {
previousProgress?.cancel()
return
}
if let installedApp = storeApp.installedApp, installedApp.isUpdateAvailable
{
AppManager.shared.update(installedApp, presentingViewController: self, completionHandler: finish(_:))
}
else
{
AppManager.shared.install(storeApp, presentingViewController: self, completionHandler: finish(_:))
}
UIView.performWithoutAnimation {
self.collectionView.reloadSections(IndexSet(integer: indexPath.section))
}
func finish(_ result: Result<InstalledApp, Error>)
{
2019-09-03 21:58:07 -07:00
DispatchQueue.main.async {
switch result
{
case .failure(OperationError.cancelled): break // Ignore
case .failure(let error):
merge AltStore 1.6.3, add dynamic anisette lists, merge SideJITServer integration * Change error from Swift.Error to NSError * Adds ResultOperation.localizedFailure * Finish Riley's monster commit 3b38d725d7e8e45fb2c0cb465a3968828616c209 May the Gods have mercy on my soul. * Fix format strings I broke * Include "Enable JIT" errors in Error Log * Fix minimuxer status checking * [skip ci] Update the no wifi message to include VPN * Opens Error Log when tapping ToastView * Fixes Error Log context menu covering cell content * Fixes Error Log context menu appearing while scrolling * Fixes incorrect Search FAQ URL * Fix Error Log showing UIAlertController on iOS 14+ * Fix Error Log not showing UIAlertController on iOS <=13 * Fix wrong color in AuthenticationViewController * Fix typo * Fixes logging non-AltServerErrors as AltServerError.underlyingError * Limits quitting other AltStore/SideStore processes to database migrations * Skips logging cancelled errors * Replaces StoreApp.latestVersion with latestSupportedVersion + latestAvailableVersion We now store the latest supported version as a relationship on StoreApp, rather than the latest available version. This allows us to reference the latest supported version in predicates and sort descriptors. However, we kept the underlying Core Data property name the same to avoid extra migration. * Conforms OperatingSystemVersion to Comparable * Parses AppVersion.minOSVersion/maxOSVersion from source JSON * Supports non-NSManagedObjects for @Managed properties This allows us to use @Managed with properties that may or may not be NSManagedObjects at runtime (e.g. protocols). If they are, Managed will keep strong reference to context like before. * Supports optional @Managed properties * Conforms AppVersion to AppProtocol * Verifies min/max OS version before downloading app + asks user to download older app version if necessary * Improves error message when file does not exist at AppVersion.downloadURL * Removes unnecessary StoreApp convenience properties * Removes unnecessary StoreApp convenience properties as well as fix other issues * Remove Settings bundle, add SwiftUI view instead Fix refresh all shortcut intent * Update AuthenticationOperation.swift Signed-off-by: June Park <rjp2030@outlook.com> * Fix build issues given by develop * Add availability check to fix CI build(?) * If it's gonna be that way... --------- Signed-off-by: June Park <rjp2030@outlook.com> Co-authored-by: nythepegasus <nythepegasus84@gmail.com> Co-authored-by: Riley Testut <riley@rileytestut.com> Co-authored-by: ny <me@nythepegas.us>
2024-08-06 10:43:52 +09:00
ToastView(error: error, opensLog: true).show(in: self)
2019-09-03 21:58:07 -07:00
case .success: print("Installed app:", storeApp.bundleIdentifier)
}
UIView.performWithoutAnimation {
self.collectionView.reloadSections(IndexSet(integer: indexPath.section))
}
}
}
}
func open(_ installedApp: InstalledApp)
{
UIApplication.shared.open(installedApp.openAppURL)
}
}
private extension NewsViewController
{
@objc func importApp(_ notification: Notification)
{
self.presentedViewController?.dismiss(animated: true, completion: nil)
}
}
2019-09-03 21:58:07 -07:00
extension NewsViewController
{
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
let newsItem = self.dataSource.item(at: indexPath)
if let externalURL = newsItem.externalURL
{
let safariViewController = SFSafariViewController(url: externalURL)
safariViewController.preferredControlTintColor = newsItem.tintColor
self.present(safariViewController, animated: true, completion: nil)
}
else if let storeApp = newsItem.storeApp
{
let appViewController = AppViewController.makeAppViewController(app: storeApp)
self.navigationController?.pushViewController(appViewController, animated: true)
}
}
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView
{
let item = self.dataSource.item(at: indexPath)
let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "AppBanner", for: indexPath) as! AppBannerFooterView
guard let storeApp = item.storeApp else { return footerView }
footerView.layoutMargins.left = self.view.layoutMargins.left
footerView.layoutMargins.right = self.view.layoutMargins.right
footerView.bannerView.button.isIndicatingActivity = false
2020-08-27 15:23:21 -07:00
footerView.bannerView.configure(for: storeApp)
2019-09-03 21:58:07 -07:00
footerView.bannerView.tintColor = storeApp.tintColor
footerView.bannerView.button.addTarget(self, action: #selector(NewsViewController.performAppAction(_:)), for: .primaryActionTriggered)
footerView.tapGestureRecognizer.addTarget(self, action: #selector(NewsViewController.handleTapGesture(_:)))
Nuke.loadImage(with: storeApp.iconURL, into: footerView.bannerView.iconImageView)
return footerView
}
}
extension NewsViewController: UICollectionViewDelegateFlowLayout
{
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
2019-09-03 21:58:07 -07:00
let item = self.dataSource.item(at: indexPath)
if let previousSize = self.cachedCellSizes[item.identifier]
{
return previousSize
}
// Take layout margins into account.
self.prototypeCell.layoutMargins.left = self.view.layoutMargins.left
self.prototypeCell.layoutMargins.right = self.view.layoutMargins.right
let widthConstraint = self.prototypeCell.contentView.widthAnchor.constraint(equalToConstant: collectionView.bounds.width)
2019-09-03 21:58:07 -07:00
NSLayoutConstraint.activate([widthConstraint])
defer { NSLayoutConstraint.deactivate([widthConstraint]) }
self.dataSource.cellConfigurationHandler(self.prototypeCell, item, indexPath)
let size = self.prototypeCell.contentView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
self.cachedCellSizes[item.identifier] = size
return size
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize
{
let item = self.dataSource.item(at: IndexPath(row: 0, section: section))
if item.storeApp != nil
{
return CGSize(width: 88, height: 88)
}
else
{
return .zero
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
{
var insets = UIEdgeInsets(top: 30, left: 0, bottom: 13, right: 0)
2019-09-03 21:58:07 -07:00
if section == 0
{
insets.top = 10
}
return insets
}
}
extension NewsViewController: UIViewControllerPreviewingDelegate
{
2023-03-02 15:48:33 -06:00
@available(iOS, deprecated: 13.0)
2019-09-03 21:58:07 -07:00
func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController?
{
if let indexPath = self.collectionView.indexPathForItem(at: location), let cell = self.collectionView.cellForItem(at: indexPath)
{
// Previewing news item.
previewingContext.sourceRect = cell.frame
let newsItem = self.dataSource.item(at: indexPath)
if let externalURL = newsItem.externalURL
{
let safariViewController = SFSafariViewController(url: externalURL)
safariViewController.preferredControlTintColor = newsItem.tintColor
return safariViewController
}
else if let storeApp = newsItem.storeApp
{
let appViewController = AppViewController.makeAppViewController(app: storeApp)
return appViewController
}
return nil
}
else
{
// Previewing app banner (or nothing).
let indexPaths = self.collectionView.indexPathsForVisibleSupplementaryElements(ofKind: UICollectionView.elementKindSectionFooter)
guard let indexPath = indexPaths.first(where: { (indexPath) -> Bool in
let layoutAttributes = self.collectionView.layoutAttributesForSupplementaryElement(ofKind: UICollectionView.elementKindSectionFooter, at: indexPath)
return layoutAttributes?.frame.contains(location) ?? false
}) else { return nil }
guard let layoutAttributes = self.collectionView.layoutAttributesForSupplementaryElement(ofKind: UICollectionView.elementKindSectionFooter, at: indexPath) else { return nil }
previewingContext.sourceRect = layoutAttributes.frame
let item = self.dataSource.item(at: indexPath)
guard let storeApp = item.storeApp else { return nil }
let appViewController = AppViewController.makeAppViewController(app: storeApp)
return appViewController
}
}
2023-03-02 15:48:33 -06:00
@available(iOS, deprecated: 13.0)
2019-09-03 21:58:07 -07:00
func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController)
{
if let safariViewController = viewControllerToCommit as? SFSafariViewController
{
self.present(safariViewController, animated: true, completion: nil)
}
else
{
self.navigationController?.pushViewController(viewControllerToCommit, animated: true)
}
}
}