diff --git a/ruuvi-widgets/RuuviWidgets.swift b/ruuvi-widgets/RuuviWidgets.swift index fba90b869..703b0cf4a 100644 --- a/ruuvi-widgets/RuuviWidgets.swift +++ b/ruuvi-widgets/RuuviWidgets.swift @@ -27,7 +27,7 @@ struct RuuviWidgetEntryView: View { } else { UnauthorizedView() } - } + }.containerBackground() } } @@ -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) } } @@ -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)) diff --git a/ruuvi-widgets/View/SimpleWidgetView.swift b/ruuvi-widgets/View/SimpleWidgetView.swift index edf54061c..ab9b1605a 100644 --- a/ruuvi-widgets/View/SimpleWidgetView.swift +++ b/ruuvi-widgets/View/SimpleWidgetView.swift @@ -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 { @@ -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)) @@ -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) @@ -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) @@ -59,3 +65,13 @@ struct SimpleWidgetView: View { } } } + +extension EnvironmentValues { + var canShowWidgetContainerBackground: Bool { + if #available(iOSApplicationExtension 15.0, *) { + return self.showsWidgetContainerBackground + } else { + return false + } + } +}