[AltStore] Update apps from UpdatesViewController

This commit is contained in:
Riley Testut
2019-06-17 14:49:23 -07:00
parent 9538d05f9f
commit d65cef8817
10 changed files with 231 additions and 30 deletions

View File

@@ -21,19 +21,36 @@ class UpdatesViewController: UITableViewController
return dateFormatter
}()
@IBOutlet private var progressView: UIProgressView!
required init?(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)
NotificationCenter.default.addObserver(self, selector: #selector(UpdatesViewController.didFetchApps(_:)), name: AppManager.didFetchAppsNotification, object: nil)
}
override func viewDidLoad()
{
super.viewDidLoad()
self.tableView.dataSource = self.dataSource
if let navigationBar = self.navigationController?.navigationBar
{
self.progressView.translatesAutoresizingMaskIntoConstraints = false
navigationBar.addSubview(self.progressView)
NSLayoutConstraint.activate([self.progressView.widthAnchor.constraint(equalTo: navigationBar.widthAnchor),
self.progressView.bottomAnchor.constraint(equalTo: navigationBar.bottomAnchor)])
}
}
override func viewDidAppear(_ animated: Bool)
{
super.viewDidAppear(animated)
let count = self.tableView.numberOfRows(inSection: 0)
self.navigationController?.tabBarItem.badgeValue = count > 0 ? String(describing: count) : nil
self.update()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
@@ -71,6 +88,98 @@ private extension UpdatesViewController
cell.detailTextLabel?.text = detailText
}
let placeholderView = RSTPlaceholderView()
placeholderView.textLabel.text = NSLocalizedString("No Updates", comment: "")
placeholderView.detailTextLabel.text = NSLocalizedString("There are no app updates at this time.", comment: "")
dataSource.placeholderView = placeholderView
return dataSource
}
func update()
{
if let count = self.dataSource.fetchedResultsController.fetchedObjects?.count, count > 0
{
self.navigationController?.tabBarItem.badgeValue = String(describing: count)
}
else
{
self.navigationController?.tabBarItem.badgeValue = nil
}
}
}
private extension UpdatesViewController
{
func update(_ installedApp: InstalledApp)
{
let toastView = RSTToastView(text: "Updating...", detailText: nil)
toastView.tintColor = .altPurple
toastView.activityIndicatorView.startAnimating()
toastView.show(in: self.navigationController?.view ?? self.view)
let progress = AppManager.shared.install(installedApp.app, presentingViewController: self) { (result) in
do
{
let app = try result.get()
try app.managedObjectContext?.save()
DispatchQueue.main.async {
let installedApp = DatabaseManager.shared.persistentContainer.viewContext.object(with: installedApp.objectID) as! InstalledApp
let toastView = RSTToastView(text: "Updated \(installedApp.app.name) to version \(installedApp.version)!", detailText: nil)
toastView.tintColor = .altPurple
toastView.show(in: self.navigationController?.view ?? self.view, duration: 2)
self.update()
}
}
catch
{
DispatchQueue.main.async {
let toastView = RSTToastView(text: "Failed to update \(installedApp.app.name)", detailText: error.localizedDescription)
toastView.tintColor = .altPurple
toastView.show(in: self.navigationController?.view ?? self.view, duration: 2)
}
}
DispatchQueue.main.async {
self.progressView.observedProgress = nil
self.progressView.progress = 0.0
}
}
self.progressView.observedProgress = progress
}
@objc func didFetchApps(_ notification: Notification)
{
DispatchQueue.main.async {
if self.dataSource.fetchedResultsController.fetchedObjects == nil
{
do { try self.dataSource.fetchedResultsController.performFetch() }
catch { print("Error fetching:", error) }
}
self.update()
}
}
}
extension UpdatesViewController
{
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?
{
let updateAction = UITableViewRowAction(style: .normal, title: "Update") { [weak self] (action, indexPath) in
guard let installedApp = self?.dataSource.item(at: indexPath) else { return }
self?.update(installedApp)
}
updateAction.backgroundColor = .altPurple
return [updateAction]
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)
{
}
}