Skip to content

Commit b42c4fc

Browse files
authored
Merge pull request #212 from virtusize/bugfix/NSDK-383-ui-state-fixes
NSDK-293: fix button height on error state
2 parents 41a663d + 8d8e17d commit b42c4fc

5 files changed

Lines changed: 67 additions & 8 deletions

File tree

CHANGES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ Use list notation, and following prefixes:
1111
- Docs - for any improvement to documentation
1212

1313
### Changes
14-
- Fix: issue with no size found text
14+
- Bugfix: set button height to zero on error
15+
- Bugfix: Virtusize button state when loading a new product
1516

1617
### 2.12.21
1718
- Bugfix: Added async Task cancellation to fix concurrency crash

Virtusize/Sources/SwiftUI/SwiftUIVirtusizeInPageMini.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ public struct SwiftUIVirtusizeInPageMini: View {
5656
uiKitView: uiView,
5757
defaultStyle: virtusizeDefaultStyle
5858
)
59-
.frame(width: desiredSize.width, height: max(desiredSize.height, 35), alignment: .center)
59+
.frame(width: desiredSize.width, height: desiredSize.height == 0 ? 0.1 : max(desiredSize.height, 35), alignment: .center)
60+
.opacity(desiredSize.height == 0 ? 0 : 1)
61+
.frame(height: desiredSize.height == 0 ? 0.1 : nil)
62+
.clipped()
6063
}
6164
}
6265

@@ -122,9 +125,12 @@ private struct VirtusizeInPageMiniWrapper: UIViewRepresentable {
122125

123126
uiView.setContentViewListener(listener: { view in
124127
if let label = (view as? VirtusizeInPageMini)?.inPageMiniMessageLabel {
128+
// Set height to 0 if product is invalid, there's an error, or view is hidden
129+
let calculatedHeight = label.text?.height(width: label.frame.width, font: label.font) ?? 35
130+
let height = (view.invalidProduct || view.isError || view.isHidden) ? 0 : calculatedHeight
125131
desiredSize = CGSize(
126132
width: UIScreen.main.bounds.size.width - uiView.userSetMargin * 2,
127-
height: label.text?.height(width: label.frame.width, font: label.font) ?? 35
133+
height: height
128134
)
129135
}
130136
})

Virtusize/Sources/SwiftUI/SwiftUIVirtusizeInPageStandard.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,16 @@ public struct SwiftUIVirtusizeInPageStandard: View {
5252
VirtusizeInPageStandardWrapper(
5353
product: product,
5454
desiredSize: $desiredSize,
55-
horizontalMargin: $horizontalMargin,
55+
horizontalMargin: $horizontalMargin,
5656
action: action,
5757
uiView: uiView,
5858
defaultStyle: virtusizeDefaultStyle
5959
)
60-
.offset(x: horizontalMargin)
61-
.frame(width: desiredSize.width, height: max(desiredSize.height, 92), alignment: .center)
60+
.offset(x: horizontalMargin)
61+
.frame(width: desiredSize.width, height: desiredSize.height == 0 ? 0.1 : max(desiredSize.height, 92), alignment: .center)
62+
.opacity(desiredSize.height == 0 ? 0 : 1)
63+
.frame(height: desiredSize.height == 0 ? 0.1 : nil)
64+
.clipped()
6265
}
6366
}
6467

@@ -133,9 +136,11 @@ private struct VirtusizeInPageStandardWrapper: UIViewRepresentable {
133136

134137
uiView.setContentViewListener(listener: { view in
135138
horizontalMargin = view.userSetMargin
139+
// Set height to 0 if product is invalid, there's an error, or view is hidden
140+
let height = (view.invalidProduct || view.isError || view.isHidden) ? 0 : view.frame.height
136141
desiredSize = CGSize(
137142
width: UIScreen.main.bounds.size.width,
138-
height: view.frame.height
143+
height: height
139144
)
140145
})
141146
}

Virtusize/Sources/UI/VirtusizeInPageMini.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,19 @@ public class VirtusizeInPageMini: VirtusizeInPageView {
162162
"messageAndButtonMargin": messageAndButtonMargin
163163
]
164164

