Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix custom tap gestures for NavigationMapView #56

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

michaelkirk
Copy link
Collaborator

@michaelkirk michaelkirk commented Jun 12, 2024

Description

The current MLNMapView supports implementers adding their own TapGesture,
and even documents how to do this:

let mapTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(myCustomFunction))
for recognizer in mapView.gestureRecognizers! where recognizer is UITapGestureRecognizer {
  mapTapGestureRecognizer.require(toFail: recognizer)
}
mapView.addGestureRecognizer(mapTapGestureRecognizer)

For example I use this in maps.earth for selecting POIs rendered in the map layer.

tap-poi.mov.mp4

Unfortunately this doesn't work with NavigationMapView, which has a
catch-all tap gesture that greedily consumes all taps.

With this change, we will fail the gesture unless the NavigationMapView
intends to actually handle it by selecting a WayPoint or Route.

Unfortunately this presents some redundant work, but I've rearranged
things so that the redundant work only occurs in the case that the user
has successfully selected a Route or WayPoint.

Open Tasks

none

Infos for Reviewer

This is kind of a brittle solution. There is logic mostly but not quite duplicated across two methods. I've tried to add asserts that should help keep them in sync if one is changed. I'm open to suggestions if someone has a better idea of how to handle this.

One alternative would be to add some kind of mapView(_:unhandledTap:) to a delegate somewhere. But would it be added to the only the NavigationMapView which requires it or MLNMapViewDelegate for consistency? I landed on my approach as the most consistent/least surprising based on current behavior and documentation.

🚨- I haven't actually tested the Waypoint/Route selection from NavigationMapView because I'm not using these features yet. I'd appreciate if someone who is could test this. Otherwise, I expect to add these features in the coming weeks and this could remain open until then.

Apparently it's conventional in the codebase to organize notifications
into resume/suspend notification methods.

That's fine, but this code isn't related to notifications.

Currently this method is only called in one place anyway, so there's no
change in behavior, it was just confusing to read.
The current MLNMapView supports implementers adding their own TapGesture,
and even documents how:

```swift
let mapTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(myCustomFunction))
for recognizer in mapView.gestureRecognizers! where recognizer is UITapGestureRecognizer {
  mapTapGestureRecognizer.require(toFail: recognizer)
}
mapView.addGestureRecognizer(mapTapGestureRecognizer)
```

Unfortunately this doesn't work with NavigationMapView, which has a
catch-all tap gesture the greedily consumes all taps.

With this change, we will fail the gesture unless the NavigationMapView
intends to handle it.

Unfortunately this presents some redundant work, but I've rearranged
things so that the redundant work only occurs in the case that the user
has successfully selected a Route or WayPoint.
@boldtrn
Copy link
Collaborator

boldtrn commented Jun 13, 2024

I am wondering, isn't it possible to change this on the client side using something like this:

        for gestureRecognizer in mapView.gestureRecognizers ?? [] where gestureRecognizer is UITapGestureRecognizer{
            gestureRecognizer.addTarget(self, action: #selector(didReceiveTap))
        }

@boldtrn
Copy link
Collaborator

boldtrn commented Jun 13, 2024

I agree that the whole Gesture Recognizer logic is a bit odd, so improving this would be nice.

🚨- I haven't actually tested the Waypoint/Route selection from NavigationMapView because I'm not using these features yet. I'd appreciate if someone who is could test this. Otherwise, I expect to add these features in the coming weeks and this could remain open until then.

We should be sure that this actually works :).

One alternative would be to add some kind of mapView(_:unhandledTap:) to a delegate somewhere. But would it be added to the only the NavigationMapView which requires it or MLNMapViewDelegate for consistency? I landed on my approach as the most consistent/least surprising based on current behavior and documentation.

To be honest, I think adding this to the delegate, maybe even a way to either handle taps before and taps after the waypoint/route logic isn't a bad idea either. I am not 100% sure which option would be better though and which option would be more like other iOS libs?

This is kind of a brittle solution.

Yes. One thing that I am especially concerned about is the as casting. If we change the method signature this won't work anymore? I guess there is a 50% chance that we miss this in a refactor and then break the code in an unexpected way.

@michaelkirk
Copy link
Collaborator Author

I am wondering, isn't it possible to change this on the client side using something like this:

