refactor(preferences): DataStore 설정 저장 안정성을 높인다
This commit is contained in:
@@ -1,21 +1,14 @@
|
||||
package kr.co.vividnext.sodalive.settings.language
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import android.os.Build
|
||||
import androidx.core.content.edit
|
||||
import androidx.preference.PreferenceManager
|
||||
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
|
||||
|
||||
object LanguageManager {
|
||||
const val LANG_KO = "ko"
|
||||
const val LANG_EN = "en"
|
||||
const val LANG_JA = "ja"
|
||||
|
||||
private const val PREF_KEY_APP_LANGUAGE = "pref_app_language_code"
|
||||
|
||||
private fun prefs(context: Context): SharedPreferences =
|
||||
PreferenceManager.getDefaultSharedPreferences(context.applicationContext)
|
||||
|
||||
fun isSupported(code: String): Boolean = when (code) {
|
||||
LANG_KO, LANG_EN, LANG_JA -> true
|
||||
else -> false
|
||||
@@ -25,8 +18,9 @@ object LanguageManager {
|
||||
* 사용자가 앱 내에서 명시적으로 선택한 언어 코드를 반환한다. 없으면 null.
|
||||
*/
|
||||
fun getUserSelectedLanguageOrNull(context: Context): String? {
|
||||
val code = prefs(context).getString(PREF_KEY_APP_LANGUAGE, null)
|
||||
return code?.takeIf { it.isNotBlank() }
|
||||
SharedPreferenceManager.init(context.applicationContext)
|
||||
val code = SharedPreferenceManager.appLanguageCode
|
||||
return code.takeIf { it.isNotBlank() }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -64,6 +58,7 @@ object LanguageManager {
|
||||
|
||||
fun setSelectedLanguage(context: Context, code: String) {
|
||||
val normalized = if (isSupported(code)) code else LANG_KO
|
||||
prefs(context).edit { putString(PREF_KEY_APP_LANGUAGE, normalized) }
|
||||
SharedPreferenceManager.init(context.applicationContext)
|
||||
SharedPreferenceManager.appLanguageCode = normalized
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user