Skip to content

[NMA-1079] (Explore-Dash) Sorting by distance - #823

Merged
HashEngineering merged 12 commits into
feature-explore-dashfrom
feature-explore-sorting
Nov 23, 2021
Merged

HashEngineering merged 12 commits into
feature-explore-dashfrom
feature-explore-sorting

Conversation

@Syn-McJ

@Syn-McJ Syn-McJ commented Nov 23, 2021

Copy link
Copy Markdown
Member

If the user has location services enabled, physical results should be sorted by distance from the user location.

Issue being fixed or feature implemented

  • Physical results are sorted within database request to support pagination, using a simple Pythagorean formula. This makes sorting less precise on the big distances, but for our purposes it's ok.
  • Both "sort by distance" and "sort by name" options are supported.
  • Changes to the map: instead of waiting for all merchant icons to resolve or fail before adding them to the map (which can take several seconds), add the icons incrementally as they are loaded and placeholders for the failed ones.

Related PR's and Dependencies

Screenshots / Videos

How Has This Been Tested?

  • QA (Mobile Team)

Checklist:

  • I have performed a self-review of my own code and added comments where necessary
  • I have added or updated relevant unit/integration/functional/e2e tests

@Syn-McJ Syn-McJ self-assigned this Nov 23, 2021
eastLng == other.eastLng &&
southLat == other.southLat &&
westLng == other.westLng &&
centerLat == other.centerLng &&

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Found a bug during unit testing

val latitude: Double,
val longitude: Double,
val logoUrl: Bitmap
)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Not needed anymore

private var markerManager: MarkerManager? = null
private var currentMapItems: List<SearchResult> = listOf()
private var markerCollection: MarkerManager.Collection? = null
private var circleManager: CircleManager? = null

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Managers don't need to be stored

center.latitude, center.longitude,
it.latitude ?: 0.0, it.longitude ?: 0.0
)
}.take(ExploreViewModel.MAX_MARKERS)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Only MAX_MARKERS closest to the center of the map are displayed

}

private fun addCircleAroundCurrentPosition() {
currentLocationCircle = circleCollection?.addCircle(CircleOptions().apply {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No need to have a separate collection for a single circle.

private fun addMarkerOnCurrentPosition() {
val bitmap = getBitmapFromDrawable(R.drawable.user_location_map_marker)
currentLocationMarker = markerCollection?.addMarker(MarkerOptions().apply {
currentLocationMarker = googleMap?.addMarker(MarkerOptions().apply {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There is no need to save the current location marker in the same collection as the merchant/atm markers. This simplifies managing other markers like handling clicks and calculating the difference to render.


currentLocationMarker?.position = mCurrentUserLocation
currentLocationCircle?.center = mCurrentUserLocation
currentLocationCircle?.radius = currentAccuracy

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Instead of re-creating the current location marker and circle, we can simply update their locations.

pair.first.id,
it, it1,
try {
pair.second.get()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Calling .get() before rendering markers makes the map wait until all of the merchant/atm icons have been either resolved or failed to resolve. This can take several seconds with a slow connection. Better to add markers to the map incrementally as they are load or fail.

withContext(Dispatchers.Main){
exploreMarkers.forEach{
it?.let { it1 -> addMarkerItemToMap(it1.latitude, it.longitude, it.logoUrl, it.id) }
private fun removeOldMarkers(newItems: List<SearchResult>) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Instead of resetting the map on every collection change, we calculate the difference and only render new and remove old markers.

return false
}

private fun handleClickOnMarkerItem(results: List<SearchResult>) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Moved to the viewModel

@Syn-McJ Syn-McJ changed the title [NMA-1079] Sorting by distance [NMA-1079] (Explore-Dash) Sorting by distance Nov 23, 2021
Comment on lines +39 to +40
CASE WHEN :sortByDistance = 1 THEN (latitude - :anchorLat)*(latitude - :anchorLat) + (longitude - :anchorLng)*(longitude - :anchorLng) END ASC,
CASE WHEN :sortByDistance = 0 THEN name END COLLATE NOCASE ASC

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Very cool, here is the Pythagorean theorem.

@HashEngineering HashEngineering left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Very good. Tested.

@ClaudeHangui ClaudeHangui left a comment

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.

Nothing to add, looks awesome

@HashEngineering
HashEngineering merged commit 822f3a2 into feature-explore-dash Nov 23, 2021
@Syn-McJ
Syn-McJ deleted the feature-explore-sorting branch November 25, 2021 08:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants