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
|
2021-03-24 13:30:21 +01:00
|
|
|
import Account
|
2020-11-18 03:49:12 +01:00
|
|
|
|
2020-12-03 13:32:26 +01:00
|
|
|
|
|
|
|
public final class WidgetDataEncoder {
|
|
|
|
|
|
|
|
private let log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "Application")
|
2021-03-24 13:30:21 +01:00
|
|
|
private let fetchLimit = 7
|
2020-12-03 13:32:26 +01:00
|
|
|
|
|
|
|
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 dataURL = containerURL?.appendingPathComponent("widget-data.json")
|
2020-11-18 03:49:12 +01:00
|
|
|
|
2020-12-03 13:32:26 +01:00
|
|
|
static let shared = WidgetDataEncoder()
|
|
|
|
private init () {}
|
2020-11-29 10:12:53 +01:00
|
|
|
|
2020-12-03 13:32:26 +01:00
|
|
|
@available(iOS 14, *)
|
2020-12-03 13:41:37 +01:00
|
|
|
func encodeWidgetData() throws {
|
2020-11-29 10:12:53 +01:00
|
|
|
os_log(.debug, log: log, "Starting encoding widget data.")
|
2020-12-03 13:32:26 +01:00
|
|
|
|
2020-11-18 03:49:12 +01:00
|
|
|
do {
|
2021-03-24 13:30:21 +01:00
|
|
|
let unreadArticles = Array(try AccountManager.shared.fetchArticles(.unread(fetchLimit))).sortedByDate(.orderedDescending)
|
|
|
|
let starredArticles = Array(try AccountManager.shared.fetchArticles(.starred(fetchLimit))).sortedByDate(.orderedDescending)
|
|
|
|
let todayArticles = Array(try AccountManager.shared.fetchArticles(.today(fetchLimit))).sortedByDate(.orderedDescending)
|
2020-12-03 13:32:26 +01:00
|
|
|
|
2020-11-18 03:49:12 +01:00
|
|
|
var unread = [LatestArticle]()
|
2020-12-03 13:32:26 +01:00
|
|
|
var today = [LatestArticle]()
|
|
|
|
var starred = [LatestArticle]()
|
|
|
|
|
2020-11-18 03:49:12 +01:00
|
|
|
for article in unreadArticles {
|
|
|
|
let latestArticle = LatestArticle(id: article.sortableArticleID,
|
|
|
|
feedTitle: article.sortableName,
|
2021-01-30 02:02:42 +01:00
|
|
|
articleTitle: ArticleStringFormatter.truncatedTitle(article).isEmpty ? ArticleStringFormatter.truncatedSummary(article) : ArticleStringFormatter.truncatedTitle(article),
|
2020-11-18 03:49:12 +01:00
|
|
|
articleSummary: article.summary,
|
|
|
|
feedIcon: article.iconImage()?.image.dataRepresentation(),
|
2021-02-24 23:00:01 +01:00
|
|
|
pubDate: article.datePublished?.description ?? "")
|
2020-11-18 03:49:12 +01:00
|
|
|
unread.append(latestArticle)
|
2020-11-29 10:38:17 +01:00
|
|
|
if unread.count == 7 { break }
|
2020-11-18 03:49:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for article in starredArticles {
|
|
|
|
let latestArticle = LatestArticle(id: article.sortableArticleID,
|
|
|
|
feedTitle: article.sortableName,
|
2021-01-30 02:02:42 +01:00
|
|
|
articleTitle: ArticleStringFormatter.truncatedTitle(article).isEmpty ? ArticleStringFormatter.truncatedSummary(article) : ArticleStringFormatter.truncatedTitle(article),
|
2020-11-18 03:49:12 +01:00
|
|
|
articleSummary: article.summary,
|
|
|
|
feedIcon: article.iconImage()?.image.dataRepresentation(),
|
2021-02-24 23:00:01 +01:00
|
|
|
pubDate: article.datePublished?.description ?? "")
|
2020-11-18 03:49:12 +01:00
|
|
|
starred.append(latestArticle)
|
2020-11-29 10:38:17 +01:00
|
|
|
if starred.count == 7 { break }
|
2020-11-18 03:49:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for article in todayArticles {
|
|
|
|
let latestArticle = LatestArticle(id: article.sortableArticleID,
|
|
|
|
feedTitle: article.sortableName,
|
2021-01-30 02:02:42 +01:00
|
|
|
articleTitle: ArticleStringFormatter.truncatedTitle(article).isEmpty ? ArticleStringFormatter.truncatedSummary(article) : ArticleStringFormatter.truncatedTitle(article),
|
2020-11-18 03:49:12 +01:00
|
|
|
articleSummary: article.summary,
|
|
|
|
feedIcon: article.iconImage()?.image.dataRepresentation(),
|
2021-02-24 23:00:01 +01:00
|
|
|
pubDate: article.datePublished?.description ?? "")
|
2020-11-18 03:49:12 +01:00
|
|
|
today.append(latestArticle)
|
2020-11-29 10:38:17 +01:00
|
|
|
if today.count == 7 { break }
|
2020-11-18 03:49:12 +01:00
|
|
|
}
|
|
|
|
|
2021-03-24 13:30:21 +01:00
|
|
|
let latestData = WidgetData(currentUnreadCount: try! AccountManager.shared.fetchArticles(.unread()).count,
|
|
|
|
currentTodayCount: try! AccountManager.shared.fetchArticles(.today()).count,
|
|
|
|
currentStarredCount: try! AccountManager.shared.fetchArticles(.starred()).count,
|
2020-11-18 03:49:12 +01:00
|
|
|
unreadArticles: unread,
|
|
|
|
starredArticles: starred,
|
|
|
|
todayArticles:today,
|
|
|
|
lastUpdateTime: Date())
|
|
|
|
|
2020-12-03 13:32:26 +01:00
|
|
|
|
|
|
|
DispatchQueue.global().async { [weak self] in
|
|
|
|
guard let self = self else { return }
|
|
|
|
|
|
|
|
self.backgroundTaskID = UIApplication.shared.beginBackgroundTask (withName: "com.ranchero.NetNewsWire.Encode") {
|
|
|
|
UIApplication.shared.endBackgroundTask(self.backgroundTaskID!)
|
|
|
|
self.backgroundTaskID = .invalid
|
|
|
|
}
|
|
|
|
let encodedData = try? JSONEncoder().encode(latestData)
|
|
|
|
|
|
|
|
os_log(.debug, log: self.log, "Finished encoding widget data.")
|
|
|
|
|
|
|
|
if self.fileExists() {
|
|
|
|
try? FileManager.default.removeItem(at: self.dataURL!)
|
|
|
|
os_log(.debug, log: self.log, "Removed widget data from container.")
|
|
|
|
}
|
|
|
|
if FileManager.default.createFile(atPath: self.dataURL!.path, contents: encodedData, attributes: nil) {
|
|
|
|
os_log(.debug, log: self.log, "Wrote widget data to container.")
|
2020-12-03 13:41:37 +01:00
|
|
|
WidgetCenter.shared.reloadAllTimelines()
|
|
|
|
UIApplication.shared.endBackgroundTask(self.backgroundTaskID!)
|
|
|
|
self.backgroundTaskID = .invalid
|
2020-12-03 13:32:26 +01:00
|
|
|
} else {
|
|
|
|
UIApplication.shared.endBackgroundTask(self.backgroundTaskID!)
|
|
|
|
self.backgroundTaskID = .invalid
|
2020-11-30 03:08:23 +01:00
|
|
|
}
|
2020-12-03 13:32:26 +01:00
|
|
|
|
2020-11-18 03:49:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-03 13:32:26 +01:00
|
|
|
private func fileExists() -> Bool {
|
|
|
|
FileManager.default.fileExists(atPath: dataURL!.path)
|
|
|
|
}
|
2020-11-18 03:49:12 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|