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

85 lines
2.7 KiB
Swift
Raw Normal View History

2019-09-03 21:58:07 -07:00
//
// NewsItem.swift
// AltStore
//
// Created by Riley Testut on 8/29/19.
// Copyright © 2019 Riley Testut. All rights reserved.
//
import CoreData
2023-03-01 00:48:36 -05:00
import UIKit
2019-09-03 21:58:07 -07:00
@objc(NewsItem)
2023-03-01 00:48:36 -05:00
public class NewsItem: NSManagedObject, Decodable, Fetchable {
2019-09-03 21:58:07 -07:00
/* Properties */
@NSManaged public var identifier: String
@NSManaged public var date: Date
2023-03-01 00:48:36 -05:00
@NSManaged public var title: String
@NSManaged public var caption: String
@NSManaged public var tintColor: UIColor
@NSManaged public var sortIndex: Int32
@NSManaged public var isSilent: Bool
2023-03-01 00:48:36 -05:00
@NSManaged public var imageURL: URL?
@NSManaged public var externalURL: URL?
2023-03-01 00:48:36 -05:00
@NSManaged public var appID: String?
@NSManaged public var sourceIdentifier: String?
2023-03-01 00:48:36 -05:00
2019-09-03 21:58:07 -07:00
/* Relationships */
@NSManaged public var storeApp: StoreApp?
@NSManaged public var source: Source?
2023-03-01 00:48:36 -05:00
private enum CodingKeys: String, CodingKey {
2019-09-03 21:58:07 -07:00
case identifier
case date
case title
case caption
case tintColor
case imageURL
case externalURL = "url"
case appID
case notify
}
2023-03-01 00:48:36 -05:00
override private init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?) {
2019-09-03 21:58:07 -07:00
super.init(entity: entity, insertInto: context)
}
2023-03-01 00:48:36 -05:00
public required init(from decoder: Decoder) throws {
2019-09-03 21:58:07 -07:00
guard let context = decoder.managedObjectContext else { preconditionFailure("Decoder must have non-nil NSManagedObjectContext.") }
2023-03-01 00:48:36 -05:00
2019-09-03 21:58:07 -07:00
super.init(entity: NewsItem.entity(), insertInto: context)
2023-03-01 00:48:36 -05:00
2019-09-03 21:58:07 -07:00
let container = try decoder.container(keyedBy: CodingKeys.self)
2023-03-01 00:48:36 -05:00
identifier = try container.decode(String.self, forKey: .identifier)
date = try container.decode(Date.self, forKey: .date)
title = try container.decode(String.self, forKey: .title)
caption = try container.decode(String.self, forKey: .caption)
if let tintColorHex = try container.decodeIfPresent(String.self, forKey: .tintColor) {
2019-09-03 21:58:07 -07:00
guard let tintColor = UIColor(hexString: tintColorHex) else {
throw DecodingError.dataCorruptedError(forKey: .tintColor, in: container, debugDescription: "Hex code is invalid.")
}
2023-03-01 00:48:36 -05:00
2019-09-03 21:58:07 -07:00
self.tintColor = tintColor
}
2023-03-01 00:48:36 -05:00
imageURL = try container.decodeIfPresent(URL.self, forKey: .imageURL)
externalURL = try container.decodeIfPresent(URL.self, forKey: .externalURL)
appID = try container.decodeIfPresent(String.self, forKey: .appID)
2019-09-03 21:58:07 -07:00
let notify = try container.decodeIfPresent(Bool.self, forKey: .notify) ?? false
2023-03-01 00:48:36 -05:00
isSilent = !notify
2019-09-03 21:58:07 -07:00
}
}
2023-03-01 00:48:36 -05:00
public extension NewsItem {
@nonobjc class func fetchRequest() -> NSFetchRequest<NewsItem> {
NSFetchRequest<NewsItem>(entityName: "NewsItem")
2019-09-03 21:58:07 -07:00
}
}