for gestureRecognizer in mapView.gestureRecognizers ?? [] where gestureRecognizer is UITapGestureRecognizer{
     gestureRecognizer.addTarget(self, action: #selector(didReceiveTap))
}

Not a bad idea, but I think that approach might also be brittle. There are 4 pre-existing tap gestures, so we'd probably have to encode some kind of private knowledge of the implementation to get the tap gesture we actually care about.

let mapView = NavigationMapView(frame: .zero, styleURL: styleURL)
let tapGestures = mapView.gestureRecognizers!.filter { $0 is UITapGestureRecognizer }
print("\(tapGestures.count) tap gestures: \(tapGestures)")
4 tap gestures: [<UITapGestureRecognizer: 0x1469132d0; state = Possible; view = <MLNavigationMapView: 0x145812200>; targets= <( "(action=handleDoubleTapGesture:, target=<MLNavigationMapView 0x145812200>)", "(action=updateCourseView:, target=<MLNavigationMapView 0x145812200>)" )>; numberOfTapsRequired = 2; must-fail-for = { <UITapGestureRecognizer: 0x145643320; state = Possible; view = <MLNavigationMapView: 0x145812200>; target= <(action=didRecieveTapWithSender:, target=<MLNavigationMapView 0x145812200>)>>, <UITapGestureRecognizer: 0x1469142a0; state = Possible; view = <MLNavigationMapView: 0x145812200>; targets= <( "(action=handleSingleTapGesture:, target=<MLNavigationMapView 0x145812200>)", "(action=updateCourseView:, target=<MLNavigationMapView 0x145812200>)" )>>, <UILongPressGestureRecognizer: 0x1469138c0; state = Possible; view = <MLNavigationMapView: 0x145812200>; targets= <( "(action=handleQuickZoomGesture:, target=<MLNavigationMapView 0x145812200>)", "(action=updateCourseView:, target=<MLNavigationMapView 0x145812200>)" )>> }>, <UITapGestureRecognizer: 0x1469137a0; state = Possible; view = <MLNavigationMapView: 0x145812200>; targets= <( "(action=handleTwoFingerTapGesture:, target=<MLNavigationMapView 0x145812200>)", "(action=updateCourseView:, target=<MLNavigationMapView 0x145812200>)" )>; numberOfTouchesRequired = 2; must-fail = { <UIPinchGestureRecognizer: 0x146912fd0; state = Possible; view = <MLNavigationMapView: 0x145812200>; targets= <( "(action=handlePinchGesture:, target=<MLNavigationMapView 0x145812200>)", "(action=updateCourseView:, target=<MLNavigationMapView 0x145812200>)" )>>, <UIPanGestureRecognizer: 0x1469133f0; state = Possible; view = <MLNavigationMapView: 0x145812200>; targets= <( "(action=handleTwoFingerDragGesture:, target=<MLNavigationMapView 0x145812200>)", "(action=disableUserCourseTracking, target=<MLNavigationMapView 0x145812200>)", "(action=updateCourseView:, target=<MLNavigationMapView 0x145812200>)" )>>, <UIRotationGestureRecognizer: 0x146913170; state = Possible; view = <MLNavigationMapView: 0x145812200>; targets= <( "(action=handleRotateGesture:, target=<MLNavigationMapView 0x145812200>)", "(action=disableUserCourseTracking, target=<MLNavigationMapView 0x145812200>)", "(action=updateCourseView:, target=<MLNavigationMapView 0x145812200>)" )>> }; must-fail-for = { <UITapGestureRecognizer: 0x145643320; state = Possible; view = <MLNavigationMapView: 0x145812200>; target= <(action=didRecieveTapWithSender:, target=<MLNavigationMapView 0x145812200>)>> }>, <UITapGestureRecognizer: 0x1469142a0; state = Possible; view = <MLNavigationMapView: 0x145812200>; targets= <( "(action=handleSingleTapGesture:, target=<MLNavigationMapView 0x145812200>)", "(action=updateCourseView:, target=<MLNavigationMapView 0x145812200>)" )>; must-fail = { <UITapGestureRecognizer: 0x1469132d0; state = Possible; view = <MLNavigationMapView: 0x145812200>; targets= <( "(action=handleDoubleTapGesture:, target=<MLNavigationMapView 0x145812200>)", "(action=updateCourseView:, target=<MLNavigationMapView 0x145812200>)" )>; numberOfTapsRequired = 2>, <UILongPressGestureRecognizer: 0x1469138c0; state = Possible; view = <MLNavigationMapView: 0x145812200>; targets= <( "(action=handleQuickZoomGesture:, target=<MLNavigationMapView 0x145812200>)", "(action=updateCourseView:, target=<MLNavigationMapView 0x145812200>)" )>> }; must-fail-for = { <UITapGestureRecognizer: 0x145643320; state = Possible; view = <MLNavigationMapView: 0x145812200>; target= <(action=didRecieveTapWithSender:, target=<MLNavigationMapView 0x145812200>)>> }>, <UITapGestureRecognizer: 0x145643320; state = Possible; view = <MLNavigationMapView: 0x145812200>; target= <(action=didRecieveTapWithSender:, target=<MLNavigationMapView 0x145812200>)>; must-fail = { <UIRotationGestureRecognizer: 0x146913170; state = Possible; view = <MLNavigationMapView: 0x145812200>; targets= <( "(action=handleRotateGesture:, target=<MLNavigationMapView 0x145812200>)", "(action=disableUserCourseTracking, target=<MLNavigationMapView 0x145812200>)", "(action=updateCourseView:, target=<MLNavigationMapView 0x145812200>)" )>>, <UITapGestureRecognizer: 0x1469132d0; state = Possible; view = <MLNavigationMapView: 0x145812200>; targets= <( "(action=handleDoubleTapGesture:, target=<MLNavigationMapView 0x145812200>)", "(action=updateCourseView:, target=<MLNavigationMapView 0x145812200>)" )>; numberOfTapsRequired = 2>, <UIPinchGestureRecognizer: 0x146912fd0; state = Possible; view = <MLNavigationMapView: 0x145812200>; targets= <( "(action=handlePinchGesture:, target=<MLNavigationMapView 0x145812200>)", "(action=updateCourseView:, target=<MLNavigationMapView 0x145812200>)" )>>, <UIPanGestureRecognizer: 0x1469133f0; state = Possible; view = <MLNavigationMapView: 0x145812200>; targets= <( "(action=handleTwoFingerDragGesture:, target=<MLNavigationMapView 0x145812200>)", "(action=disableUserCourseTracking, target=<MLNavigationMapView 0x145812200>)", "(action=updateCourseView:, target=<MLNavigationMapView 0x145812200>)" )>>, <UILongPressGestureRecognizer: 0x1469138c0; state = Possible; view = <MLNavigationMapView: 0x145812200>; targets= <( "(action=handleQuickZoomGesture:, target=<MLNavigationMapView 0x145812200>)", "(action=updateCourseView:, target=<MLNavigationMapView 0x145812200>)" )>>, <UITapGestureRecognizer: 0x1469142a0; state = Possible; view = <MLNavigationMapView: 0x145812200>; targets= <( "(action=handleSingleTapGesture:, target=<MLNavigationMapView 0x145812200>)", "(action=updateCourseView:, target=<MLNavigationMapView 0x145812200>)" )>>, <UIPanGestureRecognizer: 0x146912e20; state = Possible; view = <MLNavigationMapView: 0x145812200>; targets= <( "(action=handlePanGesture:, target=<MLNavigationMapView 0x145812200>)", "(action=disableUserCourseTracking, target=<MLNavigationMapView 0x145812200>)", "(action=updateCourseView:, target=<MLNavigationMapView 0x145812200>)" )>>, <UITapGestureRecognizer: 0x1469137a0; state = Possible; view = <MLNavigationMapView: 0x145812200>; targets= <( "(action=handleTwoFingerTapGesture:, target=<MLNavigationMapView 0x145812200>)", "(action=updateCourseView:, target=<MLNavigationMapView 0x145812200>)" )>; numberOfTouchesRequired = 2> }>]

I suppose we could expose the tap gesture that we want people to extend publicly, and then document it, but I think the delegate approach would probably be more idiomatic/discoverable at that point.

One thing that I am especially concerned about is the as casting. If we change the method signature this won't work anymore?

It's brittle, but it's not that brittle. Note that it's infallible as casting, not as?, so really it's just a type checking hint. It's required because we have to disambiguate between an overloaded method that takes 2 different types.

If we were to change the method signature, it would fail to compile like this:
Screenshot 2024-06-13 at 10 44 26

We should be sure that this actually works :).

I'll convert to a draft until I'm able to test the other code paths.

I'm happy to sit on it for a while to see if we can come up with something better. I do think it's likely that other implementers will want this feature, but I'm not married to this implementation.

@michaelkirk michaelkirk marked this pull request as draft June 13, 2024 17:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants