diff --git a/AltStore/Model/DatabaseManager.swift b/AltStore/Model/DatabaseManager.swift index 5279da24..f5d65222 100644 --- a/AltStore/Model/DatabaseManager.swift +++ b/AltStore/Model/DatabaseManager.swift @@ -168,7 +168,6 @@ private extension DatabaseManager { installedApp = InstalledApp(resignedApp: localApp, originalBundleIdentifier: StoreApp.altstoreAppID, context: context) installedApp.storeApp = storeApp - installedApp.installedDate = Date() } let fileURL = installedApp.fileURL @@ -195,13 +194,7 @@ private extension DatabaseManager } // Must go after comparing versions to see if we need to update our cached AltStore app bundle. - installedApp.version = localApp.version - - if let provisioningProfile = localApp.provisioningProfile - { - installedApp.refreshedDate = provisioningProfile.creationDate - installedApp.expirationDate = provisioningProfile.expirationDate - } + installedApp.update(resignedApp: localApp) do { diff --git a/AltStore/Model/InstalledApp.swift b/AltStore/Model/InstalledApp.swift index 61c49f0b..e2b5dd73 100644 --- a/AltStore/Model/InstalledApp.swift +++ b/AltStore/Model/InstalledApp.swift @@ -54,10 +54,21 @@ class InstalledApp: NSManagedObject, InstalledAppProtocol { super.init(entity: InstalledApp.entity(), insertInto: context) - self.name = resignedApp.name self.bundleIdentifier = originalBundleIdentifier - self.resignedBundleIdentifier = resignedApp.bundleIdentifier + self.refreshedDate = Date() + self.installedDate = Date() + + self.expirationDate = self.refreshedDate.addingTimeInterval(60 * 60 * 24 * 7) // Rough estimate until we get real values from provisioning profile. + + self.update(resignedApp: resignedApp) + } + + func update(resignedApp: ALTApplication) + { + self.name = resignedApp.name + + self.resignedBundleIdentifier = resignedApp.bundleIdentifier self.version = resignedApp.version if let provisioningProfile = resignedApp.provisioningProfile @@ -65,13 +76,6 @@ class InstalledApp: NSManagedObject, InstalledAppProtocol self.refreshedDate = provisioningProfile.creationDate self.expirationDate = provisioningProfile.expirationDate } - else - { - self.refreshedDate = Date() - self.expirationDate = self.refreshedDate.addingTimeInterval(60 * 60 * 24 * 7) // Rough estimate until we get real values from provisioning profile. - } - - self.installedDate = Date() } } diff --git a/AltStore/Model/InstalledExtension.swift b/AltStore/Model/InstalledExtension.swift index fe9fcea0..f766fa5b 100644 --- a/AltStore/Model/InstalledExtension.swift +++ b/AltStore/Model/InstalledExtension.swift @@ -36,10 +36,21 @@ class InstalledExtension: NSManagedObject, InstalledAppProtocol { super.init(entity: InstalledExtension.entity(), insertInto: context) - self.name = resignedAppExtension.name self.bundleIdentifier = originalBundleIdentifier - self.resignedBundleIdentifier = resignedAppExtension.bundleIdentifier + self.refreshedDate = Date() + self.installedDate = Date() + + self.expirationDate = self.refreshedDate.addingTimeInterval(60 * 60 * 24 * 7) // Rough estimate until we get real values from provisioning profile. + + self.update(resignedAppExtension: resignedAppExtension) + } + + func update(resignedAppExtension: ALTApplication) + { + self.name = resignedAppExtension.name + + self.resignedBundleIdentifier = resignedAppExtension.bundleIdentifier self.version = resignedAppExtension.version if let provisioningProfile = resignedAppExtension.provisioningProfile @@ -47,13 +58,6 @@ class InstalledExtension: NSManagedObject, InstalledAppProtocol self.refreshedDate = provisioningProfile.creationDate self.expirationDate = provisioningProfile.expirationDate } - else - { - self.refreshedDate = Date() - self.expirationDate = self.refreshedDate.addingTimeInterval(60 * 60 * 24 * 7) // Rough estimate until we get real values from provisioning profile. - } - - self.installedDate = Date() } } diff --git a/AltStore/Operations/InstallAppOperation.swift b/AltStore/Operations/InstallAppOperation.swift index f6e7c299..ca7fdc9a 100644 --- a/AltStore/Operations/InstallAppOperation.swift +++ b/AltStore/Operations/InstallAppOperation.swift @@ -46,6 +46,8 @@ class InstallAppOperation: ResultOperation let backgroundContext = DatabaseManager.shared.persistentContainer.newBackgroundContext() backgroundContext.perform { + + /* App */ let installedApp: InstalledApp // Fetch + update rather than insert + resolve merge conflicts to prevent potential context-level conflicts. @@ -56,9 +58,16 @@ class InstallAppOperation: ResultOperation else { installedApp = InstalledApp(resignedApp: resignedApp, originalBundleIdentifier: self.context.bundleIdentifier, context: backgroundContext) - installedApp.installedDate = Date() } + installedApp.update(resignedApp: resignedApp) + + if let team = DatabaseManager.shared.activeTeam(in: backgroundContext) + { + installedApp.team = team + } + + /* App Extensions */ var installedExtensions = Set() if @@ -71,37 +80,31 @@ class InstallAppOperation: ResultOperation guard let appExtensionBundle = Bundle(url: fileURL) else { continue } guard let appExtension = ALTApplication(fileURL: appExtensionBundle.bundleURL) else { continue } + let parentBundleID = self.context.bundleIdentifier + let resignedParentBundleID = resignedApp.bundleIdentifier + + let resignedBundleID = appExtension.bundleIdentifier + let originalBundleID = resignedBundleID.replacingOccurrences(of: resignedParentBundleID, with: parentBundleID) + let installedExtension: InstalledExtension - if let appExtension = installedApp.appExtensions.first(where: { $0.bundleIdentifier == appExtension.bundleIdentifier }) + if let appExtension = installedApp.appExtensions.first(where: { $0.bundleIdentifier == originalBundleID }) { installedExtension = appExtension } else { - installedExtension = InstalledExtension(resignedAppExtension: appExtension, originalBundleIdentifier: appExtension.bundleIdentifier, context: backgroundContext) - installedExtension.installedDate = Date() + installedExtension = InstalledExtension(resignedAppExtension: appExtension, originalBundleIdentifier: originalBundleID, context: backgroundContext) } + installedExtension.update(resignedAppExtension: appExtension) + installedExtensions.insert(installedExtension) } } installedApp.appExtensions = installedExtensions - installedApp.version = resignedApp.version - - if let profile = resignedApp.provisioningProfile - { - installedApp.refreshedDate = profile.creationDate - installedApp.expirationDate = profile.expirationDate - } - - if let team = Team.first(satisfying: NSPredicate(format: "%K == %@", #keyPath(Team.isActiveTeam), NSNumber(value: true)), in: backgroundContext) - { - installedApp.team = team - } - // Temporary directory and resigned .ipa no longer needed, so delete them now to ensure AltStore doesn't quit before we get the chance to. self.cleanUp()