Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] - allow early callback registration
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Apr 24, 2018
1 parent f495ad8 commit 66162c1
Showing 1 changed file with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
Expand All @@ -73,9 +74,10 @@
public class MapView extends FrameLayout implements NativeMapView.ViewCallback {

private final MapCallback mapCallback = new MapCallback();
private MapboxMap mapboxMap;
private final CopyOnWriteArrayList<OnMapChangedListener> onMapChangedListeners = new CopyOnWriteArrayList<>();

private NativeMapView nativeMapView;
private MapboxMap mapboxMap;
private MapboxMapOptions mapboxMapOptions;
private MapRenderer mapRenderer;
private boolean destroyed;
Expand Down Expand Up @@ -137,7 +139,7 @@ private void initialise(@NonNull final Context context, @NonNull final MapboxMap

private void initialiseMap() {
Context context = getContext();
addOnMapChangedListener(mapCallback);
nativeMapView.addOnMapChangedListener(mapCallback);

// callback for focal point invalidation
final FocalPointInvalidator focalPointInvalidator = new FocalPointInvalidator();
Expand Down Expand Up @@ -305,6 +307,17 @@ public void onSurfaceCreated(GL10 gl, EGLConfig config) {
}

nativeMapView = new NativeMapView(getContext(), this, mapRenderer);
nativeMapView.addOnMapChangedListener(new OnMapChangedListener() {
@Override
public void onMapChanged(int change) {
// dispatch events to external listeners
if (!onMapChangedListeners.isEmpty()) {
for (OnMapChangedListener onMapChangedListener : onMapChangedListeners) {
onMapChangedListener.onMapChanged(change);
}
}
}
});
nativeMapView.resizeView(getMeasuredWidth(), getMeasuredHeight());
}

Expand Down Expand Up @@ -586,7 +599,7 @@ public Bitmap getViewContent() {
*/
public void addOnMapChangedListener(@Nullable OnMapChangedListener listener) {
if (listener != null) {
nativeMapView.addOnMapChangedListener(listener);
onMapChangedListeners.add(listener);
}
}

Expand All @@ -597,8 +610,8 @@ public void addOnMapChangedListener(@Nullable OnMapChangedListener listener) {
* @see MapView#addOnMapChangedListener(OnMapChangedListener)
*/
public void removeOnMapChangedListener(@Nullable OnMapChangedListener listener) {
if (listener != null) {
nativeMapView.removeOnMapChangedListener(listener);
if (listener != null && onMapChangedListeners.contains(listener)) {
onMapChangedListeners.remove(listener);
}
}

Expand Down

0 comments on commit 66162c1

Please sign in to comment.