mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
create managedobject models for feeditem and feeditemlabel
This commit is contained in:
parent
bb11fac055
commit
5ff6d6789b
5 changed files with 95 additions and 10 deletions
|
|
@ -1,3 +1,4 @@
|
|||
import CoreData
|
||||
import Foundation
|
||||
|
||||
public struct HomeFeedData {
|
||||
|
|
@ -10,6 +11,29 @@ public struct HomeFeedData {
|
|||
}
|
||||
}
|
||||
|
||||
public class FeedItemManagedObject: NSManagedObject {
|
||||
static let entityName = "FeedItemManagedObject"
|
||||
|
||||
@NSManaged public var id: String
|
||||
@NSManaged public var title: String
|
||||
@NSManaged public var createdAt: Date
|
||||
@NSManaged public var savedAt: Date
|
||||
@NSManaged public var readingProgress: Double
|
||||
@NSManaged public var readingProgressAnchor: Int
|
||||
@NSManaged public var imageURLString: String?
|
||||
@NSManaged public var onDeviceImageURLString: String?
|
||||
@NSManaged public var documentDirectoryPath: String?
|
||||
@NSManaged public var pageURLString: String
|
||||
@NSManaged public var descriptionText: String?
|
||||
@NSManaged public var publisherURLString: String?
|
||||
@NSManaged public var author: String?
|
||||
@NSManaged public var publishDate: Date?
|
||||
@NSManaged public var slug: String
|
||||
@NSManaged public var isArchived: Bool
|
||||
@NSManaged public var contentReader: String?
|
||||
@NSManaged public var labels: Set<FeedItemLabelManagedObject>
|
||||
}
|
||||
|
||||
public struct FeedItem: Identifiable, Hashable {
|
||||
public let id: String
|
||||
public let title: String
|
||||
|
|
@ -21,7 +45,7 @@ public struct FeedItem: Identifiable, Hashable {
|
|||
public let onDeviceImageURLString: String?
|
||||
public let documentDirectoryPath: String?
|
||||
public let pageURLString: String
|
||||
public let description: String?
|
||||
public let descriptionText: String?
|
||||
public let publisherURLString: String?
|
||||
public let author: String?
|
||||
public let publishDate: Date?
|
||||
|
|
@ -41,7 +65,7 @@ public struct FeedItem: Identifiable, Hashable {
|
|||
onDeviceImageURLString: String?,
|
||||
documentDirectoryPath: String?,
|
||||
pageURLString: String,
|
||||
description: String?,
|
||||
descriptionText: String?,
|
||||
publisherURLString: String?,
|
||||
author: String?,
|
||||
publishDate: Date?,
|
||||
|
|
@ -60,7 +84,7 @@ public struct FeedItem: Identifiable, Hashable {
|
|||
self.onDeviceImageURLString = onDeviceImageURLString
|
||||
self.documentDirectoryPath = documentDirectoryPath
|
||||
self.pageURLString = pageURLString
|
||||
self.description = description
|
||||
self.descriptionText = descriptionText
|
||||
self.publisherURLString = publisherURLString
|
||||
self.author = author
|
||||
self.publishDate = publishDate
|
||||
|
|
@ -70,6 +94,40 @@ public struct FeedItem: Identifiable, Hashable {
|
|||
self.labels = labels
|
||||
}
|
||||
|
||||
func toManagedObject(inContext context: NSManagedObjectContext) -> FeedItemManagedObject? {
|
||||
guard let entityDescription = NSEntityDescription.entity(forEntityName: FeedItemManagedObject.entityName, in: context) else {
|
||||
print("Failed to create \(FeedItemManagedObject.entityName)")
|
||||
return nil
|
||||
}
|
||||
|
||||
let object = FeedItemManagedObject(entity: entityDescription, insertInto: context)
|
||||
object.id = id
|
||||
object.title = title
|
||||
object.createdAt = createdAt
|
||||
object.savedAt = savedAt
|
||||
object.readingProgress = readingProgress
|
||||
object.readingProgressAnchor = readingProgressAnchor
|
||||
object.imageURLString = imageURLString
|
||||
object.onDeviceImageURLString = onDeviceImageURLString
|
||||
object.documentDirectoryPath = documentDirectoryPath
|
||||
object.pageURLString = pageURLString
|
||||
object.descriptionText = descriptionText
|
||||
object.publisherURLString = publisherURLString
|
||||
object.author = author
|
||||
object.publishDate = publishDate
|
||||
object.slug = slug
|
||||
object.isArchived = isArchived
|
||||
object.contentReader = contentReader
|
||||
|
||||
for label in labels {
|
||||
if let managedLabel = label.toManagedObject(inContext: context) {
|
||||
object.labels.insert(managedLabel)
|
||||
}
|
||||
}
|
||||
|
||||
return object
|
||||
}
|
||||
|
||||
public static func fromJsonArticle(linkData: Data) -> FeedItem? {
|
||||
try? JSONDecoder().decode(JSONArticle.self, from: linkData).feedItem
|
||||
}
|
||||
|
|
@ -126,7 +184,7 @@ struct JSONArticle: Decodable {
|
|||
onDeviceImageURLString: nil,
|
||||
documentDirectoryPath: nil,
|
||||
pageURLString: url,
|
||||
description: title,
|
||||
descriptionText: title,
|
||||
publisherURLString: nil,
|
||||
author: nil,
|
||||
publishDate: nil,
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import CoreData
|
||||
import Foundation
|
||||
|
||||
public struct FeedItemLabel: Decodable, Hashable {
|
||||
|
|
@ -5,19 +6,45 @@ public struct FeedItemLabel: Decodable, Hashable {
|
|||
public let name: String
|
||||
public let color: String
|
||||
public let createdAt: Date?
|
||||
public let description: String?
|
||||
public let labelDescription: String?
|
||||
|
||||
public init(
|
||||
id: String,
|
||||
name: String,
|
||||
color: String,
|
||||
createdAt: Date?,
|
||||
description: String?
|
||||
labelDescription: String?
|
||||
) {
|
||||
self.id = id
|
||||
self.name = name
|
||||
self.color = color
|
||||
self.createdAt = createdAt
|
||||
self.description = description
|
||||
self.labelDescription = labelDescription
|
||||
}
|
||||
|
||||
func toManagedObject(inContext context: NSManagedObjectContext) -> FeedItemLabelManagedObject? {
|
||||
let entityName = FeedItemLabelManagedObject.entityName
|
||||
guard let entityDescription = NSEntityDescription.entity(forEntityName: entityName, in: context) else {
|
||||
print("Failed to create \(entityName)")
|
||||
return nil
|
||||
}
|
||||
|
||||
let object = FeedItemLabelManagedObject(entity: entityDescription, insertInto: context)
|
||||
object.id = id
|
||||
object.name = name
|
||||
object.color = color
|
||||
object.createdAt = createdAt
|
||||
object.labelDescription = labelDescription
|
||||
return object
|
||||
}
|
||||
}
|
||||
|
||||
public class FeedItemLabelManagedObject: NSManagedObject {
|
||||
static let entityName = "FeedItemLabelManagedObject"
|
||||
|
||||
@NSManaged public var id: String
|
||||
@NSManaged public var name: String
|
||||
@NSManaged public var color: String
|
||||
@NSManaged public var createdAt: Date?
|
||||
@NSManaged public var labelDescription: String?
|
||||
}
|
||||
|
|
|
|||
|
|
@ -137,7 +137,7 @@ let homeFeedItemSelection = Selection.Article {
|
|||
onDeviceImageURLString: nil,
|
||||
documentDirectoryPath: nil,
|
||||
pageURLString: try $0.url(),
|
||||
description: try $0.description(),
|
||||
descriptionText: try $0.description(),
|
||||
publisherURLString: try $0.originalArticleUrl(),
|
||||
author: try $0.author(),
|
||||
publishDate: try $0.publishedAt()?.value,
|
||||
|
|
|
|||
|
|
@ -7,6 +7,6 @@ let feedItemLabelSelection = Selection.Label {
|
|||
name: try $0.name(),
|
||||
color: try $0.color(),
|
||||
createdAt: try $0.createdAt()?.value,
|
||||
description: try $0.description()
|
||||
labelDescription: try $0.description()
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -117,7 +117,7 @@ public struct GridCard: View {
|
|||
|
||||
// Link description and image
|
||||
HStack(alignment: .top) {
|
||||
Text(item.description ?? item.title)
|
||||
Text(item.descriptionText ?? item.title)
|
||||
.font(.appSubheadline)
|
||||
.foregroundColor(.appGrayTextContrast)
|
||||
.lineLimit(nil)
|
||||
|
|
|
|||
Loading…
Reference in a new issue