From 99fe7e22ac6462621e0bdaf4fd02f2689cca96f1 Mon Sep 17 00:00:00 2001 From: Vasavi Kanamarlapudi Date: Fri, 19 Jul 2024 16:41:40 +0530 Subject: [PATCH] Digital ACT-191 ONEAPP-7013 story: fixed observed issue on content scroll while changing peek, gutter and layout. --- VDS/Components/Carousel/Carousel.swift | 44 +++++++++----------------- 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/VDS/Components/Carousel/Carousel.swift b/VDS/Components/Carousel/Carousel.swift index b7c0477a..3a754ae8 100644 --- a/VDS/Components/Carousel/Carousel.swift +++ b/VDS/Components/Carousel/Carousel.swift @@ -120,7 +120,7 @@ open class Carousel: View { open var peek: Peek = .standard { didSet { setNeedsUpdate() } } /// 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. open var slotAlignment: CarouselSlotAlignmentModel? = .init(vertical: .top, horizontal: .left) { didSet { setNeedsUpdate() } } @@ -277,12 +277,6 @@ open class Carousel: View { updatePaginationControls() 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. @@ -304,6 +298,12 @@ open class Carousel: View { nextButton.onClick = { _ in self.nextButtonClick() } 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. carouselScrollBar.onMoveForward = { [weak self] scrubberId in guard let self else { return } @@ -484,7 +484,7 @@ open class Carousel: View { break } } - minimumSlotWidth = minimumSlotWidth / CGFloat(layout.value) + minimumSlotWidth = ceil(minimumSlotWidth / CGFloat(layout.value)) } private func nextButtonClick() { @@ -533,27 +533,18 @@ open class Carousel: View { let isScrollbarSuppressed = views.count > 0 && layout.value == views.count let isPeekMinimumOnTablet = UIDevice.isIPad && peek == .minimum if !isScrollbarSuppressed { - let subpart = minimumSlotWidth + gutter.value - let xPosition = CGFloat( Float(position-1) * Float(layout.value) * Float(subpart)) - switch peek { - case .standard: - 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 - } + let slotWidthWithGutter = minimumSlotWidth + gutter.value + let xPosition = CGFloat( Float(position-1) * Float(layout.value) * Float(slotWidthWithGutter)) + let peekWidth = (containerView.frame.size.width - gutter.value - (Double(layout.value) * (minimumSlotWidth + gutter.value)))/2 + xPos = (peek == .none || isPeekMinimumOnTablet) ? xPosition : xPosition - gutter.value - peekWidth } } carouselScrollBar.scrubberId = position+1 let yPos = scrollView.contentOffset.y - scrollView.setContentOffset(CGPoint(x: xPos, y: yPos), animated: true) + scrollView.setContentOffset(CGPoint(x: xPos, y: yPos), animated: false) showPaginationControls() - onChangePublisher.send(position-1) + groupIndex = position-1 + onChangePublisher.send(groupIndex) } } @@ -571,9 +562,4 @@ extension Carousel: UIScrollViewDelegate { updateScrollbarPosition(targetContentOffsetXPos: targetContentOffset.pointee.x) } - public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { - var visibleRect = CGRect() - visibleRect.origin = scrollView.contentOffset - updateScrollbarPosition(targetContentOffsetXPos: visibleRect.origin.x) - } }