Skip to content

Refactor default cluster item rendering into onBeforeClusterItemRendered() #630

@barbeau

Description

@barbeau

Is your feature request related to a problem? Please describe.

DefaultClusterRenderer currently contains two protected methods that can be overridden by applications to change the default rendering of cluster items and clusters:

  • Item - onBeforeClusterItemRendered() - Default implementation is empty (no-op)
  • Cluster - onBeforeClusterRendered() - Default implementation draws a circle with a rough count of the number of items and sets it as the icon

Looking at these methods, you would think that items are rendered as markers without titles or snippets by default. However, the default item rendering actually occurs in the private method DefaultClusterRenderer.CreateMarkerTask.perform():

if (!(item.getTitle() == null) && !(item.getSnippet() == null)) {
	markerOptions.title(item.getTitle());
	markerOptions.snippet(item.getSnippet());
} else if (!(item.getSnippet() == null)) {
	markerOptions.title(item.getSnippet());
} else if (!(item.getTitle() == null)) {
	markerOptions.title(item.getTitle());
}

This is strange to me for the following reasons:

  • The default rendering implementation for clusters lives in a protected method while the default for items does not
  • I would expect if I overrode onBeforeClusterItemRendered() with a custom icon or a no-op that a marker wouldn't have a title or snippet, even if that data existed in the model. However, that's not what happens - the title gets set anyway. And again, this design doesn't match the behavior for clusters.

For example, in the demo app CustomMarkerClusteringDemoActivity, onBeforeClusterItemRendered() is overridden with a custom icon and title, with the title being from another model field (the Person's name). The only reason the marker title doesn't get set twice is that the demo app is returning null from the item's getTitle() and getSnippet() methods - but that seems a bit arbitrary.

Describe the solution you'd like
Refactor the default implementation for item rendering currently in DefaultClusterRenderer.CreateMarkerTask.perform() into onBeforeClusterItemRendered().

  • Pros:
    • This would match the implementation of onBeforeClusterRendered() for clusters, with the default implementation living in the protected method
    • This would allow applications extending DefaultClusterRenderer to inherit this default behavior along with a custom implementation via a call to super.onBeforeClusterItemRendered(), or eliminate it by not calling super.onBeforeClusterItemRendered().
  • Cons:
    • Moving this code would change the behavior of the library for apps already overriding onBeforeClusterItemRendered() that aren't calling super.onBeforeClusterItemRendered() - the marker title and snippet would no longer be updated.

Describe alternatives you've considered

  1. Leave the code as-is. Better document that default item rendering happens outside of the protected method onBeforeClusterItemRendered().
  2. Move default cluster implementation out of protected method and into private method to match the default item implementation

Additional context
Discussion for this started in #627 (review), but was moved here to be handled independent of
PR #627.

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: p2Moderately-important priority. Fix may not be included in next release.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions