Skip to content

Commit

Permalink
Adopted containerBackground for iOS widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
nrubin29 committed May 15, 2024
1 parent 1ee6aae commit 64e52f0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 15 deletions.
30 changes: 24 additions & 6 deletions ios/FinaleWidget/Components/FinaleWidget.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import SwiftUI

func getWidgetBackgroundGradient(for themeColor: ThemeColor) -> LinearGradient {
return LinearGradient(gradient: Gradient(colors: [themeColor.gradientStart, themeColor.gradientEnd]), startPoint: .top, endPoint: .bottom)
}

func getLinkUrl(_ method: String, queryItems: [URLQueryItem]? = nil) -> URL {
var components = URLComponents()
components.scheme = "finale"
Expand All @@ -12,6 +8,29 @@ func getLinkUrl(_ method: String, queryItems: [URLQueryItem]? = nil) -> URL {
return components.url!
}

struct FinaleWidget<Content> : View where Content : View {
let themeColor: ThemeColor
@ViewBuilder let content: () -> Content

private var gradient: some View {
return LinearGradient(gradient: Gradient(colors: [themeColor.gradientStart, themeColor.gradientEnd]), startPoint: .top, endPoint: .bottom)
}

var body: some View {
if #available(iOS 17.0, *) {
content()
.containerBackground(for: .widget) {
gradient
}
} else {
ZStack {
gradient
content()
}
}
}
}

struct FinaleWidgetLarge<Content> : View where Content : View {
let title: String
let period: Period
Expand All @@ -21,8 +40,7 @@ struct FinaleWidgetLarge<Content> : View where Content : View {
@ViewBuilder let content: () -> Content

var body: some View {
ZStack {
getWidgetBackgroundGradient(for: themeColor)
FinaleWidget(themeColor: themeColor) {
VStack {
TitleBar(title: title, period: period, themeColor: themeColor)
if !isPreview && username?.isEmpty ?? true {
Expand Down
2 changes: 1 addition & 1 deletion ios/FinaleWidget/FinaleWidgets.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import SwiftUI

@available(iOS 14.0, *)
@available(iOS 15.0, *)
@main
struct FinaleWidgets: WidgetBundle {
@WidgetBundleBuilder
Expand Down
5 changes: 3 additions & 2 deletions ios/FinaleWidget/StatisticsWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ struct StatisticsWidgetEntryViewSmall : View {
var entry: StatisticsProvider.Entry

var body: some View {
ZStack {
getWidgetBackgroundGradient(for: entry.configuration.themeColor)
FinaleWidget(themeColor: entry.configuration.themeColor) {
if !entry.isPreview && entry.configuration.username?.isEmpty ?? true {
VStack {
Image("FinaleIcon")
Expand Down Expand Up @@ -128,6 +127,7 @@ struct StatisticsWidgetEntryViewLarge : View {
}
}

@available(iOS 15.0, *)
struct StatisticsWidget: Widget {
let kind: String = "StatisticsWidget"

Expand All @@ -138,6 +138,7 @@ struct StatisticsWidget: Widget {
.configurationDisplayName("Statistics")
.description("Your statistics for a given period.")
.supportedFamilies([.systemSmall, .systemMedium])
.contentMarginsDisabled()
}
}

Expand Down
13 changes: 12 additions & 1 deletion ios/FinaleWidget/TopEntitiesWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ struct TopEntitiesWidgetEntryViewSmall : View {
}
}

var body: some View {
var content: some View {
ZStack(alignment: .bottomLeading) {
EntityImage(imageUrl: imageUrl, entityType: entity?.type, size: .large)
.aspectRatio(contentMode: .fit)
Expand Down Expand Up @@ -172,6 +172,15 @@ struct TopEntitiesWidgetEntryViewSmall : View {
}
.widgetURL(entity != nil ? getLinkUrl(entity!) : nil)
}

var body: some View {
if #available(iOS 17.0, *) {
content
.containerBackground(for: .widget) { }
} else {
content
}
}
}

struct TopEntitiesWidgetEntryViewLarge : View {
Expand Down Expand Up @@ -233,6 +242,7 @@ struct TopEntitiesWidgetEntryViewLarge : View {
}
}

@available(iOS 15.0, *)
struct TopEntitiesWidget: Widget {
let kind: String = "TopEntitiesWidget"

Expand All @@ -242,6 +252,7 @@ struct TopEntitiesWidget: Widget {
}
.configurationDisplayName("Top Charts")
.description("Your top albums, artists, or tracks for a given period.")
.contentMarginsDisabled()
}
}

Expand Down
10 changes: 5 additions & 5 deletions macos/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ PODS:
- FlutterMacOS
- url_launcher_macos (0.0.1):
- FlutterMacOS
- wakelock_macos (0.0.1):
- wakelock_plus (0.0.1):
- FlutterMacOS

DEPENDENCIES:
Expand All @@ -39,7 +39,7 @@ DEPENDENCIES:
- shared_preferences_foundation (from `Flutter/ephemeral/.symlinks/plugins/shared_preferences_foundation/darwin`)
- sqflite (from `Flutter/ephemeral/.symlinks/plugins/sqflite/darwin`)
- url_launcher_macos (from `Flutter/ephemeral/.symlinks/plugins/url_launcher_macos/macos`)
- wakelock_macos (from `Flutter/ephemeral/.symlinks/plugins/wakelock_macos/macos`)
- wakelock_plus (from `Flutter/ephemeral/.symlinks/plugins/wakelock_plus/macos`)

EXTERNAL SOURCES:
flutter_local_notifications:
Expand All @@ -64,8 +64,8 @@ EXTERNAL SOURCES:
:path: Flutter/ephemeral/.symlinks/plugins/sqflite/darwin
url_launcher_macos:
:path: Flutter/ephemeral/.symlinks/plugins/url_launcher_macos/macos
wakelock_macos:
:path: Flutter/ephemeral/.symlinks/plugins/wakelock_macos/macos
wakelock_plus:
:path: Flutter/ephemeral/.symlinks/plugins/wakelock_plus/macos

SPEC CHECKSUMS:
flutter_local_notifications: 3805ca215b2fb7f397d78b66db91f6a747af52e4
Expand All @@ -79,7 +79,7 @@ SPEC CHECKSUMS:
shared_preferences_foundation: fcdcbc04712aee1108ac7fda236f363274528f78
sqflite: 673a0e54cc04b7d6dba8d24fb8095b31c3a99eec
url_launcher_macos: 5f437abeda8c85500ceb03f5c1938a8c5a705399
wakelock_macos: bc3f2a9bd8d2e6c89fee1e1822e7ddac3bd004a9
wakelock_plus: 4783562c9a43d209c458cb9b30692134af456269

PODFILE CHECKSUM: f123aa273a6e264bc3e7f1cdc07e8b0ccfdecac7

Expand Down

0 comments on commit 64e52f0

Please sign in to comment.