Skip to content

Commit

Permalink
Add BadgeModifier
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkVillacampa committed Dec 16, 2024
1 parent fc4b37b commit 782ab80
Show file tree
Hide file tree
Showing 7 changed files with 521 additions and 16 deletions.
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 @@ -23,16 +23,36 @@ 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(
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(parentShape: self.shape)
}

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(parentShape: 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,
parentShape: parentShape
)
}

}

#endif
Loading

0 comments on commit 782ab80

Please sign in to comment.