mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2025-08-12 06:26:36 +00:00
Merge pull request #3458 from stuartbreckenridge/ios-ui-widget-images
iOS Widget Image Handling
This commit is contained in:
@@ -26,7 +26,7 @@ struct LatestArticle: Codable, Identifiable {
|
||||
let feedTitle: String
|
||||
let articleTitle: String?
|
||||
let articleSummary: String?
|
||||
let feedIcon: Data? // Base64 encoded image data
|
||||
let feedIconPath: String? // Path to image data in shared container.
|
||||
let pubDate: String
|
||||
|
||||
}
|
||||
|
||||
@@ -23,13 +23,19 @@ public final class WidgetDataEncoder {
|
||||
private var backgroundTaskID: UIBackgroundTaskIdentifier!
|
||||
private lazy var appGroup = Bundle.main.object(forInfoDictionaryKey: "AppGroup") as! String
|
||||
private lazy var containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup)
|
||||
private lazy var imageContainer = containerURL?.appendingPathComponent("widgetImages", isDirectory: true)
|
||||
private lazy var dataURL = containerURL?.appendingPathComponent("widget-data.json")
|
||||
|
||||
static let shared = WidgetDataEncoder()
|
||||
private init () {}
|
||||
private init () {
|
||||
if imageContainer != nil {
|
||||
try? FileManager.default.createDirectory(at: imageContainer!, withIntermediateDirectories: true, attributes: nil)
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 14, *)
|
||||
func encodeWidgetData() throws {
|
||||
flushSharedContainer()
|
||||
os_log(.debug, log: log, "Starting encoding widget data.")
|
||||
|
||||
do {
|
||||
@@ -46,7 +52,7 @@ public final class WidgetDataEncoder {
|
||||
feedTitle: article.sortableName,
|
||||
articleTitle: ArticleStringFormatter.truncatedTitle(article).isEmpty ? ArticleStringFormatter.truncatedSummary(article) : ArticleStringFormatter.truncatedTitle(article),
|
||||
articleSummary: article.summary,
|
||||
feedIcon: article.iconImage()?.image.dataRepresentation(),
|
||||
feedIconPath: writeImageDataToSharedContainer(article.iconImage()?.image.dataRepresentation()),
|
||||
pubDate: article.datePublished?.description ?? "")
|
||||
unread.append(latestArticle)
|
||||
}
|
||||
@@ -56,7 +62,7 @@ public final class WidgetDataEncoder {
|
||||
feedTitle: article.sortableName,
|
||||
articleTitle: ArticleStringFormatter.truncatedTitle(article).isEmpty ? ArticleStringFormatter.truncatedSummary(article) : ArticleStringFormatter.truncatedTitle(article),
|
||||
articleSummary: article.summary,
|
||||
feedIcon: article.iconImage()?.image.dataRepresentation(),
|
||||
feedIconPath: writeImageDataToSharedContainer(article.iconImage()?.image.dataRepresentation()),
|
||||
pubDate: article.datePublished?.description ?? "")
|
||||
starred.append(latestArticle)
|
||||
}
|
||||
@@ -66,7 +72,7 @@ public final class WidgetDataEncoder {
|
||||
feedTitle: article.sortableName,
|
||||
articleTitle: ArticleStringFormatter.truncatedTitle(article).isEmpty ? ArticleStringFormatter.truncatedSummary(article) : ArticleStringFormatter.truncatedTitle(article),
|
||||
articleSummary: article.summary,
|
||||
feedIcon: article.iconImage()?.image.dataRepresentation(),
|
||||
feedIconPath: writeImageDataToSharedContainer(article.iconImage()?.image.dataRepresentation()),
|
||||
pubDate: article.datePublished?.description ?? "")
|
||||
today.append(latestArticle)
|
||||
}
|
||||
@@ -113,5 +119,28 @@ public final class WidgetDataEncoder {
|
||||
FileManager.default.fileExists(atPath: dataURL!.path)
|
||||
}
|
||||
|
||||
private func writeImageDataToSharedContainer(_ imageData: Data?) -> String? {
|
||||
if imageData == nil { return nil }
|
||||
// Each image gets a UUID
|
||||
let uuid = UUID().uuidString
|
||||
if let imagePath = imageContainer?.appendingPathComponent(uuid, isDirectory: false) {
|
||||
do {
|
||||
try imageData!.write(to: imagePath)
|
||||
return imagePath.path
|
||||
} catch {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
private func flushSharedContainer() {
|
||||
if let imageContainer = imageContainer {
|
||||
try? FileManager.default.removeItem(atPath: imageContainer.path)
|
||||
try? FileManager.default.createDirectory(at: imageContainer, withIntermediateDirectories: true, attributes: nil)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -11,6 +11,8 @@ import RSWeb
|
||||
|
||||
struct ArticleItemView: View {
|
||||
|
||||
|
||||
|
||||
var article: LatestArticle
|
||||
var deepLink: URL
|
||||
@State private var iconImage: UIImage?
|
||||
@@ -49,7 +51,13 @@ struct ArticleItemView: View {
|
||||
}
|
||||
})
|
||||
}).onAppear {
|
||||
iconImage = thumbnail(article.feedIcon)
|
||||
guard let feedIconPath = article.feedIconPath else {
|
||||
iconImage = thumbnail(nil)
|
||||
return
|
||||
}
|
||||
let path = URL(fileURLWithPath: feedIconPath)
|
||||
let data = try? Data(contentsOf: path)
|
||||
iconImage = thumbnail(data)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,4 +82,5 @@ struct ArticleItemView: View {
|
||||
|
||||
return displayFormatter.string(from: date)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user