From d4df9c73f3a50f2087531c8a15fa139145a69a07 Mon Sep 17 00:00:00 2001 From: Jason Whitmore Date: Tue, 18 Feb 2025 17:15:23 -0800 Subject: [PATCH] NearbyParentFragment.kt: rewrite Java Drawable caching code to Kotlin Before this commit, the code which cached Drawables was written in Java. This commit rewrites that code into the new Kotlin file, which replaces the Java file. --- .../nearby/fragments/NearbyParentFragment.kt | 38 +++++++++++++++++-- 1 file changed, 34 insertions(+), 4 deletions(-) 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)