2020-11-18 03:49:12 +01:00
|
|
|
//
|
|
|
|
// WidgetDataEncoder.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Stuart Breckenridge on 18/11/20.
|
|
|
|
// Copyright © 2020 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import WidgetKit
|
|
|
|
import os.log
|
|
|
|
import UIKit
|
|
|
|
import RSCore
|
|
|
|
import Articles
|
|
|
|
|
|
|
|
@available(iOS 14, *)
|
|
|
|
struct WidgetDataEncoder {
|
|
|
|
|
|
|
|
static let taskIdentifier = "com.ranchero.NetNewsWire.WidgetEncode"
|
|
|
|
|
|
|
|
static func encodeWidgetData() {
|
|
|
|
os_log(.info, "Starting widget data encoding")
|
|
|
|
do {
|
|
|
|
// Unread Articles
|
|
|
|
let unreadArticles = try SmartFeedsController.shared.unreadFeed.fetchArticles().sorted(by: { $0.datePublished! > $1.datePublished! })
|
|
|
|
var unread = [LatestArticle]()
|
|
|
|
for article in unreadArticles {
|
|
|
|
let latestArticle = LatestArticle(id: article.sortableArticleID,
|
|
|
|
feedTitle: article.sortableName,
|
|
|
|
articleTitle: article.title?.isEmpty ?? true ? article.summary : ArticleStringFormatter.truncatedTitle(article),
|
|
|
|
articleSummary: article.summary,
|
|
|
|
feedIcon: article.iconImage()?.image.dataRepresentation(),
|
|
|
|
pubDate: article.datePublished!.description)
|
|
|
|
unread.append(latestArticle)
|
2020-11-18 08:43:14 +01:00
|
|
|
if unread.count == 8 { break }
|
2020-11-18 03:49:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Starred Articles
|
|
|
|
let starredArticles = try SmartFeedsController.shared.starredFeed.fetchArticles().sorted(by: { $0.datePublished! > $1.datePublished! })
|
|
|
|
var starred = [LatestArticle]()
|
|
|
|
for article in starredArticles {
|
|
|
|
let latestArticle = LatestArticle(id: article.sortableArticleID,
|
|
|
|
feedTitle: article.sortableName,
|
|
|
|
articleTitle: article.title?.isEmpty ?? true ? article.summary : ArticleStringFormatter.truncatedTitle(article),
|
|
|
|
articleSummary: article.summary,
|
|
|
|
feedIcon: article.iconImage()?.image.dataRepresentation(),
|
|
|
|
pubDate: article.datePublished!.description)
|
|
|
|
starred.append(latestArticle)
|
2020-11-18 08:43:14 +01:00
|
|
|
if starred.count == 8 { break }
|
2020-11-18 03:49:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Today Articles
|
|
|
|
let todayArticles = try SmartFeedsController.shared.todayFeed.fetchArticles().sorted(by: { $0.datePublished! > $1.datePublished! })
|
|
|
|
var today = [LatestArticle]()
|
|
|
|
for article in todayArticles {
|
|
|
|
let latestArticle = LatestArticle(id: article.sortableArticleID,
|
|
|
|
feedTitle: article.sortableName,
|
|
|
|
articleTitle: article.title?.isEmpty ?? true ? article.summary : ArticleStringFormatter.truncatedTitle(article),
|
|
|
|
articleSummary: article.summary,
|
|
|
|
feedIcon: article.iconImage()?.image.dataRepresentation(),
|
|
|
|
pubDate: article.datePublished!.description)
|
|
|
|
today.append(latestArticle)
|
2020-11-18 08:43:14 +01:00
|
|
|
if today.count == 8 { break }
|
2020-11-18 03:49:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let latestData = WidgetData(currentUnreadCount: SmartFeedsController.shared.unreadFeed.unreadCount,
|
|
|
|
currentTodayCount: try! SmartFeedsController.shared.todayFeed.fetchArticles().count,
|
|
|
|
currentStarredCount: try! SmartFeedsController.shared.starredFeed.fetchArticles().count,
|
|
|
|
unreadArticles: unread,
|
|
|
|
starredArticles: starred,
|
|
|
|
todayArticles:today,
|
|
|
|
lastUpdateTime: Date())
|
|
|
|
|
|
|
|
let encodedData = try JSONEncoder().encode(latestData)
|
|
|
|
let appGroup = Bundle.main.object(forInfoDictionaryKey: "AppGroup") as! String
|
|
|
|
let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup)
|
|
|
|
let dataURL = containerURL?.appendingPathComponent("widget-data.json")
|
|
|
|
if FileManager.default.fileExists(atPath: dataURL!.path) {
|
|
|
|
try FileManager.default.removeItem(at: dataURL!)
|
|
|
|
}
|
|
|
|
try encodedData.write(to: dataURL!)
|
2020-11-18 08:43:14 +01:00
|
|
|
print(latestData.unreadArticles.count)
|
2020-11-18 03:49:12 +01:00
|
|
|
WidgetCenter.shared.reloadAllTimelines()
|
|
|
|
os_log(.info, "Finished encoding widget data")
|
|
|
|
} catch {
|
|
|
|
os_log(.error, "%@", error.localizedDescription)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|