Digital ACT-191 ONEAPP-7013 story: refactored code

This commit is contained in:
Vasavi Kanamarlapudi 2024-07-02 14:47:20 +05:30
parent 668b20f77b
commit 8619c64109

View File

@ -187,9 +187,7 @@ open class Carousel: View {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Private Properties // MARK: - Private Properties
//-------------------------------------------------- //--------------------------------------------------
// Sizes are from InVision design specs. internal var containerSize: CGSize { CGSize(width: frame.size.width, height: 44) }
internal var containerSize: CGSize { CGSize(width: 320, height: 44) }
private let contentStackView = UIStackView().with { private let contentStackView = UIStackView().with {
$0.translatesAutoresizingMaskIntoConstraints = false $0.translatesAutoresizingMaskIntoConstraints = false
$0.axis = .vertical $0.axis = .vertical
@ -268,18 +266,21 @@ open class Carousel: View {
var peekMinimum = 24.0 var peekMinimum = 24.0
var minimumSlotWidth = 0.0 var minimumSlotWidth = 0.0
var carouselScrollbarMinWidth = 96.0 var carouselScrollbarMinWidth = 96.0
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Lifecycle // MARK: - Lifecycle
//-------------------------------------------------- //--------------------------------------------------
/// Executed on initialization for this View.
open override func initialSetup() { open override func initialSetup() {
super.initialSetup() super.initialSetup()
} }
/// Called once when a view is initialized and is used to Setup additional UI or other constants and configurations.
open override func setup() { open override func setup() {
super.setup() super.setup()
isAccessibilityElement = false isAccessibilityElement = false
// add containerView // Add containerView
addSubview(containerView) addSubview(containerView)
containerView containerView
.pinTop() .pinTop()
@ -287,18 +288,19 @@ open class Carousel: View {
.pinLeadingGreaterThanOrEqualTo() .pinLeadingGreaterThanOrEqualTo()
.pinTrailing() .pinTrailing()
.heightGreaterThanEqualTo(containerSize.height) .heightGreaterThanEqualTo(containerSize.height)
containerView.centerYAnchor.constraint(equalTo: centerYAnchor).activate() containerView.centerYAnchor.constraint(equalTo: centerYAnchor).activate()
containerLeadingConstraint = containerView.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor, constant: 0) containerLeadingConstraint = containerView.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor, constant: 0)
containerLeadingConstraint?.activate() containerLeadingConstraint?.activate()
// add content stackview // Add content stackview
containerView.addSubview(contentStackView) containerView.addSubview(contentStackView)
// add scrollview // Add scrollview
scrollContainerView.addSubview(scrollView) scrollContainerView.addSubview(scrollView)
scrollView.pinToSuperView() scrollView.pinToSuperView()
// add pagination button icons // Add pagination button icons
scrollContainerView.addSubview(previousButton) scrollContainerView.addSubview(previousButton)
previousButton previousButton
.pinLeadingGreaterThanOrEqualTo() .pinLeadingGreaterThanOrEqualTo()
@ -309,7 +311,7 @@ open class Carousel: View {
.pinTrailingLessThanOrEqualTo() .pinTrailingLessThanOrEqualTo()
.pinCenterY() .pinCenterY()
// add scroll container view & carousel scrollbar // Add scroll container view & carousel scrollbar
contentStackView.addArrangedSubview(scrollContainerView) contentStackView.addArrangedSubview(scrollContainerView)
contentStackView.addArrangedSubview(carouselScrollBar) contentStackView.addArrangedSubview(carouselScrollBar)
contentStackView.setCustomSpacing(scrollbarTopSpace, after: scrollContainerView) contentStackView.setCustomSpacing(scrollbarTopSpace, after: scrollContainerView)
@ -323,6 +325,7 @@ open class Carousel: View {
addlisteners() addlisteners()
} }
/// Used to make changes to the View based off a change events or from local properties.
open override func updateView() { open override func updateView() {
super.updateView() super.updateView()
@ -368,6 +371,7 @@ open class Carousel: View {
addCarouselSlots() addCarouselSlots()
} }
/// Resets to default settings.
open override func reset() { open override func reset() {
super.reset() super.reset()
shouldUpdateView = false shouldUpdateView = false
@ -384,19 +388,69 @@ open class Carousel: View {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Private Methods // MARK: - Private Methods
//-------------------------------------------------- //--------------------------------------------------
private func addlisteners() {
nextButton.onClick = { _ in self.nextButtonClick() }
previousButton.onClick = { _ in self.previousButtonClick() }
/// Will be called when the thumb move forward.
carouselScrollBar.onMoveForward = { [weak self] scrubberId in
guard let self else { return }
updateScrollPosition(position: scrubberId, callbackText:"onMoveForward")
}
/// Will be called when the thumb move backward.
carouselScrollBar.onMoveBackward = { [weak self] scrubberId in
guard let self else { return }
updateScrollPosition(position: scrubberId, callbackText:"onMoveBackward")
}
/// Will be called when the thumb touch start.
carouselScrollBar.onThumbTouchStart = { [weak self] scrubberId in
guard let self else { return }
updateScrollPosition(position: scrubberId, callbackText:"onThumbTouchStart")
}
/// Will be called when the thumb touch end.
carouselScrollBar.onThumbTouchEnd = { [weak self] scrubberId in
guard let self else { return }
updateScrollPosition(position: scrubberId, callbackText:"onThumbTouchEnd")
}
}
private func updatePaginationControls() {
containerView.surface = surface
showPaginationControls()
previousButton.kind = pagination.kind
previousButton.floating = pagination.floating
nextButton.kind = pagination.kind
nextButton.floating = pagination.floating
previousButton.surface = surface
nextButton.surface = surface
}
private func showPaginationControls() {
if carouselScrollBar.numberOfSlides == _layout.value {
previousButton.isHidden = true
nextButton.isHidden = true
} else {
previousButton.isHidden = (carouselScrollBar.position == 1) || (paginationDisplay == .none)
nextButton.isHidden = (carouselScrollBar.position == totalPositions()) || (paginationDisplay == .none)
}
}
private func addCarouselSlots() { private func addCarouselSlots() {
getSlotWidth() getSlotWidth()
if containerView.frame.size.width > 0 { if containerView.frame.size.width > 0 {
containerViewHeightConstraint?.isActive = false containerViewHeightConstraint?.isActive = false
containerStackHeightConstraint?.isActive = false containerStackHeightConstraint?.isActive = false
// perform a loop to iterate each subView // Perform a loop to iterate each subView
scrollView.subviews.forEach { subView in scrollView.subviews.forEach { subView in
// removing subView from its parent view // Removing subView from its parent view
subView.removeFromSuperview() subView.removeFromSuperview()
} }
// add carousel items // Add carousel items
if data.count > 0 { if data.count > 0 {
var xPos = 0.0 var xPos = 0.0
for x in 0...data.count - 1 { for x in 0...data.count - 1 {
@ -433,6 +487,44 @@ open class Carousel: View {
} }
} }
private func getSlotWidth() {
let actualWidth = containerView.frame.size.width
let isScrollbarSuppressed = data.count > 0 && layout.value == data.count
let isPeekMinimumOnTablet = UIDevice.isIPad && peek == .minimum
let isPeekNone: Bool = peek == .none
minimumSlotWidth = isScrollbarSuppressed || isPeekMinimumOnTablet || isPeekNone ? actualWidth - ((CGFloat(layout.value)-1) * gutter.value): actualWidth - (CGFloat(layout.value) * gutter.value)
if !isScrollbarSuppressed {
switch peek {
case .standard:
// Standard(Default) Peek - Supported for all Tablet viewports and layouts. Supported only for 1up layouts on Mobile viewports.
if UIDevice.isIPad {
minimumSlotWidth = minimumSlotWidth - (minimumSlotWidth/(CGFloat(layout.value) + 3))
} else if layout == .oneUP {
minimumSlotWidth = minimumSlotWidth - (minimumSlotWidth/4)
}
case .minimum:
// Peek Mimumum Width: 24px from edge of container (at the default view of the carousel with one peek visible)
// Minimum (Mobile only) Supported only on Mobile viewports. If a user passes Minimum for tablet carousel, the peek reverts to Standard.
minimumSlotWidth = isPeekMinimumOnTablet ? minimumSlotWidth : minimumSlotWidth - peekMinimum
case .none:
break
}
}
minimumSlotWidth = minimumSlotWidth / CGFloat(layout.value)
}
private func nextButtonClick() {
carouselScrollBar.position = carouselScrollBar.position+1
showPaginationControls()
updateScrollPosition(position: carouselScrollBar.position, callbackText:"pageControlClicks")
}
private func previousButtonClick() {
carouselScrollBar.position = carouselScrollBar.position-1
showPaginationControls()
updateScrollPosition(position: carouselScrollBar.position, callbackText:"pageControlClicks")
}
private func ratioSize(for width: CGFloat) -> CGSize { private func ratioSize(for width: CGFloat) -> CGSize {
var height: CGFloat = width var height: CGFloat = width
@ -463,47 +555,7 @@ open class Carousel: View {
return CGSize(width: width, height: height) return CGSize(width: width, height: height)
} }
func addlisteners() { private func updatePaginationInset() {
nextButton.onClick = { _ in self.nextButtonClick() }
previousButton.onClick = { _ in self.previousButtonClick() }
/// will be called when the thumb move forward.
carouselScrollBar.onMoveForward = { [weak self] scrubberId in
guard let self else { return }
updateScrollPosition(position: scrubberId, callbackText:"onMoveForward")
}
/// will be called when the thumb move backward.
carouselScrollBar.onMoveBackward = { [weak self] scrubberId in
guard let self else { return }
updateScrollPosition(position: scrubberId, callbackText:"onMoveBackward")
}
/// will be called when the thumb touch start.
carouselScrollBar.onThumbTouchStart = { [weak self] scrubberId in
guard let self else { return }
updateScrollPosition(position: scrubberId, callbackText:"onThumbTouchStart")
}
/// will be called when the thumb touch end.
carouselScrollBar.onThumbTouchEnd = { [weak self] scrubberId in
guard let self else { return }
updateScrollPosition(position: scrubberId, callbackText:"onThumbTouchEnd")
}
}
func updatePaginationControls() {
containerView.surface = surface
showPaginationControls()
previousButton.kind = pagination.kind
previousButton.floating = pagination.floating
nextButton.kind = pagination.kind
nextButton.floating = pagination.floating
previousButton.surface = surface
nextButton.surface = surface
}
func updatePaginationInset() {
prevButtonLeadingConstraint?.isActive = false prevButtonLeadingConstraint?.isActive = false
nextButtonTrailingConstraint?.isActive = false nextButtonTrailingConstraint?.isActive = false
prevButtonLeadingConstraint = previousButton.leadingAnchor.constraint(equalTo: scrollContainerView.leadingAnchor, constant: paginationInset) prevButtonLeadingConstraint = previousButton.leadingAnchor.constraint(equalTo: scrollContainerView.leadingAnchor, constant: paginationInset)
@ -512,45 +564,18 @@ open class Carousel: View {
nextButtonTrailingConstraint?.isActive = true nextButtonTrailingConstraint?.isActive = true
} }
func getSlotWidth() { private func updateScrollbarPosition(targetContentOffsetXPos:CGFloat) {
let actualWidth = containerView.frame.size.width let scrollContentSizeWidth = scrollView.contentSize.width
let isScrollbarSuppressed = data.count > 0 && layout.value == data.count let totalPositions = totalPositions()
let isPeekMinimumOnTablet = UIDevice.isIPad && peek == .minimum let layoutSpace = Int (floor( Double(scrollContentSizeWidth / Double(totalPositions))))
let isPeekNone: Bool = peek == .none let remindSpace = Int(targetContentOffsetXPos) % layoutSpace
minimumSlotWidth = isScrollbarSuppressed || isPeekMinimumOnTablet || isPeekNone ? actualWidth - ((CGFloat(layout.value)-1) * gutter.value): actualWidth - (CGFloat(layout.value) * gutter.value) var contentPos = (Int(targetContentOffsetXPos) / layoutSpace) + 1
if !isScrollbarSuppressed { contentPos = remindSpace > layoutSpace/2 ? contentPos+1 : contentPos
switch peek { carouselScrollBar.position = contentPos
case .standard: updateScrollPosition(position: contentPos, callbackText: "ScrollViewMoved")
// Standard(Default) Peek - Supported for all Tablet viewports and layouts. Supported only for 1up layouts on Mobile viewports.
if UIDevice.isIPad {
minimumSlotWidth = minimumSlotWidth - (minimumSlotWidth/(CGFloat(layout.value) + 3))
} else if layout == .oneUP {
minimumSlotWidth = minimumSlotWidth - (minimumSlotWidth/4)
}
case .minimum:
// Peek Mimumum Width: 24px from edge of container (at the default view of the carousel with one peek visible)
// Minimum (Mobile only) Supported only on Mobile viewports. If a user passes Minimum for tablet carousel, the peek reverts to Standard.
minimumSlotWidth = isPeekMinimumOnTablet ? minimumSlotWidth : minimumSlotWidth - peekMinimum
case .none:
break
}
}
minimumSlotWidth = minimumSlotWidth / CGFloat(layout.value)
} }
func nextButtonClick() { private func updateScrollPosition(position: Int, callbackText: String) {
carouselScrollBar.position = carouselScrollBar.position+1
showPaginationControls()
updateScrollPosition(position: carouselScrollBar.position, callbackText:"pageControlClicks")
}
func previousButtonClick() {
carouselScrollBar.position = carouselScrollBar.position-1
showPaginationControls()
updateScrollPosition(position: carouselScrollBar.position, callbackText:"pageControlClicks")
}
func updateScrollPosition(position: Int, callbackText: String) {
if carouselScrollBar.numberOfSlides > 0 { if carouselScrollBar.numberOfSlides > 0 {
let scrollContentSizeWidth = scrollView.contentSize.width let scrollContentSizeWidth = scrollView.contentSize.width
let totalPositions = totalPositions() let totalPositions = totalPositions()
@ -586,30 +611,9 @@ open class Carousel: View {
} }
} }
func updateScrollbarPosition(targetContentOffsetXPos:CGFloat) {
let scrollContentSizeWidth = scrollView.contentSize.width
let totalPositions = totalPositions()
let layoutSpace = Int (floor( Double(scrollContentSizeWidth / Double(totalPositions))))
let remindSpace = Int(targetContentOffsetXPos) % layoutSpace
var contentPos = (Int(targetContentOffsetXPos) / layoutSpace) + 1
contentPos = remindSpace > layoutSpace/2 ? contentPos+1 : contentPos
carouselScrollBar.position = contentPos
updateScrollPosition(position: contentPos, callbackText: "ScrollViewMoved")
}
private func totalPositions() -> Int { private func totalPositions() -> Int {
return Int (ceil (Double(carouselScrollBar.numberOfSlides) / Double(_layout.value))) return Int (ceil (Double(carouselScrollBar.numberOfSlides) / Double(_layout.value)))
} }
func showPaginationControls() {
if carouselScrollBar.numberOfSlides == _layout.value {
previousButton.isHidden = true
nextButton.isHidden = true
} else {
previousButton.isHidden = (carouselScrollBar.position == 1) || (paginationDisplay == .none)
nextButton.isHidden = (carouselScrollBar.position == totalPositions()) || (paginationDisplay == .none)
}
}
} }
extension Carousel: UIScrollViewDelegate { extension Carousel: UIScrollViewDelegate {
@ -619,6 +623,7 @@ extension Carousel: UIScrollViewDelegate {
public func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) { public func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
updateScrollbarPosition(targetContentOffsetXPos: targetContentOffset.pointee.x) updateScrollbarPosition(targetContentOffsetXPos: targetContentOffset.pointee.x)
} }
public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
var visibleRect = CGRect() var visibleRect = CGRect()
visibleRect.origin = scrollView.contentOffset visibleRect.origin = scrollView.contentOffset