|
|
|
|
@ -7,47 +7,55 @@ import java.util.* |
|
|
|
|
class Preferences { |
|
|
|
|
|
|
|
|
|
enum class Keys(var identifier: String) { |
|
|
|
|
CURRENCY_LANGUAGE("CurrencyLanguage") |
|
|
|
|
CURRENCY_CODE("CurrencyCode") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
companion object { |
|
|
|
|
|
|
|
|
|
var currencyLocale : Locale? = null |
|
|
|
|
|
|
|
|
|
fun setString(key: Keys, value: String, context: Context) { |
|
|
|
|
var preferences = PreferenceManager.getDefaultSharedPreferences(context) |
|
|
|
|
var editor = preferences.edit() |
|
|
|
|
val preferences = PreferenceManager.getDefaultSharedPreferences(context) |
|
|
|
|
val editor = preferences.edit() |
|
|
|
|
editor.putString(key.identifier, value) |
|
|
|
|
editor.commit() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun getString(key: Keys, context: Context) : String? { |
|
|
|
|
var preferences = PreferenceManager.getDefaultSharedPreferences(context) |
|
|
|
|
val preferences = PreferenceManager.getDefaultSharedPreferences(context) |
|
|
|
|
return preferences.getString(key.identifier, null) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun setCurrencyLanguage(language: String, context: Context) { |
|
|
|
|
Preferences.setString(Keys.CURRENCY_LANGUAGE, language, context) |
|
|
|
|
fun setCurrencyCode(currencyCode: String, context: Context) { |
|
|
|
|
Preferences.setString(Keys.CURRENCY_CODE, currencyCode, context) |
|
|
|
|
currencyLocale = null |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun getCurrencyLocale(context : Context) : Locale { |
|
|
|
|
private fun getCurrencyCode(context: Context) : String? { |
|
|
|
|
return Preferences.getString(Keys.CURRENCY_CODE, context) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
fun getCurrencyLocale(context : Context) : Locale { |
|
|
|
|
|
|
|
|
|
java.util.Currency usd = java.util.Currency.getInstance("USD"); |
|
|
|
|
java.text.NumberFormat format = java.text.NumberFormat.getCurrencyInstance( |
|
|
|
|
java.util.Locale.JAPAN); |
|
|
|
|
format.setCurrency(usd); |
|
|
|
|
System.out.println(format.format(23.23)); |
|
|
|
|
format.setMaximumFractionDigits(usd.getDefaultFractionDigits()); |
|
|
|
|
System.out.println(format.format(23.23)); |
|
|
|
|
currencyLocale?. let { |
|
|
|
|
return it |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
|
val currencyLanguage = Preferences.getString(Keys.CURRENCY_LANGUAGE, context) |
|
|
|
|
if (currencyLanguage != null) { |
|
|
|
|
return Locale(currencyLanguage) |
|
|
|
|
Preferences.getCurrencyCode(context)?.let { currencyCode -> |
|
|
|
|
Locale.getAvailableLocales().filter{ |
|
|
|
|
try { |
|
|
|
|
Currency.getInstance(it).currencyCode == currencyCode |
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
false |
|
|
|
|
} |
|
|
|
|
}.first().let { |
|
|
|
|
currencyLocale = it |
|
|
|
|
return it |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return Locale.getDefault() |
|
|
|
|
currencyLocale = Locale.getDefault() |
|
|
|
|
return currencyLocale!! |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |