Skip to content

Commit

Permalink
Paywalls: fix template 5 scrolling on iOS 15 (#3608)
Browse files Browse the repository at this point in the history
The previous implementation used `ScrollableIfNecessaryModifier` which
is a hacky work around for iOS 15 not having `ViewThatFits`.

To work around issues with that, specific to template 5, this new
approach avoids the "if necessary" part on iOS 15 which removes the
problematic usage of `GeometryReader`.
  • Loading branch information
NachoSoto authored Jan 29, 2024
1 parent a572fc8 commit cf1885f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 25 deletions.
68 changes: 47 additions & 21 deletions RevenueCatUI/Modifiers/ViewExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
//
// Created by Nacho Soto on 7/13/23.

// swiftlint:disable file_length

import Foundation
import SwiftUI

Expand Down Expand Up @@ -53,7 +55,7 @@ extension View {

// MARK: - Scrolling

@available(iOS 13.0, tvOS 13.0, macOS 10.15, watchOS 6.2, *)
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
extension View {

@ViewBuilder
Expand Down Expand Up @@ -83,7 +85,6 @@ extension View {
#endif
}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
@ViewBuilder
func scrollableIfNecessary(_ axis: Axis = .vertical, enabled: Bool = true) -> some View {
if enabled {
Expand All @@ -102,6 +103,48 @@ extension View {
self
}
}

/// Equivalent to `scrollableIfNecessary` except that it's always scrollable on iOS 15
/// to work around issues with that iOS 15 implementation in some instances.
@ViewBuilder
func scrollableIfNecessaryWhenAvailable(_ axis: Axis = .vertical, enabled: Bool = true) -> some View {
if enabled {
if #available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *) {
ViewThatFits(in: axis.scrollViewAxis) {
self

ScrollView(axis.scrollViewAxis) {
self
}
}
} else {
self
.centeredContent(axis)
.scrollable(if: enabled)
}
} else {
self
}
}

@ViewBuilder
fileprivate func centeredContent(_ axis: Axis) -> some View {
switch axis {
case .horizontal:
HStack {
Spacer()
self
Spacer()
}
case .vertical:
VStack {
Spacer()
self
Spacer()
}
}
}

}

// MARK: - Padding
Expand Down Expand Up @@ -163,7 +206,8 @@ private struct ScrollableIfNecessaryModifier: ViewModifier {

func body(content: Content) -> some View {
GeometryReader { geometry in
self.centeredContent(content)
content
.centeredContent(self.axis)
.background(
GeometryReader { contentGeometry in
Color.clear
Expand All @@ -181,24 +225,6 @@ private struct ScrollableIfNecessaryModifier: ViewModifier {
.scrollable(self.axis.scrollViewAxis, if: self.overflowing)
}

@ViewBuilder
private func centeredContent(_ content: Content) -> some View {
switch self.axis {
case .horizontal:
HStack {
Spacer()
content
Spacer()
}
case .vertical:
VStack {
Spacer()
content
Spacer()
}
}
}

}

// MARK: - Size changes
Expand Down
6 changes: 3 additions & 3 deletions RevenueCatUI/Templates/Template5View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ struct Template5View: TemplateViewType {
self.features
}
.padding(.top, self.defaultVerticalPaddingLength)
.scrollableIfNecessary()
.scrollableIfNecessaryWhenAvailable()

self.packages
.padding(.top, self.defaultVerticalPaddingLength)
.scrollableIfNecessary()
.scrollableIfNecessaryWhenAvailable()
}

Spacer()
Expand All @@ -108,7 +108,7 @@ struct Template5View: TemplateViewType {
// Compensate for additional padding on condensed mode + iPad
: self.defaultVerticalPaddingLength.map { $0 * -1 }
)
.scrollableIfNecessary(enabled: self.configuration.mode.isFullScreen)
.scrollableIfNecessaryWhenAvailable(enabled: self.configuration.mode.isFullScreen)
}

if self.configuration.mode.shouldDisplayInlineOfferDetails(displayingAllPlans: self.displayingAllPlans) {
Expand Down
2 changes: 1 addition & 1 deletion Tests/purchases-ios-snapshots-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
07f89b6e14d285d8b4534af32ba2f25935105a18
04bf0f2f07f042f642f50df0c876e805120c6e71

0 comments on commit cf1885f

Please sign in to comment.