Skip to content

Commit

Permalink
First pass at removing language shared preference
Browse files Browse the repository at this point in the history
  • Loading branch information
nbradbury committed Dec 17, 2024
1 parent 58be3db commit a75a91b
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ import org.wordpress.android.localcontentmigration.LocalContentEntityData.UserFl
import org.wordpress.android.ui.prefs.AppPrefs.DeletablePrefKey
import org.wordpress.android.ui.prefs.AppPrefs.UndeletablePrefKey
import org.wordpress.android.ui.prefs.AppPrefsWrapper
import org.wordpress.android.util.LocaleManagerWrapper
import org.wordpress.android.viewmodel.ContextProvider
import javax.inject.Inject

class UserFlagsProviderHelper @Inject constructor(
private val appPrefsWrapper: AppPrefsWrapper,
contextProvider: ContextProvider,
localeManagerWrapper: LocaleManagerWrapper
) : LocalDataProviderHelper {
override fun getData(localEntityId: Int?): LocalContentEntityData =
UserFlagsData(
Expand Down Expand Up @@ -73,6 +71,5 @@ class UserFlagsProviderHelper @Inject constructor(
contextProvider.getContext().getString(R.string.pref_key_app_theme),
contextProvider.getContext().getString(R.string.pref_key_initial_screen),
contextProvider.getContext().getString(R.string.pref_key_send_crash),
localeManagerWrapper.getLocalePrefKeyString()
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.content.res.Configuration
import android.text.TextUtils
import androidx.preference.PreferenceManager
import org.wordpress.android.R
import org.wordpress.android.util.PerAppLocaleManager.Companion.OLD_LOCALE_PREF_KEY_STRING
import java.text.Collator
import java.util.Locale
import java.util.regex.Pattern
Expand All @@ -16,11 +17,6 @@ import java.util.regex.Pattern
* android version.
*/
object LocaleManager {
/**
* Key used for saving the language selection to shared preferences.
*/
private const val LOCALE_PREF_KEY_STRING: String = "language-pref"

/**
* Pattern to split a language string (to parse the language and region values).
*/
Expand Down Expand Up @@ -56,7 +52,7 @@ object LocaleManager {
@JvmStatic
fun getLanguage(context: Context): String {
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
return prefs.getString(LOCALE_PREF_KEY_STRING, LanguageUtils.getCurrentDeviceLanguageCode())!!
return prefs.getString(OLD_LOCALE_PREF_KEY_STRING, LanguageUtils.getCurrentDeviceLanguageCode())!!
}

/**
Expand Down Expand Up @@ -237,7 +233,4 @@ object LocaleManager {
displayLanguage
}
}

@JvmStatic
fun getLocalePrefKeyString(): String = LOCALE_PREF_KEY_STRING
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class LocaleManagerWrapper
fun getTimeZone(): TimeZone = TimeZone.getDefault()
fun getCurrentCalendar(): Calendar = Calendar.getInstance(getLocale())
fun getLanguage(): String = LocaleManager.getLanguage(context)
fun getLocalePrefKeyString(): String = LocaleManager.getLocalePrefKeyString()
fun isSameLanguage(language: String): Boolean = LocaleManager.isSameLanguage(language)
fun setLocale(context: Context): Context = LocaleManager.setLocale(context)
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,10 @@ class PerAppLocaleManager @Inject constructor(
* this once the per-app language pref is no longer experimental.
*/
fun checkAndUpdateOldLanguagePrefKey() {
val prefKey = LocaleManager.getLocalePrefKeyString()
val inAppLanguage = appPrefsWrapper.getPrefString(prefKey, "")
val inAppLanguage = appPrefsWrapper.getPrefString(OLD_LOCALE_PREF_KEY_STRING, "")
val perAppLanguage = getCurrentLocale().language
if (perAppLanguage.isNotEmpty() && inAppLanguage.equals(perAppLanguage).not()) {
appPrefsWrapper.setPrefString(prefKey, perAppLanguage)
appPrefsWrapper.setPrefString(OLD_LOCALE_PREF_KEY_STRING, perAppLanguage)
appLogWrapper.d(
AppLog.T.SETTINGS,
"PerAppLocaleManager: changed inAppLanguage from $inAppLanguage to $perAppLanguage"
Expand Down Expand Up @@ -94,8 +93,7 @@ class PerAppLocaleManager @Inject constructor(
*/
fun performMigrationIfNecessary() {
if (isApplicationLocaleEmpty()) {
val prefKey = LocaleManager.getLocalePrefKeyString()
val previousLanguage = appPrefsWrapper.getPrefString(prefKey, "")
val previousLanguage = appPrefsWrapper.getPrefString(OLD_LOCALE_PREF_KEY_STRING, "")
if (previousLanguage?.isNotEmpty() == true) {
appLogWrapper.d(
AppLog.T.SETTINGS,
Expand Down Expand Up @@ -155,4 +153,9 @@ class PerAppLocaleManager @Inject constructor(
// update Reader tags as they need be localized
ReaderUpdateServiceStarter.startService(getContext(), EnumSet.of(UpdateTask.TAGS))
}

companion object {
// Key previously used for saving the language selection to shared preferences.
const val OLD_LOCALE_PREF_KEY_STRING: String = "language-pref"
}
}

0 comments on commit a75a91b

Please sign in to comment.