• android: Preferences DataStore 和 Proto DataStore use guide


    replace SharedPreferences by DataStore with kotlin

    in Datastore Doc, it provide two way to use DataStore,

    user can use kotlin or java language to implement it. But in fact, use java to implement it will be very different.
    Maybe you can use Rxjava to simplify it, but in fact, use Rxjava to implement it is also not easy.
    use kotlin will very simple to implement it? yes. and it’s ONLY ONE WAY to use it by kotlin.

    steps

    1. init DataStore object:
      private val dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")
      private val switchStateKey = booleanPreferencesKey("toggle")
      
      • 1
      • 2
    2. get value from defined key:
          dataStore.data.map {
              it[switchStateKey] ?: false
          }.asLiveData().observe(this) {
              Log.d(TAG, "getToggleSwitchState: $it")
              binding.preferenceSwitch.isChecked = it
          }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
    3. set value to defined key:
      isChecked = !isChecked
      lifecycleScope.launch {
          val key = switchStateKey
          dataStore.edit { preferences ->
              preferences[key] = isChecked
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7

    it’s all of use DataStore replace SharedPreferences.

    use Proto DataStore

    steps:

    1. define xx.proto file (path: [module]/src/main/proto/xxx.proto)
      syntax = "proto3";
      
      option java_package = "com.example.application";
      option java_multiple_files = true;
      
      message ProtoCounter {
         int32 example_counter = 1;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
    2. define proto datastore struct
      object SettingsSerializer : Serializer<ProtoCounter> {
         override val defaultValue: ProtoCounter = ProtoCounter.getDefaultInstance()
      
          override suspend fun readFrom(input: InputStream): ProtoCounter {
              try {
                  return ProtoCounter.parseFrom(input)
              } catch (exception: InvalidProtocolBufferException) {
                  throw CorruptionException("Cannot read proto.", exception)
              }
          }
         
          override suspend fun writeTo(
              t: ProtoCounter,
              output: OutputStream
          ) = t.writeTo(output)
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
    3. init proto datastore object
         val Context.settingsDataStore: DataStore<ProtoCounter> by dataStore(
              fileName = "settings.pb",
              serializer = SettingsSerializer
         )
      
      • 1
      • 2
      • 3
      • 4
    4. set value to proto datastore
           binding.protoCounter.setOnClickListener {
               lifecycleScope.launch {
                   settingsDataStore.updateData { currentSettings ->
                       currentSettings.toBuilder()
                           .setExampleCounter(currentSettings.exampleCounter + 1)
                           .build()
                   }
               }
           }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
    5. get value from proto datastore
           settingsDataStore.data
               .map { settings ->
                   // The exampleCounter property is generated from the proto schema.
                   settings.exampleCounter
               }.asLiveData().observe(this) {
                   it?.let {
                       binding.protoCounter.text = getString(R.string.proto_counter_text, it)
                   }
               }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9

    it’s all of use Proto DataStore.

    can not use a chinese input method, so …

  • 相关阅读:
    尚硅谷Vue视频学习打卡-Vue组件化编程以及Vue脚手架的安装-简要介绍
    FFA interface
    springboot+poi-tl根据模板导出word(含动态表格和图片),并将导出的文档压缩zip导出
    Rycky9.0安装k8s1.25.0+containerd
    AVL树的插入(C++实现)
    1)SpringBoot简介
    Python基础语法(五)—— 文件基本操作(打开、写入、关闭、查找)
    OOALV总结
    进程转态及其转换过程
    C++项目实战——基于多设计模式下的同步&异步日志系统-⑤-实用工具类设计
  • 原文地址:https://blog.csdn.net/DucklikeJAVA/article/details/126304897