Skip to content

Commit d05e2b6

Browse files
committed
fix(locale): make default country determination more robust
Signed-off-by: Brandon McAnsh <[email protected]>
1 parent 2003062 commit d05e2b6

File tree

6 files changed

+39
-11
lines changed

6 files changed

+39
-11
lines changed

libs/locale/impl/src/main/kotlin/com/getcode/util/locale/AndroidLocale.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class AndroidLocale @Inject constructor(
1212
return LocaleUtils.getDefaultCurrency(context)
1313
}
1414

15-
override fun getDefaultCountry(): String {
15+
override suspend fun getDefaultCountry(): String? {
1616
return LocaleUtils.getDefaultCountry(context)
1717
}
1818
}

libs/locale/public/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ android {
3232
dependencies {
3333
implementation(project(":libs:datetime"))
3434
implementation(project(":libs:currency"))
35+
implementation(Libs.okhttp)
3536
api(Libs.androidx_annotation)
3637
api(Libs.kotlin_stdlib)
3738
api(Libs.kotlinx_coroutines_core)

libs/locale/public/src/main/kotlin/com/getcode/util/locale/LocaleHelper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ package com.getcode.util.locale
22

33
interface LocaleHelper {
44
fun getDefaultCurrencyName(): String
5-
fun getDefaultCountry(): String
5+
suspend fun getDefaultCountry(): String?
66
}

libs/locale/public/src/main/kotlin/com/getcode/util/locale/LocaleUtils.kt

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,44 @@ import android.telephony.TelephonyManager
55
import com.getcode.model.CurrencyCode
66
import com.getcode.model.CurrencyCode.Companion
77
import com.getcode.model.RegionCode
8+
import kotlinx.coroutines.Dispatchers
9+
import kotlinx.coroutines.withContext
10+
import okhttp3.OkHttpClient
11+
import okhttp3.Request
12+
import org.json.JSONObject
813
import java.util.*
14+
import java.util.concurrent.TimeUnit
915

1016
object LocaleUtils {
11-
fun getDefaultCountry(context: Context): String {
17+
suspend fun getDefaultCountry(context: Context): String? {
1218
val tm = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
1319
val networkCountryIso = tm.networkCountryIso.uppercase()
1420
val simCountryIso = tm.simCountryIso.uppercase()
15-
return simCountryIso.ifBlank { networkCountryIso }
21+
val localeCountry = context.resources.configuration.locale.country.uppercase()
22+
23+
val simCountry = if (simCountryIso.isNotBlank() && simCountryIso.length == 2) simCountryIso else null
24+
val localeCountryValid = if (localeCountry.isNotBlank() && localeCountry.length == 2) localeCountry else null
25+
val networkCountry = if (networkCountryIso.isNotBlank() && networkCountryIso.length == 2) networkCountryIso else null
26+
27+
return simCountry ?: localeCountryValid ?: networkCountry ?:getCountryFromIp()
28+
}
29+
30+
private suspend fun getCountryFromIp(): String? = withContext(Dispatchers.IO) {
31+
try {
32+
val client = OkHttpClient.Builder()
33+
.connectTimeout(5, TimeUnit.SECONDS)
34+
.readTimeout(5, TimeUnit.SECONDS)
35+
.build()
36+
val request = Request.Builder().url("http://ip-api.com/json/?fields=countryCode").build()
37+
val response = client.newCall(request).execute()
38+
if (response.isSuccessful) {
39+
val json = JSONObject(response.body?.string() ?: "{}")
40+
val ipCountry = json.optString("countryCode", "").uppercase()
41+
if (ipCountry.length == 2) ipCountry else "US"
42+
} else null
43+
} catch (e: Exception) {
44+
return@withContext null
45+
}
1646
}
1747

1848
fun getDefaultCurrency(context: Context): String {

services/flipcash/src/main/kotlin/com/flipcash/services/controllers/AccountController.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.flipcash.services.repository.AccountRepository
55
import com.flipcash.services.user.UserManager
66
import com.getcode.opencode.model.core.ID
77
import com.getcode.util.locale.LocaleHelper
8+
import com.getcode.utils.trace
89
import javax.inject.Inject
910

1011
class AccountController @Inject constructor(
@@ -31,7 +32,9 @@ class AccountController @Inject constructor(
3132
val userId = userManager.accountId
3233
?: return Result.failure(Throwable("No user ID in UserManager"))
3334

34-
val countryCode = localeHelper.getDefaultCountry()
35+
val countryCode = localeHelper.getDefaultCountry().orEmpty()
36+
37+
trace("user's device country code is $countryCode")
3538

3639
return repository.getUserFlags(
3740
owner = owner,

services/flipcash/src/main/kotlin/com/flipcash/services/controllers/ProfileController.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ class ProfileController @Inject constructor(
3030
type = TraceType.Process,
3131
)
3232
userManager.set(it)
33-
}.onFailure {
34-
trace(
35-
tag = "Profile",
36-
message = "Failed to update user profile",
37-
type = TraceType.Error
38-
)
3933
}
4034
}
4135

0 commit comments

Comments
 (0)