Skip to content

Commit

Permalink
Add a onDrag callback
Browse files Browse the repository at this point in the history
  • Loading branch information
theo-brlle committed Oct 3, 2022
1 parent 3cdfe3d commit 24f639c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ChartUI.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |spec|

spec.name = "ChartUI"
spec.version = "0.4.0"
spec.version = "0.5.0"
spec.summary = "📈 A SwiftUI chart library"
spec.homepage = "https://github.com/theo-brlle/chart-ui"
spec.license = { :type => "MIT", :file => "LICENSE" }
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Just add this repo as a dependency of your project. Here is the repo URL: https:
Add ChartUI as a dependency in your `Podfile`.

```
pod 'ChartUI', '~> 0.4.0'
pod 'ChartUI', '~> 0.5.0'
```

Then run `pod install` and open the `.xcworkspace` file in Xcode.
Expand Down
4 changes: 4 additions & 0 deletions Sources/ChartUI/LineChart/ViewModels/LineChartViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ final class LineChartViewModel: ObservableObject {
@Published var plotDetailsViewSize: CGSize = .zero
@Published var rightLabelsViewSize: CGSize = .zero

// MARK: - Callbacks

var onDragAction: ((Bool) -> Void)?

// MARK: - Computed properties

var chartColor: Color {
Expand Down
15 changes: 15 additions & 0 deletions Sources/ChartUI/LineChart/Views/LineChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,20 +238,35 @@ private extension LineChartView {
var dragGesture: some Gesture {
DragGesture(minimumDistance: 0)
.onChanged { value in
if !viewModel.isPlotDetailsViewPresented {
viewModel.onDragAction?(true)
}

withAnimation {
viewModel.isPlotDetailsViewPresented = true
}

viewModel.updateSelectedPlot(from: value.location.x)
}
.onEnded { value in
viewModel.onDragAction?(false)

withAnimation {
viewModel.isPlotDetailsViewPresented = false
}
}
}
}

// MARK: - Callbacks

public extension LineChartView {
func onDrag(_ action: @escaping (Bool) -> Void) -> Self {
self.viewModel.onDragAction = action
return self
}
}

// MARK: - Previews

struct LineChartView_Previews: PreviewProvider {
Expand Down

0 comments on commit 24f639c

Please sign in to comment.