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

[Do not merge][Paywalls] Add Badge Modifier #4596

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ struct StackComponentView: View {
// Without compositingGroup(), the shadow is applied to the stack's children as well.
view.compositingGroup().shadow(shadow: shadow)
}
.badge(style.badge, textComponentViewModel: viewModel.badgeTextViewModel)
.padding(style.margin)
}

Expand Down Expand Up @@ -455,7 +456,8 @@ fileprivate extension StackComponentViewModel {

try self.init(
component: component,
viewModels: viewModels
viewModels: viewModels,
localizationProvider: localizationProvider
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,35 @@ class StackComponentViewModel {
private let component: PaywallComponent.StackComponent
private let presentedOverrides: PresentedOverrides<PresentedStackPartial>?

let badgeTextViewModel: TextComponentViewModel?
let viewModels: [PaywallComponentViewModel]

init(
component: PaywallComponent.StackComponent,
viewModels: [PaywallComponentViewModel]
viewModels: [PaywallComponentViewModel],
localizationProvider: LocalizationProvider
) throws {
self.component = component
self.viewModels = viewModels

if let badge = component.badge {
badgeTextViewModel = try TextComponentViewModel(
Copy link
Member Author

Choose a reason for hiding this comment

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

Initialising the TextComponentViewModel of the badge in the StackComponentViewModel constructor so we can throw if necessary.

localizationProvider: localizationProvider,
component: PaywallComponent.TextComponent(
text: badge.textLid,
fontName: badge.fontName,
fontWeight: badge.fontWeight,
color: badge.color,
padding: badge.padding,
margin: .zero,
fontSize: badge.fontSize,
horizontalAlignment: badge.horizontalAlignment
)
)
} else {
badgeTextViewModel = nil
}

self.presentedOverrides = try self.component.overrides?.toPresentedOverrides { $0 }
}

Expand Down Expand Up @@ -60,7 +80,8 @@ class StackComponentViewModel {
margin: partial?.margin ?? self.component.margin,
shape: partial?.shape ?? self.component.shape,
border: partial?.border ?? self.component.border,
shadow: partial?.shadow ?? self.component.shadow
shadow: partial?.shadow ?? self.component.shadow,
badge: partial?.badge ?? self.component.badge
)

apply(style)
Expand Down Expand Up @@ -105,6 +126,7 @@ struct StackComponentStyle {
let shape: ShapeModifier.Shape?
let border: ShapeModifier.BorderInfo?
let shadow: ShadowModifier.ShadowInfo?
let badge: BadgeModifier.BadgeInfo?

init(
visible: Bool,
Expand All @@ -116,7 +138,8 @@ struct StackComponentStyle {
margin: PaywallComponent.Padding,
shape: PaywallComponent.Shape?,
border: PaywallComponent.Border?,
shadow: PaywallComponent.Shadow?
shadow: PaywallComponent.Shadow?,
badge: PaywallComponent.Badge?
) {
self.visible = visible
self.dimension = dimension
Expand All @@ -128,6 +151,7 @@ struct StackComponentStyle {
self.shape = shape?.shape
self.border = border?.border
self.shadow = shadow?.shadow
self.badge = badge?.badge(stackShape: self.shape)
Copy link
Member Author

Choose a reason for hiding this comment

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

The stack shape is only used to calculate the the corner radius of the edge-to-edge leading/trailing style badge, and make it match the parent stack.

}

var vstackStrategy: StackStrategy {
Expand Down Expand Up @@ -163,7 +187,7 @@ struct StackComponentStyle {
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
private extension PaywallComponent.Shape {

var shape: ShapeModifier.Shape? {
var shape: ShapeModifier.Shape {
switch self {
case .rectangle(let cornerRadiuses):
let corners = cornerRadiuses.flatMap { cornerRadiuses in
Expand Down Expand Up @@ -208,4 +232,27 @@ private extension PaywallComponent.Shadow {

}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
private extension PaywallComponent.Badge {

func badge(stackShape: ShapeModifier.Shape?) -> BadgeModifier.BadgeInfo? {
BadgeModifier.BadgeInfo(
style: self.style,
alignment: self.alignment,
shape: self.shape.shape,
padding: self.padding,
margin: self.margin,
textLid: self.textLid,
fontName: self.fontName,
fontWeight: self.fontWeight,
fontSize: self.fontSize,
horizontalAlignment: self.horizontalAlignment,
color: self.color,
backgroundColor: self.backgroundColor,
stackShape: stackShape
)
}

}

#endif
Loading