2019-07-30 17:00:04 -07:00
|
|
|
//
|
|
|
|
|
// Source.swift
|
|
|
|
|
// AltStore
|
|
|
|
|
//
|
|
|
|
|
// Created by Riley Testut on 7/30/19.
|
|
|
|
|
// Copyright © 2019 Riley Testut. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import CoreData
|
2023-04-04 13:46:04 -05:00
|
|
|
import UIKit
|
2019-07-30 17:00:04 -07:00
|
|
|
|
2020-09-03 16:39:08 -07:00
|
|
|
public extension Source
|
2019-07-30 17:00:04 -07:00
|
|
|
{
|
2020-04-01 11:51:00 -07:00
|
|
|
#if ALPHA
|
|
|
|
|
static let altStoreIdentifier = "com.rileytestut.AltStore.Alpha"
|
|
|
|
|
#else
|
2019-07-30 17:00:04 -07:00
|
|
|
static let altStoreIdentifier = "com.rileytestut.AltStore"
|
2020-04-01 11:51:00 -07:00
|
|
|
#endif
|
2019-11-04 13:38:54 -08:00
|
|
|
|
|
|
|
|
#if STAGING
|
2020-04-01 11:51:00 -07:00
|
|
|
|
|
|
|
|
#if ALPHA
|
|
|
|
|
static let altStoreSourceURL = URL(string: "https://f000.backblazeb2.com/file/altstore-staging/sources/alpha/apps-alpha-staging.json")!
|
|
|
|
|
#else
|
2019-11-04 13:38:54 -08:00
|
|
|
static let altStoreSourceURL = URL(string: "https://f000.backblazeb2.com/file/altstore-staging/apps-staging.json")!
|
2020-04-01 11:51:00 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
#if ALPHA
|
|
|
|
|
static let altStoreSourceURL = URL(string: "https://alpha.altstore.io/")!
|
2019-11-04 13:38:54 -08:00
|
|
|
#else
|
2020-03-24 13:27:44 -07:00
|
|
|
static let altStoreSourceURL = URL(string: "https://apps.altstore.io/")!
|
2019-11-04 13:38:54 -08:00
|
|
|
#endif
|
2020-04-01 11:51:00 -07:00
|
|
|
|
|
|
|
|
#endif
|
2019-07-30 17:00:04 -07:00
|
|
|
}
|
|
|
|
|
|
2023-04-04 13:46:04 -05:00
|
|
|
public extension Source
|
|
|
|
|
{
|
|
|
|
|
// Fallbacks for optional JSON values.
|
|
|
|
|
|
|
|
|
|
var effectiveIconURL: URL? {
|
|
|
|
|
return self.iconURL ?? self.apps.first?.iconURL
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var effectiveHeaderImageURL: URL? {
|
|
|
|
|
return self.headerImageURL ?? self.effectiveIconURL
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var effectiveTintColor: UIColor? {
|
|
|
|
|
return self.tintColor ?? self.apps.first?.tintColor
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var effectiveFeaturedApps: [StoreApp] {
|
|
|
|
|
return self.featuredApps ?? self.apps
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-30 17:00:04 -07:00
|
|
|
@objc(Source)
|
2020-09-03 16:39:08 -07:00
|
|
|
public class Source: NSManagedObject, Fetchable, Decodable
|
2019-07-30 17:00:04 -07:00
|
|
|
{
|
|
|
|
|
/* Properties */
|
2020-09-03 16:39:08 -07:00
|
|
|
@NSManaged public var name: String
|
|
|
|
|
@NSManaged public var identifier: String
|
|
|
|
|
@NSManaged public var sourceURL: URL
|
2019-07-30 17:00:04 -07:00
|
|
|
|
2023-04-04 13:46:04 -05:00
|
|
|
/* Source Detail */
|
|
|
|
|
@NSManaged public var subtitle: String?
|
|
|
|
|
@NSManaged public var websiteURL: URL?
|
|
|
|
|
@NSManaged public var localizedDescription: String?
|
|
|
|
|
|
|
|
|
|
// Optional properties with fallbacks.
|
|
|
|
|
// `private` to prevent accidentally using instead of `effective[PropertyName]`
|
|
|
|
|
@NSManaged private var iconURL: URL?
|
|
|
|
|
@NSManaged private var headerImageURL: URL?
|
|
|
|
|
@NSManaged private var tintColor: UIColor?
|
|
|
|
|
|
2020-09-03 16:39:08 -07:00
|
|
|
@NSManaged public var error: NSError?
|
2020-08-27 16:39:03 -07:00
|
|
|
|
2019-11-04 13:42:19 -08:00
|
|
|
/* Non-Core Data Properties */
|
2020-09-03 16:39:08 -07:00
|
|
|
public var userInfo: [ALTSourceUserInfoKey: String]?
|
2019-11-04 13:42:19 -08:00
|
|
|
|
2019-07-30 17:00:04 -07:00
|
|
|
/* Relationships */
|
2020-09-03 16:39:08 -07:00
|
|
|
@objc(apps) @NSManaged public private(set) var _apps: NSOrderedSet
|
|
|
|
|
@objc(newsItems) @NSManaged public private(set) var _newsItems: NSOrderedSet
|
2019-07-30 17:00:04 -07:00
|
|
|
|
2023-04-04 13:46:04 -05:00
|
|
|
@objc(featuredApps) @NSManaged public private(set) var _featuredApps: NSOrderedSet
|
|
|
|
|
@objc(hasFeaturedApps) @NSManaged private var _hasFeaturedApps: Bool
|
|
|
|
|
|
2020-09-03 16:39:08 -07:00
|
|
|
@nonobjc public var apps: [StoreApp] {
|
2019-07-30 17:00:04 -07:00
|
|
|
get {
|
2019-07-31 14:07:00 -07:00
|
|
|
return self._apps.array as! [StoreApp]
|
2019-07-30 17:00:04 -07:00
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
self._apps = NSOrderedSet(array: newValue)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-03 16:39:08 -07:00
|
|
|
@nonobjc public var newsItems: [NewsItem] {
|
2019-09-03 21:58:07 -07:00
|
|
|
get {
|
|
|
|
|
return self._newsItems.array as! [NewsItem]
|
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
self._newsItems = NSOrderedSet(array: newValue)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-04 13:46:04 -05:00
|
|
|
// `internal` to prevent accidentally using instead of `effectiveFeaturedApps`
|
|
|
|
|
@nonobjc internal var featuredApps: [StoreApp]? {
|
|
|
|
|
return self._hasFeaturedApps ? self._featuredApps.array as? [StoreApp] : nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-30 17:00:04 -07:00
|
|
|
private enum CodingKeys: String, CodingKey
|
|
|
|
|
{
|
|
|
|
|
case name
|
|
|
|
|
case identifier
|
|
|
|
|
case sourceURL
|
2023-04-04 13:46:04 -05:00
|
|
|
case subtitle
|
|
|
|
|
case localizedDescription = "description"
|
|
|
|
|
case iconURL
|
|
|
|
|
case headerImageURL = "headerURL"
|
|
|
|
|
case websiteURL = "website"
|
|
|
|
|
case tintColor
|
|
|
|
|
|
2019-07-30 17:00:04 -07:00
|
|
|
case apps
|
2019-09-03 21:58:07 -07:00
|
|
|
case news
|
2023-04-04 13:46:04 -05:00
|
|
|
case featuredApps
|
|
|
|
|
case userInfo
|
2019-07-30 17:00:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?)
|
|
|
|
|
{
|
|
|
|
|
super.init(entity: entity, insertInto: context)
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-03 16:39:08 -07:00
|
|
|
public required init(from decoder: Decoder) throws
|
2019-07-30 17:00:04 -07:00
|
|
|
{
|
|
|
|
|
guard let context = decoder.managedObjectContext else { preconditionFailure("Decoder must have non-nil NSManagedObjectContext.") }
|
2020-03-24 13:27:44 -07:00
|
|
|
guard let sourceURL = decoder.sourceURL else { preconditionFailure("Decoder must have non-nil sourceURL.") }
|
2019-07-30 17:00:04 -07:00
|
|
|
|
2020-08-27 16:23:50 -07:00
|
|
|
super.init(entity: Source.entity(), insertInto: context)
|
2019-07-30 17:00:04 -07:00
|
|
|
|
2020-08-27 16:23:50 -07:00
|
|
|
do
|
2019-09-03 21:58:07 -07:00
|
|
|
{
|
2020-08-27 16:23:50 -07:00
|
|
|
self.sourceURL = sourceURL
|
|
|
|
|
|
|
|
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
|
|
|
self.name = try container.decode(String.self, forKey: .name)
|
|
|
|
|
self.identifier = try container.decode(String.self, forKey: .identifier)
|
|
|
|
|
|
2023-04-04 13:46:04 -05:00
|
|
|
// Optional Values
|
|
|
|
|
self.subtitle = try container.decodeIfPresent(String.self, forKey: .subtitle)
|
|
|
|
|
self.websiteURL = try container.decodeIfPresent(URL.self, forKey: .websiteURL)
|
|
|
|
|
self.localizedDescription = try container.decodeIfPresent(String.self, forKey: .localizedDescription)
|
|
|
|
|
self.iconURL = try container.decodeIfPresent(URL.self, forKey: .iconURL)
|
|
|
|
|
self.headerImageURL = try container.decodeIfPresent(URL.self, forKey: .headerImageURL)
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-27 16:23:50 -07:00
|
|
|
let userInfo = try container.decodeIfPresent([String: String].self, forKey: .userInfo)
|
|
|
|
|
self.userInfo = userInfo?.reduce(into: [:]) { $0[ALTSourceUserInfoKey($1.key)] = $1.value }
|
2019-09-03 21:58:07 -07:00
|
|
|
|
2020-08-27 16:23:50 -07:00
|
|
|
let apps = try container.decodeIfPresent([StoreApp].self, forKey: .apps) ?? []
|
|
|
|
|
let appsByID = Dictionary(apps.map { ($0.bundleIdentifier, $0) }, uniquingKeysWith: { (a, b) in return a })
|
|
|
|
|
|
|
|
|
|
for (index, app) in apps.enumerated()
|
|
|
|
|
{
|
|
|
|
|
app.sourceIdentifier = self.identifier
|
|
|
|
|
app.sortIndex = Int32(index)
|
|
|
|
|
}
|
|
|
|
|
self._apps = NSMutableOrderedSet(array: apps)
|
|
|
|
|
|
|
|
|
|
let newsItems = try container.decodeIfPresent([NewsItem].self, forKey: .news) ?? []
|
|
|
|
|
for (index, item) in newsItems.enumerated()
|
2019-09-03 21:58:07 -07:00
|
|
|
{
|
2020-08-27 16:23:50 -07:00
|
|
|
item.sourceIdentifier = self.identifier
|
|
|
|
|
item.sortIndex = Int32(index)
|
2019-09-03 21:58:07 -07:00
|
|
|
}
|
2020-08-27 16:23:50 -07:00
|
|
|
|
|
|
|
|
for newsItem in newsItems
|
2020-03-24 13:27:44 -07:00
|
|
|
{
|
2020-08-27 16:23:50 -07:00
|
|
|
guard let appID = newsItem.appID else { continue }
|
|
|
|
|
|
|
|
|
|
if let storeApp = appsByID[appID]
|
|
|
|
|
{
|
|
|
|
|
newsItem.storeApp = storeApp
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
newsItem.storeApp = nil
|
|
|
|
|
}
|
2020-03-24 13:27:44 -07:00
|
|
|
}
|
2020-08-27 16:23:50 -07:00
|
|
|
self._newsItems = NSMutableOrderedSet(array: newsItems)
|
2023-04-04 13:46:04 -05:00
|
|
|
|
|
|
|
|
let featuredAppBundleIDs = try container.decodeIfPresent([String].self, forKey: .featuredApps)
|
|
|
|
|
let featuredApps = featuredAppBundleIDs?.compactMap { appsByID[$0] }
|
|
|
|
|
self.setFeaturedApps(featuredApps)
|
2020-08-27 16:23:50 -07:00
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
if let context = self.managedObjectContext
|
|
|
|
|
{
|
|
|
|
|
context.delete(self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw error
|
2019-09-03 21:58:07 -07:00
|
|
|
}
|
2019-07-30 17:00:04 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-04 13:46:04 -05:00
|
|
|
internal extension Source
|
|
|
|
|
{
|
|
|
|
|
func setFeaturedApps(_ featuredApps: [StoreApp]?)
|
|
|
|
|
{
|
|
|
|
|
// Explicitly update relationships for all apps to ensure featuredApps merges correctly.
|
|
|
|
|
|
|
|
|
|
for case let storeApp as StoreApp in self._apps
|
|
|
|
|
{
|
|
|
|
|
if let featuredApps, featuredApps.contains(where: { $0.bundleIdentifier == storeApp.bundleIdentifier })
|
|
|
|
|
{
|
|
|
|
|
storeApp.featuringSource = self
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
storeApp.featuringSource = nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self._featuredApps = NSOrderedSet(array: featuredApps ?? [])
|
|
|
|
|
self._hasFeaturedApps = (featuredApps != nil)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-03 16:39:08 -07:00
|
|
|
public extension Source
|
2019-07-30 17:00:04 -07:00
|
|
|
{
|
2020-03-24 13:27:44 -07:00
|
|
|
@nonobjc class func fetchRequest() -> NSFetchRequest<Source>
|
|
|
|
|
{
|
|
|
|
|
return NSFetchRequest<Source>(entityName: "Source")
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-30 17:00:04 -07:00
|
|
|
class func makeAltStoreSource(in context: NSManagedObjectContext) -> Source
|
|
|
|
|
{
|
|
|
|
|
let source = Source(context: context)
|
|
|
|
|
source.name = "AltStore"
|
|
|
|
|
source.identifier = Source.altStoreIdentifier
|
2019-09-14 12:38:53 -07:00
|
|
|
source.sourceURL = Source.altStoreSourceURL
|
2019-07-30 17:00:04 -07:00
|
|
|
|
|
|
|
|
return source
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class func fetchAltStoreSource(in context: NSManagedObjectContext) -> Source?
|
|
|
|
|
{
|
|
|
|
|
let source = Source.first(satisfying: NSPredicate(format: "%K == %@", #keyPath(Source.identifier), Source.altStoreIdentifier), in: context)
|
|
|
|
|
return source
|
|
|
|
|
}
|
|
|
|
|
}
|