Simplify cacheCountryForIP

pull/1684/head
bemusementpark 7 months ago
parent 13be4720ba
commit 784de3487d

@ -111,26 +111,16 @@ class IP2Country private constructor(private val context: Context) {
} }
private fun cacheCountryForIP(ip: String): String? { private fun cacheCountryForIP(ip: String): String? {
// return early if cached // return early if cached
countryNamesCache[ip]?.let { return it } countryNamesCache[ip]?.let { return it }
val ipInt = ipv4Int(ip) val ipInt = ipv4Int(ip)
val bestMatchCountry = ipv4ToCountry.floorEntry(ipInt)?.let { (_, code) -> val bestMatchCountry = ipv4ToCountry.floorEntry(ipInt)?.value?.let { countryToNames[it] }
if (code != null) {
countryToNames[code]
} else {
null
}
}
if (bestMatchCountry != null) { if (bestMatchCountry != null) countryNamesCache[ip] = bestMatchCountry
countryNamesCache[ip] = bestMatchCountry else Log.d("Loki","Country name for $ip couldn't be found")
return bestMatchCountry
} else { return bestMatchCountry
Log.d("Loki","Country name for $ip couldn't be found")
}
return null
} }
private fun populateCacheIfNeeded() { private fun populateCacheIfNeeded() {

Loading…
Cancel
Save