[Widgets]: Enhanced to isolate operations from multiple views of same widget type

This commit is contained in:
Magesh K
2025-01-11 03:25:25 +05:30
parent f69b293004
commit e29d9f7904
12 changed files with 346 additions and 245 deletions

View File

@@ -8,7 +8,7 @@
/// Simulator data generator
#if DEBUG
#if targetEnvironment(simulator)
@available(iOS 17, *)
extension ActiveAppsTimelineProvider {
@@ -18,7 +18,7 @@ extension ActiveAppsTimelineProvider {
// this dummy data is for simulator (uncomment when testing ActiveAppsWidget pagination)
if (apps.count > 0){
let app = apps[0]
for i in 0..<10 {
for i in 1...10 {
let name = "\(app.name) - \(i)"
let x = AppSnapshot(name: name,
bundleIdentifier: app.bundleIdentifier,

View File

@@ -8,77 +8,88 @@
import WidgetKit
protocol Navigation{
var direction: Direction? { get }
protocol WidgetInfo{
var ID: Int? { get }
}
@available(iOS 17, *)
class ActiveAppsTimelineProvider: AppsTimelineProviderBase<Navigation> {
let uuid = UUID().uuidString
class ActiveAppsTimelineProvider<T: WidgetInfo>: AppsTimelineProviderBase<WidgetInfo> {
public struct WidgetData: WidgetInfo {
let ID: Int?
}
private let dataHolder: PaginationDataHolder
private let widgetID: String
init(kind: String){
print("Executing ActiveAppsTimelineProvider.init() for instance \(uuid)")
let itemsPerPage = ActiveAppsWidget.Constants.MAX_ROWS_PER_PAGE
self.dataHolder = PaginationDataHolder(itemsPerPage: itemsPerPage)
self.widgetID = kind
}
override func getUpdatedData(_ apps: [AppSnapshot], _ context: Navigation?) -> [AppSnapshot] {
guard let context = context else { return apps }
deinit{
// if this provider goes out of scope, clear all entries
PageInfoManager.shared.clearAll()
}
override func getUpdatedData(_ apps: [AppSnapshot], _ context: WidgetInfo?) -> [AppSnapshot] {
var apps = apps
// #if DEBUG
// apps = getSimulatedData(apps: apps)
// #endif
#if targetEnvironment(simulator)
apps = getSimulatedData(apps: apps)
#endif
if let direction = context.direction{
// get paged data if available
switch (direction){
case Direction.up:
apps = dataHolder.prevPage(inItems: apps, whenUnavailable: .current)!
case Direction.down:
apps = dataHolder.nextPage(inItems: apps, whenUnavailable: .current)!
var currentPageApps = dataHolder.currentPage(inItems: apps)
if let widgetInfo = context,
let widgetID = widgetInfo.ID {
var navEvent: NavigationEvent? = PageInfoManager.shared.getPageInfo(for: widgetID)
if let event = navEvent,
let direction = event.direction
{
// process navigation request only if event wasn't consumed yet
if !event.consumed {
switch (direction){
case Direction.up:
currentPageApps = dataHolder.prevPage(inItems: apps, whenUnavailable: .current)!
case Direction.down:
currentPageApps = dataHolder.nextPage(inItems: apps, whenUnavailable: .current)!
}
// mark the event as consumed
// this prevents duplicate getUpdatedData() requests for same navigation event
navEvent!.consumed = true
}
}
}else{
// retain what ever page we were on as-is
apps = dataHolder.currentPage(inItems: apps)
PageInfoManager.shared.setPageInfo(for: widgetID, value: navEvent)
}
return apps
return currentPageApps
}
}
/// TimelineProvider for WidgetAppIntentConfiguration widget type
@available(iOS 17, *)
extension ActiveAppsTimelineProvider: AppIntentTimelineProvider {
struct IntentData: Navigation{
let direction: Direction?
}
typealias Intent = WidgetUpdateIntent
func snapshot(for intent: Intent, in context: Context) async -> AppsEntry {
let data = IntentData(direction: intent.getDirection(widgetID))
func snapshot(for intent: Intent, in context: Context) async -> AppsEntry<WidgetInfo> {
// system retains the previously configured ID value and posts the same here
let widgetData = WidgetData(ID: intent.ID)
let bundleIDs = await super.fetchActiveAppBundleIDs()
let snapshot = await self.snapshot(for: bundleIDs, in: data)
let snapshot = await self.snapshot(for: bundleIDs, in: widgetData)
return snapshot
}
func timeline(for intent: Intent, in context: Context) async -> Timeline<AppsEntry> {
let data = IntentData(direction: intent.getDirection(widgetID))
func timeline(for intent: Intent, in context: Context) async -> Timeline<AppsEntry<WidgetInfo>> {
// system retains the previously configured ID value and posts the same here
let widgetData = WidgetData(ID: intent.ID)
let bundleIDs = await self.fetchActiveAppBundleIDs()
let timeline = await self.timeline(for: bundleIDs, in: data)
let timeline = await self.timeline(for: bundleIDs, in: widgetData)
return timeline
}

View File

@@ -11,25 +11,28 @@ import CoreData
import AltStoreCore
struct AppsEntry: TimelineEntry
struct AppsEntry<T>: TimelineEntry
{
var date: Date
var relevance: TimelineEntryRelevance?
var apps: [AppSnapshot]
var isPlaceholder: Bool = false
var context: T?
}
class AppsTimelineProviderBase<T>
{
typealias Entry = AppsEntry
func placeholder(in context: TimelineProviderContext) -> AppsEntry
func placeholder(in context: TimelineProviderContext) -> AppsEntry<T>
{
return AppsEntry(date: Date(), apps: [], isPlaceholder: true)
}
func snapshot(for appBundleIDs: [String], in context: T? = nil) async -> AppsEntry
func snapshot(for appBundleIDs: [String], in context: T? = nil) async -> AppsEntry<T>
{
do
{
@@ -39,19 +42,19 @@ class AppsTimelineProviderBase<T>
apps = getUpdatedData(apps, context)
let entry = AppsEntry(date: Date(), apps: apps)
let entry = AppsEntry(date: Date(), apps: apps, context: context)
return entry
}
catch
{
print("Failed to prepare widget snapshot:", error)
let entry = AppsEntry(date: Date(), apps: [])
let entry = AppsEntry(date: Date(), apps: [], context: context)
return entry
}
}
func timeline(for appBundleIDs: [String], in context: T? = nil) async -> Timeline<AppsEntry>
func timeline(for appBundleIDs: [String], in context: T? = nil) async -> Timeline<AppsEntry<T>>
{
do
{
@@ -61,7 +64,14 @@ class AppsTimelineProviderBase<T>
apps = getUpdatedData(apps, context)
let entries = self.makeEntries(for: apps)
var entries = self.makeEntries(for: apps, in: context)
// #if targetEnvironment(simulator)
// if let first = entries.first{
// entries = [first]
// }
// #endif
let timeline = Timeline(entries: entries, policy: .atEnd)
return timeline
}
@@ -69,7 +79,7 @@ class AppsTimelineProviderBase<T>
{
print("Failed to prepare widget timeline:", error)
let entry = AppsEntry(date: Date(), apps: [])
let entry = AppsEntry(date: Date(), apps: [], context: context)
let timeline = Timeline(entries: [entry], policy: .atEnd)
return timeline
}
@@ -109,7 +119,7 @@ extension AppsTimelineProviderBase
return apps
}
func makeEntries(for snapshots: [AppSnapshot]) -> [AppsEntry]
func makeEntries(for snapshots: [AppSnapshot], in context: T? = nil) -> [AppsEntry<T>]
{
let sortedAppsByExpirationDate = snapshots.sorted { $0.expirationDate < $1.expirationDate }
guard let firstExpiringApp = sortedAppsByExpirationDate.first, let lastExpiringApp = sortedAppsByExpirationDate.last else { return [] }
@@ -118,16 +128,16 @@ extension AppsTimelineProviderBase
let numberOfDays = lastExpiringApp.expirationDate.numberOfCalendarDays(since: currentDate)
// Generate a timeline consisting of one entry per day.
var entries: [AppsEntry] = []
var entries: [AppsEntry<T>] = []
switch numberOfDays
{
case ..<0:
let entry = AppsEntry(date: currentDate, relevance: TimelineEntryRelevance(score: 0.0), apps: snapshots)
let entry = AppsEntry(date: currentDate, relevance: TimelineEntryRelevance(score: 0.0), apps: snapshots, context: context)
entries.append(entry)
case 0:
let entry = AppsEntry(date: currentDate, relevance: TimelineEntryRelevance(score: 1.0), apps: snapshots)
let entry = AppsEntry(date: currentDate, relevance: TimelineEntryRelevance(score: 1.0), apps: snapshots, context: context)
entries.append(entry)
default:
@@ -151,7 +161,7 @@ extension AppsTimelineProviderBase
score = 0
}
let entry = AppsEntry(date: entryDate, relevance: TimelineEntryRelevance(score: score), apps: snapshots)
let entry = AppsEntry(date: entryDate, relevance: TimelineEntryRelevance(score: score), apps: snapshots, context: context)
return entry
}
@@ -188,11 +198,11 @@ extension AppsTimelineProviderBase
}
}
class AppsTimelineProvider: AppsTimelineProviderBase<ViewAppIntent>, IntentTimelineProvider
typealias Intent = ViewAppIntent
class AppsTimelineProvider: AppsTimelineProviderBase<Intent>, IntentTimelineProvider
{
typealias Intent = ViewAppIntent
func getSnapshot(for intent: Intent, in context: Context, completion: @escaping (AppsEntry) -> Void)
func getSnapshot(for intent: Intent, in context: Context, completion: @escaping (AppsEntry<Intent>) -> Void)
{
Task<Void, Never> {
let bundleIDs = [intent.app?.identifier ?? StoreApp.altstoreAppID]
@@ -202,7 +212,7 @@ class AppsTimelineProvider: AppsTimelineProviderBase<ViewAppIntent>, IntentTimel
}
}
func getTimeline(for intent: Intent, in context: Context, completion: @escaping (Timeline<AppsEntry>) -> Void)
func getTimeline(for intent: Intent, in context: Context, completion: @escaping (Timeline<AppsEntry<Intent>>) -> Void)
{
Task<Void, Never> {
let bundleIDs = [intent.app?.identifier ?? StoreApp.altstoreAppID]