165-
let horizontalConstraints = NSLayoutConstraint.constraints(
165+
var horizontalConstraints = NSLayoutConstraint.constraints(
166166
// swiftlint:disable:next line_length
167167
withVisualFormat: "H:|-horizontalMargin-[inPageMiniImageView]-0-[messageLabel]-(>=messageAndButtonMargin)-[sizeCheckButton]-horizontalMargin-|",
168168
options: NSLayoutConstraint.FormatOptions(rawValue: 0),
169169
metrics: metrics,
170170
views: views
171171
)
172+
// Lower the priority of leading and trailing constraints to prevent conflicts during initial layout
173+
for constraint in horizontalConstraints {
174+
if constraint.firstItem === self || constraint.secondItem === self {
175+
constraint.priority = UILayoutPriority(999)
176+
}
177+
}
172178

173179
let inPageMiniImageViewVerticalConstraints = NSLayoutConstraint.constraints(
174180
withVisualFormat: "V:|-(>=verticalMargin)-[inPageMiniImageView(18)]-(>=verticalMargin)-|",
@@ -267,6 +273,10 @@ public class VirtusizeInPageMini: VirtusizeInPageView {
267273
inPageMiniImageView.image = loading ? VirtusizeAssets.icon : nil
268274
inPageMiniMessageLabel.textColor = loading ? .vsGray900Color : .white
269275
setupTextsStyle(messageLabelIsBold: loading)
276+
277+
// Re-enable user interaction when loading valid product
278+
isUserInteractionEnabled = true
279+
270280
if loading {
271281
startLoadingTextAnimation(
272282
label: inPageMiniMessageLabel,

Virtusize/Sources/UI/VirtusizeInPageView.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public class VirtusizeInPageView: UIView, VirtusizeView, VirtusizeViewEventProto
7272
internal var isLoading: Bool = false
7373
internal var isError: Bool = false
7474
internal var invalidProduct: Bool = false
75+
private var heightConstraint: NSLayoutConstraint?
7576

7677
private func addSubviews() {
7778
// Add loading GIF image view
@@ -104,6 +105,19 @@ public class VirtusizeInPageView: UIView, VirtusizeView, VirtusizeViewEventProto
104105
isLoading = show
105106
loadingGifImageView.isHidden = !show
106107
contentContainerView.isHidden = show
108+
109+
// Set height to 0 for invalid product or error
110+
if invalidProduct || isError {
111+
if heightConstraint == nil {
112+
heightConstraint = heightAnchor.constraint(equalToConstant: 0)
113+
heightConstraint?.priority = .defaultHigh
114+
heightConstraint?.isActive = true
115+
}
116+
} else {
117+
// Remove height constraint for valid product
118+
heightConstraint?.isActive = false
119+
heightConstraint = nil
120+
}
107121
}
108122

109123
/// Add observers to listen to notification data from the sender (Virtusize.self)
@@ -155,6 +169,8 @@ public class VirtusizeInPageView: UIView, VirtusizeView, VirtusizeViewEventProto
155169
@objc internal func didReceiveProductCheckData(_ notification: Notification) {
156170
shouldUpdateProductCheckData(notification) { productWithPDCData in
157171
self.clientProduct = productWithPDCData
172+
self.invalidProduct = false
173+
self.isError = false
158174
showLoadingGif(false)
159175
setLoadingScreen(loading: true)
160176
isLoading = true
@@ -171,10 +187,18 @@ public class VirtusizeInPageView: UIView, VirtusizeView, VirtusizeViewEventProto
171187
@objc internal func didReceiveSizeRecommendationData(_ notification: Notification) {
172188
isLoading = false
173189
isError = false
190+
// Remove height constraint when data is successfully loaded
191+
heightConstraint?.isActive = false
192+
heightConstraint = nil
174193
}
175194

176195
@objc func productCheckDidFail(_ notification: Notification) {
177196
invalidProduct = true
197+
showLoadingGif(false)
198+
// Notify SwiftUI wrappers to update height
199+
DispatchQueue.main.async {
200+
self.contentViewListener?(self)
201+
}
178202
}
179203

180204
internal func shouldUpdateInPageRecommendation(
@@ -204,6 +228,16 @@ public class VirtusizeInPageView: UIView, VirtusizeView, VirtusizeViewEventProto
204228
/// A parent function for showing the error screen
205229
@objc internal func didReceiveInPageError(_ notification: Notification) {
206230
isError = true
231+
// Update height constraint for error state
232+
if heightConstraint == nil {
233+
heightConstraint = heightAnchor.constraint(equalToConstant: 0)
234+
heightConstraint?.priority = .defaultHigh
235+
heightConstraint?.isActive = true
236+
}
237+
// Notify SwiftUI wrappers to update height
238+
DispatchQueue.main.async {
239+
self.contentViewListener?(self)
240+
}
207241
}
208242

209243
/// Sets up the styles for the loading screen and the screen after finishing loading
@@ -212,6 +246,9 @@ public class VirtusizeInPageView: UIView, VirtusizeView, VirtusizeViewEventProto
212246
/// - loading: Pass true when it's loading, and pass false when finishing loading
213247
internal func setLoadingScreen(loading: Bool) {
214248
isError = false
249+
// Remove height constraint when loading valid product
250+
heightConstraint?.isActive = false
251+
heightConstraint = nil
215252
}
216253

217254
internal func setup() {}

0 commit comments

Comments
 (0)