Skip to content

Commit

Permalink
feat:
Browse files Browse the repository at this point in the history
follow selected position to latest drag position
  • Loading branch information
tomosaaan committed Oct 25, 2024
1 parent cfe0ada commit f6195f3
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 23 deletions.
10 changes: 1 addition & 9 deletions Example/Example/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@ struct Element: GraphRangeElement {
}

struct ContentView: View {
let data: [Element] = [
.init(x: 10, y: 10),
.init(x: 20, y: 20),
.init(x: 30, y: 30),
.init(x: 40, y: 40),
.init(x: 50, y: 50),
.init(x: 60, y: 60),
.init(x: 70, y: 70),
]
let data: [Element] = (1...50).map { .init(x: $0 * 10, y: $0 * 10) }

@State var selectedData = [Element]()

Expand Down
7 changes: 7 additions & 0 deletions Sources/GraphRangeSlider/Extensions/Array+.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Foundation

extension Array where Element: Comparable {
func argmin() -> Index? {
indices.min(by: { self[$0] < self[$1] })
}
}
2 changes: 1 addition & 1 deletion Sources/GraphRangeSlider/GraphRangeSlider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public struct GraphRangeSlider<Data, ID>: View where Data: RandomAccessCollectio
@Binding private var selectedData: Data
@State private var leftCurrentIndex = 0
@State private var rightCurrentIndex = 0
@State private var positions = [CGFloat]()
@State private var positions = ContiguousArray<CGFloat>()
@Environment(\.graphBarWidth) private var graphBarWidth: CGFloat
@Environment(\.activeColor) private var activeColor: Color
@Environment(\.inactiveColor) private var inactiveColor: Color
Expand Down
2 changes: 1 addition & 1 deletion Sources/GraphRangeSlider/Slider.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SwiftUI

struct Slider: View {
let positions: [CGFloat]
let positions: ContiguousArray<CGFloat>
@Binding var leftCurrentIndex: Int
@Binding var rightCurrentIndex: Int
@State private var viewSize = CGSize.zero
Expand Down
17 changes: 5 additions & 12 deletions Sources/GraphRangeSlider/SliderToggle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ struct SliderToggle: View {
case left, right
}

let togglePositions: [CGFloat]
let togglePositions: ContiguousArray<CGFloat>
let limitIndex: Int
@Binding var currentIndex: Int
@Environment(\.activeColor) private var activeColor: Color
Expand All @@ -31,23 +31,16 @@ struct SliderToggle: View {
.gesture(
DragGesture()
.onChanged { value in
let nextIndex = min(currentIndex + 1, togglePositions.endIndex - 1)
let previousIndex = max(currentIndex - 1, 0)

switch position {
case .left:
let locationX = value.location.x - toggleRadius
if togglePositions[nextIndex] < locationX {
currentIndex = min(nextIndex, limitIndex - 1)
} else if locationX < togglePositions[previousIndex] {
currentIndex = previousIndex
if let index = togglePositions.map { abs($0 - locationX) }.argmin() {
currentIndex = min(index, limitIndex - 1)
}
case .right:
let locationX = value.location.x + toggleRadius * 3
if togglePositions[nextIndex] < locationX {
currentIndex = nextIndex
} else if locationX < togglePositions[previousIndex] {
currentIndex = max(previousIndex, limitIndex + 1)
if let index = togglePositions.map { abs($0 - locationX) }.argmin() {
currentIndex = max(index, limitIndex + 1)
}
}
}
Expand Down

0 comments on commit f6195f3

Please sign in to comment.