[ Kotlin ] Integrate JetPack DataStore Into KotlinNative

发布于:2025-01-14 ⋅ 阅读:(19) ⋅ 点赞:(0)
Dependency

this is a kotlin dependency that can be applied in non-android project

dependencies {
    api("androidx.datastore:datastore-core:+")
    api("androidx.datastore:datastore-preferences-core:+")
    api("com.google.protobuf:protobuf-kotlin:3.22.2")
    api("io.github.hellogoogle2000:kotlin-commons:+")
}
Preference DataStore

visiting DataStore is an IO action, so preference value should always be obtained in coroutine scope

import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.intPreferencesKey
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import okio.Path.Companion.toPath
import x.kotlin.commons.coroutine.XDispatchers

fun main() {
    val storeFile = "/home/easing/datastore.preferences_pb"
    val dataStore = createDataStore(storeFile)
    val key = intPreferencesKey("count")
    val valueFlow = dataStore.data.map { preferences -> preferences[key] ?: 0 }
    // write
    GlobalScope.launch(XDispatchers.new()) {
        dataStore.edit { preferences -> preferences[key] = 101 }
        dataStore.edit { preferences -> preferences[key] = 102 }
    }
    // observe once
    GlobalScope.launch(XDispatchers.new()) {
        val count = valueFlow.first()
        println("observe once $count")
    }
    // observe forever
    GlobalScope.launch(XDispatchers.new()) {
        valueFlow.collect {
            println("observe forever $it")
        }
    }
    Thread.sleep(1000)
}

fun createDataStore(storeFile: String): DataStore<Preferences> {
    return PreferenceDataStoreFactory.createWithPath(produceFile = { storeFile.toPath() })
}
ProtoBuffer DataStore

before use this form of data store, you should learn how to use proto buffer first, we will skip at this time

import androidx.datastore.core.DataStore
import androidx.datastore.core.DataStoreFactory
import com.kotlin.sample.proto.User
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import x.kotlin.commons.coroutine.XDispatchers
import java.io.File

fun main() {
    val storeFile = "/home/easing/temp/user.pb"
    val dataStore = createDataStore(storeFile)
    // write
    GlobalScope.launch(XDispatchers.new()) {
        dataStore.updateData { user ->
            user.toBuilder().setId(1).setName("A").build()
        }
        dataStore.updateData { user ->
            user.toBuilder().setId(2).setName("B").build()
        }
    }
    // observe once
    GlobalScope.launch(XDispatchers.new()) {
        val user = dataStore.data.first()
        println("observe once $user")
    }
    // observe forever
    GlobalScope.launch(XDispatchers.new()) {
        dataStore.data.collect {
            println("observe forever $it")
        }
    }
    Thread.sleep(1000)
}

fun createDataStore(storeFile: String): DataStore<User> {
    return DataStoreFactory.create(UserSerializer, produceFile = { File(storeFile) })
}