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

Paywalls: fix template 4 layout bug on iOS 16 #3381

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion RevenueCatUI/Modifiers/FooterHidingModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private struct FooterHidingModifier: ViewModifier {

case .condensedFooter:
content
.onSizeChange(.vertical) { if $0 > 0 { self.height = $0 } }
.onHeightChange { if $0 > 0 { self.height = $0 } }
.opacity(self.hide ? 0 : 1)
.offset(
y: self.hide
Expand Down
55 changes: 43 additions & 12 deletions RevenueCatUI/Modifiers/ViewExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,24 +192,40 @@ extension View {
.onPreferenceChange(ViewSizePreferenceKey.self, perform: closure)
}

/// Invokes the given closure with the dimension specified by `axis` changes whenever it changes.
func onSizeChange(
_ axis: Axis,
/// Invokes the given closure with the view width whenever it changes.
@ViewBuilder
func onWidthChange(
_ closure: @escaping (CGFloat) -> Void
) -> some View {
self
.overlay(
GeometryReader { geometry in
Color.clear
.preference(
key: ViewWidthPreferenceKey.self,
value: geometry.size.width
)
}
)
.onPreferenceChange(ViewWidthPreferenceKey.self, perform: closure)
}

/// Invokes the given closure with the view height whenever it changes.
@ViewBuilder
func onHeightChange(
_ closure: @escaping (CGFloat) -> Void
) -> some View {
self
.overlay(
GeometryReader { geometry in
Color.clear
.preference(
key: ViewDimensionPreferenceKey.self,
value: axis == .horizontal
? geometry.size.width
: geometry.size.height
key: ViewHeightPreferenceKey.self,
value: geometry.size.height
)
}
)
.onPreferenceChange(ViewDimensionPreferenceKey.self, perform: closure)
.onPreferenceChange(ViewHeightPreferenceKey.self, perform: closure)
}

}
Expand Down Expand Up @@ -272,14 +288,29 @@ private struct RoundedCorner: Shape {

// MARK: - Preference Keys

/// `PreferenceKey` for keeping track of a view dimension.
private struct ViewDimensionPreferenceKey: PreferenceKey {
@available(iOS 13.0, tvOS 13.0, macOS 10.15, watchOS 6.2, *)
private protocol ViewDimensionPreferenceKey: PreferenceKey where Value == CGFloat {}

typealias Value = CGFloat
/// `PreferenceKey` for keeping track of a view width.
private struct ViewWidthPreferenceKey: ViewDimensionPreferenceKey {

static var defaultValue: Value = 10

static func reduce(value: inout Value, nextValue: () -> Value) {
static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to move this static method to the protocol considering it's repeated between width and height?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

let newValue = max(value, nextValue())
if newValue != value {
value = newValue
}
}

}

/// `PreferenceKey` for keeping track of a view height.
private struct ViewHeightPreferenceKey: ViewDimensionPreferenceKey {

static var defaultValue: Value = 10

static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
let newValue = max(value, nextValue())
if newValue != value {
value = newValue
Expand Down
6 changes: 3 additions & 3 deletions RevenueCatUI/Templates/Template4View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ struct Template4View: TemplateViewType {
.scrollableIfNecessary(.horizontal)
.frame(height: self.packageContentHeight)
.frame(maxWidth: .infinity)
.onSizeChange(.horizontal) {
.onWidthChange {
self.containerWidth = $0
}
}
Expand Down Expand Up @@ -179,7 +179,7 @@ struct Template4View: TemplateViewType {
selected: false,
packageWidth: self.packageWidth,
desiredHeight: nil)
.onSizeChange(.vertical) {
.onHeightChange {
if $0 > self.packageContentHeight ?? 0 {
self.packageContentHeight = $0
}
Expand Down Expand Up @@ -374,7 +374,7 @@ private struct PackageButton: View {
.lineLimit(1)
.minimumScaleFactor(0.5)
.padding(.horizontal, 2)
.onSizeChange(.vertical) {
.onHeightChange {
self.discountLabelHeight = $0
}
.offset(
Expand Down