为什么我的 Fragment 总是只从 jetPack dataStore 获取默认值,即使它(值)已经被更改了?
Why my Fragment gets always only default value from jetPack dataStore, even if it (the value) has been changed already?
为什么每次我打开 Fragment 时,switchButton 总是处于默认 nativeToForeign 值的位置(始终是默认分配给 dataStore 中的 nativToForeign 变量的值)(带有相应的文本),尽管当我单击 switchButton 时,在 dataStore 日志中,我看到更改已成功完成?
数据存储:
@Singleton
class PreferencesManager @Inject constructor(@ApplicationContext context: Context) {
private val dataStore = context.createDataStore("user_preferences")
val categoryNumberFlow = dataStore.data
.catch { exception ->
if (exception is IOException) {
Log.e(TAG, "Error reading preferences", exception)
emit(emptyPreferences())
} else {
throw exception
}
}
.map { preferences ->
val categoryChosen = preferences[PreferencesKeys.CATEGORY_CHOSEN] ?:
categoryChosen
}
val translationDirectionFlow = dataStore.data
.catch { exception ->
if (exception is IOException) {
Log.e(TAG, "Error reading preferences", exception)
emit(emptyPreferences())
} else {
throw exception
}
}
.map { preferences ->
val nativToForeign = preferences[PreferencesKeys.NATIV_TO_FOREIGN] ?: false //always this value, if
//if I change it to true - switchButton is always turned ON (with respective String value of text)
Log.d(TAG, "So, trans nativeTOForeign: " + nativToForeign)
nativToForeign
}
suspend fun updateCategoryChosen(categoryChosen: Int) {
dataStore.edit { preferences ->
preferences[PreferencesKeys.CATEGORY_CHOSEN] = categoryChosen
Log.d(TAG, "updateCategoryChosen: $categoryChosen")
}
}
suspend fun updateTranslationDirection(nativToForeign: Boolean) {
dataStore.edit { preferences ->
preferences[PreferencesKeys.NATIV_TO_FOREIGN]
Log.d(TAG, "updateTranslationDirection: $nativToForeign")
}
}
private object PreferencesKeys {
val CATEGORY_CHOSEN = preferencesKey<Int>("category_chosen")
val NATIV_TO_FOREIGN = preferencesKey<Boolean>("nativ_to_foreign")
}
}
片段:
class SettingsFragment : Fragment(R.layout.settings_layout) {
private val viewModel: SettingsViewModel by viewModels()
private lateinit var binding: SettingsLayoutBinding
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
/*val */binding = SettingsLayoutBinding.bind(view)
binding.apply {
viewModel.readTransDir.observe(viewLifecycleOwner) {
Log.d(TAG, "onViewCreated: transDirPreference = $it")
switchTranslationDirection.isChecked = it
switchTranslationDirection.text =
if (it) "Native to swedish" else "Swedish to native"
}
}
binding.switchTranslationDirection.setOnCheckedChangeListener { compoundButton, boolean ->
viewModel.saveTransDir(boolean)
}
setHasOptionsMenu(true)
}
}
视图模型:
class SettingsViewModel @ViewModelInject constructor(
private val preferencesManager: PreferencesManager
) : ViewModel() {
private val transDirFlow = preferencesManager.translationDirectionFlow
val readTransDir = transDirFlow.asLiveData()
fun saveTransDir(nativToForeign: Boolean) = viewModelScope.launch {
preferencesManager.updateTranslationDirection(nativToForeign)
}
}
您在 updateTranslationDirection
中忘记了“= nativeToForeign”
为什么每次我打开 Fragment 时,switchButton 总是处于默认 nativeToForeign 值的位置(始终是默认分配给 dataStore 中的 nativToForeign 变量的值)(带有相应的文本),尽管当我单击 switchButton 时,在 dataStore 日志中,我看到更改已成功完成?
数据存储:
@Singleton
class PreferencesManager @Inject constructor(@ApplicationContext context: Context) {
private val dataStore = context.createDataStore("user_preferences")
val categoryNumberFlow = dataStore.data
.catch { exception ->
if (exception is IOException) {
Log.e(TAG, "Error reading preferences", exception)
emit(emptyPreferences())
} else {
throw exception
}
}
.map { preferences ->
val categoryChosen = preferences[PreferencesKeys.CATEGORY_CHOSEN] ?:
categoryChosen
}
val translationDirectionFlow = dataStore.data
.catch { exception ->
if (exception is IOException) {
Log.e(TAG, "Error reading preferences", exception)
emit(emptyPreferences())
} else {
throw exception
}
}
.map { preferences ->
val nativToForeign = preferences[PreferencesKeys.NATIV_TO_FOREIGN] ?: false //always this value, if
//if I change it to true - switchButton is always turned ON (with respective String value of text)
Log.d(TAG, "So, trans nativeTOForeign: " + nativToForeign)
nativToForeign
}
suspend fun updateCategoryChosen(categoryChosen: Int) {
dataStore.edit { preferences ->
preferences[PreferencesKeys.CATEGORY_CHOSEN] = categoryChosen
Log.d(TAG, "updateCategoryChosen: $categoryChosen")
}
}
suspend fun updateTranslationDirection(nativToForeign: Boolean) {
dataStore.edit { preferences ->
preferences[PreferencesKeys.NATIV_TO_FOREIGN]
Log.d(TAG, "updateTranslationDirection: $nativToForeign")
}
}
private object PreferencesKeys {
val CATEGORY_CHOSEN = preferencesKey<Int>("category_chosen")
val NATIV_TO_FOREIGN = preferencesKey<Boolean>("nativ_to_foreign")
}
}
片段:
class SettingsFragment : Fragment(R.layout.settings_layout) {
private val viewModel: SettingsViewModel by viewModels()
private lateinit var binding: SettingsLayoutBinding
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
/*val */binding = SettingsLayoutBinding.bind(view)
binding.apply {
viewModel.readTransDir.observe(viewLifecycleOwner) {
Log.d(TAG, "onViewCreated: transDirPreference = $it")
switchTranslationDirection.isChecked = it
switchTranslationDirection.text =
if (it) "Native to swedish" else "Swedish to native"
}
}
binding.switchTranslationDirection.setOnCheckedChangeListener { compoundButton, boolean ->
viewModel.saveTransDir(boolean)
}
setHasOptionsMenu(true)
}
}
视图模型:
class SettingsViewModel @ViewModelInject constructor(
private val preferencesManager: PreferencesManager
) : ViewModel() {
private val transDirFlow = preferencesManager.translationDirectionFlow
val readTransDir = transDirFlow.asLiveData()
fun saveTransDir(nativToForeign: Boolean) = viewModelScope.launch {
preferencesManager.updateTranslationDirection(nativToForeign)
}
}
您在 updateTranslationDirection
中忘记了“= nativeToForeign”