From 300d1eac60554574f6dd175076a77c7bcfe4d3a6 Mon Sep 17 00:00:00 2001 From: Stuart Breckenridge Date: Sat, 11 Jul 2020 22:24:49 +0800 Subject: [PATCH] Widget.md --- Technotes/Widget.md | 43 ++++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/Technotes/Widget.md b/Technotes/Widget.md index 5d99ff6d4..c14ae3255 100644 --- a/Technotes/Widget.md +++ b/Technotes/Widget.md @@ -4,39 +4,32 @@ The NetNewsWire iOS widget supports the `systemSmall` and `systemMedium` styles. -The `systemSmall` style displays the current Today and Unread counts; `systemMedium` displays the latest two articles. +For the purpose of this PoC: the `systemSmall` style displays the current Today and Unread counts; `systemMedium` displays the latest two articles. ## Passing Data from the App to the Widget -Data is made available to the widget by encoding JSON data and saving it to a file in a directory available to app extensions. +Data is made available to the widget by encoding smart feed and article data as JSON, and saving it to a file in a directory available to app extensions. +Three `struct`s are responsible for this: -``` -struct WidgetData: Codable { - - let currentUnreadCount: Int - let currentTodayCount: Int - let latestArticles: [LatestArticle] - let lastUpdateTime: Date - -} - -struct LatestArticle: Codable { - - let feedTitle: String - let articleTitle: String? - let articleSummary: String? - let feedIcon: Data? // Base64 encoded image data - let pubDate: String - -} -``` +- `WidgetDataEncoder` (which is available to the app); +- `WidgetDataDecoder` (which is available to the widget); and, +- `WidgetData` (which is available to both app and widget, and includes the data neccessary for the widget to function) ## When is JSON Data Saved? -1. When the app enters the background -2. After a background refresh +1. When the app enters the background (monitored via the `scenePhase` changing (tested)); or, +2. After a background refresh (untested at the time of writing) + +Encoding tasks are fenced in +``` +UIApplication.shared.beginBackgroundTask +_{ encoding task }_ +UIApplication.shared.endBackgroundTask +``` +in order to ensure that the file can be written with sufficient time to spare. + +After JSON data is saved, Widget timelines are reloaded. -After JSON data is saved, Widget timelines are reloaded.