From 13e82e4953a464a3347c22976ceeba588b017276 Mon Sep 17 00:00:00 2001 From: Vinh Nguyen Date: Sat, 28 Oct 2023 09:22:57 +0700 Subject: [PATCH] Fix iOS 17 Widget background view modifier --- Clendar.xcodeproj/project.pbxproj | 6 +++++ .../Calendar Grid/CalendarGridWidget.swift | 2 +- .../Components/WidgetView+Extensions.swift | 22 +++++++++++++++++++ .../Widget/Date Info/DateInfoWidget.swift | 2 +- .../Event List/EventListWidgetView.swift | 2 +- .../Widget/Lunar/LunarDateInfoWidget.swift | 2 +- 6 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 ClendarWidget/Widget/Components/WidgetView+Extensions.swift diff --git a/Clendar.xcodeproj/project.pbxproj b/Clendar.xcodeproj/project.pbxproj index 93489193..31f46c3d 100644 --- a/Clendar.xcodeproj/project.pbxproj +++ b/Clendar.xcodeproj/project.pbxproj @@ -97,6 +97,8 @@ A371156E25C51AEB00D65F59 /* Color+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A37F0B2425C5190000DFFC41 /* Color+Extensions.swift */; }; A371157B25C51B9000D65F59 /* View+Gradient.swift in Sources */ = {isa = PBXBuildFile; fileRef = A371157A25C51B9000D65F59 /* View+Gradient.swift */; }; A371157C25C51B9000D65F59 /* View+Gradient.swift in Sources */ = {isa = PBXBuildFile; fileRef = A371157A25C51B9000D65F59 /* View+Gradient.swift */; }; + A3792E492AECA55C0059B3C8 /* WidgetView+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3792E482AECA55C0059B3C8 /* WidgetView+Extensions.swift */; }; + A3792E4A2AECA55C0059B3C8 /* WidgetView+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3792E482AECA55C0059B3C8 /* WidgetView+Extensions.swift */; }; A37B3B6F22506FB000D0488B /* Date+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A37B3B6E22506FB000D0488B /* Date+Extensions.swift */; }; A37D5F2327C4F96900495638 /* Shift in Frameworks */ = {isa = PBXBuildFile; productRef = A37D5F2227C4F96900495638 /* Shift */; }; A37F0AE425C4807500DFFC41 /* ClendarWidget.intentdefinition in Sources */ = {isa = PBXBuildFile; fileRef = A37F0AE725C4807500DFFC41 /* ClendarWidget.intentdefinition */; }; @@ -346,6 +348,7 @@ A368474C2554457B004BDCC6 /* Bundle+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Bundle+Extensions.swift"; sourceTree = ""; }; A36A504F2250EE6A00B12034 /* LoggerConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoggerConfiguration.swift; sourceTree = ""; }; A371157A25C51B9000D65F59 /* View+Gradient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "View+Gradient.swift"; sourceTree = ""; }; + A3792E482AECA55C0059B3C8 /* WidgetView+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WidgetView+Extensions.swift"; sourceTree = ""; }; A37B3B6E22506FB000D0488B /* Date+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Date+Extensions.swift"; sourceTree = ""; }; A37F0AE625C4807500DFFC41 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.intentdefinition; name = Base; path = Base.lproj/ClendarWidget.intentdefinition; sourceTree = ""; }; A37F0AF725C4807B00DFFC41 /* vi */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = vi; path = vi.lproj/ClendarWidget.strings; sourceTree = ""; }; @@ -825,6 +828,7 @@ isa = PBXGroup; children = ( A33F846525AF3404001324A0 /* WidgetBackgroundView.swift */, + A3792E482AECA55C0059B3C8 /* WidgetView+Extensions.swift */, ); path = Components; sourceTree = ""; @@ -1414,6 +1418,7 @@ A38A4D98254D73F400B35F98 /* Constants.swift in Sources */, A3064D47254CF15500C7F228 /* WidgetEntry.swift in Sources */, A3209BA7258CE315002470CF /* ViewModifiers.swift in Sources */, + A3792E4A2AECA55C0059B3C8 /* WidgetView+Extensions.swift in Sources */, A3C23FE027C9FC1D004838E8 /* Color+Theme.swift in Sources */, A38A4DCE254D752A00B35F98 /* WorkItem.swift in Sources */, A38A4DAB254D742B00B35F98 /* UserDefaults+Extensions.swift in Sources */, @@ -1474,6 +1479,7 @@ A30E48232541F4EF00B4B027 /* Utilities.swift in Sources */, A315F9FB25CF04A800E909DB /* ModalWrapperView.swift in Sources */, A367FBF3254DCA840059F6D7 /* SettingsConstants.swift in Sources */, + A3792E492AECA55C0059B3C8 /* WidgetView+Extensions.swift in Sources */, A3C2400027CA108C004838E8 /* ClendarPlusView.swift in Sources */, A3D6843725A9F467009C31D5 /* RatingManager.swift in Sources */, A31AFDB725CE6E2A0092D44D /* PurchaseButtonStyle.swift in Sources */, diff --git a/ClendarWidget/Widget/Calendar Grid/CalendarGridWidget.swift b/ClendarWidget/Widget/Calendar Grid/CalendarGridWidget.swift index 3cbbda13..bdb53229 100644 --- a/ClendarWidget/Widget/Calendar Grid/CalendarGridWidget.swift +++ b/ClendarWidget/Widget/Calendar Grid/CalendarGridWidget.swift @@ -17,7 +17,7 @@ struct CalendarGridWidget: Widget { ) { entry in CalendarGridWidgetView(entry: entry) .frame(maxWidth: .infinity, maxHeight: .infinity) - .background(WidgetBackgroundView()) + .widgetBackground(WidgetBackgroundView()) } .configurationDisplayName(NSLocalizedString("Calendar grid view", comment: "")) .description(NSLocalizedString("Month view calendar", comment: "")) diff --git a/ClendarWidget/Widget/Components/WidgetView+Extensions.swift b/ClendarWidget/Widget/Components/WidgetView+Extensions.swift new file mode 100644 index 00000000..f479c3c5 --- /dev/null +++ b/ClendarWidget/Widget/Components/WidgetView+Extensions.swift @@ -0,0 +1,22 @@ +// +// WidgetView+Extensions.swift +// Clendar +// +// Created by Vinh Nguyen on 28/10/2023. +// Copyright © 2023 Vinh Nguyen. All rights reserved. +// + +import SwiftUI + +// https://stackoverflow.com/a/76842922/1477298 +extension View { + func widgetBackground(_ backgroundView: some View) -> some View { + if #available(iOS 17, macCatalyst 17, *) { + return containerBackground(for: .widget) { + backgroundView + } + } else { + return background(backgroundView) + } + } +} diff --git a/ClendarWidget/Widget/Date Info/DateInfoWidget.swift b/ClendarWidget/Widget/Date Info/DateInfoWidget.swift index 89b841a5..7415115f 100644 --- a/ClendarWidget/Widget/Date Info/DateInfoWidget.swift +++ b/ClendarWidget/Widget/Date Info/DateInfoWidget.swift @@ -23,7 +23,7 @@ struct DateInfoWidget: Widget { provider: DateInfoWidgetTimelineProvider()) { entry in DateInfoWidgetEntryView(entry: entry) .frame(maxWidth: .infinity, maxHeight: .infinity) - .background(WidgetBackgroundView()) + .widgetBackground(WidgetBackgroundView()) } .configurationDisplayName(NSLocalizedString("Date Info Widget", comment: "")) .description(NSLocalizedString("Check calendar at a glance", comment: "")) diff --git a/ClendarWidget/Widget/Event List/EventListWidgetView.swift b/ClendarWidget/Widget/Event List/EventListWidgetView.swift index 8d477d13..a2136f1c 100644 --- a/ClendarWidget/Widget/Event List/EventListWidgetView.swift +++ b/ClendarWidget/Widget/Event List/EventListWidgetView.swift @@ -16,7 +16,7 @@ struct EventListWidget: Widget { provider: DateInfoWidgetTimelineProvider()) { entry in EventsListWidgetView(entry: entry, minimizeContents: true) .frame(maxWidth: .infinity, maxHeight: .infinity) - .background(WidgetBackgroundView()) + .widgetBackground(WidgetBackgroundView()) } .configurationDisplayName(NSLocalizedString("Event List Widget", comment: "")) .description(NSLocalizedString("Your day events at a glance", comment: "")) diff --git a/ClendarWidget/Widget/Lunar/LunarDateInfoWidget.swift b/ClendarWidget/Widget/Lunar/LunarDateInfoWidget.swift index ca4be827..340c2ca2 100644 --- a/ClendarWidget/Widget/Lunar/LunarDateInfoWidget.swift +++ b/ClendarWidget/Widget/Lunar/LunarDateInfoWidget.swift @@ -17,7 +17,7 @@ struct LunarDateInfoWidget: Widget { ) { entry in LunarSmallDateWidgetView(entry: entry) .frame(maxWidth: .infinity, maxHeight: .infinity) - .background(WidgetBackgroundView()) + .widgetBackground(WidgetBackgroundView()) } .configurationDisplayName(NSLocalizedString("Lunar Date", comment: "")) .description(NSLocalizedString("Lunar calendar at a glance", comment: ""))