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.
This commit is contained in:
Jason Whitmore 2025-02-18 17:15:23 -08:00
parent 30332a21d6
commit d4df9c73f3

View file

@ -11,6 +11,7 @@ import android.content.IntentFilter
import android.content.res.Configuration import android.content.res.Configuration
import android.graphics.Color import android.graphics.Color
import android.graphics.Paint import android.graphics.Paint
import android.graphics.drawable.Drawable
import android.location.Location import android.location.Location
import android.location.LocationManager import android.location.LocationManager
import android.net.Uri import android.net.Uri
@ -219,6 +220,7 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(), NearbyParentFragmen
@Volatile @Volatile
private var stopQuery = false private var stopQuery = false
private var drawableCache: MutableMap<Pair<Context, Int>, Drawable>? = null
// Explore map data (for if we came from Explore) // Explore map data (for if we came from Explore)
private var prevZoom = 0.0 private var prevZoom = 0.0
@ -721,6 +723,7 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(), NearbyParentFragmen
startMapWithoutPermission() startMapWithoutPermission()
} }
} }
drawableCache = HashMap()
} }
/** /**
@ -1501,7 +1504,7 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(), NearbyParentFragmen
marker.showInfoWindow() marker.showInfoWindow()
presenter!!.handlePinClicked(updatedPlace) presenter!!.handlePinClicked(updatedPlace)
savePlaceToDatabase(place) savePlaceToDatabase(place)
val icon = ContextCompat.getDrawable( val icon = getDrawable(
requireContext(), requireContext(),
getIconFor(updatedPlace, isBookMarked) 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 { 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 point = GeoPoint(place.location.latitude, place.location.longitude)
val marker = Marker(binding!!.map) val marker = Marker(binding!!.map)
marker.position = point marker.position = point
@ -2430,7 +2460,7 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(), NearbyParentFragmen
Marker.ANCHOR_BOTTOM Marker.ANCHOR_BOTTOM
) )
startMarker.icon = startMarker.icon =
ContextCompat.getDrawable( getDrawable(
this.requireContext(), this.requireContext(),
fr.free.nrw.commons.R.drawable.current_location_marker fr.free.nrw.commons.R.drawable.current_location_marker
) )
@ -2488,7 +2518,7 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(), NearbyParentFragmen
Marker(binding?.map).apply { Marker(binding?.map).apply {
position = it position = it
setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM) 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" title = "Your Location"
textLabelFontSize = 24 textLabelFontSize = 24
overlays.add(this) overlays.add(this)