mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2025-08-12 06:26:36 +00:00
Widget Updates
• Can now read data written by main app • Has preview capability in widget gallery • Still to solve using ORGANIZATION_IDENTIFIER
This commit is contained in:
@@ -89,9 +89,8 @@ struct MainApp: App {
|
||||
.modifier(PreferredColorSchemeModifier(preferredColorScheme: defaults.userInterfaceColorPalette))
|
||||
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didEnterBackgroundNotification)) { _ in
|
||||
print("didEnterBackgroundNotification")
|
||||
appDelegate.refreshWidgetData()
|
||||
WidgetDataEncoder.encodeWidgetData()
|
||||
}
|
||||
|
||||
}
|
||||
.commands {
|
||||
CommandGroup(after: .newItem, addition: {
|
||||
|
||||
40
Multiplatform/Shared/Widget Data/WidgetDataDecoder.swift
Normal file
40
Multiplatform/Shared/Widget Data/WidgetDataDecoder.swift
Normal file
@@ -0,0 +1,40 @@
|
||||
//
|
||||
// WidgetDataDecoder.swift
|
||||
// NetNewsWire
|
||||
//
|
||||
// Created by Stuart Breckenridge on 11/7/20.
|
||||
// Copyright © 2020 Ranchero Software. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
struct WidgetDataDecoder {
|
||||
|
||||
static func decodeWidgetData() throws -> WidgetData {
|
||||
let appGroup = Bundle.main.object(forInfoDictionaryKey: "AppGroup") as! String
|
||||
print(appGroup)
|
||||
let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup)
|
||||
let dataURL = containerURL?.appendingPathComponent("widget-data.json")
|
||||
print("decoder path: \(dataURL!.path)")
|
||||
|
||||
if FileManager.default.fileExists(atPath: dataURL!.path) {
|
||||
let decodedWidgetData = try JSONDecoder().decode(WidgetData.self, from: Data(contentsOf: dataURL!))
|
||||
return decodedWidgetData
|
||||
} else {
|
||||
print("No data at location")
|
||||
return WidgetData(currentUnreadCount: 0, currentTodayCount: 0, latestArticles: [], lastUpdateTime: Date())
|
||||
}
|
||||
}
|
||||
|
||||
static func sampleData() -> WidgetData {
|
||||
let pathToSample = Bundle.main.url(forResource: "widget-data-sample", withExtension: "json")
|
||||
do {
|
||||
let data = try Data(contentsOf: pathToSample!)
|
||||
let decoded = try JSONDecoder().decode(WidgetData.self, from: data)
|
||||
return decoded
|
||||
} catch {
|
||||
return WidgetData(currentUnreadCount: 12, currentTodayCount: 12, latestArticles: [], lastUpdateTime: Date())
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
54
Multiplatform/Shared/Widget Data/WidgetDataEncoder.swift
Normal file
54
Multiplatform/Shared/Widget Data/WidgetDataEncoder.swift
Normal file
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// WidgetDataEncoder.swift
|
||||
// Multiplatform iOS
|
||||
//
|
||||
// Created by Stuart Breckenridge on 11/7/20.
|
||||
// Copyright © 2020 Ranchero Software. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import WidgetKit
|
||||
import os.log
|
||||
|
||||
struct WidgetDataEncoder {
|
||||
|
||||
static func encodeWidgetData() {
|
||||
os_log(.info, "Starting Widget data refresh")
|
||||
do {
|
||||
let articles = try SmartFeedsController.shared.unreadFeed.fetchArticles().sorted(by: { $0.datePublished! > $1.datePublished! })
|
||||
var latest = [LatestArticle]()
|
||||
for article in articles {
|
||||
let latestArticle = LatestArticle(feedTitle: article.sortableName,
|
||||
articleTitle: article.title,
|
||||
articleSummary: article.summary,
|
||||
feedIcon: article.iconImage()?.image.dataRepresentation(),
|
||||
pubDate: article.datePublished!.description)
|
||||
latest.append(latestArticle)
|
||||
if latest.count == 5 { break }
|
||||
}
|
||||
|
||||
let latestData = WidgetData(currentUnreadCount: SmartFeedsController.shared.unreadFeed.unreadCount,
|
||||
currentTodayCount: SmartFeedsController.shared.todayFeed.unreadCount,
|
||||
latestArticles: latest,
|
||||
lastUpdateTime: Date())
|
||||
|
||||
let encodedData = try JSONEncoder().encode(latestData)
|
||||
let appGroup = Bundle.main.object(forInfoDictionaryKey: "AppGroup") as! String
|
||||
print(appGroup)
|
||||
let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup)
|
||||
let dataURL = containerURL?.appendingPathComponent("widget-data.json")
|
||||
print("Encoder path: \(dataURL!.path)")
|
||||
if FileManager.default.fileExists(atPath: dataURL!.path) {
|
||||
try FileManager.default.removeItem(at: dataURL!)
|
||||
}
|
||||
try encodedData.write(to: dataURL!)
|
||||
|
||||
WidgetCenter.shared.reloadAllTimelines()
|
||||
os_log(.info, "Finished data refresh")
|
||||
} catch {
|
||||
os_log(.error, "%@", error.localizedDescription)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -179,7 +179,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
|
||||
@objc func unreadCountDidChange(_ note: Notification) {
|
||||
if note.object is AccountManager {
|
||||
unreadCount = AccountManager.shared.unreadCount
|
||||
refreshWidgetData()
|
||||
WidgetDataEncoder.encodeWidgetData()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -415,7 +415,7 @@ private extension AppDelegate {
|
||||
}
|
||||
AccountManager.shared.refreshAll(errorHandler: ErrorHandler.log) { [unowned self] in
|
||||
if !AccountManager.shared.isSuspended {
|
||||
self.refreshWidgetData()
|
||||
WidgetDataEncoder.encodeWidgetData()
|
||||
self.suspendApplication()
|
||||
os_log("Account refresh operation completed.", log: self.log, type: .info)
|
||||
task.setTaskCompleted(success: true)
|
||||
|
||||
@@ -1,6 +1,33 @@
|
||||
{
|
||||
"colors" : [
|
||||
{
|
||||
"color" : {
|
||||
"color-space" : "srgb",
|
||||
"components" : {
|
||||
"alpha" : "1.000",
|
||||
"blue" : "0.933",
|
||||
"green" : "0.416",
|
||||
"red" : "0.031"
|
||||
}
|
||||
},
|
||||
"idiom" : "universal"
|
||||
},
|
||||
{
|
||||
"appearances" : [
|
||||
{
|
||||
"appearance" : "luminosity",
|
||||
"value" : "dark"
|
||||
}
|
||||
],
|
||||
"color" : {
|
||||
"color-space" : "srgb",
|
||||
"components" : {
|
||||
"alpha" : "1.000",
|
||||
"blue" : "0.957",
|
||||
"green" : "0.620",
|
||||
"red" : "0.369"
|
||||
}
|
||||
},
|
||||
"idiom" : "universal"
|
||||
}
|
||||
],
|
||||
|
||||
@@ -1,6 +1,28 @@
|
||||
{
|
||||
"colors" : [
|
||||
{
|
||||
"color" : {
|
||||
"color-space" : "display-p3",
|
||||
"components" : {
|
||||
"alpha" : "1.000",
|
||||
"blue" : "0.000",
|
||||
"green" : "0.000",
|
||||
"red" : "0.000"
|
||||
}
|
||||
},
|
||||
"idiom" : "universal"
|
||||
},
|
||||
{
|
||||
"appearances" : [
|
||||
{
|
||||
"appearance" : "luminosity",
|
||||
"value" : "dark"
|
||||
}
|
||||
],
|
||||
"color" : {
|
||||
"platform" : "ios",
|
||||
"reference" : "systemGray6Color"
|
||||
},
|
||||
"idiom" : "universal"
|
||||
}
|
||||
],
|
||||
|
||||
@@ -2,6 +2,12 @@
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>OrganizationIdentifier</key>
|
||||
<string>$(ORGANIZATION_IDENTIFIER)</string>
|
||||
<key>AppGroup</key>
|
||||
<string>group.$(ORGANIZATION_IDENTIFIER).NetNewsWire.iOS</string>
|
||||
<key>AppIdentifierPrefix</key>
|
||||
<string>$(AppIdentifierPrefix)</string>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>$(DEVELOPMENT_LANGUAGE)</string>
|
||||
<key>CFBundleDisplayName</key>
|
||||
|
||||
@@ -10,59 +10,192 @@ import WidgetKit
|
||||
import SwiftUI
|
||||
|
||||
struct Provider: TimelineProvider {
|
||||
public typealias Entry = SimpleEntry
|
||||
public typealias Entry = SummaryEntry
|
||||
|
||||
|
||||
public func snapshot(with context: Context, completion: @escaping (SimpleEntry) -> ()) {
|
||||
let entry = SimpleEntry(date: Date())
|
||||
completion(entry)
|
||||
public func snapshot(with context: Context, completion: @escaping (SummaryEntry) -> ()) {
|
||||
|
||||
if context.isPreview {
|
||||
let entry = SummaryEntry(date: Date(),
|
||||
widgetData: WidgetDataDecoder.sampleData())
|
||||
completion(entry)
|
||||
} else {
|
||||
do {
|
||||
let widgetData = try WidgetDataDecoder.decodeWidgetData()
|
||||
let entry = SummaryEntry(date: Date(), widgetData: widgetData)
|
||||
completion(entry)
|
||||
} catch {
|
||||
let entry = SummaryEntry(date: Date(),
|
||||
widgetData: WidgetData(currentUnreadCount: 42, currentTodayCount: 42, latestArticles: [], lastUpdateTime: Date()))
|
||||
completion(entry)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public func timeline(with context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
|
||||
var entries: [SimpleEntry] = []
|
||||
|
||||
// Create current timeline entry for now.
|
||||
let date = Date()
|
||||
var entry: SummaryEntry
|
||||
|
||||
do {
|
||||
let widgetData = try WidgetDataDecoder.decodeWidgetData()
|
||||
entry = SummaryEntry(date: date, widgetData: widgetData)
|
||||
} catch {
|
||||
entry = SummaryEntry(date: date, widgetData: WidgetData(currentUnreadCount: 42, currentTodayCount: 42, latestArticles: [], lastUpdateTime: Date()))
|
||||
}
|
||||
|
||||
// Generate a timeline consisting of five entries an hour apart, starting from the current date.
|
||||
let currentDate = Date()
|
||||
for hourOffset in 0 ..< 5 {
|
||||
let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
|
||||
let entry = SimpleEntry(date: entryDate)
|
||||
entries.append(entry)
|
||||
}
|
||||
|
||||
let timeline = Timeline(entries: entries, policy: .atEnd)
|
||||
// Configure next update in 1 hour.
|
||||
let nextUpdateDate = Calendar.current.date(byAdding: .hour, value: 1, to: date)!
|
||||
|
||||
let timeline = Timeline(
|
||||
entries:[entry],
|
||||
policy: .after(nextUpdateDate))
|
||||
|
||||
completion(timeline)
|
||||
}
|
||||
}
|
||||
|
||||
struct SimpleEntry: TimelineEntry {
|
||||
struct SummaryEntry: TimelineEntry {
|
||||
public let date: Date
|
||||
public let widgetData: WidgetData
|
||||
}
|
||||
|
||||
struct PlaceholderView : View {
|
||||
|
||||
@Environment(\.widgetFamily) var family: WidgetFamily
|
||||
|
||||
var body: some View {
|
||||
Text("Placeholder View")
|
||||
}
|
||||
}
|
||||
|
||||
struct WidgetEntryView : View {
|
||||
var entry: Provider.Entry
|
||||
struct NetNewsWireWidgetView : View {
|
||||
|
||||
@Environment(\.widgetFamily) var family: WidgetFamily
|
||||
var entry: Provider.Entry
|
||||
|
||||
var body: some View {
|
||||
Text(entry.date, style: .time)
|
||||
@ViewBuilder var body: some View {
|
||||
switch family {
|
||||
case .systemSmall:
|
||||
compactWidget
|
||||
case .systemMedium:
|
||||
mediumWidget
|
||||
case .systemLarge:
|
||||
compactWidget
|
||||
@unknown default:
|
||||
compactWidget
|
||||
}
|
||||
}
|
||||
|
||||
var compactWidget: some View {
|
||||
VStack(alignment: .leading) {
|
||||
Spacer()
|
||||
// Today
|
||||
HStack(alignment: .firstTextBaseline) {
|
||||
Image(systemName: "sun.max.fill")
|
||||
.foregroundColor(.orange)
|
||||
.font(.title3)
|
||||
VStack(alignment: .leading) {
|
||||
Text("Today")
|
||||
.font(.title3)
|
||||
.bold()
|
||||
.foregroundColor(.white)
|
||||
Text(String(entry.widgetData.currentTodayCount))
|
||||
.font(.body)
|
||||
.bold()
|
||||
.foregroundColor(.white)
|
||||
}
|
||||
Spacer()
|
||||
}
|
||||
// Unread
|
||||
HStack(alignment: .firstTextBaseline) {
|
||||
Image(systemName: "largecircle.fill.circle")
|
||||
.foregroundColor(.accentColor)
|
||||
.font(.title3)
|
||||
VStack(alignment: .leading) {
|
||||
Text("Unread")
|
||||
.font(.title3)
|
||||
.bold()
|
||||
.foregroundColor(.white)
|
||||
|
||||
Text(String(entry.widgetData.currentUnreadCount))
|
||||
.font(.body)
|
||||
.bold()
|
||||
.foregroundColor(.white)
|
||||
}
|
||||
Spacer()
|
||||
}
|
||||
Spacer()
|
||||
}
|
||||
.padding()
|
||||
.background(Color("WidgetBackground"))
|
||||
}
|
||||
|
||||
var mediumWidget: some View {
|
||||
VStack(alignment: .leading) {
|
||||
HStack {
|
||||
Text("LATEST UNREAD ARTICLES")
|
||||
.font(.headline)
|
||||
.foregroundColor(.white)
|
||||
Spacer()
|
||||
}
|
||||
if entry.widgetData.latestArticles.count > 2 {
|
||||
VStack(alignment: .leading) {
|
||||
ForEach(0..<2, content: { i in
|
||||
HStack(alignment: .top) {
|
||||
Image(uiImage: thumbnail(entry.widgetData.latestArticles[i].feedIcon))
|
||||
.resizable()
|
||||
.frame(width: 20, height: 20)
|
||||
VStack(alignment: .leading) {
|
||||
Text(entry.widgetData.latestArticles[i].articleTitle ?? "")
|
||||
.font(.headline)
|
||||
.foregroundColor(.white)
|
||||
Text(entry.widgetData.latestArticles[i].feedTitle)
|
||||
.font(.footnote)
|
||||
.foregroundColor(.gray)
|
||||
Spacer()
|
||||
}
|
||||
Spacer()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
ForEach(0..<entry.widgetData.latestArticles.count, content: { i in
|
||||
Text(entry.widgetData.latestArticles[i].articleTitle ?? "").font(.headline)
|
||||
Text(entry.widgetData.latestArticles[i].feedTitle)
|
||||
.font(.footnote)
|
||||
})
|
||||
}
|
||||
Spacer()
|
||||
}.padding()
|
||||
.background(Color("WidgetBackground"))
|
||||
}
|
||||
|
||||
func thumbnail(_ data: Data?) -> UIImage {
|
||||
if data == nil {
|
||||
return UIImage(systemName: "globe")!
|
||||
} else {
|
||||
return UIImage(data: data!)!
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@main
|
||||
struct LatestWidget: Widget {
|
||||
private let kind: String = "Widget"
|
||||
private let kind: String = "com.ranchero.NetNewsWire.widget"
|
||||
|
||||
public var body: some WidgetConfiguration {
|
||||
StaticConfiguration(kind: kind,
|
||||
provider: Provider(),
|
||||
placeholder: PlaceholderView()) { entry in
|
||||
WidgetEntryView(entry: entry)
|
||||
NetNewsWireWidgetView(entry: entry)
|
||||
}
|
||||
.configurationDisplayName("NetNewsWire Now")
|
||||
.description("This is NetNewsWire.")
|
||||
.configurationDisplayName("NetNewsWire")
|
||||
.description("NetNewsWire")
|
||||
.supportedFamilies([.systemSmall, .systemMedium])
|
||||
|
||||
}
|
||||
@@ -70,7 +203,7 @@ struct LatestWidget: Widget {
|
||||
|
||||
struct Widget_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
WidgetEntryView(entry: SimpleEntry(date: Date()))
|
||||
.previewContext(WidgetPreviewContext(family: .systemSmall))
|
||||
NetNewsWireWidgetView(entry: SummaryEntry(date: Date(), widgetData: WidgetDataDecoder.sampleData()))
|
||||
.previewContext(WidgetPreviewContext(family: .systemMedium))
|
||||
}
|
||||
}
|
||||
|
||||
10
Multiplatform/iOS/Widget/WidgetExtension.entitlements
Normal file
10
Multiplatform/iOS/Widget/WidgetExtension.entitlements
Normal file
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>com.apple.security.application-groups</key>
|
||||
<array>
|
||||
<string>group.$(ORGANIZATION_IDENTIFIER).NetNewsWire.iOS</string>
|
||||
</array>
|
||||
</dict>
|
||||
</plist>
|
||||
4
Multiplatform/iOS/Widget/widget-data-sample.json
Normal file
4
Multiplatform/iOS/Widget/widget-data-sample.json
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user