android - Is it possible for a Proto Datastore boolean value to be stored with a default value of `true`? - Stack Overflow

admin2025-04-22  1

In my application I am using Proto Datastore to store user preferences.

Among those values ​​there is field called useVoiceReader of type boolean that is created with a default value of false.

Is there a way to force that field to be created with a default value of true?

This is the class that serves as model for preferences.

data class UserData(
    val themeBrand: ThemeBrand,
    val shouldHideOnboarding: Boolean,
    val dynamic: UserDataDynamic    
)

data class UserDataDynamic(
    val darkThemeConfig: DarkThemeConfig,
    val useDynamicColor: Boolean,
    val useVoiceReader: Boolean = true,
    val useMultipleInvitatory: Boolean,
    val rubricColor: RubricColorConfig,
)

I want useVoiceReader to be created with a default value of true, but that doesn't happen.

This is the relevant part of my class PreferencesDataSource:

class PreferencesDataSource @Inject constructor(
    private val userPreferences: DataStore<UserPreferences>,
) {
    val userData = userPreferences.data
        .map {
            UserData(
                bookmarkedNewsResources = it.bookmarkedNewsResourceIdsMap.keys,
                viewedNewsResources = it.viewedNewsResourceIdsMap.keys,
                followedTopics = it.followedTopicIdsMap.keys,
                themeBrand = when (it.themeBrand) {
                    null,
                    ThemeBrandProto.THEME_BRAND_UNSPECIFIED,
                    ThemeBrandProto.UNRECOGNIZED,
                    ThemeBrandProto.THEME_BRAND_DEFAULT,
                    -> ThemeBrand.DEFAULT

                    ThemeBrandProto.THEME_BRAND_ANDROID -> ThemeBrand.ANDROID
                },
                shouldHideOnboarding = it.shouldHideOnboarding,

                dynamic = UserDataDynamic(
                darkThemeConfig = when (it.darkThemeConfig) {
                    null,
                    DarkThemeConfigProto.DARK_THEME_CONFIG_UNSPECIFIED,
                    DarkThemeConfigProto.UNRECOGNIZED,
                    DarkThemeConfigProto.DARK_THEME_CONFIG_FOLLOW_SYSTEM,
                    ->
                        DarkThemeConfig.FOLLOW_SYSTEM
                    DarkThemeConfigProto.DARK_THEME_CONFIG_LIGHT ->
                        DarkThemeConfig.LIGHT
                    DarkThemeConfigProto.DARK_THEME_CONFIG_DARK -> DarkThemeConfig.DARK
                },
                    rubricColor = when (it.rubricColor) {
                        null,
                        RubricColorProto.RUBRIC_COLOR_LIGHT,
                        ->
                            RubricColorConfig.LIGHT

                        RubricColorProto.RUBRIC_COLOR_DARK,
                        ->
                            RubricColorConfig.DARK

                        RubricColorProto.UNRECOGNIZED -> RubricColorConfig.LIGHT
                    },
                useDynamicColor = it.useDynamicColor,
                    useVoiceReader = it.useVoiceReader,
                    useMultipleInvitatory = it.useMultipleInvitatory
                )
            )
        }

    suspend fun setVoiceReaderPreference(useVoiceReader: Boolean) {
        userPreferences.updateData {
            it.copy { this.useVoiceReader = useVoiceReader }
        }
    }

}

Here the relevant parts of my proto file:

syntax = "proto3";

option java_package = "***";
option java_multiple_files = true;

message UserPreferences {
  reserved 2;
  
  // ...

  ThemeBrandProto theme_brand = 16;
  DarkThemeConfigProto dark_theme_config = 17;
  RubricColorProto rubric_color = 18;

  bool should_hide_onboarding = 19;

  bool use_dynamic_color = 21;

  bool use_voice_reader = 22;

  bool use_multiple_invitatory = 23;

  // NEXT AVAILABLE ID: 24

}

转载请注明原文地址:http://anycun.com/QandA/1745300202a90562.html