Skip to content

Commit da2a014

Browse files
committed
Update remove function
1 parent 2c6556a commit da2a014

File tree

2 files changed

+123
-34
lines changed

2 files changed

+123
-34
lines changed

lib-compose/src/main/java/com/what3words/components/compose/maps/W3WMapComponent.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fun W3WMapComponent(
112112
},
113113
onMapClicked = {
114114
coroutineScope.launch {
115-
mapManager.setSelectedAddress(it)
115+
mapManager.setSelectedAt(it)
116116
}
117117
},
118118
onMyLocationClicked =
@@ -137,7 +137,7 @@ fun W3WMapComponent(
137137
onMarkerClicked =
138138
{ marker ->
139139
coroutineScope.launch {
140-
mapManager.setSelectedAddress(marker.center)
140+
mapManager.setSelectedAt(marker.center)
141141
}
142142
},
143143
onCameraUpdated = {

lib-compose/src/main/java/com/what3words/components/compose/maps/W3WMapManager.kt

Lines changed: 121 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -449,17 +449,13 @@ class W3WMapManager(
449449
* @param suggestion The [W3WSuggestion] to select.
450450
*/
451451
@JvmName("setSelectedAddressAtSuggestion")
452-
suspend fun setSelectedAddress(
452+
suspend fun setSelectedAtAddress(
453453
suggestion: W3WSuggestion
454454
) = withContext(dispatcher) {
455-
_mapState.update {
456-
it.copy(
457-
selectedAddress = suggestion.w3wAddress
458-
)
459-
}
460-
461-
if (_buttonState.value.isRecallButtonEnabled) {
462-
handleRecallButton()
455+
suggestion.w3wAddress.center?.let {
456+
setSelectedInternal(suggestion.w3wAddress)
457+
}?:run {
458+
setSelectedAt(suggestion.w3wAddress.words)
463459
}
464460
}
465461

@@ -469,17 +465,13 @@ class W3WMapManager(
469465
* @param address The [W3WAddress] to select.
470466
*/
471467
@JvmName("setSelectedAddressAtAddress")
472-
suspend fun setSelectedAddress(
468+
suspend fun setSelectedAt(
473469
address: W3WAddress
474470
) = withContext(dispatcher) {
475-
_mapState.update {
476-
it.copy(
477-
selectedAddress = address
478-
)
479-
}
480-
481-
if (_buttonState.value.isRecallButtonEnabled) {
482-
handleRecallButton()
471+
address.center?.let {
472+
setSelectedInternal(address)
473+
}?:run {
474+
setSelectedAt(address.words)
483475
}
484476
}
485477

@@ -489,7 +481,7 @@ class W3WMapManager(
489481
* @param words The what3words address as a [String].
490482
*/
491483
@JvmName("setSelectedAddressAtWords")
492-
suspend fun setSelectedAddress(
484+
suspend fun setSelectedAt(
493485
words: String
494486
) = withContext(dispatcher) {
495487
when (val result = textDataSource.convertToCoordinates(words)) {
@@ -498,7 +490,7 @@ class W3WMapManager(
498490
}
499491

500492
is W3WResult.Success -> {
501-
setSelectedAddress(result.value)
493+
setSelectedInternal(result.value)
502494
}
503495
}
504496
}
@@ -509,7 +501,7 @@ class W3WMapManager(
509501
* @param coordinates The [W3WCoordinates] to be selected.
510502
*/
511503
@JvmName("setSelectedAddressAtCoordinates")
512-
suspend fun setSelectedAddress(
504+
suspend fun setSelectedAt(
513505
coordinates: W3WCoordinates
514506
) = withContext(dispatcher) {
515507
when (val result = textDataSource.convertTo3wa(
@@ -521,11 +513,25 @@ class W3WMapManager(
521513
}
522514

523515
is W3WResult.Success -> {
524-
setSelectedAddress(result.value)
516+
setSelectedInternal(result.value)
525517
}
526518
}
527519
}
528520

521+
private suspend fun setSelectedInternal(
522+
address: W3WAddress
523+
) {
524+
_mapState.update {
525+
it.copy(
526+
selectedAddress = address
527+
)
528+
}
529+
530+
if (_buttonState.value.isRecallButtonEnabled) {
531+
handleRecallButton()
532+
}
533+
}
534+
529535
/**
530536
* Removes all markers from the map.
531537
*/
@@ -565,6 +571,7 @@ class W3WMapManager(
565571
* @param words The What3Words address as a [String].
566572
* @param listName The name of the list from which markers should be removed. If null, markers will be removed from all lists.
567573
*/
574+
@JvmName("removeMarkerAtWords")
568575
suspend fun removeMarkerAt(
569576
words: String,
570577
listName: String? = null
@@ -609,6 +616,7 @@ class W3WMapManager(
609616
* @param coordinates The [W3WCoordinates] to remove markers from.
610617
* @param listName The name of the list from which markers should be removed. If null, markers will be removed from all lists.
611618
*/
619+
@JvmName("removeMarkerAtCoordinates")
612620
suspend fun removeMarkerAt(
613621
coordinates: W3WCoordinates,
614622
listName: String? = null
@@ -653,11 +661,16 @@ class W3WMapManager(
653661
* @param address The What3Words address as a [W3WAddress].
654662
* @param listName The name of the list from which markers should be removed. If null, markers will be removed from all lists.
655663
*/
664+
@JvmName("removeMarkerAtAddress")
656665
suspend fun removeMarkerAt(
657666
address: W3WAddress,
658667
listName: String? = null
659668
): List<W3WMarker> = withContext(dispatcher) {
660-
removeMarkerAt(address.words, listName)
669+
address.center?.let {
670+
removeMarkerAt(it, listName)
671+
}?:run {
672+
removeMarkerAt(address.words, listName)
673+
}
661674
}
662675

663676
/**
@@ -666,11 +679,16 @@ class W3WMapManager(
666679
* @param suggestion The What3Words address as a [W3WSuggestion].
667680
* @param listName The name of the list from which markers should be removed. If null, markers will be removed from all lists.
668681
*/
682+
@JvmName("removeMarkerAtSuggestion")
669683
suspend fun removeMarkerAt(
670684
suggestion: W3WSuggestion,
671685
listName: String? = null
672686
): List<W3WMarker> = withContext(dispatcher) {
673-
removeMarkerAt(suggestion.w3wAddress.words, listName)
687+
suggestion.w3wAddress.center?.let {
688+
removeMarkerAt(it, listName)
689+
}?:run {
690+
removeMarkerAt(suggestion.w3wAddress.words, listName)
691+
}
674692
}
675693

676694
/**
@@ -679,14 +697,41 @@ class W3WMapManager(
679697
* @param listWords list of The What3Words address as a [String].
680698
* @param listName The name of the list from which markers should be removed. If null, markers will be removed from all lists.
681699
*/
700+
@JvmName("removeMarkerAtListWords")
682701
suspend fun removeMarkersAt(
683702
listWords: List<String>,
684703
listName: String? = null
685704
): List<W3WMarker> = withContext(dispatcher) {
686705
val removedMarkers = mutableListOf<W3WMarker>()
687-
listWords.forEach {
688-
val markers = removeMarker(it, listName)
689-
removedMarkers.addAll(markers)
706+
listWords.forEach { words ->
707+
if (listName == null) {
708+
// Remove markers from all lists
709+
markersMap.forEach { (_, markers) ->
710+
val toRemove = markers.filter { it.words == words }
711+
removedMarkers.addAll(toRemove)
712+
markers.removeAll(toRemove)
713+
}
714+
715+
markersMap.entries.removeIf { it.value.isEmpty() }
716+
} else {
717+
// Remove markers only from the specified list
718+
val markers = markersMap[listName]
719+
if (markers != null) {
720+
val toRemove = markers.filter { it.words == words }
721+
removedMarkers.addAll(toRemove)
722+
markers.removeAll(toRemove)
723+
724+
if (markers.isEmpty()) {
725+
markersMap.remove(listName)
726+
}
727+
}
728+
}
729+
}
730+
731+
_mapState.update {
732+
it.copy(
733+
markers = markersMap.toMarkers().toImmutableList()
734+
)
690735
}
691736

692737
return@withContext removedMarkers
@@ -698,14 +743,41 @@ class W3WMapManager(
698743
* @param listCoordinates list of The [W3WCoordinates] to remove markers from.
699744
* @param listName The name of the list from which markers should be removed. If null, markers will be removed from all lists.
700745
*/
746+
@JvmName("removeMarkerAtListCoordinates")
701747
suspend fun removeMarkersAt(
702748
listCoordinates: List<W3WCoordinates>,
703749
listName: String? = null
704750
): List<W3WMarker> = withContext(dispatcher) {
705751
val removedMarkers = mutableListOf<W3WMarker>()
706-
listCoordinates.forEach {
707-
val markers = removeMarker(it, listName)
708-
removedMarkers.addAll(markers)
752+
listCoordinates.forEach { coordinates ->
753+
if (listName == null) {
754+
// Remove markers from all lists
755+
markersMap.forEach { (_, markers) ->
756+
val toRemove = markers.filter { it.square.contains(coordinates) }
757+
removedMarkers.addAll(toRemove)
758+
markers.removeAll(toRemove)
759+
}
760+
761+
markersMap.entries.removeIf { it.value.isEmpty() }
762+
} else {
763+
// Remove markers only from the specified list
764+
val markers = markersMap[listName]
765+
if (markers != null) {
766+
val toRemove = markers.filter { it.square.contains(coordinates) }
767+
removedMarkers.addAll(toRemove)
768+
markers.removeAll(toRemove)
769+
770+
if (markers.isEmpty()) {
771+
markersMap.remove(listName)
772+
}
773+
}
774+
}
775+
}
776+
777+
_mapState.update {
778+
it.copy(
779+
markers = markersMap.toMarkers().toImmutableList()
780+
)
709781
}
710782

711783
return@withContext removedMarkers
@@ -717,11 +789,27 @@ class W3WMapManager(
717789
* @param addresses list of The What3Words address as a [W3WAddress].
718790
* @param listName The name of the list from which markers should be removed. If null, markers will be removed from all lists.
719791
*/
792+
@JvmName("removeMarkerAtAddresses")
720793
suspend fun removeMarkersAt(
721794
addresses: List<W3WAddress>,
722795
listName: String? = null
723796
): List<W3WMarker> = withContext(dispatcher) {
724-
removeMarkersAt(addresses.map { it.words }, listName)
797+
val removedMarkers = mutableListOf<W3WMarker>()
798+
799+
addresses.forEach { address ->
800+
val markersToRemove = address.center?.let {
801+
removeMarkerAt(it, listName)
802+
} ?: removeMarkerAt(address.words, listName)
803+
removedMarkers.addAll(markersToRemove)
804+
}
805+
806+
_mapState.update {
807+
it.copy(
808+
markers = markersMap.toMarkers().toImmutableList()
809+
)
810+
}
811+
812+
return@withContext removedMarkers
725813
}
726814

727815
/**
@@ -730,11 +818,12 @@ class W3WMapManager(
730818
* @param suggestions list of The What3Words address as a [W3WSuggestion].
731819
* @param listName The name of the list from which markers should be removed. If null, markers will be removed from all lists.
732820
*/
821+
@JvmName("removeMarkerAtSuggestions")
733822
suspend fun removeMarkersAt(
734823
suggestions: List<W3WSuggestion>,
735824
listName: String? = null
736825
): List<W3WMarker> = withContext(dispatcher) {
737-
removeMarkersAt(suggestions.map { it.w3wAddress.words }, listName)
826+
removeMarkersAt(suggestions.map { it.w3wAddress },listName)
738827
}
739828

740829
/**

0 commit comments

Comments
 (0)