mirror of
https://github.com/SideStore/SideStore.git
synced 2026-02-18 11:13:28 +01:00
[Widgets]: Use AppIntentConfiguration instead of StaticConfiguration and cleanup
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
//
|
||||
// ActiveAppsTimelineProvider+Simulator.swift
|
||||
// AltStore
|
||||
//
|
||||
// Created by Magesh K on 10/01/25.
|
||||
// Copyright © 2025 SideStore. All rights reserved.
|
||||
//
|
||||
|
||||
|
||||
/// Simulator data generator
|
||||
#if DEBUG
|
||||
@available(iOS 17, *)
|
||||
extension ActiveAppsTimelineProvider {
|
||||
|
||||
func getSimulatedData(apps: [AppSnapshot]) -> [AppSnapshot]{
|
||||
var apps = apps
|
||||
var newSets: [AppSnapshot] = []
|
||||
// this dummy data is for simulator (uncomment when testing ActiveAppsWidget pagination)
|
||||
if (apps.count > 0){
|
||||
let app = apps[0]
|
||||
for i in 0..<10 {
|
||||
let name = "\(app.name) - \(i)"
|
||||
let x = AppSnapshot(name: name,
|
||||
bundleIdentifier: app.bundleIdentifier,
|
||||
expirationDate: app.expirationDate,
|
||||
refreshedDate: app.refreshedDate
|
||||
)
|
||||
newSets.append(x)
|
||||
}
|
||||
apps = newSets
|
||||
}
|
||||
return apps
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
85
AltWidget/Providers/ActiveAppsTimelineProvider.swift
Normal file
85
AltWidget/Providers/ActiveAppsTimelineProvider.swift
Normal file
@@ -0,0 +1,85 @@
|
||||
//
|
||||
// ActiveAppsTimelineProvider.swift
|
||||
// AltStore
|
||||
//
|
||||
// Created by Magesh K on 10/01/25.
|
||||
// Copyright © 2025 SideStore. All rights reserved.
|
||||
//
|
||||
|
||||
import WidgetKit
|
||||
|
||||
protocol Navigation{
|
||||
var direction: Direction? { get }
|
||||
}
|
||||
|
||||
@available(iOS 17, *)
|
||||
class ActiveAppsTimelineProvider: AppsTimelineProviderBase<Navigation> {
|
||||
|
||||
let uuid = UUID().uuidString
|
||||
|
||||
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 }
|
||||
|
||||
var apps = apps
|
||||
|
||||
// #if DEBUG
|
||||
// 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)!
|
||||
}
|
||||
}else{
|
||||
// retain what ever page we were on as-is
|
||||
apps = dataHolder.currentPage(inItems: apps)
|
||||
}
|
||||
|
||||
return apps
|
||||
}
|
||||
}
|
||||
|
||||
@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))
|
||||
|
||||
let bundleIDs = await super.fetchActiveAppBundleIDs()
|
||||
|
||||
let snapshot = await self.snapshot(for: bundleIDs, in: data)
|
||||
|
||||
return snapshot
|
||||
}
|
||||
|
||||
func timeline(for intent: Intent, in context: Context) async -> Timeline<AppsEntry> {
|
||||
let data = IntentData(direction: intent.getDirection(widgetID))
|
||||
|
||||
let bundleIDs = await self.fetchActiveAppBundleIDs()
|
||||
|
||||
let timeline = await self.timeline(for: bundleIDs, in: data)
|
||||
|
||||
return timeline
|
||||
}
|
||||
}
|
||||
214
AltWidget/Providers/AppsTimelineProvider.swift
Normal file
214
AltWidget/Providers/AppsTimelineProvider.swift
Normal file
@@ -0,0 +1,214 @@
|
||||
//
|
||||
// AppsTimelineProvider.swift
|
||||
// AltWidgetExtension
|
||||
//
|
||||
// Created by Riley Testut on 8/23/23.
|
||||
// Copyright © 2023 Riley Testut. All rights reserved.
|
||||
//
|
||||
|
||||
import WidgetKit
|
||||
import CoreData
|
||||
|
||||
import AltStoreCore
|
||||
|
||||
struct AppsEntry: TimelineEntry
|
||||
{
|
||||
var date: Date
|
||||
var relevance: TimelineEntryRelevance?
|
||||
|
||||
var apps: [AppSnapshot]
|
||||
var isPlaceholder: Bool = false
|
||||
}
|
||||
|
||||
class AppsTimelineProviderBase<T>
|
||||
{
|
||||
typealias Entry = AppsEntry
|
||||
|
||||
func placeholder(in context: TimelineProviderContext) -> AppsEntry
|
||||
{
|
||||
return AppsEntry(date: Date(), apps: [], isPlaceholder: true)
|
||||
}
|
||||
|
||||
func snapshot(for appBundleIDs: [String], in context: T? = nil) async -> AppsEntry
|
||||
{
|
||||
do
|
||||
{
|
||||
try await self.prepare()
|
||||
|
||||
var apps = try await self.fetchApps(withBundleIDs: appBundleIDs)
|
||||
|
||||
apps = getUpdatedData(apps, context)
|
||||
|
||||
let entry = AppsEntry(date: Date(), apps: apps)
|
||||
return entry
|
||||
}
|
||||
catch
|
||||
{
|
||||
print("Failed to prepare widget snapshot:", error)
|
||||
|
||||
let entry = AppsEntry(date: Date(), apps: [])
|
||||
return entry
|
||||
}
|
||||
}
|
||||
|
||||
func timeline(for appBundleIDs: [String], in context: T? = nil) async -> Timeline<AppsEntry>
|
||||
{
|
||||
do
|
||||
{
|
||||
try await self.prepare()
|
||||
|
||||
var apps = try await self.fetchApps(withBundleIDs: appBundleIDs)
|
||||
|
||||
apps = getUpdatedData(apps, context)
|
||||
|
||||
let entries = self.makeEntries(for: apps)
|
||||
let timeline = Timeline(entries: entries, policy: .atEnd)
|
||||
return timeline
|
||||
}
|
||||
catch
|
||||
{
|
||||
print("Failed to prepare widget timeline:", error)
|
||||
|
||||
let entry = AppsEntry(date: Date(), apps: [])
|
||||
let timeline = Timeline(entries: [entry], policy: .atEnd)
|
||||
return timeline
|
||||
}
|
||||
}
|
||||
|
||||
func getUpdatedData(_ apps: [AppSnapshot], _ context: T?) -> [AppSnapshot]{
|
||||
// override in subclasses as required
|
||||
return apps
|
||||
}
|
||||
}
|
||||
|
||||
extension AppsTimelineProviderBase
|
||||
{
|
||||
|
||||
private func prepare() async throws
|
||||
{
|
||||
try await DatabaseManager.shared.start()
|
||||
}
|
||||
|
||||
private func fetchApps(withBundleIDs bundleIDs: [String]) async throws -> [AppSnapshot]
|
||||
{
|
||||
let context = DatabaseManager.shared.persistentContainer.newBackgroundContext()
|
||||
let apps = try await context.performAsync {
|
||||
let fetchRequest = InstalledApp.fetchRequest()
|
||||
fetchRequest.predicate = NSPredicate(format: "%K IN %@", #keyPath(InstalledApp.bundleIdentifier), bundleIDs)
|
||||
fetchRequest.returnsObjectsAsFaults = false
|
||||
|
||||
let installedApps = try context.fetch(fetchRequest)
|
||||
|
||||
let apps = installedApps.map { AppSnapshot(installedApp: $0) }
|
||||
|
||||
// Always list apps in alphabetical order.
|
||||
let sortedApps = apps.sorted { $0.name < $1.name }
|
||||
return sortedApps
|
||||
}
|
||||
|
||||
return apps
|
||||
}
|
||||
|
||||
func makeEntries(for snapshots: [AppSnapshot]) -> [AppsEntry]
|
||||
{
|
||||
let sortedAppsByExpirationDate = snapshots.sorted { $0.expirationDate < $1.expirationDate }
|
||||
guard let firstExpiringApp = sortedAppsByExpirationDate.first, let lastExpiringApp = sortedAppsByExpirationDate.last else { return [] }
|
||||
|
||||
let currentDate = Calendar.current.startOfDay(for: Date())
|
||||
let numberOfDays = lastExpiringApp.expirationDate.numberOfCalendarDays(since: currentDate)
|
||||
|
||||
// Generate a timeline consisting of one entry per day.
|
||||
var entries: [AppsEntry] = []
|
||||
|
||||
switch numberOfDays
|
||||
{
|
||||
case ..<0:
|
||||
let entry = AppsEntry(date: currentDate, relevance: TimelineEntryRelevance(score: 0.0), apps: snapshots)
|
||||
entries.append(entry)
|
||||
|
||||
case 0:
|
||||
let entry = AppsEntry(date: currentDate, relevance: TimelineEntryRelevance(score: 1.0), apps: snapshots)
|
||||
entries.append(entry)
|
||||
|
||||
default:
|
||||
// To reduce memory consumption, we only generate entries for the next week. This includes:
|
||||
// * 1 for each day the "least expired" app is valid (up to 7)
|
||||
// * 1 "0 days remaining"
|
||||
// * 1 "Expired"
|
||||
|
||||
let numberOfEntries = min(numberOfDays, 7) + 2
|
||||
|
||||
let appEntries = (0 ..< numberOfEntries).map { (dayOffset) -> AppsEntry in
|
||||
let entryDate = Calendar.current.date(byAdding: .day, value: dayOffset, to: currentDate) ?? currentDate.addingTimeInterval(Double(dayOffset) * 60 * 60 * 24)
|
||||
|
||||
let daysSinceRefresh = entryDate.numberOfCalendarDays(since: firstExpiringApp.refreshedDate)
|
||||
let totalNumberOfDays = firstExpiringApp.expirationDate.numberOfCalendarDays(since: firstExpiringApp.refreshedDate)
|
||||
|
||||
var score = (entryDate <= firstExpiringApp.expirationDate) ? Float(daysSinceRefresh + 1) / Float(totalNumberOfDays + 1) : 1 // Expired apps have a score of 1.
|
||||
if snapshots.allSatisfy({ $0.expirationDate > currentDate })
|
||||
{
|
||||
// Unless ALL apps are expired, in which case relevance is 0.
|
||||
score = 0
|
||||
}
|
||||
|
||||
let entry = AppsEntry(date: entryDate, relevance: TimelineEntryRelevance(score: score), apps: snapshots)
|
||||
return entry
|
||||
}
|
||||
|
||||
entries.append(contentsOf: appEntries)
|
||||
}
|
||||
|
||||
return entries
|
||||
}
|
||||
|
||||
func fetchActiveAppBundleIDs() async -> [String]
|
||||
{
|
||||
do
|
||||
{
|
||||
try await self.prepare()
|
||||
|
||||
let context = DatabaseManager.shared.persistentContainer.newBackgroundContext()
|
||||
let bundleIDs = try await context.performAsync {
|
||||
let fetchRequest = InstalledApp.activeAppsFetchRequest() as! NSFetchRequest<NSDictionary>
|
||||
fetchRequest.resultType = .dictionaryResultType
|
||||
fetchRequest.propertiesToFetch = [#keyPath(InstalledApp.bundleIdentifier)]
|
||||
|
||||
let bundleIDs = try context.fetch(fetchRequest).compactMap { $0[#keyPath(InstalledApp.bundleIdentifier)] as? String }
|
||||
return bundleIDs
|
||||
}
|
||||
|
||||
return bundleIDs
|
||||
}
|
||||
catch
|
||||
{
|
||||
print("Failed to fetch active bundle IDs, falling back to AltStore bundle ID.", error)
|
||||
|
||||
return [StoreApp.altstoreAppID]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class AppsTimelineProvider: AppsTimelineProviderBase<ViewAppIntent>, IntentTimelineProvider
|
||||
{
|
||||
typealias Intent = ViewAppIntent
|
||||
|
||||
func getSnapshot(for intent: Intent, in context: Context, completion: @escaping (AppsEntry) -> Void)
|
||||
{
|
||||
Task<Void, Never> {
|
||||
let bundleIDs = [intent.app?.identifier ?? StoreApp.altstoreAppID]
|
||||
|
||||
let snapshot = await self.snapshot(for: bundleIDs, in: intent)
|
||||
completion(snapshot)
|
||||
}
|
||||
}
|
||||
|
||||
func getTimeline(for intent: Intent, in context: Context, completion: @escaping (Timeline<AppsEntry>) -> Void)
|
||||
{
|
||||
Task<Void, Never> {
|
||||
let bundleIDs = [intent.app?.identifier ?? StoreApp.altstoreAppID]
|
||||
|
||||
let timeline = await self.timeline(for: bundleIDs, in: intent)
|
||||
completion(timeline)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user