Match currency search against issuing country names too
Searching "Japan" now finds JPY, "Canada" finds CAD, etc. — matching just the currency's own display name wasn't enough, since e.g. "Canadian Dollar" doesn't contain "Canada" as a substring. Adds a per-platform regionDisplayName(regionCode) (java.util.Locale on JVM/Android, NSLocale on Apple, Intl.DisplayNames on web) and reuses CurrencyFlag.kt's currency-to-country-code derivation (now extracted as issuingCountryCodes) to look up the country/countries behind a currency code. "EU" is hardcoded to "European Union" since it's not a real ISO 3166-1 code platform locale data can resolve. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FXcrACM5sok3M1KQJ8kByy
This commit is contained in:
@@ -16,24 +16,31 @@ private val MULTI_COUNTRY_CURRENCY_REGION_CODES: Map<String, List<String>> = map
|
||||
private const val MAX_FLAGS_PER_CURRENCY = 3
|
||||
|
||||
/**
|
||||
* A country flag emoji for [code], derived from the first two letters of the ISO 4217 code —
|
||||
* which double as the issuing country's ISO 3166-1 alpha-2 code for ordinary national
|
||||
* currencies — or null when there's no flag to show. [MULTI_COUNTRY_CURRENCY_REGION_CODES]
|
||||
* lists every country sharing a currency with no single issuer, so those show all their flags
|
||||
* side by side, unless there are more than [MAX_FLAGS_PER_CURRENCY] of them; the remaining
|
||||
* "X"-prefixed codes are precious metals, testing codes, and other non-national codes (XAU, XTS,
|
||||
* XXX, ...), none of which has a country to show. EUR is a special case handled separately, using
|
||||
* the EU's own flag.
|
||||
* ISO 3166-1 alpha-2 codes of every country that issues [code], derived from the first two
|
||||
* letters of the ISO 4217 code — which double as the issuing country's ISO 3166-1 alpha-2 code
|
||||
* for ordinary national currencies — or empty when [code] isn't tied to any country.
|
||||
* [MULTI_COUNTRY_CURRENCY_REGION_CODES] lists every country sharing a currency with no single
|
||||
* issuer; the remaining "X"-prefixed codes are precious metals, testing codes, and other
|
||||
* non-national codes (XAU, XTS, XXX, ...), none of which has a country. EUR is a special case,
|
||||
* using the EU's own region code. Shared by [currencyFlagEmoji] and currency search-by-country.
|
||||
*/
|
||||
internal fun issuingCountryCodes(code: String): List<String> {
|
||||
MULTI_COUNTRY_CURRENCY_REGION_CODES[code]?.let { return it }
|
||||
if (code.startsWith("X")) return emptyList()
|
||||
val regionCode = if (code == "EUR") "EU" else code.take(2)
|
||||
if (regionCode.length != 2 || regionCode.any { it !in 'A'..'Z' }) return emptyList()
|
||||
return listOf(regionCode)
|
||||
}
|
||||
|
||||
/**
|
||||
* A country flag emoji for [code], or null when there's no flag to show — either because [code]
|
||||
* isn't tied to any country, or because it's shared by more than [MAX_FLAGS_PER_CURRENCY]
|
||||
* countries, which would be too visually noisy to show side by side.
|
||||
*/
|
||||
fun currencyFlagEmoji(code: String): String? {
|
||||
MULTI_COUNTRY_CURRENCY_REGION_CODES[code]?.let { regionCodes ->
|
||||
if (regionCodes.size > MAX_FLAGS_PER_CURRENCY) return null
|
||||
return regionCodes.joinToString(" ") { regionFlagEmoji(it) }
|
||||
}
|
||||
if (code.startsWith("X")) return null
|
||||
val regionCode = if (code == "EUR") "EU" else code.take(2)
|
||||
if (regionCode.length != 2 || regionCode.any { it !in 'A'..'Z' }) return null
|
||||
return regionFlagEmoji(regionCode)
|
||||
val regionCodes = issuingCountryCodes(code)
|
||||
if (regionCodes.isEmpty() || regionCodes.size > MAX_FLAGS_PER_CURRENCY) return null
|
||||
return regionCodes.joinToString(" ") { regionFlagEmoji(it) }
|
||||
}
|
||||
|
||||
/** The flag emoji for the 2-letter ISO 3166-1 alpha-2 [regionCode]. */
|
||||
|
||||
@@ -3,14 +3,31 @@ package xyz.tyiu.satsprice
|
||||
data class CurrencyInfo(val code: String, val displayName: String)
|
||||
|
||||
/**
|
||||
* Whether [info]'s code or display name contains [query], case-insensitively. A plain function
|
||||
* (rather than a `CurrencyInfo` extension) so Kotlin/Native exports a predictable, positionally
|
||||
* clear Swift signature, matching [currencyFlagEmoji]'s style.
|
||||
* Whether [info]'s code, display name, or the localized name of any country that issues it (e.g.
|
||||
* "Japan" for JPY) contains [query], case-insensitively. A plain function (rather than a
|
||||
* `CurrencyInfo` extension) so Kotlin/Native exports a predictable, positionally clear Swift
|
||||
* signature, matching [currencyFlagEmoji]'s style.
|
||||
*/
|
||||
fun matchesCurrencySearch(info: CurrencyInfo, query: String): Boolean =
|
||||
query.isBlank() ||
|
||||
info.code.contains(query, ignoreCase = true) ||
|
||||
info.displayName.contains(query, ignoreCase = true)
|
||||
fun matchesCurrencySearch(info: CurrencyInfo, query: String): Boolean {
|
||||
if (query.isBlank()) return true
|
||||
if (info.code.contains(query, ignoreCase = true)) return true
|
||||
if (info.displayName.contains(query, ignoreCase = true)) return true
|
||||
return issuingCountryCodes(info.code).any { regionCode ->
|
||||
localizedRegionName(regionCode)?.contains(query, ignoreCase = true) == true
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A localized display name for ISO 3166-1 alpha-2 [regionCode] (e.g. "Canada"), or null if the
|
||||
* platform doesn't recognize it. "EU" is handled directly since it's not a real ISO 3166-1
|
||||
* country code — it's [issuingCountryCodes]' own stand-in for EUR's region/flag — and platform
|
||||
* locale data doesn't reliably resolve it to a name the way it does real country codes.
|
||||
*/
|
||||
private fun localizedRegionName(regionCode: String): String? =
|
||||
if (regionCode == "EU") "European Union" else regionDisplayName(regionCode)
|
||||
|
||||
/** The current platform's localized display name for ISO 3166-1 alpha-2 [regionCode], if known. */
|
||||
expect fun regionDisplayName(regionCode: String): String?
|
||||
|
||||
/**
|
||||
* ISO 4217 codes for the precious metals actively traded today. These aren't tied to any
|
||||
|
||||
Reference in New Issue
Block a user