Skip to content

Commit

Permalink
feat: changed package minimum version to iOS 15, applied cleaner code…
Browse files Browse the repository at this point in the history
… do DragView and DropView
  • Loading branch information
pedroesli committed May 5, 2023
1 parent 80fd97c commit 59463f5
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 53 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import PackageDescription

let package = Package(
name: "DragAndDrop",
platforms: [.iOS(.v14)],
platforms: [.iOS(.v15)],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
Expand Down
78 changes: 42 additions & 36 deletions Sources/DragAndDrop/DragView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@

import SwiftUI

public struct DragInfo {
public let didDrop: Bool
public let isDragging: Bool
public let isColliding: Bool
}

/// A draging view that needs to be inside a `InteractiveDragDropContainer` to work properly.
public struct DragView<Content: View> : View {

@EnvironmentObject private var manager: DragDropManager

@State private var dragOffset: CGSize = CGSize.zero
Expand All @@ -21,12 +27,6 @@ public struct DragView<Content: View> : View {
private var dragginStoppedAction: ((_ isSuccessfullDrop: Bool) -> Void)?
private let elementID: UUID

public struct DragInfo {
public let didDrop: Bool
public let isDragging: Bool
public let isColliding: Bool
}

/// Initialize this view with its unique ID and custom view.
///
/// - Parameters:
Expand All @@ -45,44 +45,28 @@ public struct DragView<Content: View> : View {
else{
content(DragInfo(didDrop: isDroped, isDragging: isDragging, isColliding: manager.isColliding(dragID: elementID)))
.offset(dragOffset)
.overlay(GeometryReader(content: { geometry in
Color.clear
.onAppear {
self.manager.addFor(drag: elementID, frame: geometry.frame(in: .dragAndDrop))
}
}))
.background {
GeometryReader { geometry in
Color.clear
.onAppear {
self.manager.addFor(drag: elementID, frame: geometry.frame(in: .dragAndDrop))
}
}
}
.gesture(
DragGesture()
.onChanged { value in
withAnimation(.interactiveSpring()) {
dragOffset = value.translation
}
}
.simultaneously(with: DragGesture(coordinateSpace: .dragAndDrop)
.onChanged { value in
manager.report(drag: elementID, offset: value.translation)

if !isDragging {
isDragging = true
}
}
.onEnded { value in
if manager.canDrop(id: elementID, offset: value.translation) {
manager.dropDragView(of: elementID, at: value.translation)
isDroped = true
dragginStoppedAction?(true)
} else {
withAnimation(.spring()) {
dragOffset = CGSize.zero
}
dragginStoppedAction?(false)
}
isDragging = false
}
)
.simultaneously(with:
DragGesture(coordinateSpace: .dragAndDrop)
.onChanged(onDragChanged(_:))
.onEnded(onDragEnded(_:))
)
)
.zIndex(isDragging ? 1 : 0)
.animation(.spring(), value: isDragging)
}
}

Expand All @@ -96,4 +80,26 @@ public struct DragView<Content: View> : View {
new.dragginStoppedAction = action
return new
}

private func onDragChanged(_ value: DragGesture.Value) {
manager.report(drag: elementID, offset: value.translation)

if !isDragging {
isDragging = true
}
}

private func onDragEnded(_ value: DragGesture.Value) {
if manager.canDrop(id: elementID, offset: value.translation) {
manager.dropDragView(of: elementID, at: value.translation)
isDroped = true
dragginStoppedAction?(true)
} else {
withAnimation(.spring()) {
dragOffset = CGSize.zero
}
dragginStoppedAction?(false)
}
isDragging = false
}
}
32 changes: 17 additions & 15 deletions Sources/DragAndDrop/DropView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

import SwiftUI

public struct DropInfo {
public let didDrop: Bool
public let isColliding: Bool
}

/// A drop view that needs to be inside a `InteractiveDragDropContainer` to work properly.
public struct DropView<Content: View>: View {

Expand All @@ -19,11 +24,6 @@ public struct DropView<Content: View>: View {
private let content: (_ dragInfo: DropInfo) -> Content
private var receivedAction: ((_ receivingViewID: UUID) -> Void)?

public struct DropInfo {
public let didDrop: Bool
public let isColliding: Bool
}

/// Initializer for the drop view that uses the id of the receiving drag view.
///
/// - Parameters:
Expand All @@ -41,16 +41,18 @@ public struct DropView<Content: View>: View {

public var body: some View {
content(DropInfo(didDrop: isDropped, isColliding: manager.isColliding(dropId: elementID)))
.overlay(GeometryReader(content: { geometry in
Color.clear
.onAppear {
self.manager.addFor(
drop: elementID,
frame: geometry.frame(in: .dragAndDrop),
canRecieveAnyDragView: canRecieveAnyDragView
)
}
}))
.background {
GeometryReader { geometry in
Color.clear
.onAppear {
self.manager.addFor(
drop: elementID,
frame: geometry.frame(in: .dragAndDrop),
canRecieveAnyDragView: canRecieveAnyDragView
)
}
}
}
.onChange(of: manager.droppedViewID) { newValue in
if newValue == elementID {
isDropped = true
Expand Down
2 changes: 1 addition & 1 deletion Tests/DragAndDropTests/DragAndDropTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ final class DragAndDropTests: XCTestCase {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct
// results.
XCTAssertEqual(DragAndDrop().text, "Hello, World!")
//XCTAssertEqual(DragAndDrop().text, "Hello, World!")
}
}

0 comments on commit 59463f5

Please sign in to comment.