mapbox / mapbox-gl-native-android

Interactive, thoroughly customizable maps in native Android powered by vector tiles and OpenGL
https://mapbox.com/mobile
Other
218 stars 117 forks source link

Order of events' management in `OnTouchEven` #655

Open Ph0tonic opened 3 years ago

Ph0tonic commented 3 years ago

Hello,

There is an issue in mapbox-annotation-plugin https://github.com/mapbox/mapbox-plugins-android/issues/1162 which is link on the order that events are handle. When we drag a symbol and we have also a long click listener on the map then we first trigger the event in mapView and after that the event added via addTouchListener triggers the drag event.

One solution to this problem would be to first trigger the added listener and then the mapview ones. So if the drag event is triggered it will consume the event and no long click will be triggered.

So instead of having :

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    if (isGestureDetectorInitialized() && mapGestureDetector.onTouchEvent(event) || super.onTouchEvent(event)) {
      return true;
    }

    for (OnTouchListener listener : onTouchListeners) {
      if (listener.onTouch(this, event) {
        return true;
      }
    }
    return false;
  }

We would have

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    for (OnTouchListener listener : onTouchListeners) {
      if (listener.onTouch(this, event) {
        return true;
      }
    }
    return isGestureDetectorInitialized() && mapGestureDetector.onTouchEvent(event) || super.onTouchEvent(event);
  }

What do you think, let me know and then I will open a PR. Thanks @mapbox/maps-android

\cc @samcrawford

Chaoba commented 3 years ago

Thanks, @Ph0tonic. We need mapGestureDetector to handle the map movement events, so if there is a registered OnTouchListener and consumes the touch event, we will not be able to move the map.

Ph0tonic commented 3 years ago

Hello, Sorry for the late answer @Chaoba but this is the point. We should be able to handle events before any map update. For example with the annotation plugin, we want to handle the drag event of an annotation and consume it before the map. Because it would means that the map will be moved and the event consumed. Maybe I'm missing a requirement of the architecture but I think that for the annotation plugin it's required.