[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

@@ -11,6 +11,8 @@ import WidgetKit
import AltStoreCore
import GameplayKit
private extension Color
{
static let altGradientLight = Color.init(.displayP3, red: 123.0/255.0, green: 200.0/255.0, blue: 176.0/255.0)
@@ -19,6 +21,9 @@ private extension Color
static let altGradientExtraDark = Color.init(.displayP3, red: 2.0/255.0, green: 82.0/255.0, blue: 103.0/255.0)
}
struct WidgetTag: WidgetInfo{
let ID: Int?
}
//@available(iOS 17, *)
struct ActiveAppsWidget: Widget
@@ -27,22 +32,17 @@ struct ActiveAppsWidget: Widget
static let MAX_ROWS_PER_PAGE: UInt = 3
}
let ID = UUID().uuidString
public var body: some WidgetConfiguration {
print("Executing ActiveAppsWidget.body for instance \(ID)")
if #available(iOS 17, *)
{
let widgetID = "ActiveApps - \(UUID().uuidString)"
let widgetConfig = AppIntentConfiguration(
kind: widgetID,
// intent: PaginationIntent.self, // Use the defined AppIntent
intent: WidgetUpdateIntent.self, // Use the defined AppIntent
provider: ActiveAppsTimelineProvider(kind: widgetID)
intent: WidgetUpdateIntent.self,
provider: ActiveAppsTimelineProvider<WidgetTag>(kind: widgetID)
) { entry in
ActiveAppsWidgetView(entry: entry, widgetID: widgetID)
ActiveAppsWidgetView(entry: entry)
}
.supportedFamilies([.systemMedium])
.configurationDisplayName("Active Apps")
@@ -62,17 +62,7 @@ struct ActiveAppsWidget: Widget
@available(iOS 17, *)
private struct ActiveAppsWidgetView: View
{
var entry: AppsEntry
var widgetID: String
let ID = UUID().uuidString
init(entry: AppsEntry, widgetID: String) {
print("Executing ActiveAppsWidgetView.init() for instance \(ID)")
self.entry = entry
self.widgetID = widgetID
}
var entry: AppsEntry<WidgetInfo>
@Environment(\.colorScheme)
private var colorScheme
@@ -103,104 +93,131 @@ private struct ActiveAppsWidgetView: View
private var content: some View {
GeometryReader { (geometry) in
let itemsPerPage = ActiveAppsWidget.Constants.MAX_ROWS_PER_PAGE
let preferredRowHeight = (geometry.size.height / Double(itemsPerPage)) - 8
let rowHeight = min(preferredRowHeight, geometry.size.height / 2)
HStack(alignment: .center) {
LazyVStack(spacing: 12) {
ForEach(Array(entry.apps.enumerated()), id: \.offset) { index, app in
let icon: UIImage = app.icon ?? UIImage(named: "SideStore")!
// 1024x1024 images are not supported by previews but supported by device
// so we scale the image to 97% so as to reduce its actual size but not too much
// to somewhere below value, acceptable by previews ie < 1042x948
let scalingFactor = 0.97
let resizedSize = CGSize(
width: icon.size.width * scalingFactor,
height: icon.size.height * scalingFactor
)
let resizedIcon = icon.resizing(to: resizedSize)!
let daysRemaining = app.expirationDate.numberOfCalendarDays(since: entry.date)
let cornerRadius = rowHeight / 5.0
HStack(spacing: 10) {
Image(uiImage: resizedIcon)
.resizable()
.aspectRatio(contentMode: .fit)
.cornerRadius(cornerRadius)
VStack(alignment: .leading, spacing: 1) {
Text(app.name)
.font(.system(size: 15, weight: .semibold, design: .rounded))
let text = if entry.date > app.expirationDate
{
Text("Expired")
}
else
{
Text("Expires in \(daysRemaining) ") + (daysRemaining == 1 ? Text("day") : Text("days"))
}
text
.font(.system(size: 13, weight: .semibold, design: .rounded))
.foregroundStyle(.secondary)
}
Spacer()
Countdown(startDate: app.refreshedDate,
endDate: app.expirationDate,
currentDate: entry.date,
strokeWidth: 3.0) // Slightly thinner circle stroke width
.background {
Color.black.opacity(0.1)
.mask(Capsule())
.padding(.all, -5)
}
.font(.system(size: 16, weight: .semibold, design: .rounded))
// this modifier invalidates the view (disables userinteraction and shows a blinking effect)
// until new timeline events occur, unless a observable boolean state is presented as parameter
.invalidatableContent()
.activatesRefreshAllAppsIntent()
}
.frame(height: rowHeight)
}
}
appsListView(reader: geometry)
Spacer(minLength: 16)
let buttonWidth: CGFloat = 16
VStack {
Image(systemName: "arrow.up")
.resizable()
.frame(width: buttonWidth, height: buttonWidth)
.opacity(0.3)
// .mask(Capsule())
.pageUpButton(widgetID)
Spacer()
Image(systemName: "arrow.down")
.resizable()
.frame(width: buttonWidth, height: buttonWidth)
.opacity(0.3)
// .mask(Capsule())
.pageDownButton(widgetID)
}
.padding(.vertical)
navigationBarView()
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
private func appsListView(reader: GeometryProxy) -> some View {
let itemsPerPage = ActiveAppsWidget.Constants.MAX_ROWS_PER_PAGE
let preferredRowHeight = (reader.size.height / Double(itemsPerPage)) - 8
let rowHeight = min(preferredRowHeight, reader.size.height / 2)
return LazyVStack(spacing: 12) {
ForEach(Array(entry.apps.enumerated()), id: \.offset) { index, app in
appEntryRowView(app: app, rowHeight: rowHeight)
}
}
}
private func appEntryRowView(app: AppSnapshot, rowHeight: Double) -> some View {
let icon: UIImage = app.icon ?? UIImage(named: "SideStore")!
// 1024x1024 images are not supported by previews but supported by device
// so we scale the image to 97% so as to reduce its actual size but not too much
// to somewhere below value, acceptable by previews ie < 1042x948
let scalingFactor = 0.97
let resizedSize = CGSize(
width: icon.size.width * scalingFactor,
height: icon.size.height * scalingFactor
)
let resizedIcon = icon.resizing(to: resizedSize)!
let cornerRadius = rowHeight / 5.0
return HStack(spacing: 10) {
Image(uiImage: resizedIcon)
.resizable()
.aspectRatio(contentMode: .fit)
.cornerRadius(cornerRadius)
appDetailsView(app)
Spacer()
countDownView(app)
}
.frame(height: rowHeight)
}
private func appDetailsView(_ app: AppSnapshot) -> some View {
let daysRemaining = app.expirationDate.numberOfCalendarDays(since: entry.date)
return VStack(alignment: .leading, spacing: 1) {
Text(app.name)
.font(.system(size: 15, weight: .semibold, design: .rounded))
let text = if entry.date > app.expirationDate
{
Text("Expired")
}
else
{
Text("Expires in \(daysRemaining) ") + (daysRemaining == 1 ? Text("day") : Text("days"))
}
text
.font(.system(size: 13, weight: .semibold, design: .rounded))
.foregroundStyle(.secondary)
}
}
private func countDownView(_ app: AppSnapshot) -> some View {
Countdown(startDate: app.refreshedDate,
endDate: app.expirationDate,
currentDate: entry.date,
strokeWidth: 3.0) // Slightly thinner circle stroke width
.background {
Color.black.opacity(0.1)
.mask(Capsule())
.padding(.all, -5)
}
.font(.system(size: 16, weight: .semibold, design: .rounded))
// this modifier invalidates the view (disables user interaction and shows a blinking effect)
.invalidatableContent()
.activatesRefreshAllAppsIntent()
}
private func navigationBarView() -> some View {
let buttonWidth: CGFloat = 16
let widgetID = entry.context?.ID
return VStack {
Image(systemName: "arrow.up")
.resizable()
.frame(width: buttonWidth, height: buttonWidth)
.opacity(0.3)
// .mask(Capsule())
.pageUpButton(widgetID)
Spacer()
Image(systemName: "arrow.down")
.resizable()
.frame(width: buttonWidth, height: buttonWidth)
.opacity(0.3)
// .mask(Capsule())
.pageDownButton(widgetID)
}
.padding(.vertical)
}
private var placeholder: some View {
Text("App Not Found")
.font(.system(.body, design: .rounded))
@@ -216,14 +233,14 @@ private struct ActiveAppsWidgetView: View
let expiredDate = Date().addingTimeInterval(1 * 60 * 60 * 24 * 7)
let (altstore, delta, clip, longAltStore, longDelta, longClip) = AppSnapshot.makePreviewSnapshots()
AppsEntry(date: Date(), apps: [altstore, delta, clip])
AppsEntry(date: Date(), apps: [longAltStore, longDelta, longClip])
AppsEntry<Any>(date: Date(), apps: [altstore, delta, clip])
AppsEntry<Any>(date: Date(), apps: [longAltStore, longDelta, longClip])
AppsEntry(date: expiredDate, apps: [altstore, delta, clip])
AppsEntry<Any>(date: expiredDate, apps: [altstore, delta, clip])
AppsEntry(date: Date(), apps: [altstore, delta])
AppsEntry(date: Date(), apps: [altstore])
AppsEntry<Any>(date: Date(), apps: [altstore, delta])
AppsEntry<Any>(date: Date(), apps: [altstore])
AppsEntry(date: Date(), apps: [])
AppsEntry(date: Date(), apps: [], isPlaceholder: true)
AppsEntry<Any>(date: Date(), apps: [])
AppsEntry<Any>(date: Date(), apps: [], isPlaceholder: true)
}

View File

@@ -39,7 +39,7 @@ struct AppDetailWidget: Widget
private struct AppDetailWidgetView: View
{
var entry: AppsEntry
var entry: AppsEntry<Intent>
var body: some View {
Group {
@@ -200,11 +200,11 @@ private extension AppDetailWidgetView
} timeline: {
let expiredDate = Date().addingTimeInterval(1 * 60 * 60 * 24 * 7)
let (altstore, _, _, longAltStore, _, _) = AppSnapshot.makePreviewSnapshots()
AppsEntry(date: Date(), apps: [altstore])
AppsEntry(date: Date(), apps: [longAltStore])
AppsEntry<Any>(date: Date(), apps: [altstore])
AppsEntry<Any>(date: Date(), apps: [longAltStore])
AppsEntry(date: expiredDate, apps: [altstore])
AppsEntry<Any>(date: expiredDate, apps: [altstore])
AppsEntry(date: Date(), apps: [])
AppsEntry(date: Date(), apps: [], isPlaceholder: true)
AppsEntry<Any>(date: Date(), apps: [])
AppsEntry<Any>(date: Date(), apps: [], isPlaceholder: true)
}

View File

@@ -70,7 +70,7 @@ extension ComplicationView
@available(iOS 16, *)
private struct ComplicationView: View
{
let entry: AppsEntry
let entry: AppsEntry<Intent>
let style: Style
var body: some View {
@@ -144,10 +144,10 @@ private let widgetFamily = if #available(iOS 16, *) { WidgetFamily.accessoryCirc
let expiredDate = Date().addingTimeInterval(1 * 60 * 60 * 24 * 7)
let (altstore, _, _, longAltStore, _, _) = AppSnapshot.makePreviewSnapshots()
AppsEntry(date: Date(), apps: [altstore])
AppsEntry(date: Date(), apps: [longAltStore])
AppsEntry<Any>(date: Date(), apps: [altstore])
AppsEntry<Any>(date: Date(), apps: [longAltStore])
AppsEntry(date: expiredDate, apps: [altstore])
AppsEntry<Any>(date: expiredDate, apps: [altstore])
}
@available(iOS 17, *)
@@ -157,8 +157,8 @@ private let widgetFamily = if #available(iOS 16, *) { WidgetFamily.accessoryCirc
let expiredDate = Date().addingTimeInterval(1 * 60 * 60 * 24 * 7)
let (altstore, _, _, longAltStore, _, _) = AppSnapshot.makePreviewSnapshots()
AppsEntry(date: Date(), apps: [altstore])
AppsEntry(date: Date(), apps: [longAltStore])
AppsEntry<Any>(date: Date(), apps: [altstore])
AppsEntry<Any>(date: Date(), apps: [longAltStore])
AppsEntry(date: expiredDate, apps: [altstore])
AppsEntry<Any>(date: expiredDate, apps: [altstore])
}