0

为什么每次我打开 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)
    }

}
4

1 回答 1

1

您在 updateTranslationDirection 中忘记了“= nativeToForeign”

于 2021-04-20T09:00:45.720 回答