Files
SideStore/AltStore/Settings/PatreonViewController.swift

334 lines
13 KiB
Swift
Raw Normal View History

//
// PatreonViewController.swift
// AltStore
//
2019-09-05 15:37:58 -07:00
// Created by Riley Testut on 9/5/19.
// Copyright © 2019 Riley Testut. All rights reserved.
//
import UIKit
2019-09-05 15:37:58 -07:00
import SafariServices
import AuthenticationServices
import AltStoreCore
import Roxas
2019-09-05 15:37:58 -07:00
extension PatreonViewController
{
private enum Section: Int, CaseIterable
{
case about
case patrons
}
}
class PatreonViewController: UICollectionViewController
{
private lazy var dataSource = self.makeDataSource()
2019-09-05 15:37:58 -07:00
private lazy var patronsDataSource = self.makePatronsDataSource()
private var prototypeAboutHeader: AboutPatreonHeaderView!
2019-10-24 13:04:30 -07:00
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
override func viewDidLoad()
{
super.viewDidLoad()
2019-09-05 15:37:58 -07:00
let aboutHeaderNib = UINib(nibName: "AboutPatreonHeaderView", bundle: nil)
self.prototypeAboutHeader = aboutHeaderNib.instantiate(withOwner: nil, options: nil)[0] as? AboutPatreonHeaderView
self.collectionView.dataSource = self.dataSource
self.collectionView.register(aboutHeaderNib, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "AboutHeader")
self.collectionView.register(PatronsHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "PatronsHeader")
self.collectionView.register(PatronsFooterView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "PatronsFooter")
NotificationCenter.default.addObserver(self, selector: #selector(PatreonViewController.didUpdatePatrons(_:)), name: AppManager.didUpdatePatronsNotification, object: nil)
self.update()
}
override func viewWillAppear(_ animated: Bool)
{
super.viewWillAppear(animated)
self.fetchPatrons()
2019-09-05 15:37:58 -07:00
self.update()
}
override func viewDidLayoutSubviews()
{
super.viewDidLayoutSubviews()
let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
var itemWidth = (self.collectionView.bounds.width - (layout.sectionInset.left + layout.sectionInset.right + layout.minimumInteritemSpacing)) / 2
itemWidth.round(.down)
layout.itemSize = CGSize(width: itemWidth, height: layout.itemSize.height)
}
}
private extension PatreonViewController
{
func makeDataSource() -> RSTCompositeCollectionViewDataSource<PatreonAccount>
{
let aboutDataSource = RSTDynamicCollectionViewDataSource<PatreonAccount>()
2019-09-05 15:37:58 -07:00
aboutDataSource.numberOfSectionsHandler = { 1 }
aboutDataSource.numberOfItemsHandler = { _ in 0 }
let dataSource = RSTCompositeCollectionViewDataSource<PatreonAccount>(dataSources: [aboutDataSource, self.patronsDataSource])
2019-09-05 15:37:58 -07:00
dataSource.proxy = self
return dataSource
}
func makePatronsDataSource() -> RSTFetchedResultsCollectionViewDataSource<PatreonAccount>
2019-09-05 15:37:58 -07:00
{
let fetchRequest: NSFetchRequest<PatreonAccount> = PatreonAccount.fetchRequest()
fetchRequest.predicate = NSPredicate(format: "%K == YES", #keyPath(PatreonAccount.isFriendZonePatron))
fetchRequest.sortDescriptors = [NSSortDescriptor(key: #keyPath(PatreonAccount.name), ascending: true, selector: #selector(NSString.caseInsensitiveCompare(_:)))]
let patronsDataSource = RSTFetchedResultsCollectionViewDataSource<PatreonAccount>(fetchRequest: fetchRequest, managedObjectContext: DatabaseManager.shared.viewContext)
2019-09-05 15:37:58 -07:00
patronsDataSource.cellConfigurationHandler = { (cell, patron, indexPath) in
let cell = cell as! PatronCollectionViewCell
cell.textLabel.text = patron.name
}
return patronsDataSource
}
func update()
{
self.collectionView.reloadData()
}
func prepare(_ headerView: AboutPatreonHeaderView)
{
headerView.layoutMargins = self.view.layoutMargins
headerView.supportButton.addTarget(self, action: #selector(PatreonViewController.openPatreonURL(_:)), for: .primaryActionTriggered)
headerView.accountButton.removeTarget(self, action: nil, for: .primaryActionTriggered)
let defaultSupportButtonTitle = NSLocalizedString("Become a patron", comment: "")
let isPatronSupportButtonTitle = NSLocalizedString("View Patreon", comment: "")
let defaultText = NSLocalizedString("""
Hey y'all,
2022-04-13 19:40:11 -07:00
You can support future development of our apps by donating to us on Patreon. In return, you'll receive access to the beta versions of our apps and be among the first to try the latest features.
Thanks for all your support 💜
2022-04-13 19:40:11 -07:00
Riley & Shane
""", comment: "")
let isPatronText = NSLocalizedString("""
Hey ,
2022-04-13 19:40:11 -07:00
Youre the best. Your account was linked successfully, so you now have access to the beta versions of all of our apps. You can find them all in the Browse tab.
Thanks for all of your support. Enjoy!
2022-04-13 19:40:11 -07:00
Riley & Shane
""", comment: "")
if let account = DatabaseManager.shared.patreonAccount(), PatreonAPI.shared.isAuthenticated
{
headerView.accountButton.addTarget(self, action: #selector(PatreonViewController.signOut(_:)), for: .primaryActionTriggered)
headerView.accountButton.setTitle(String(format: NSLocalizedString("Unlink %@", comment: ""), account.name), for: .normal)
if account.isPatron
{
headerView.supportButton.setTitle(isPatronSupportButtonTitle, for: .normal)
let font = UIFont.systemFont(ofSize: 16)
let attributedText = NSMutableAttributedString(string: isPatronText, attributes: [.font: font,
.foregroundColor: UIColor.white])
let boldedName = NSAttributedString(string: account.firstName ?? account.name,
attributes: [.font: UIFont.boldSystemFont(ofSize: font.pointSize),
.foregroundColor: UIColor.white])
attributedText.insert(boldedName, at: 4)
headerView.textView.attributedText = attributedText
}
else
{
headerView.supportButton.setTitle(defaultSupportButtonTitle, for: .normal)
headerView.textView.text = defaultText
}
}
else
{
headerView.accountButton.addTarget(self, action: #selector(PatreonViewController.authenticate(_:)), for: .primaryActionTriggered)
headerView.supportButton.setTitle(defaultSupportButtonTitle, for: .normal)
headerView.accountButton.setTitle(NSLocalizedString("Link Patreon account", comment: ""), for: .normal)
headerView.textView.text = defaultText
}
}
2019-09-05 15:37:58 -07:00
}
private extension PatreonViewController
{
@objc func fetchPatrons()
{
AppManager.shared.updatePatronsIfNeeded()
self.update()
}
2019-09-05 15:37:58 -07:00
@objc func openPatreonURL(_ sender: UIButton)
{
let patreonURL = URL(string: "https://altstore.io/patreon")!
2019-09-05 15:37:58 -07:00
let safariViewController = SFSafariViewController(url: patreonURL)
safariViewController.preferredControlTintColor = self.view.tintColor
self.present(safariViewController, animated: true, completion: nil)
}
@IBAction func authenticate(_ sender: UIBarButtonItem)
{
PatreonAPI.shared.authenticate { (result) in
do
{
let account = try result.get()
try account.managedObjectContext?.save()
DispatchQueue.main.async {
self.update()
}
}
catch ASWebAuthenticationSessionError.canceledLogin
{
// Ignore
}
catch
{
DispatchQueue.main.async {
let toastView = ToastView(error: error)
toastView.show(in: self)
}
}
}
}
@IBAction func signOut(_ sender: UIBarButtonItem)
{
2019-09-05 15:37:58 -07:00
func signOut()
{
PatreonAPI.shared.signOut { (result) in
do
{
try result.get()
DispatchQueue.main.async {
self.update()
}
}
catch
{
DispatchQueue.main.async {
let toastView = ToastView(error: error)
toastView.show(in: self)
2019-09-05 15:37:58 -07:00
}
}
}
2019-09-05 15:37:58 -07:00
}
let alertController = UIAlertController(title: NSLocalizedString("Are you sure you want to unlink your Patreon account?", comment: ""), message: NSLocalizedString("You will no longer have access to beta versions of apps.", comment: ""), preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction(title: NSLocalizedString("Unlink Patreon Account", comment: ""), style: .destructive) { _ in signOut() })
2019-09-05 15:37:58 -07:00
alertController.addAction(.cancel)
self.present(alertController, animated: true, completion: nil)
}
@objc func didUpdatePatrons(_ notification: Notification)
{
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
2019-09-05 15:37:58 -07:00
}
extension PatreonViewController
{
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView
{
let section = Section.allCases[indexPath.section]
switch section
{
case .about:
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "AboutHeader", for: indexPath) as! AboutPatreonHeaderView
self.prepare(headerView)
2019-09-05 15:37:58 -07:00
return headerView
case .patrons:
if kind == UICollectionView.elementKindSectionHeader
{
2019-09-05 15:37:58 -07:00
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "PatronsHeader", for: indexPath) as! PatronsHeaderView
headerView.textLabel.text = NSLocalizedString("Special thanks to...", comment: "")
return headerView
}
else
{
let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "PatronsFooter", for: indexPath) as! PatronsFooterView
footerView.button.isIndicatingActivity = false
footerView.button.isHidden = false
footerView.button.addTarget(self, action: #selector(PatreonViewController.fetchPatrons), for: .primaryActionTriggered)
if self.patronsDataSource.itemCount > 0
2019-09-05 15:37:58 -07:00
{
footerView.button.isHidden = true
}
else
{
switch AppManager.shared.updatePatronsResult
{
case .none: footerView.button.isIndicatingActivity = true
case .success?: footerView.button.isHidden = true
case .failure?: footerView.button.setTitle(NSLocalizedString("Error Loading Patrons", comment: ""), for: .normal)
}
}
2019-09-05 15:37:58 -07:00
return footerView
}
}
}
}
2019-09-05 15:37:58 -07:00
extension PatreonViewController: UICollectionViewDelegateFlowLayout
{
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize
{
let section = Section.allCases[section]
switch section
{
case .about:
let widthConstraint = self.prototypeAboutHeader.widthAnchor.constraint(equalToConstant: collectionView.bounds.width)
NSLayoutConstraint.activate([widthConstraint])
defer { NSLayoutConstraint.deactivate([widthConstraint]) }
self.prepare(self.prototypeAboutHeader)
2019-09-05 15:37:58 -07:00
let size = self.prototypeAboutHeader.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
return size
case .patrons:
return CGSize(width: 320, height: 20)
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize
{
let section = Section.allCases[section]
switch section
{
case .about: return .zero
case .patrons: return CGSize(width: 320, height: 20)
}
}
}