2022-09-12 15:42:33 -07:00
|
|
|
//
|
|
|
|
|
// AppVersion.swift
|
|
|
|
|
// AltStoreCore
|
|
|
|
|
//
|
|
|
|
|
// Created by Riley Testut on 8/18/22.
|
|
|
|
|
// Copyright © 2022 Riley Testut. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import CoreData
|
|
|
|
|
|
|
|
|
|
@objc(AppVersion)
|
|
|
|
|
public class AppVersion: NSManagedObject, Decodable, Fetchable
|
|
|
|
|
{
|
|
|
|
|
/* Properties */
|
|
|
|
|
@NSManaged public var version: String
|
2023-05-18 14:51:26 -05:00
|
|
|
|
|
|
|
|
// NULL does not work as expected with SQL Unique Constraints (because NULL != NULL),
|
|
|
|
|
// so we store non-optional value and provide public accessor with optional return type.
|
|
|
|
|
@nonobjc public var buildVersion: String? {
|
|
|
|
|
get { _buildVersion.isEmpty ? nil : _buildVersion }
|
|
|
|
|
set { _buildVersion = newValue ?? "" }
|
|
|
|
|
}
|
|
|
|
|
@NSManaged @objc(buildVersion) public private(set) var _buildVersion: String
|
|
|
|
|
|
2022-09-12 15:42:33 -07:00
|
|
|
@NSManaged public var date: Date
|
|
|
|
|
@NSManaged public var localizedDescription: String?
|
|
|
|
|
@NSManaged public var downloadURL: URL
|
|
|
|
|
@NSManaged public var size: Int64
|
2023-05-11 17:02:20 -05:00
|
|
|
@NSManaged public var sha256: String?
|
2022-09-12 15:42:33 -07:00
|
|
|
|
|
|
|
|
@nonobjc public var minOSVersion: OperatingSystemVersion? {
|
|
|
|
|
guard let osVersionString = self._minOSVersion else { return nil }
|
|
|
|
|
|
|
|
|
|
let osVersion = OperatingSystemVersion(string: osVersionString)
|
|
|
|
|
return osVersion
|
|
|
|
|
}
|
|
|
|
|
@NSManaged @objc(minOSVersion) private var _minOSVersion: String?
|
|
|
|
|
|
|
|
|
|
@nonobjc public var maxOSVersion: OperatingSystemVersion? {
|
|
|
|
|
guard let osVersionString = self._maxOSVersion else { return nil }
|
|
|
|
|
|
|
|
|
|
let osVersion = OperatingSystemVersion(string: osVersionString)
|
|
|
|
|
return osVersion
|
|
|
|
|
}
|
|
|
|
|
@NSManaged @objc(maxOSVersion) private var _maxOSVersion: String?
|
|
|
|
|
|
|
|
|
|
@NSManaged public var appBundleID: String
|
|
|
|
|
@NSManaged public var sourceID: String?
|
2024-12-18 02:32:11 +05:30
|
|
|
|
|
|
|
|
// TODO: @mahee96: retire isBeta and use a string type to decode and store values as enum
|
2024-12-17 17:00:53 +05:30
|
|
|
@NSManaged public var isBeta: Bool
|
2024-12-18 02:32:11 +05:30
|
|
|
@NSManaged public var revision: String?
|
2022-09-12 15:42:33 -07:00
|
|
|
|
|
|
|
|
/* Relationships */
|
|
|
|
|
@NSManaged public private(set) var app: StoreApp?
|
2024-08-06 10:43:52 +09:00
|
|
|
@NSManaged @objc(latestVersionApp) public internal(set) var latestSupportedVersionApp: StoreApp?
|
2022-09-12 15:42:33 -07:00
|
|
|
|
|
|
|
|
private override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?)
|
|
|
|
|
{
|
|
|
|
|
super.init(entity: entity, insertInto: context)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-15 18:39:27 -06:00
|
|
|
internal enum CodingKeys: String, CodingKey
|
2022-09-12 15:42:33 -07:00
|
|
|
{
|
|
|
|
|
case version
|
2023-05-18 14:51:26 -05:00
|
|
|
case buildVersion
|
2022-09-12 15:42:33 -07:00
|
|
|
case date
|
|
|
|
|
case localizedDescription
|
|
|
|
|
case downloadURL
|
|
|
|
|
case size
|
2023-05-11 17:02:20 -05:00
|
|
|
case sha256
|
2024-08-06 10:43:52 +09:00
|
|
|
case minOSVersion
|
|
|
|
|
case maxOSVersion
|
2024-12-17 17:00:53 +05:30
|
|
|
case isBeta
|
2024-12-18 02:32:11 +05:30
|
|
|
case revision = "commitID"
|
2022-09-12 15:42:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public required init(from decoder: Decoder) throws
|
|
|
|
|
{
|
|
|
|
|
guard let context = decoder.managedObjectContext else { preconditionFailure("Decoder must have non-nil NSManagedObjectContext.") }
|
|
|
|
|
|
2022-09-12 17:05:55 -07:00
|
|
|
super.init(entity: AppVersion.entity(), insertInto: context)
|
2022-09-12 15:42:33 -07:00
|
|
|
|
2022-09-12 17:05:55 -07:00
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
|
|
|
|
|
|
|
|
self.version = try container.decode(String.self, forKey: .version)
|
2023-05-18 14:51:26 -05:00
|
|
|
self.buildVersion = try container.decodeIfPresent(String.self, forKey: .buildVersion)
|
|
|
|
|
|
2022-09-12 17:05:55 -07:00
|
|
|
self.date = try container.decode(Date.self, forKey: .date)
|
|
|
|
|
self.localizedDescription = try container.decodeIfPresent(String.self, forKey: .localizedDescription)
|
|
|
|
|
|
|
|
|
|
self.downloadURL = try container.decode(URL.self, forKey: .downloadURL)
|
|
|
|
|
self.size = try container.decode(Int64.self, forKey: .size)
|
2023-05-11 17:02:20 -05:00
|
|
|
|
|
|
|
|
self.sha256 = try container.decodeIfPresent(String.self, forKey: .sha256)?.lowercased()
|
2024-08-06 10:43:52 +09:00
|
|
|
self._minOSVersion = try container.decodeIfPresent(String.self, forKey: .minOSVersion)
|
|
|
|
|
self._maxOSVersion = try container.decodeIfPresent(String.self, forKey: .maxOSVersion)
|
2024-12-17 21:01:33 +05:30
|
|
|
|
2024-12-18 02:50:50 +05:30
|
|
|
self.isBeta = try container.decodeIfPresent(Bool.self, forKey: .isBeta) ?? false
|
|
|
|
|
self.revision = try container.decodeIfPresent(String.self, forKey: .revision)
|
2022-09-12 17:05:55 -07:00
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
if let context = self.managedObjectContext
|
|
|
|
|
{
|
|
|
|
|
context.delete(self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw error
|
|
|
|
|
}
|
2022-09-12 15:42:33 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-18 14:51:26 -05:00
|
|
|
public extension AppVersion
|
|
|
|
|
{
|
|
|
|
|
var localizedVersion: String {
|
|
|
|
|
guard let buildVersion else { return self.version }
|
|
|
|
|
|
|
|
|
|
let localizedVersion = "\(self.version) (\(buildVersion))"
|
|
|
|
|
return localizedVersion
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var versionID: String {
|
|
|
|
|
// Use `nil` as fallback to prevent collisions between versions with builds and versions without.
|
|
|
|
|
// 1.5 (4) -> "1.5|4"
|
|
|
|
|
// 1.5.4 (no build) -> "1.5.4|nil"
|
|
|
|
|
let buildVersion = self.buildVersion ?? "nil"
|
|
|
|
|
|
|
|
|
|
let versionID = "\(self.version)|\(buildVersion)"
|
|
|
|
|
return versionID
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-12 15:42:33 -07:00
|
|
|
public extension AppVersion
|
|
|
|
|
{
|
|
|
|
|
@nonobjc class func fetchRequest() -> NSFetchRequest<AppVersion>
|
|
|
|
|
{
|
|
|
|
|
return NSFetchRequest<AppVersion>(entityName: "AppVersion")
|
|
|
|
|
}
|
2022-09-12 17:05:55 -07:00
|
|
|
|
|
|
|
|
class func makeAppVersion(
|
|
|
|
|
version: String,
|
2023-05-18 14:51:26 -05:00
|
|
|
buildVersion: String?,
|
2022-09-12 17:05:55 -07:00
|
|
|
date: Date,
|
|
|
|
|
localizedDescription: String? = nil,
|
|
|
|
|
downloadURL: URL,
|
|
|
|
|
size: Int64,
|
|
|
|
|
appBundleID: String,
|
|
|
|
|
sourceID: String? = nil,
|
|
|
|
|
in context: NSManagedObjectContext) -> AppVersion
|
|
|
|
|
{
|
|
|
|
|
let appVersion = AppVersion(context: context)
|
|
|
|
|
appVersion.version = version
|
2023-05-18 14:51:26 -05:00
|
|
|
appVersion.buildVersion = buildVersion
|
2022-09-12 17:05:55 -07:00
|
|
|
appVersion.date = date
|
|
|
|
|
appVersion.localizedDescription = localizedDescription
|
|
|
|
|
appVersion.downloadURL = downloadURL
|
|
|
|
|
appVersion.size = size
|
|
|
|
|
appVersion.appBundleID = appBundleID
|
|
|
|
|
appVersion.sourceID = sourceID
|
|
|
|
|
|
|
|
|
|
return appVersion
|
|
|
|
|
}
|
2024-08-06 10:43:52 +09:00
|
|
|
|
|
|
|
|
var isSupported: Bool {
|
2024-12-07 17:45:09 +05:30
|
|
|
if let minOSVersion = self.minOSVersion, !ProcessInfo.processInfo.isOperatingSystemAtLeast(minOSVersion)
|
|
|
|
|
{
|
2024-08-06 10:43:52 +09:00
|
|
|
return false
|
2024-12-07 17:45:09 +05:30
|
|
|
}
|
|
|
|
|
else if let maxOSVersion = self.maxOSVersion, ProcessInfo.processInfo.operatingSystemVersion > maxOSVersion
|
|
|
|
|
{
|
2024-08-06 10:43:52 +09:00
|
|
|
return false
|
|
|
|
|
}
|
2024-12-07 17:45:09 +05:30
|
|
|
|
2024-08-06 10:43:52 +09:00
|
|
|
return true
|
|
|
|
|
}
|
2022-09-12 15:42:33 -07:00
|
|
|
}
|