Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task: Adopt new widgets API of Xcode 15 #1714 #1715

Merged
merged 1 commit into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 35 additions & 7 deletions ruuvi-widgets/RuuviWidgets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct RuuviWidgetEntryView: View {
} else {
UnauthorizedView()
}
}
}.containerBackground()
}
}

Expand All @@ -51,15 +51,40 @@ struct RuuviWidgets: Widget {
}

var body: some WidgetConfiguration {
IntentConfiguration(kind: kind,
intent: RuuviTagSelectionIntent.self,
provider: WidgetProvider()) { entry in
IntentConfiguration(
kind: kind,
intent: RuuviTagSelectionIntent.self,
provider: WidgetProvider()
) { entry in
RuuviWidgetEntryView(entry: entry)
.environment(\.locale, viewModel.locale())
}.configurationDisplayName(Constants.simpleWidgetDisplayName.rawValue)
.description(LocalizedStringKey("Widgets.Description.message"))
.supportedFamilies(supportedFamilies)
.contentMarginsDisabledIfAvailable()
}
}

extension WidgetConfiguration {
func contentMarginsDisabledIfAvailable() -> some WidgetConfiguration {
if #available(iOSApplicationExtension 17.0, *) {
return self.contentMarginsDisabled()
} else {
return self
}
}
}

extension View {
@ViewBuilder
func containerBackground() -> some View {
if #available(iOSApplicationExtension 17.0, *) {
self.containerBackground(for: .widget) {
Color.backgroundColor
}
} else {
self
}
.configurationDisplayName(Constants.simpleWidgetDisplayName.rawValue)
.description(LocalizedStringKey("Widgets.Description.message"))
.supportedFamilies(supportedFamilies)
}
}

Expand All @@ -68,6 +93,9 @@ struct RuuviWidgets_Previews: PreviewProvider {
if #available(iOSApplicationExtension 16.0, *) {
RuuviWidgetEntryView(entry: .placeholder())
.previewContext(WidgetPreviewContext(family: .accessoryCircular))
} else if #available(iOSApplicationExtension 17.0, *) {
RuuviWidgetEntryView(entry: .placeholder())
.previewContext(WidgetPreviewContext(family: .systemSmall))
} else {
RuuviWidgetEntryView(entry: .placeholder())
.previewContext(WidgetPreviewContext(family: .systemSmall))
Expand Down
30 changes: 23 additions & 7 deletions ruuvi-widgets/View/SimpleWidgetView.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import SwiftUI

struct SimpleWidgetView: View {
@Environment(\.canShowWidgetContainerBackground) private var canShowBackground
private let viewModel = WidgetViewModel()
var entry: WidgetProvider.Entry
var body: some View {
ZStack {
Color.backgroundColor.edgesIgnoringSafeArea(.all)
}
GeometryReader { geometry in
VStack {
HStack {
Expand All @@ -19,7 +17,11 @@ struct SimpleWidgetView: View {
Spacer()
Text(viewModel.measurementTime(from: entry))
.foregroundColor(Color.sensorNameColor1)
.font(.custom(Constants.muliRegular.rawValue, size: 10, relativeTo: .body))
.font(.custom(
Constants.muliRegular.rawValue,
size: canShowBackground ? 10 : 14,
relativeTo: .body
))
.minimumScaleFactor(0.5)
}.padding(EdgeInsets(top: 12, leading: 12, bottom: 0, trailing: 12))

Expand All @@ -29,7 +31,11 @@ struct SimpleWidgetView: View {
HStack {
Text(entry.tag.displayString)
.foregroundColor(Color.sensorNameColor1)
.font(.custom(Constants.muliBold.rawValue, size: 16, relativeTo: .headline))
.font(.custom(
Constants.muliBold.rawValue,
size: canShowBackground ? 16 : 22,
relativeTo: .headline)
)
.frame(maxWidth: .infinity, alignment: .leading)
.fixedSize(horizontal: false, vertical: true)
.minimumScaleFactor(0.5)
Expand All @@ -42,13 +48,13 @@ struct SimpleWidgetView: View {
.environment(\.locale, viewModel.locale())
.foregroundColor(.bodyTextColor)
.font(.custom(Constants.oswaldBold.rawValue,
size: 36,
size: canShowBackground ? 36 : 66,
relativeTo: .largeTitle))
.minimumScaleFactor(0.5)
Text(viewModel.getUnit(for: WidgetSensorEnum(rawValue: entry.config.sensor.rawValue)))
.foregroundColor(Color.unitTextColor)
.font(.custom(Constants.oswaldExtraLight.rawValue,
size: 16,
size: canShowBackground ? 16 : 24,
relativeTo: .title3))
.baselineOffset(14)
.minimumScaleFactor(0.5)
Expand All @@ -59,3 +65,13 @@ struct SimpleWidgetView: View {
}
}
}

extension EnvironmentValues {
var canShowWidgetContainerBackground: Bool {
if #available(iOSApplicationExtension 15.0, *) {
return self.showsWidgetContainerBackground
} else {
return false
}
}
}