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-01-04 09:31:28 -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
|
|
|
{
|
2025-03-23 11:56:17 -07:00
|
|
|
#if ALPHA
|
|
|
|
|
static let altStoreGroupIdentifier = Bundle.Info.appbundleIdentifier
|
|
|
|
|
#else
|
|
|
|
|
static let altStoreGroupIdentifier = Bundle.Info.appbundleIdentifier
|
|
|
|
|
#endif
|
2019-11-04 13:38:54 -08:00
|
|
|
|
2023-02-02 08:09:15 -08:00
|
|
|
#if STAGING
|
2020-04-01 11:51:00 -07:00
|
|
|
|
2023-02-02 08:09:15 -08:00
|
|
|
#if ALPHA
|
|
|
|
|
static let altStoreSourceURL = URL(string: "https://apps.sidestore.io/")!
|
|
|
|
|
#else
|
|
|
|
|
static let altStoreSourceURL = URL(string: "https://apps.sidestore.io/")!
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
#if ALPHA
|
2025-03-23 11:56:17 -07:00
|
|
|
static let altStoreSourceURL = URL(string: "https://sidestore.io/apps-v2.json/")!
|
2023-02-02 08:09:15 -08:00
|
|
|
#else
|
2025-03-23 11:56:17 -07:00
|
|
|
static let altStoreSourceURL = URL(string: "https://sidestore.io/apps-v2.json/")!
|
2023-02-02 08:09:15 -08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#endif
|
2025-03-23 11:56:17 -07:00
|
|
|
|
|
|
|
|
// normalized url is the source identifier (or) p-key!
|
|
|
|
|
static let altStoreIdentifier = try! Source.sourceID(from: altStoreSourceURL)
|
2019-07-30 17:00:04 -07:00
|
|
|
}
|
|
|
|
|
|
2025-02-09 16:18:41 +05:30
|
|
|
// public struct AppPermissionFeed: Codable {
|
|
|
|
|
// let type: String // ALTAppPermissionType
|
|
|
|
|
// let usageDescription: String
|
2023-01-04 09:31:28 -05:00
|
|
|
|
2025-02-09 16:18:41 +05:30
|
|
|
// enum CodingKeys: String, CodingKey
|
|
|
|
|
// {
|
|
|
|
|
// case type
|
|
|
|
|
// case usageDescription
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2023-01-04 09:31:28 -05:00
|
|
|
|
2025-02-09 16:18:41 +05:30
|
|
|
// public struct AppVersionFeed: Codable {
|
|
|
|
|
// /* Properties */
|
|
|
|
|
// let version: String
|
|
|
|
|
// let date: Date
|
|
|
|
|
// let localizedDescription: String?
|
|
|
|
|
|
|
|
|
|
// let downloadURL: URL
|
|
|
|
|
// let size: Int64
|
|
|
|
|
// // added in 0.6.0
|
|
|
|
|
// let sha256: String? // sha 256 of the uploaded IPA
|
|
|
|
|
|
|
|
|
|
// enum CodingKeys: String, CodingKey
|
|
|
|
|
// {
|
|
|
|
|
// case version
|
|
|
|
|
// case date
|
|
|
|
|
// case localizedDescription
|
|
|
|
|
// case downloadURL
|
|
|
|
|
// case size
|
|
|
|
|
// // added in 0.6.0
|
|
|
|
|
// case sha256
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2023-01-04 09:31:28 -05:00
|
|
|
|
2025-02-09 16:18:41 +05:30
|
|
|
// public struct PlatformURLFeed: Codable {
|
|
|
|
|
// /* Properties */
|
|
|
|
|
// let platform: Platform
|
|
|
|
|
// let downloadURL: URL
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// private enum CodingKeys: String, CodingKey
|
|
|
|
|
// {
|
|
|
|
|
// case platform
|
|
|
|
|
// case downloadURL
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2023-01-04 09:31:28 -05:00
|
|
|
|
|
|
|
|
|
2025-02-09 16:18:41 +05:30
|
|
|
// public struct StoreAppFeed: Codable {
|
|
|
|
|
// let name: String
|
|
|
|
|
// let bundleIdentifier: String
|
|
|
|
|
// let subtitle: String?
|
|
|
|
|
|
|
|
|
|
// let developerName: String
|
|
|
|
|
// let localizedDescription: String
|
|
|
|
|
// let size: Int64
|
|
|
|
|
|
|
|
|
|
// let iconURL: URL
|
|
|
|
|
// let screenshotURLs: [URL]
|
|
|
|
|
|
|
|
|
|
// let version: String
|
|
|
|
|
// let versionDate: Date
|
|
|
|
|
// let versionDescription: String?
|
|
|
|
|
// let downloadURL: URL
|
|
|
|
|
// let platformURLs: [PlatformURLFeed]?
|
|
|
|
|
|
|
|
|
|
// let tintColor: String? // UIColor?
|
|
|
|
|
// let isBeta: Bool
|
|
|
|
|
|
|
|
|
|
// // let source: Source?
|
|
|
|
|
// let appPermissions: [AppPermissionFeed]
|
|
|
|
|
// let versions: [AppVersionFeed]
|
|
|
|
|
|
|
|
|
|
// enum CodingKeys: String, CodingKey
|
|
|
|
|
// {
|
|
|
|
|
// case bundleIdentifier
|
|
|
|
|
// case developerName
|
|
|
|
|
// case downloadURL
|
|
|
|
|
// case iconURL
|
|
|
|
|
// case isBeta = "beta"
|
|
|
|
|
// case localizedDescription
|
|
|
|
|
// case name
|
|
|
|
|
// case appPermissions
|
|
|
|
|
// case platformURLs
|
|
|
|
|
// case screenshotURLs
|
|
|
|
|
// case size
|
|
|
|
|
// case subtitle
|
|
|
|
|
// case tintColor
|
|
|
|
|
// case version
|
|
|
|
|
// case versionDate
|
|
|
|
|
// case versionDescription
|
|
|
|
|
// case versions
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2023-01-04 09:31:28 -05:00
|
|
|
|
2025-02-09 16:18:41 +05:30
|
|
|
// public struct NewsItemFeed: Codable {
|
|
|
|
|
// let identifier: String
|
|
|
|
|
// let date: Date
|
|
|
|
|
|
|
|
|
|
// let title: String
|
|
|
|
|
// let caption: String
|
|
|
|
|
// let tintColor: String //UIColor
|
|
|
|
|
// let notify: Bool
|
|
|
|
|
|
|
|
|
|
// let imageURL: URL?
|
|
|
|
|
// let externalURL: URL?
|
|
|
|
|
|
|
|
|
|
// let appID: String?
|
|
|
|
|
|
|
|
|
|
// private enum CodingKeys: String, CodingKey
|
|
|
|
|
// {
|
|
|
|
|
// case identifier
|
|
|
|
|
// case date
|
|
|
|
|
// case title
|
|
|
|
|
// case caption
|
|
|
|
|
// case tintColor
|
|
|
|
|
// case imageURL
|
|
|
|
|
// case externalURL = "url"
|
|
|
|
|
// case appID
|
|
|
|
|
// case notify
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2023-01-04 09:31:28 -05:00
|
|
|
|
|
|
|
|
|
2025-02-09 16:18:41 +05:30
|
|
|
// public struct SourceJSON: Codable {
|
|
|
|
|
// let version: Int?
|
|
|
|
|
// let name: String
|
|
|
|
|
// let identifier: String
|
|
|
|
|
// let sourceURL: URL
|
|
|
|
|
// let userInfo: [String:String]? //[ALTSourceUserInfoKey:String]?
|
|
|
|
|
// let apps: [StoreAppFeed]
|
|
|
|
|
// let news: [NewsItemFeed]
|
|
|
|
|
|
|
|
|
|
// enum CodingKeys: String, CodingKey
|
|
|
|
|
// {
|
|
|
|
|
// case version
|
|
|
|
|
// case name
|
|
|
|
|
// case identifier
|
|
|
|
|
// case sourceURL
|
|
|
|
|
// case userInfo
|
|
|
|
|
// case apps
|
|
|
|
|
// case news
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2023-01-04 09:31:28 -05:00
|
|
|
|
2025-02-09 16:18:41 +05:30
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-04 13:46:04 -05:00
|
|
|
|
2019-07-30 17:00:04 -07:00
|
|
|
@objc(Source)
|
2025-02-08 04:45:22 +05:30
|
|
|
public class Source: BaseEntity, Decodable
|
2019-07-30 17:00:04 -07:00
|
|
|
{
|
|
|
|
|
/* Properties */
|
2025-02-08 04:45:22 +05:30
|
|
|
@NSManaged public var version: Int
|
2020-09-03 16:39:08 -07:00
|
|
|
@NSManaged public var name: String
|
2025-03-23 11:56:17 -07:00
|
|
|
@NSManaged public private(set) var identifier: String // NOTE: sourceID is just normalized sourceURL
|
|
|
|
|
@NSManaged public private(set) var groupID: String?
|
2020-09-03 16:39:08 -07:00
|
|
|
@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 localizedDescription: String?
|
2023-11-15 13:41:05 -06:00
|
|
|
@NSManaged public var websiteURL: URL?
|
|
|
|
|
@NSManaged public var patreonURL: URL?
|
2023-04-04 13:46:04 -05:00
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2023-12-08 14:32:57 -06:00
|
|
|
@NSManaged public var featuredSortID: String?
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-08 04:45:22 +05:30
|
|
|
|
|
|
|
|
public var isSourceAtLeastV2: Bool {
|
|
|
|
|
return self.version >= 2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
2025-02-08 04:45:22 +05:30
|
|
|
case version
|
2019-07-30 17:00:04 -07:00
|
|
|
case name
|
|
|
|
|
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
|
2023-11-15 13:41:05 -06:00
|
|
|
case patreonURL
|
2023-04-04 13:46:04 -05:00
|
|
|
|
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
|
2025-03-23 11:56:17 -07:00
|
|
|
|
|
|
|
|
// case identifier
|
|
|
|
|
case groupID = "identifier"
|
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
|
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
|
|
|
self.name = try container.decode(String.self, forKey: .name)
|
|
|
|
|
|
2023-04-04 13:46:04 -05:00
|
|
|
// Optional Values
|
2025-02-08 04:45:22 +05:30
|
|
|
|
|
|
|
|
// use sourceversion = 1 by default if not specified in source json
|
|
|
|
|
self.version = try container.decodeIfPresent(Int.self, forKey: .version) ?? 1
|
|
|
|
|
|
2023-04-04 13:46:04 -05:00
|
|
|
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)
|
2023-11-15 13:41:05 -06:00
|
|
|
self.patreonURL = try container.decodeIfPresent(URL.self, forKey: .patreonURL)
|
2023-04-04 13:46:04 -05:00
|
|
|
|
|
|
|
|
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.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.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)
|
2023-10-10 17:39:20 -05:00
|
|
|
|
|
|
|
|
// Updates identifier + apps & newsItems
|
|
|
|
|
try self.setSourceURL(sourceURL)
|
2025-03-23 11:56:17 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// NOTE: Source ID is just normalized sourceURL. coz normalized url is the primary key which needs to be unique
|
|
|
|
|
// Hence if a source's URL changed, then it means it is a different source now.
|
|
|
|
|
// This also means that the identifier field in the source is irrelevant (if any)
|
|
|
|
|
|
|
|
|
|
// if we want grouping of sources from same author or something like that then we should have used groupID (a new field)
|
|
|
|
|
// shouldn't use the existing "identifier" field, hence the following is commented out
|
|
|
|
|
|
|
|
|
|
// // if an explicit identifier is present, then use it
|
|
|
|
|
// if let identifier = try container.decodeIfPresent(String.self, forKey: .identifier)
|
|
|
|
|
// {
|
|
|
|
|
// self.identifier = identifier
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// if an explicit (group)identifier is present, then use it as groupID else use sourceID as groupID too
|
|
|
|
|
self.groupID = try container.decodeIfPresent(String.self, forKey: .groupID) ?? self.identifier
|
|
|
|
|
|
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-12-08 14:32:57 -06:00
|
|
|
|
|
|
|
|
public override func awakeFromInsert()
|
|
|
|
|
{
|
|
|
|
|
super.awakeFromInsert()
|
|
|
|
|
|
|
|
|
|
self.featuredSortID = UUID().uuidString
|
|
|
|
|
}
|
2019-07-30 17:00:04 -07:00
|
|
|
}
|
|
|
|
|
|
2023-04-04 14:19:05 -05:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-16 18:18:06 -05:00
|
|
|
|
|
|
|
|
var isRecommended: Bool {
|
|
|
|
|
guard let recommendedSources = UserDefaults.shared.recommendedSources else { return false }
|
|
|
|
|
|
|
|
|
|
// TODO: Support alternate URLs
|
|
|
|
|
let isRecommended = recommendedSources.contains { source in
|
2025-03-23 11:56:17 -07:00
|
|
|
return source.identifier == self.identifier || source.sourceURL?.absoluteString.lowercased() == self.sourceURL.absoluteString.lowercased()
|
2023-10-16 18:18:06 -05:00
|
|
|
}
|
|
|
|
|
return isRecommended
|
|
|
|
|
}
|
2023-10-17 14:49:13 -05:00
|
|
|
|
|
|
|
|
var lastUpdatedDate: Date? {
|
|
|
|
|
let allDates = self.apps.compactMap { $0.latestAvailableVersion?.date } + self.newsItems.map { $0.date }
|
|
|
|
|
|
|
|
|
|
let lastUpdatedDate = allDates.sorted().last
|
|
|
|
|
return lastUpdatedDate
|
|
|
|
|
}
|
2023-04-04 14:19:05 -05:00
|
|
|
}
|
|
|
|
|
|
2025-03-23 11:56:17 -07:00
|
|
|
public extension Source
|
2023-04-04 13:46:04 -05:00
|
|
|
{
|
2023-10-10 17:39:20 -05:00
|
|
|
class func sourceID(from sourceURL: URL) throws -> String
|
|
|
|
|
{
|
2023-11-15 13:20:50 -06:00
|
|
|
let sourceID = try sourceURL.normalized()
|
|
|
|
|
return sourceID
|
2023-10-10 17:39:20 -05:00
|
|
|
}
|
2025-03-23 11:56:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal extension Source
|
|
|
|
|
{
|
2023-04-04 13:46:04 -05:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-10 17:39:20 -05:00
|
|
|
public extension Source
|
|
|
|
|
{
|
|
|
|
|
func setSourceURL(_ sourceURL: URL) throws
|
|
|
|
|
{
|
2025-03-23 11:56:17 -07:00
|
|
|
self.sourceURL = sourceURL
|
|
|
|
|
|
|
|
|
|
// update the normalized sourceURL as the identifier
|
2025-03-23 11:57:16 -07:00
|
|
|
let identifier = try Source.sourceID(from: sourceURL)
|
2025-03-23 11:56:17 -07:00
|
|
|
try self.setSourceID(identifier)
|
|
|
|
|
}
|
2025-03-23 11:57:16 -07:00
|
|
|
|
2025-03-23 11:56:17 -07:00
|
|
|
func setSourceID(_ identifier: String) throws
|
|
|
|
|
{
|
2025-03-23 11:57:16 -07:00
|
|
|
self.identifier = identifier
|
2023-10-10 17:39:20 -05:00
|
|
|
|
|
|
|
|
for app in self.apps
|
|
|
|
|
{
|
|
|
|
|
app.sourceIdentifier = identifier
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-23 11:56:17 -07:00
|
|
|
for newsItem in self.newsItems
|
2023-10-10 17:39:20 -05:00
|
|
|
{
|
|
|
|
|
newsItem.sourceIdentifier = identifier
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
2022-10-18 01:10:18 -07:00
|
|
|
source.name = "SideStore Offical"
|
2025-03-23 11:56:17 -07:00
|
|
|
source.groupID = Source.altStoreGroupIdentifier
|
2019-07-30 17:00:04 -07:00
|
|
|
source.identifier = Source.altStoreIdentifier
|
2024-12-07 17:45:09 +05:30
|
|
|
try! source.setSourceURL(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
|
|
|
|
|
}
|
2023-10-17 14:49:13 -05:00
|
|
|
|
2025-03-23 11:56:17 -07:00
|
|
|
class func make(name: String, groupID: String, sourceURL: URL, context: NSManagedObjectContext) -> Source
|
2023-10-17 14:49:13 -05:00
|
|
|
{
|
|
|
|
|
let source = Source(context: context)
|
|
|
|
|
source.name = name
|
|
|
|
|
source.sourceURL = sourceURL
|
2025-03-23 11:56:17 -07:00
|
|
|
source.sourceURL = sourceURL
|
|
|
|
|
source.identifier = try! Source.sourceID(from: sourceURL)
|
|
|
|
|
|
2023-10-17 14:49:13 -05:00
|
|
|
return source
|
|
|
|
|
}
|
2019-07-30 17:00:04 -07:00
|
|
|
}
|