Digital ACT-191 ONEAPP-7013 story: fixed observed issue on content scroll while changing peek, gutter and layout.

This commit is contained in:
Vasavi Kanamarlapudi 2024-07-19 16:41:40 +05:30
parent 938364535b
commit 99fe7e22ac

View File

@ -120,7 +120,7 @@ open class Carousel: View {
open var peek: Peek = .standard { didSet { setNeedsUpdate() } } open var peek: Peek = .standard { didSet { setNeedsUpdate() } }
/// The initial visible slide's index in the carousel. /// The initial visible slide's index in the carousel.
open var groupIndex: Int? { didSet { setNeedsUpdate() } } open var groupIndex: Int = 0 { didSet { setNeedsUpdate() } }
/// If provided, will set the alignment for slot content when the slots has different heights. /// If provided, will set the alignment for slot content when the slots has different heights.
open var slotAlignment: CarouselSlotAlignmentModel? = .init(vertical: .top, horizontal: .left) { didSet { setNeedsUpdate() } } open var slotAlignment: CarouselSlotAlignmentModel? = .init(vertical: .top, horizontal: .left) { didSet { setNeedsUpdate() } }
@ -277,12 +277,6 @@ open class Carousel: View {
updatePaginationControls() updatePaginationControls()
addCarouselSlots() addCarouselSlots()
// If groupIndex is received, the carousel should update its position.
if let groupIndex {
let totalPos = totalPositions()
carouselScrollBar.position = groupIndex >= totalPos ? totalPos : groupIndex+1
}
} }
/// Resets to default settings. /// Resets to default settings.
@ -304,6 +298,12 @@ open class Carousel: View {
nextButton.onClick = { _ in self.nextButtonClick() } nextButton.onClick = { _ in self.nextButtonClick() }
previousButton.onClick = { _ in self.previousButtonClick() } previousButton.onClick = { _ in self.previousButtonClick() }
/// Will be called when the scrubber position changes.
carouselScrollBar.onScrubberDrag = { [weak self] scrubberId in
guard let self else { return }
updateScrollPosition(position: scrubberId, callbackText:"onThumbPositionChange")
}
/// Will be called when the scrollbar thumb move forward. /// Will be called when the scrollbar thumb move forward.
carouselScrollBar.onMoveForward = { [weak self] scrubberId in carouselScrollBar.onMoveForward = { [weak self] scrubberId in
guard let self else { return } guard let self else { return }
@ -484,7 +484,7 @@ open class Carousel: View {
break break
} }
} }
minimumSlotWidth = minimumSlotWidth / CGFloat(layout.value) minimumSlotWidth = ceil(minimumSlotWidth / CGFloat(layout.value))
} }
private func nextButtonClick() { private func nextButtonClick() {
@ -533,27 +533,18 @@ open class Carousel: View {
let isScrollbarSuppressed = views.count > 0 && layout.value == views.count let isScrollbarSuppressed = views.count > 0 && layout.value == views.count
let isPeekMinimumOnTablet = UIDevice.isIPad && peek == .minimum let isPeekMinimumOnTablet = UIDevice.isIPad && peek == .minimum
if !isScrollbarSuppressed { if !isScrollbarSuppressed {
let subpart = minimumSlotWidth + gutter.value let slotWidthWithGutter = minimumSlotWidth + gutter.value
let xPosition = CGFloat( Float(position-1) * Float(layout.value) * Float(subpart)) let xPosition = CGFloat( Float(position-1) * Float(layout.value) * Float(slotWidthWithGutter))
switch peek { let peekWidth = (containerView.frame.size.width - gutter.value - (Double(layout.value) * (minimumSlotWidth + gutter.value)))/2
case .standard: xPos = (peek == .none || isPeekMinimumOnTablet) ? xPosition : xPosition - gutter.value - peekWidth
if UIDevice.isIPad {
xPos = xPosition - (minimumSlotWidth/(CGFloat(layout.value) + 3))/2
} else if layout == .oneUP {
xPos = xPosition - gutter.value - (minimumSlotWidth/4)/2
}
case .minimum:
xPos = isPeekMinimumOnTablet ? xPosition : xPosition - peekMinimum
case .none:
xPos = xPosition
}
} }
} }
carouselScrollBar.scrubberId = position+1 carouselScrollBar.scrubberId = position+1
let yPos = scrollView.contentOffset.y let yPos = scrollView.contentOffset.y
scrollView.setContentOffset(CGPoint(x: xPos, y: yPos), animated: true) scrollView.setContentOffset(CGPoint(x: xPos, y: yPos), animated: false)
showPaginationControls() showPaginationControls()
onChangePublisher.send(position-1) groupIndex = position-1
onChangePublisher.send(groupIndex)
} }
} }
@ -571,9 +562,4 @@ extension Carousel: UIScrollViewDelegate {
updateScrollbarPosition(targetContentOffsetXPos: targetContentOffset.pointee.x) updateScrollbarPosition(targetContentOffsetXPos: targetContentOffset.pointee.x)
} }
public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
var visibleRect = CGRect()
visibleRect.origin = scrollView.contentOffset
updateScrollbarPosition(targetContentOffsetXPos: visibleRect.origin.x)
}
} }