in my build.
Код: Выделить всё
androidTestImplementation(libs.androidx.datastore.core)
androidTestImplementation(libs.androidx.datastore.preferences)
androidTestImplementation(libs.androidx.datastore.preferences.core)
androidTestImplementation(project(":common"))
testImplementation(project(":common"))
androidTestImplementation(libs.protobuf.javalite)
testImplementation(libs.protobuf.javalite)
< /code>
Я также добавил это: < /p>
allprojects {
configurations.all {
resolutionStrategy.eachDependency {
if (requested.group == "com.google.protobuf" && requested.name == "protobuf-javalite") {
useVersion("3.25.1")
because("Older versions lack registerDefaultInstance()")
}
}
}
}
< /code>
Просто чтобы убедиться, что это та же версия. Stringlistserializer
Код: Выделить всё
private class StringListSerializer : Serializer {
override val defaultValue: List
get() = emptyList()
override suspend fun readFrom(input: InputStream): List {
try {
val longList = ListOfString.parseFrom(input)
return longList.itemList
} catch (exception: Exception) {
throw CorruptionException("Cannot read proto.", exception)
}
}
override suspend fun writeTo(t: List, output: OutputStream) {
val longList = ListOfString.newBuilder()
.addAllItem(t)
.build()
longList.writeTo(output)
}
}
Код: Выделить всё
class StringListDataStoreProperty(
private val context: Context,
private val dataStoreName: String,
private val coroutineScope: CoroutineScope
) : ReadWriteProperty {
private val Context.listOfLongDataStore: DataStore by dataStore(
fileName = dataStoreName,
serializer = StringListSerializer(),
)
override fun getValue(thisRef: Any, property: KProperty): List = runBlocking {
return@runBlocking context.listOfLongDataStore.data.first()
}
override fun setValue(thisRef: Any, property: KProperty, value: List?) {
coroutineScope.launch {
context.listOfLongDataStore.updateData {
value!!
}
}
}
}
< /code>
Реализация: < /p>
class MyDataStoreManager @Inject constructor(
@ApplicationContext val context: Context,
@ApplicationScope val coroutineScope: CoroutineScope,
private val dataStore: DataStore
) : MyDataStore {
//...
override var myListOfStrings: List? by StringListDataStoreProperty(
context = context,
dataStoreName = "protoStoreName",
coroutineScope = coroutineScope
)
//...
}
Подробнее здесь: https://stackoverflow.com/questions/795 ... oto-listof