Skip to content

Commit

Permalink
Add CoreTest.test_handleGesture_endWithoutAttraction()
Browse files Browse the repository at this point in the history
  • Loading branch information
scenee committed Feb 17, 2024
1 parent d39c4b5 commit dcb89f5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
20 changes: 12 additions & 8 deletions Sources/Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -736,14 +736,7 @@ class Core: NSObject, UIGestureRecognizerDelegate {
}

guard shouldAttract(to: target) else {
self.state = target
self.updateLayout(to: target)
self.unlockScrollView()
// The `floatingPanelDidEndDragging(_:willAttract:)` must be called after the state property changes.
// This allows library users to get the correct state in the delegate method.
if let vc = ownerVC {
vc.delegate?.floatingPanelDidEndDragging?(vc, willAttract: false)
}
self.endWithoutAttraction(target)
return
}

Expand Down Expand Up @@ -886,6 +879,17 @@ class Core: NSObject, UIGestureRecognizerDelegate {
return true
}

func endWithoutAttraction(_ target: FloatingPanelState) {
self.state = target
self.updateLayout(to: target)
self.unlockScrollView()
// The `floatingPanelDidEndDragging(_:willAttract:)` must be called after the state property changes.
// This allows library users to get the correct state in the delegate method.
if let vc = ownerVC {
vc.delegate?.floatingPanelDidEndDragging?(vc, willAttract: false)
}
}

private func startAttraction(to state: FloatingPanelState, with velocity: CGPoint, completion: @escaping (() -> Void)) {
os_log(msg, log: devLog, type: .debug, "startAnimation to \(state) -- velocity = \(value(of: velocity))")
guard let vc = ownerVC else { return }
Expand Down
21 changes: 21 additions & 0 deletions Tests/CoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,27 @@ class CoreTests: XCTestCase {
)
}
}

func test_handleGesture_endWithoutAttraction() throws {
class Delegate: FloatingPanelControllerDelegate {
var willAttract: Bool?
func floatingPanelDidEndDragging(_ fpc: FloatingPanelController, willAttract attract: Bool) {
willAttract = attract
}
}
let fpc = FloatingPanelController()
let scrollView = UIScrollView()
let delegate = Delegate()
fpc.showForTest()
fpc.delegate = delegate
XCTAssertEqual(fpc.state, .half)

fpc.floatingPanel.endWithoutAttraction(.full)

XCTAssertEqual(fpc.state, .full)
XCTAssertEqual(fpc.surfaceLocation(for: .full).y, fpc.surfaceLocation.y)
XCTAssertEqual(delegate.willAttract, false)
}
}

private class FloatingPanelLayout3Positions: FloatingPanelTestLayout {
Expand Down

0 comments on commit dcb89f5

Please sign in to comment.