Skip to content

Call plugin onStart after start loading style#680

Merged
axti merged 3 commits intomainfrom
ag-fix-plugins-start-lifecycle
Oct 5, 2021
Merged

Call plugin onStart after start loading style#680
axti merged 3 commits intomainfrom
ag-fix-plugins-start-lifecycle

Conversation

@axti
Copy link
Copy Markdown
Contributor

@axti axti commented Sep 27, 2021

PRs must be submitted under the terms of our Contributor License Agreement CLA.
Fixes: < Link to related issues that will be fixed by this pull request, if they exist >

Pull request checklist:

  • Briefly describe the changes in this PR.
  • Include before/after visuals or gifs if this PR includes visual changes.
  • Write tests for all new functionality. If tests were not written, please explain why.
  • Add example if relevant.
  • Document any changes to public APIs.
  • Apply changelog label ('breaking change', 'bug 🪲', 'build', 'docs', 'feature 🍏', 'performance ⚡', 'testing 💯') or use the label 'skip changelog'
  • Add an entry inside this element for inclusion in the mapbox-maps-android changelog: <changelog>Fix initialisation location puck when no style loaded from code by changing Plugin#onStart() call after style loaded</changelog>.

Summary of changes

In this PR has been changed order of methods called on lifecycle event onStart. Now plugins will receive onStart after style has been loaded. This reorder prevent of loosing callbacks from StyleObserver when callback were added before style starts loading.

User impact (optional)

User with styleUri attribute in XML will have all plugins started correctly after style has been loaded. Before in this case it was the issue reported here: #641

@axti axti added the bug 🪲 Something isn't working label Sep 27, 2021
@axti axti requested a review from a team September 27, 2021 11:19
mapboxMap.loadStyleUri(it)
}
}
pluginRegistry.onStart()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On internal convo, you mentioned:

What if we will change here:
    pluginRegistry.onStart()
    if (!mapboxMap.isStyleLoadInitiated) {
      // Load the style in mapInitOptions if no style has loaded yet.
      mapInitOptions.styleUri?.let {
        mapboxMap.loadStyleUri(it)
      }
    }
to do like this:
    if (!mapboxMap.isStyleLoadInitiated) {
      // Load the style in mapInitOptions if no style has loaded yet.
      mapInitOptions.styleUri?.let {
        mapboxMap.loadStyleUri(it)
        pluginRegistry.onStart()
      }
    }

Proposed change here is actually not part of the style loading callback?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should call onStart inside the Activity's onStart event, regardless of the style loading status.
and I remember we do have a onStyleChanged listener, we could hook to this listener inside the plugin if the style is need for certain action, wdyt?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was no callback, I assume it is loading synchronously. If style is null plugins wouldn’t work.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pengdev you are right, this onStart is initially called from Activity/Fragment and implementation is inside MapController, It is entry point.

Copy link
Copy Markdown
Member

@pengdev pengdev Sep 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proposed change here is actually not part of the style loading callback?

@tobrun I think the point of this change is that the new style load will clear the style load listeners that previous getStyle() { style -> } sets.
In some plugins we rely on the async getStyle() call to do the initialisation in the plugin's onStart(). And when the new style is loaded after the plugin's onStart, it will result in the plugins' getStyle() callback never being fired.
This fix intentionally moved the pluginRegistry.onStart() after the loadStyleUri call, so that we made sure the styleLoaded listener set by plugins' getStyle() is not cleared.

@axti axti self-assigned this Sep 27, 2021
Copy link
Copy Markdown
Member

@pengdev pengdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work @axti , LGTM, please add tests as well 🚀

@axti axti requested a review from a team September 30, 2021 10:36
@axti axti force-pushed the ag-fix-plugins-start-lifecycle branch from 2fca8bc to e13103d Compare October 4, 2021 09:31
every { mockNativeObserver.removeOnCameraChangeListener(any()) } just Runs
every { mockNativeObserver.removeOnStyleDataLoadedListener(any()) } just Runs

testMapController.simulateStartedState()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use onStart directly instead of using simulateStartState?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for that and remove simulateStartState at all. imho there's no possibility in reality to set lifecycleState = LifecycleState.STATE_STARTED and not call onStart at the same time

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree with relaxed, I'm trying to follow Arrange-Action-Assert pattern to write a good tests.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree with relaxed, I'm trying to follow Arrange-Action-Assert pattern to write a good tests.

Is this comment related to #680 (comment) actually?

Comment on lines +67 to +71
every { mockPluginRegistry.onStart() } just Runs
every { mockMapboxMap.loadStyleUri(Style.MAPBOX_STREETS) } just Runs
every { mockNativeObserver.addOnCameraChangeListener(any()) } just Runs
every { mockNativeObserver.addOnStyleDataLoadedListener(any()) } just Runs
every { mockRenderer.onStart() } just Runs
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we can use relaxed mockk to avoid adding every for these functions.

@axti axti force-pushed the ag-fix-plugins-start-lifecycle branch from e13103d to a50b2f9 Compare October 5, 2021 15:17
@axti axti merged commit 934d016 into main Oct 5, 2021
@axti axti deleted the ag-fix-plugins-start-lifecycle branch October 5, 2021 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug 🪲 Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants