diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.kt b/app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.kt index 90167f8e3..d2e73441d 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.kt +++ b/app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.kt @@ -11,6 +11,7 @@ import android.content.IntentFilter import android.content.res.Configuration import android.graphics.Color import android.graphics.Paint +import android.graphics.drawable.Drawable import android.location.Location import android.location.LocationManager import android.net.Uri @@ -219,6 +220,7 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(), NearbyParentFragmen @Volatile private var stopQuery = false + private var drawableCache: MutableMap, Drawable>? = null // Explore map data (for if we came from Explore) private var prevZoom = 0.0 @@ -721,6 +723,7 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(), NearbyParentFragmen startMapWithoutPermission() } } + drawableCache = HashMap() } /** @@ -1501,7 +1504,7 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(), NearbyParentFragmen marker.showInfoWindow() presenter!!.handlePinClicked(updatedPlace) savePlaceToDatabase(place) - val icon = ContextCompat.getDrawable( + val icon = getDrawable( requireContext(), getIconFor(updatedPlace, isBookMarked) ) @@ -2027,8 +2030,35 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(), NearbyParentFragmen ) } + /** + * Gets the specified Drawable object. This is a wrapper method for ContextCompat.getDrawable(). + * This method caches results from previous calls for faster retrieval. + * + * @param context The context to use to get the Drawable + * @param id The integer that describes the Drawable resource + * @return The Drawable object + */ + private fun getDrawable(context: Context?, id: Int?): Drawable? { + if (drawableCache == null || context == null || id == null) { + return null + } + + val key = Pair(context, id) + if (!drawableCache!!.containsKey(key)) { + val drawable = ContextCompat.getDrawable(context, id) + + if (drawable != null) { + drawableCache!![key] = drawable + } else { + return null + } + } + + return drawableCache!![key] + } + fun convertToMarker(place: Place, isBookMarked: Boolean): Marker { - val icon = ContextCompat.getDrawable(requireContext(), getIconFor(place, isBookMarked)) + val icon = getDrawable(requireContext(), getIconFor(place, isBookMarked)) val point = GeoPoint(place.location.latitude, place.location.longitude) val marker = Marker(binding!!.map) marker.position = point @@ -2430,7 +2460,7 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(), NearbyParentFragmen Marker.ANCHOR_BOTTOM ) startMarker.icon = - ContextCompat.getDrawable( + getDrawable( this.requireContext(), fr.free.nrw.commons.R.drawable.current_location_marker ) @@ -2488,7 +2518,7 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(), NearbyParentFragmen Marker(binding?.map).apply { position = it setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM) - icon = ContextCompat.getDrawable(context, R.drawable.current_location_marker) + icon = getDrawable(context, R.drawable.current_location_marker) title = "Your Location" textLabelFontSize = 24 overlays.add(this)