Skip to content

Commit 75aeb20

Browse files
committed
extract to new POS button style
1 parent 073cdc6 commit 75aeb20

File tree

2 files changed

+67
-16
lines changed

2 files changed

+67
-16
lines changed

Modules/Sources/PointOfSale/Presentation/Reusable Views/Buttons/POSButtonStyle.swift

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import struct WooFoundation.IndefiniteCircularProgressViewStyle
55
enum POSButtonSize {
66
case normal
77
case extraSmall
8+
case compact
89
}
910

1011
/// Button state for different visual presentations
@@ -51,11 +52,37 @@ struct POSOutlinedButtonStyle: ButtonStyle {
5152
}
5253
}
5354

55+
/// Info card button style with default and primary variants.
56+
/// Use with .compact size for info card buttons.
57+
struct POSInfoCardButtonStyle: ButtonStyle {
58+
enum Variant {
59+
case `default`
60+
case primary
61+
}
62+
63+
private let size: POSButtonSize
64+
private let variant: Variant
65+
private let state: POSButtonState
66+
67+
init(size: POSButtonSize, variant: Variant, isLoading: Bool = false) {
68+
self.size = size
69+
self.variant = variant
70+
self.state = isLoading ? .loading : .idle
71+
}
72+
73+
func makeBody(configuration: Configuration) -> some View {
74+
let buttonVariant: POSButtonVariant = variant == .primary ? .filled : .infoCardOutlined
75+
return POSButtonStyleInternal(configuration: configuration, variant: buttonVariant, size: size, state: state)
76+
.disabled(state != .idle)
77+
}
78+
}
79+
5480

5581
/// The visual variant of the POS button.
5682
fileprivate enum POSButtonVariant {
5783
case filled
5884
case outlined
85+
case infoCardOutlined
5986
}
6087

6188
private struct POSButtonStyleInternal: View {
@@ -87,7 +114,7 @@ private struct POSButtonStyleInternal: View {
87114
.overlay(borderOverlay)
88115
// Makes the entire area tappable, otherwise the area with clear background is not tappable.
89116
.contentShape(Rectangle())
90-
.cornerRadius(Constants.cornerRadius)
117+
.cornerRadius(size.cornerRadius)
91118
.opacity(configuration.isPressed ? 0.7 : 1.0)
92119
.animation(.easeOut(duration: 0.15), value: configuration.isPressed)
93120
}
@@ -101,7 +128,7 @@ private struct POSButtonStyleInternal: View {
101128
content()
102129
Spacer()
103130
}
104-
case .extraSmall:
131+
case .extraSmall, .compact:
105132
content()
106133
}
107134
}
@@ -125,6 +152,8 @@ private struct POSButtonStyleInternal: View {
125152
state != .idle ? .posPrimaryContainer : .posDisabledContainer
126153
case (.outlined, _):
127154
.clear
155+
case (.infoCardOutlined, _):
156+
.posSurfaceContainerLowest
128157
}
129158
}
130159

@@ -138,15 +167,23 @@ private struct POSButtonStyleInternal: View {
138167
.posOnSurface
139168
case (.outlined, false):
140169
.posOnDisabledContainer
170+
case (.infoCardOutlined, true):
171+
.posOnSurface
172+
case (.infoCardOutlined, false):
173+
.posOnDisabledContainer
141174
}
142175
}
143176

144177
@ViewBuilder
145178
private var borderOverlay: some View {
146179
if variant == .outlined {
147-
RoundedRectangle(cornerRadius: Constants.cornerRadius)
180+
RoundedRectangle(cornerRadius: size.cornerRadius)
148181
.strokeBorder(isEnabled ? Color.posInverseSurface : .posDisabledContainer,
149182
lineWidth: Constants.borderStrokeWidth)
183+
} else if variant == .infoCardOutlined {
184+
RoundedRectangle(cornerRadius: size.cornerRadius)
185+
.strokeBorder(isEnabled ? Color.posOnSurface : .posDisabledContainer,
186+
lineWidth: Constants.borderStrokeWidth)
150187
}
151188
}
152189
}
@@ -169,6 +206,8 @@ private extension POSButtonSize {
169206
(vertical: POSPadding.large, horizontal: POSPadding.large)
170207
case .extraSmall:
171208
(vertical: POSPadding.small, horizontal: POSPadding.medium)
209+
case .compact:
210+
(vertical: POSPadding.small, horizontal: POSPadding.medium)
172211
}
173212
}
174213

@@ -178,6 +217,17 @@ private extension POSButtonSize {
178217
.posBodyLargeBold
179218
case .extraSmall:
180219
.posBodyMediumBold
220+
case .compact:
221+
.posBodySmallBold()
222+
}
223+
}
224+
225+
var cornerRadius: CGFloat {
226+
switch self {
227+
case .normal, .extraSmall:
228+
POSCornerRadiusStyle.medium.value
229+
case .compact:
230+
POSCornerRadiusStyle.small.value
181231
}
182232
}
183233
}
@@ -189,6 +239,8 @@ private extension POSButtonSize {
189239
(size: 32, lineWidth: 10)
190240
case .extraSmall:
191241
(size: 20, lineWidth: 6)
242+
case .compact:
243+
(size: 16, lineWidth: 4)
192244
}
193245
}
194246
}
@@ -257,6 +309,13 @@ struct POSButtonStyle_Previews: View {
257309
Button("Disabled Button") {}
258310
.buttonStyle(POSOutlinedButtonStyle(size: size))
259311
.disabled(true)
312+
case .infoCardOutlined:
313+
Button("Enabled Button") {}
314+
.buttonStyle(POSInfoCardButtonStyle(size: size, variant: .default))
315+
316+
Button("Disabled Button") {}
317+
.buttonStyle(POSInfoCardButtonStyle(size: size, variant: .default))
318+
.disabled(true)
260319
}
261320
}
262321
}

Modules/Sources/PointOfSale/Presentation/Settings/POSInformationCard.swift

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,11 @@ struct POSInformationCardFieldRow: View {
5757
Spacer()
5858

5959
if let buttonTitle, let buttonAction {
60-
Button(action: buttonAction) {
61-
Text(buttonTitle)
62-
.font(.posBodySmallBold())
63-
.foregroundStyle(buttonStyle == .default ? Color.posOnSurface : Color.posOnPrimaryContainer)
64-
}
65-
.buttonStyle(.plain)
66-
.padding(.horizontal, POSPadding.medium)
67-
.padding(.vertical, POSPadding.small)
68-
.background(buttonStyle == .primary ? Color.posPrimaryContainer : Color.posSurfaceContainerLowest)
69-
.overlay {
70-
RoundedRectangle(cornerRadius: POSCornerRadiusStyle.small.value)
71-
.stroke(buttonStyle == .default ? Color.posOnSurface : Color.posPrimaryContainer, lineWidth: 2)
72-
}
60+
Button(buttonTitle, action: buttonAction)
61+
.buttonStyle(POSInfoCardButtonStyle(
62+
size: .compact,
63+
variant: buttonStyle == .primary ? .primary : .default
64+
))
7365
}
7466
}
7567

0 commit comments

Comments
 (0)