From bb8a1b57cd3e6116a088dc93d4b6e3ddb11f1cd0 Mon Sep 17 00:00:00 2001 From: Magesh K <47920326+mahee96@users.noreply.github.com> Date: Wed, 8 Jan 2025 14:33:58 +0530 Subject: [PATCH] [Widgets]: Fix for AppDetailWidget previews crashing --- .../SideStore.imageset/Contents.json | 1 + AltWidget/Widgets/ActiveAppsWidget.swift | 16 ++++++++- AltWidget/Widgets/AppDetailWidget.swift | 35 +++++++++++++++---- 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/AltWidget/Assets.xcassets/SideStore.imageset/Contents.json b/AltWidget/Assets.xcassets/SideStore.imageset/Contents.json index 161b1833..db1017b1 100644 --- a/AltWidget/Assets.xcassets/SideStore.imageset/Contents.json +++ b/AltWidget/Assets.xcassets/SideStore.imageset/Contents.json @@ -1,6 +1,7 @@ { "images" : [ { + "filename" : "1024.png", "idiom" : "universal", "scale" : "1x" }, diff --git a/AltWidget/Widgets/ActiveAppsWidget.swift b/AltWidget/Widgets/ActiveAppsWidget.swift index 412de6fc..c24214e3 100644 --- a/AltWidget/Widgets/ActiveAppsWidget.swift +++ b/AltWidget/Widgets/ActiveAppsWidget.swift @@ -88,11 +88,25 @@ private struct ActiveAppsWidgetView: View VStack(spacing: 12) { ForEach(entry.apps, id: \.bundleIdentifier) { app in + let icon = 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: app.icon ?? UIImage(named: "AltStore")!) + Image(uiImage: resizedIcon) .resizable() .aspectRatio(contentMode: .fit) .cornerRadius(cornerRadius) diff --git a/AltWidget/Widgets/AppDetailWidget.swift b/AltWidget/Widgets/AppDetailWidget.swift index b59c6a77..cee091b2 100644 --- a/AltWidget/Widgets/AppDetailWidget.swift +++ b/AltWidget/Widgets/AppDetailWidget.swift @@ -130,7 +130,12 @@ private struct AppDetailWidgetView: View .frame(maxWidth: .infinity, maxHeight: .infinity) } } - .widgetBackground(backgroundView(icon: entry.apps.first?.icon, tintColor: entry.apps.first?.tintColor)) + .widgetBackground( + backgroundView( + icon: entry.apps.first?.icon, + tintColor: entry.apps.first?.tintColor + ) + ) } } @@ -146,17 +151,35 @@ private extension AppDetailWidgetView let blurRadius = 5 as CGFloat let tintOpacity = 0.45 + // 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)! + return ZStack(alignment: .topTrailing) { // Blurred Image GeometryReader { geometry in ZStack { - Image(uiImage: icon) + Image(uiImage: resizedIcon) .resizable() .aspectRatio(contentMode: .fill) .frame(width: imageHeight, height: imageHeight, alignment: .center) .saturation(saturation) .blur(radius: blurRadius, opaque: true) .scaleEffect(geometry.size.width / imageHeight, anchor: .center) + // .onAppear { + // print("Geometry size: \(geometry.size)") + // print("Image height: \(imageHeight), Geometry width: \(geometry.size.width)") + // print("Icon size: \(icon.size)") + // } + Color(tintColor) .opacity(tintOpacity) @@ -176,13 +199,11 @@ private extension AppDetailWidgetView AppDetailWidget() } timeline: { let expiredDate = Date().addingTimeInterval(1 * 60 * 60 * 24 * 7) - let (altstore, delta, _, _, longDelta, _) = AppSnapshot.makePreviewSnapshots() - + let (altstore, _, _, longAltStore, _, _) = AppSnapshot.makePreviewSnapshots() AppsEntry(date: Date(), apps: [altstore]) - AppsEntry(date: Date(), apps: [delta]) - AppsEntry(date: Date(), apps: [longDelta]) + AppsEntry(date: Date(), apps: [longAltStore]) - AppsEntry(date: expiredDate, apps: [delta]) + AppsEntry(date: expiredDate, apps: [altstore]) AppsEntry(date: Date(), apps: []) AppsEntry(date: Date(), apps: [], isPlaceholder: true)