Skip to content

Commit

Permalink
fix: improve scrolling indicator container logic
Browse files Browse the repository at this point in the history
  • Loading branch information
guoyingtao committed Jun 21, 2023
1 parent f5e049a commit 04cfd7e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
29 changes: 17 additions & 12 deletions Inchworm/Source/ProcessIndicatorContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class ProcessIndicatorContainer: UIView {

func setActiveIndicatorIndex(_ index: Int = 0, animated: Bool = false) {
guard index < progressIndicatorViewList.count else {
fatalError("Invalid index found")
return
}

activeIndicatorIndex = index
Expand All @@ -127,7 +127,7 @@ class ProcessIndicatorContainer: UIView {
return
}

indicator.active = true
indicator.isActive = true

let slideContentSize = getSlideContentSize()
let currentPositon = indicator.center
Expand All @@ -153,7 +153,7 @@ class ProcessIndicatorContainer: UIView {
extension ProcessIndicatorContainer: UIScrollViewDelegate {
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

let kMaxIndex = progressIndicatorViewList.count
let kMaxIndex = progressIndicatorViewList.count - 1

let targetX = scrollView.contentOffset.x + velocity.x * 60.0
var targetIndex = 0
Expand All @@ -173,27 +173,32 @@ extension ProcessIndicatorContainer: UIScrollViewDelegate {
if (targetIndex > kMaxIndex) {
targetIndex = kMaxIndex
}

targetContentOffset.pointee.x = CGFloat(targetIndex) * pageWidth;
setActiveIndicatorIndex(targetIndex)

guard let processIndicatorView = getActiveIndicator() else { return }
if processIndicatorView.status == .editingSelf {
if targetIndex != activeIndicatorIndex {
targetContentOffset.pointee.x = CGFloat(targetIndex) * pageWidth;
setActiveIndicatorIndex(targetIndex)
getActiveIndicator()?.handleTap()
deactiveInactiveIndicators()

guard let processIndicatorView = getActiveIndicator() else { return }
didActive(processIndicatorView.progress)
}
}
}

extension ProcessIndicatorContainer: ProcessIndicatorViewDelegate {
private func deactiveInactiveIndicators() {
progressIndicatorViewList
.filter { $0.index != activeIndicatorIndex }
.forEach { $0.deactive() }
}

func didActive(_ processIndicatorView: ProcessIndicatorView) {
if activeIndicatorIndex != processIndicatorView.index {
setActiveIndicatorIndex(processIndicatorView.index, animated: true)
}

progressIndicatorViewList
.filter { $0.index != activeIndicatorIndex }
.forEach { $0.deactive() }

deactiveInactiveIndicators()
didActive(processIndicatorView.progress)
}

Expand Down
16 changes: 8 additions & 8 deletions Inchworm/Source/ProcessIndicatorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ProcessIndicatorView: UIView {
var normalIconImage: CGImage?
var dimmedIconImage: CGImage?
var index = 0
var active = false
var isActive = false

var progress: Float {
get {
Expand Down Expand Up @@ -137,7 +137,7 @@ class ProcessIndicatorView: UIView {
}

func initialActiveStatus() {
active = true
isActive = true

if progress != 0 {
delegate?.didActive(self)
Expand All @@ -146,7 +146,7 @@ class ProcessIndicatorView: UIView {
}

func deactive() {
active = false
isActive = false

if status != .tempReset {
if viewModel.progress == 0 {
Expand All @@ -158,23 +158,23 @@ class ProcessIndicatorView: UIView {
}

@objc func handleTap() {
if active {
if status == .tempReset {
if isActive {
if status == .tempReset || status == .editingOthers {
status = .editingSelf
delegate?.didRemoveTempReset(self)
} else {
status = .tempReset
delegate?.didTempReset(self)
}
} else {
active = true
isActive = true
delegate?.didActive(self)

if status == .tempReset {
delegate?.didTempReset(self)
} else {
status = .editingSelf
}

status = .editingSelf
}
}

Expand Down

0 comments on commit 04cfd7e

Please sign in to comment.