Files
SideStore/AltStore/Model/App.swift

142 lines
4.8 KiB
Swift
Raw Normal View History

//
2019-07-31 14:07:00 -07:00
// StoreApp.swift
// AltStore
//
2019-05-20 21:24:53 +02:00
// Created by Riley Testut on 5/20/19.
// Copyright © 2019 Riley Testut. All rights reserved.
//
import Foundation
2019-05-20 21:24:53 +02:00
import CoreData
import Roxas
import AltSign
2019-07-31 14:07:00 -07:00
extension StoreApp
{
static let altstoreAppID = "com.rileytestut.AltStore"
}
2019-07-31 14:07:00 -07:00
@objc(StoreApp)
class StoreApp: NSManagedObject, Decodable, Fetchable
{
2019-05-20 21:24:53 +02:00
/* Properties */
@NSManaged private(set) var name: String
@NSManaged private(set) var bundleIdentifier: String
@NSManaged private(set) var subtitle: String?
2019-05-20 21:24:53 +02:00
@NSManaged private(set) var developerName: String
@NSManaged private(set) var localizedDescription: String
2019-07-29 16:02:15 -07:00
@NSManaged private(set) var size: Int32
@NSManaged private(set) var iconURL: URL
@NSManaged private(set) var screenshotURLs: [URL]
2019-05-20 21:24:53 +02:00
@NSManaged var version: String
@NSManaged private(set) var versionDate: Date
@NSManaged private(set) var versionDescription: String?
@NSManaged private(set) var downloadURL: URL
@NSManaged private(set) var tintColor: UIColor?
@NSManaged var sortIndex: Int32
/* Relationships */
@NSManaged var installedApp: InstalledApp?
@NSManaged var source: Source?
@objc(permissions) @NSManaged var _permissions: NSOrderedSet
@nonobjc var permissions: [AppPermission] {
return self._permissions.array as! [AppPermission]
}
2019-05-20 21:24:53 +02:00
private override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?)
{
super.init(entity: entity, insertInto: context)
}
private enum CodingKeys: String, CodingKey
{
case name
case bundleIdentifier
2019-05-20 21:24:53 +02:00
case developerName
case localizedDescription
case version
case versionDescription
case versionDate
case iconURL
case screenshotURLs
case downloadURL
case tintColor
case subtitle
case permissions
2019-07-29 16:02:15 -07:00
case size
2019-05-20 21:24:53 +02:00
}
required init(from decoder: Decoder) throws
{
guard let context = decoder.managedObjectContext else { preconditionFailure("Decoder must have non-nil NSManagedObjectContext.") }
2019-07-31 14:07:00 -07:00
super.init(entity: StoreApp.entity(), insertInto: nil)
2019-05-20 21:24:53 +02:00
let container = try decoder.container(keyedBy: CodingKeys.self)
self.name = try container.decode(String.self, forKey: .name)
self.bundleIdentifier = try container.decode(String.self, forKey: .bundleIdentifier)
2019-05-20 21:24:53 +02:00
self.developerName = try container.decode(String.self, forKey: .developerName)
self.localizedDescription = try container.decode(String.self, forKey: .localizedDescription)
self.subtitle = try container.decodeIfPresent(String.self, forKey: .subtitle)
self.version = try container.decode(String.self, forKey: .version)
self.versionDate = try container.decode(Date.self, forKey: .versionDate)
self.versionDescription = try container.decodeIfPresent(String.self, forKey: .versionDescription)
self.iconURL = try container.decode(URL.self, forKey: .iconURL)
self.screenshotURLs = try container.decodeIfPresent([URL].self, forKey: .screenshotURLs) ?? []
2019-05-20 21:24:53 +02:00
self.downloadURL = try container.decode(URL.self, forKey: .downloadURL)
if let tintColorHex = try container.decodeIfPresent(String.self, forKey: .tintColor)
{
guard let tintColor = UIColor(hexString: tintColorHex) else {
throw DecodingError.dataCorruptedError(forKey: .tintColor, in: container, debugDescription: "Hex code is invalid.")
}
self.tintColor = tintColor
}
2019-07-29 16:02:15 -07:00
self.size = try container.decode(Int32.self, forKey: .size)
let permissions = try container.decodeIfPresent([AppPermission].self, forKey: .permissions) ?? []
2019-05-20 21:24:53 +02:00
context.insert(self)
// Must assign after we're inserted into context.
self._permissions = NSOrderedSet(array: permissions)
2019-05-20 21:24:53 +02:00
}
}
2019-07-31 14:07:00 -07:00
extension StoreApp
2019-05-20 21:24:53 +02:00
{
2019-07-31 14:07:00 -07:00
@nonobjc class func fetchRequest() -> NSFetchRequest<StoreApp>
2019-05-20 21:24:53 +02:00
{
2019-07-31 14:07:00 -07:00
return NSFetchRequest<StoreApp>(entityName: "StoreApp")
2019-05-20 21:24:53 +02:00
}
2019-07-31 14:07:00 -07:00
class func makeAltStoreApp(in context: NSManagedObjectContext) -> StoreApp
{
2019-07-31 14:07:00 -07:00
let app = StoreApp(context: context)
app.name = "AltStore"
2019-07-31 14:07:00 -07:00
app.bundleIdentifier = StoreApp.altstoreAppID
app.developerName = "Riley Testut"
app.localizedDescription = "AltStore is an alternative App Store."
app.iconURL = URL(string: "https://user-images.githubusercontent.com/705880/63392210-540c5980-c37b-11e9-968c-8742fc68ab2e.png")!
app.screenshotURLs = []
app.version = "1.0"
app.versionDate = Date()
app.downloadURL = URL(string: "http://rileytestut.com")!
return app
}
}