[AltStore] Update AltStore itself from UpdatesViewController

This commit is contained in:
Riley Testut
2019-06-17 16:31:10 -07:00
parent d65cef8817
commit 205fb3d7e9
7 changed files with 151 additions and 36 deletions

View File

@@ -11,8 +11,13 @@ import CoreData
import Roxas
extension App
{
static let altstoreAppID = "com.rileytestut.AltStore"
}
@objc(App)
class App: NSManagedObject, Decodable
class App: NSManagedObject, Decodable, Fetchable
{
/* Properties */
@NSManaged private(set) var name: String
@@ -24,7 +29,7 @@ class App: NSManagedObject, Decodable
@NSManaged private(set) var iconName: String
@NSManaged private(set) var screenshotNames: [String]
@NSManaged private(set) var version: String
@NSManaged var version: String
@NSManaged private(set) var versionDate: Date
@NSManaged private(set) var versionDescription: String?
@@ -83,4 +88,20 @@ extension App
{
return NSFetchRequest<App>(entityName: "App")
}
class func makeAltStoreApp(in context: NSManagedObjectContext) -> App
{
let app = App(context: context)
app.name = "AltStore"
app.identifier = "com.rileytestut.AltStore"
app.developerName = "Riley Testut"
app.localizedDescription = "AltStore is an alternative App Store."
app.iconName = ""
app.screenshotNames = []
app.version = "1.0"
app.versionDate = Date()
app.downloadURL = URL(string: "http://rileytestut.com")!
return app
}
}

View File

@@ -33,9 +33,15 @@ public extension DatabaseManager
self.persistentContainer.loadPersistentStores { (description, error) in
guard error == nil else { return completionHandler(error!) }
self.isStarted = true
completionHandler(error)
self.prepareDatabase() { (result) in
switch result
{
case .failure(let error): completionHandler(error)
case .success:
self.isStarted = true
completionHandler(nil)
}
}
}
}
@@ -94,3 +100,45 @@ extension DatabaseManager
return activeTeam
}
}
private extension DatabaseManager
{
func prepareDatabase(completionHandler: @escaping (Result<Void, Error>) -> Void)
{
self.persistentContainer.performBackgroundTask { (context) in
let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "1.0"
let altStoreApp: App
if let app = App.first(satisfying: NSPredicate(format: "%K == %@", #keyPath(App.identifier), App.altstoreAppID), in: context)
{
altStoreApp = app
}
else
{
altStoreApp = App.makeAltStoreApp(in: context)
altStoreApp.version = version
}
if let installedApp = altStoreApp.installedApp
{
installedApp.version = version
}
else
{
let installedApp = InstalledApp(app: altStoreApp, bundleIdentifier: altStoreApp.identifier, expirationDate: Date(), context: context)
installedApp.version = version
}
do
{
try context.save()
completionHandler(.success(()))
}
catch
{
completionHandler(.failure(error))
}
}
}
}