Files
SideStore/Sources/SideStoreCore/Model/AppVersion.swift

109 lines
3.4 KiB
Swift
Raw Normal View History

//
// AppVersion.swift
2023-03-01 14:36:52 -05:00
// SideStoreCore
//
// Created by Riley Testut on 8/18/22.
// Copyright © 2022 Riley Testut. All rights reserved.
//
import CoreData
@objc(AppVersion)
2023-03-01 00:48:36 -05:00
public class AppVersion: NSManagedObject, Decodable, Fetchable {
/* Properties */
@NSManaged public var version: String
@NSManaged public var date: Date
@NSManaged public var localizedDescription: String?
2023-03-01 00:48:36 -05:00
@NSManaged public var downloadURL: URL
@NSManaged public var size: Int64
2023-03-01 00:48:36 -05:00
@nonobjc public var minOSVersion: OperatingSystemVersion? {
2023-03-01 00:48:36 -05:00
guard let osVersionString = _minOSVersion else { return nil }
let osVersion = OperatingSystemVersion(string: osVersionString)
return osVersion
}
2023-03-01 00:48:36 -05:00
@NSManaged @objc(minOSVersion) private var _minOSVersion: String?
2023-03-01 00:48:36 -05:00
@nonobjc public var maxOSVersion: OperatingSystemVersion? {
2023-03-01 00:48:36 -05:00
guard let osVersionString = _maxOSVersion else { return nil }
let osVersion = OperatingSystemVersion(string: osVersionString)
return osVersion
}
2023-03-01 00:48:36 -05:00
@NSManaged @objc(maxOSVersion) private var _maxOSVersion: String?
2023-03-01 00:48:36 -05:00
@NSManaged public var appBundleID: String
@NSManaged public var sourceID: String?
2023-03-01 00:48:36 -05:00
/* Relationships */
@NSManaged public private(set) var app: StoreApp?
@NSManaged public private(set) var latestVersionApp: StoreApp?
2023-03-01 00:48:36 -05:00
override private init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?) {
super.init(entity: entity, insertInto: context)
}
2023-03-01 00:48:36 -05:00
private enum CodingKeys: String, CodingKey {
case version
case date
case localizedDescription
case downloadURL
case size
}
2023-03-01 00:48:36 -05:00
public required init(from decoder: Decoder) throws {
guard let context = decoder.managedObjectContext else { preconditionFailure("Decoder must have non-nil NSManagedObjectContext.") }
2023-03-01 00:48:36 -05:00
super.init(entity: AppVersion.entity(), insertInto: context)
2023-03-01 00:48:36 -05:00
do {
let container = try decoder.container(keyedBy: CodingKeys.self)
2023-03-01 00:48:36 -05:00
version = try container.decode(String.self, forKey: .version)
date = try container.decode(Date.self, forKey: .date)
localizedDescription = try container.decodeIfPresent(String.self, forKey: .localizedDescription)
downloadURL = try container.decode(URL.self, forKey: .downloadURL)
size = try container.decode(Int64.self, forKey: .size)
} catch {
if let context = managedObjectContext {
context.delete(self)
}
2023-03-01 00:48:36 -05:00
throw error
}
}
}
2023-03-01 00:48:36 -05:00
public extension AppVersion {
@nonobjc class func fetchRequest() -> NSFetchRequest<AppVersion> {
NSFetchRequest<AppVersion>(entityName: "AppVersion")
}
2023-03-01 00:48:36 -05:00
class func makeAppVersion(
version: String,
date: Date,
localizedDescription: String? = nil,
downloadURL: URL,
size: Int64,
appBundleID: String,
sourceID: String? = nil,
2023-03-01 00:48:36 -05:00
in context: NSManagedObjectContext
) -> AppVersion {
let appVersion = AppVersion(context: context)
appVersion.version = version
appVersion.date = date
appVersion.localizedDescription = localizedDescription
appVersion.downloadURL = downloadURL
appVersion.size = size
appVersion.appBundleID = appBundleID
appVersion.sourceID = sourceID
return appVersion
}
}