Compose has been coming out for a long time, and various kinds of remember and localxxx. current are becoming more and more familiar. Wouldn’t it be nice to be able to persist data in a consistent way? Based on this, I wrote an open source library: ComposeDataSaver: A simple glance at gracefully doing data persistence in Jetpack Compose:

// booleanExample initializes to false
// The local data will be read automatically
var booleanExample by rememberDataSaverState(KEY_BOOLEAN_EXAMPLE, false)
// Direct assignment to complete persistence
booleanExample = true
Copy the code

Can be ok?

ComposeDataSaver

The project has the following features:

  • Brevity: Approximate native writing
  • Low coupling: Abstract interface that does not limit the implementation of the underlying save algorithm
  • Lightweight: Do not introduce any third-party libraries other than Compose by default
  • Flexibility: Support for basic data types and custom types


The introduction of

Introduces the jitpack repository location in settings.gradle

 dependencyResolutionManagement {
     repositories {
         maven { url "https://jitpack.io" }
     }
 }
Copy the code

Introduced in the project build.gradle

Dependencies {implementation 'com. Making. FunnySaltyFish: ComposeDataSaver: v1.0.0'}Copy the code

The basic use

The project uses the implementation class of the DataSaverInterface interface to store the data, so you need to provide one of these objects first.

The project default contains the implementation class DataSaverPreferences for saving data using Preference, which can be initialized as follows:

 // init preferences
 val dataSaverPreferences = DataSaverPreferences().apply {
     setContext(context = applicationContext)
 }
 CompositionLocalProvider(LocalDataSaver provides dataSaverPreferences){
     ExampleComposable()
 }
Copy the code

You can then use LocalDataSaver. Current to get the current instance within ExampleComposable and its child widgets

For basic data types (e.g. String/Int/Boolean) :

 // booleanExample initializes to false
 // The local data will be read automatically
 var booleanExample by rememberDataSaverState(KEY_BOOLEAN_EXAMPLE, false)
 // Direct assignment to complete persistence
 booleanExample = true
Copy the code

It’s that simple!

Custom storage framework

Just implement the DataSaverInterface class and rewrite the saveData and readData methods to save and readData, respectively

 interface DataSaverInterface{
     fun <T> saveData(key:String.data : T)
     fun <T> readData(key: String, default : T) : T
 }
Copy the code

Then change the objects supplied by LocalDataSaver to your own class instance

 val dataStore = DataSaverDataStore()
 CompositionLocalProvider(LocalDataSaver provides dataStore){
     ExampleComposable()
 }
Copy the code

Use it the same way later.

Save custom types

The default DataSaverPreferences does not provide for saving custom types (an error is reported when you try to do so). Although persisting entity classes is not recommended, you can do so. You can do this in the following ways.

  1. Rewrite your ownDataSaverInterfaceImplement the class (see above) and implement the associated save methods
  2. Serialize the entity class to some other primitive type (such as String) and store it

In the second case, you need to add a converter for the corresponding entity class to automatically convert to String when saved. The method is as follows:

 @Serializable
 data class ExampleBean(var id:Int.val label:String)
 // ------------ //
 ​
 // Call the registerTypeConverters method at initialization to register the corresponding conversion method
 // This method takes two arguments: the entity Class and the corresponding conversion method (Lambda expression)
 registerTypeConverters(ExampleBean::class.java) {
     val bean = it as ExampleBean
     Json.encodeToString(bean)
 }
Copy the code

See the sample project for a complete example

More Settings

  1. This can be set if you don’t want to persist frequently in certain situationsrememberDataSaverStatetheautoSaveParameters forfalseIn this case, the assignment operation of the object will not be persisted. You can manually save it in the location where you want to save it:LocalDataSaver.current.saveData()

subsequent

New project, hope you like it. Why don’t you make a Star? ! The year of the tiger is coming, I wish you all the best in advance, huhu wind! The final answer is read address: FunnySaltyFish/ComposeDataSaver: the Jetpack Compose elegant finish data persistence in (github.com)