From b8030ed0a9e358f90a887746194b7eae165368bb Mon Sep 17 00:00:00 2001 From: Riley Testut Date: Wed, 16 Nov 2022 16:00:26 -0600 Subject: [PATCH] Adds pull-to-refresh to check for updates --- AltStore/My Apps/MyAppsViewController.swift | 43 +++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/AltStore/My Apps/MyAppsViewController.swift b/AltStore/My Apps/MyAppsViewController.swift index f76494ac..61df6d40 100644 --- a/AltStore/My Apps/MyAppsViewController.swift +++ b/AltStore/My Apps/MyAppsViewController.swift @@ -54,6 +54,7 @@ final class MyAppsViewController: UICollectionViewController private var refreshGroup: RefreshGroup? private var sideloadingProgress: Progress? private var dropDestinationIndexPath: IndexPath? + private var isCheckingForUpdates = false private var _imagePickerInstalledApp: InstalledApp? @@ -97,6 +98,10 @@ final class MyAppsViewController: UICollectionViewController self.collectionView.register(InstalledAppsCollectionHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "ActiveAppsHeader") self.collectionView.register(InstalledAppsCollectionHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "InactiveAppsHeader") + let refreshControl = UIRefreshControl() + refreshControl.addTarget(self, action: #selector(MyAppsViewController.checkForUpdates(_:)), for: .primaryActionTriggered) + self.collectionView.refreshControl = refreshControl + self.sideloadingProgressView = UIProgressView(progressViewStyle: .bar) self.sideloadingProgressView.translatesAutoresizingMaskIntoConstraints = false self.sideloadingProgressView.progressTintColor = .altPrimary @@ -1441,6 +1446,44 @@ private extension MyAppsViewController } } } + + @objc func checkForUpdates(_ sender: UIRefreshControl) + { + guard !self.isCheckingForUpdates else { return } + self.isCheckingForUpdates = true + + AppManager.shared.fetchSources() { (result) in + do + { + do + { + defer { + DispatchQueue.main.async { + self.isCheckingForUpdates = false + sender.endRefreshing() + } + } + + let (_, context) = try result.get() + try context.save() + + } + catch let error as AppManager.FetchSourcesError + { + try error.managedObjectContext?.save() + throw error + } + } + catch let error as NSError + { + DispatchQueue.main.async { + let toastView = ToastView(error: error.withLocalizedTitle(NSLocalizedString("Failed to Check for Updates", comment: ""))) + toastView.addTarget(nil, action: #selector(TabBarController.presentSources), for: .touchUpInside) + toastView.show(in: self) + } + } + } + } } extension MyAppsViewController