-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Deprecate MapChange and provide dedicated callback methods #9498
Conversation
17a1012
to
b28423d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tobrun I did a first review. There are a couple of issues that need to be addressed. Sorry for the "From mbgl::RendererBackend" comment in the header. This is misleading as it is not true anymore... :(
@@ -184,21 +184,32 @@ void onPostMapReady() { | |||
/** | |||
* Called when the region is changing or has changed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this javadoc still correct?
@@ -174,6 +175,9 @@ private void initialise(@NonNull final Context context, @NonNull final MapboxMap | |||
// notify Map object about current connectivity state | |||
nativeMapView.setReachability(ConnectivityReceiver.instance(context).isConnected(context)); | |||
|
|||
// bind internal components for map change events | |||
mapChangeDispatcher.bind(mapCallback = new MapChangeResultHandler(mapboxMap)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it somehow be feasible to make this a constructor argument? Since it is required. That would make it possible to skip a lot of null checks in MapChangeDispatcher
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Been looking into it but don't think so at the moment though this clearly shows we need to rethink our architecture. The issue here is a cyclic class dependency: NativeMapview depends on MapChangeDispatcher, MapboxMap depends on NativeMapView and MapChangeDispatcher depends on MapboxMap.
Somewhere during initialization we need to bind them together and require us to have null checking. Current setup follows the requirement of being able to listen to map change events as soon as possible. I don't see an a issue with internal components missing out on the initialization change events.
@@ -54,14 +54,14 @@ | |||
namespace mbgl { | |||
namespace android { | |||
|
|||
NativeMapView::NativeMapView(jni::JNIEnv& _env, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you revert indentation changes?
: javaPeer(_obj.NewWeakGlobalRef(_env)), | ||
pixelRatio(_pixelRatio), | ||
threadPool(sharedThreadPool()) { | ||
: javaPeer(_obj.NewWeakGlobalRef(_env)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dito 3x
@@ -112,7 +112,7 @@ void NativeMapView::bind() { | |||
/** | |||
* From mbgl::RendererBackend. | |||
*/ | |||
gl::ProcAddress NativeMapView::initializeExtension(const char* name) { | |||
gl::ProcAddress NativeMapView::initializeExtension(const char *name) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dito
* | ||
* May be called from any thread | ||
*/ | ||
void NativeMapView::notifyMapChange(mbgl::MapChange change) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also remove the declaration here: https://github.com/mapbox/mapbox-gl-native/blob/master/platform/android/src/native_map_view.hpp#L67
void NativeMapView::onDidFailLoadingMap(std::exception_ptr eptr) { | ||
assert(vm != nullptr); | ||
try { | ||
std::rethrow_exception(eptr); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to throw an exception here? Seems like we always want to invoke the listener. If re-throwing is required, it should happen where the exception is produced.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rethrowing was a way of getting the string message for the listener, let me try a different approach.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the exception is produced in core in style_impl.cpp
and passed to the MapObserver, which invokes the method above. Don't think I can move re-throwing up the chain as it would impact other bindings. The code for retrieving the exception message was based of http://en.cppreference.com/w/cpp/error/exception_ptr.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, you're right. That's a valid way to do it. Should've read more carefully.
javaPeer->Call(*_env, onInvalidate); | ||
} | ||
|
||
/** | ||
* From mbgl::RendererBackend. Callback to java NativeMapView#onMapChanged(int). | ||
* From mbgl::RendererBackend. Callback to java NativeMapView#onCameraWillChange(). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually. This is from MapObserver
and will only be coming from the main (map) thread. Could you update the comment in the header file as well?
void NativeMapView::onCameraIsChanging() { | ||
notifyMapChange(MapChange::MapChangeRegionIsChanging); | ||
assert(vm != nullptr); | ||
android::UniqueEnv _env = android::AttachEnv(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this is not actually coming from any other thread then the main/map thread, this is not needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed it to something as:
void NativeMapView::onDidFinishLoadingStyle() {
JNIEnv* env = nullptr;
int resultCode = vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6);
if(resultCode == JNI_OK) {
static auto onDidFinishLoadingStyle = javaClass.GetMethod<void()>(*env, "onDidFinishLoadingStyle");
javaPeer->Call(*env, onDidFinishLoadingStyle);
}
}
lmk if there is a better way of getting the env
_createSurface(ANativeWindow_fromSurface(&env, jni::Unwrap(*_surface))); | ||
} | ||
|
||
void NativeMapView::destroySurface(jni::JNIEnv&) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
more formatting changes
3bf83d2
to
6122934
Compare
6122934
to
24b95fc
Compare
8dea25d
to
be86668
Compare
I redid this PR in light of moving to GLSurfaceView (reason this PR was on hold).
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. The only thing is that I found the naming of MapChangeHandler
and MapChangeDispatcher
a little confusing while reading the PR. But since neither is public that will probably not be a problem.
void NativeMapView::onDidFailLoadingMap(std::exception_ptr eptr) { | ||
try { | ||
if(eptr) { | ||
std::rethrow_exception(eptr); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
be86668
to
9e9f999
Compare
Thank you for the review @ivovandongen, |
9e9f999
to
8847b41
Compare
stale |
This PR deprecates using OnMapChangeListeners and replaces them with dedicated callbacks.
onMapFailedLoaded
with the thrown error messageonSourceChanged
callback to provide source/attribution that has changedProposed API:
Closes #8389 / refs #9476