From 8ccc91024e370058b5890cb7d55f373723683801 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Mon, 16 Jan 2023 12:14:30 +0530 Subject: [PATCH] Simplify country detection further. --- .../com/github/libretube/util/LocaleHelper.kt | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/github/libretube/util/LocaleHelper.kt b/app/src/main/java/com/github/libretube/util/LocaleHelper.kt index f2c377c02..9f28e1915 100644 --- a/app/src/main/java/com/github/libretube/util/LocaleHelper.kt +++ b/app/src/main/java/com/github/libretube/util/LocaleHelper.kt @@ -2,7 +2,6 @@ package com.github.libretube.util import android.content.Context import android.content.res.Configuration -import android.content.res.Resources import android.os.Build import android.telephony.TelephonyManager import androidx.core.content.getSystemService @@ -40,30 +39,30 @@ object LocaleHelper { @Suppress("DEPRECATION") private fun updateResourcesLegacy(context: Context, locale: Locale) { Locale.setDefault(locale) - val resources: Resources = context.resources + val resources = context.resources val configuration = resources.configuration configuration.locale = locale resources.updateConfiguration(configuration, resources.displayMetrics) } private fun getDetectedCountry(context: Context): String { - return detectSIMCountry(context).ifEmpty { - detectNetworkCountry(context).ifEmpty { - detectLocaleCountry(context).ifEmpty { "UK" } - } - } + return detectSIMCountry(context) + ?: detectNetworkCountry(context) + ?: detectLocaleCountry(context) + ?: "UK" } - private fun detectSIMCountry(context: Context): String { - return context.getSystemService()?.simCountryIso.orEmpty() + private fun detectSIMCountry(context: Context): String? { + return context.getSystemService()?.simCountryIso?.ifEmpty { null } } - private fun detectNetworkCountry(context: Context): String { - return context.getSystemService()?.networkCountryIso.orEmpty() + private fun detectNetworkCountry(context: Context): String? { + return context.getSystemService()?.networkCountryIso?.ifEmpty { null } } - private fun detectLocaleCountry(context: Context): String { + private fun detectLocaleCountry(context: Context): String? { return ConfigurationCompat.getLocales(context.resources.configuration)[0]!!.country + .ifEmpty { null } } fun getAvailableCountries(): List {