From 20cd6d98fc2c96d5fca4571e8b0189199275621d Mon Sep 17 00:00:00 2001 From: Riley Testut Date: Tue, 4 Apr 2023 14:19:05 -0500 Subject: [PATCH] [AltStoreCore] Adds Source.isAdded MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Convenience property to determine whether a source has been added to the user’s AltStore. --- AltStoreCore/Model/Source.swift | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/AltStoreCore/Model/Source.swift b/AltStoreCore/Model/Source.swift index 5b60eeba..288cb5ca 100644 --- a/AltStoreCore/Model/Source.swift +++ b/AltStoreCore/Model/Source.swift @@ -356,6 +356,28 @@ public class Source: NSManagedObject, Fetchable, Decodable } } +public extension Source +{ + // Source is considered added IFF it has been saved to disk, + // which we can check by fetching on a new managed object context. + var isAdded: Bool { + get async throws { + let identifier = await AsyncManaged(wrappedValue: self).identifier + let backgroundContext = DatabaseManager.shared.persistentContainer.newBackgroundContext() + + let isAdded = try await backgroundContext.performAsync { + let fetchRequest = Source.fetchRequest() + fetchRequest.predicate = NSPredicate(format: "%K == %@", #keyPath(Source.identifier), identifier) + + let count = try backgroundContext.count(for: fetchRequest) + return (count > 0) + } + + return isAdded + } + } +} + internal extension Source { func setFeaturedApps(_ featuredApps: [StoreApp]?)