Skip to content

Instantly share code, notes, and snippets.

@pavan5208
Last active July 14, 2021 06:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pavan5208/fc631e994d99a57d5a047cc9d0fca0ec to your computer and use it in GitHub Desktop.
Save pavan5208/fc631e994d99a57d5a047cc9d0fca0ec to your computer and use it in GitHub Desktop.
package com.sample.android_sample_preference_datastore.proto
import androidx.datastore.core.CorruptionException
import androidx.datastore.core.Serializer
import com.google.protobuf.InvalidProtocolBufferException
import com.sample.android_sample_preference_datastore.UserStore
import java.io.InputStream
import java.io.OutputStream
object UserStoreSerializer : Serializer<UserStore> {
override val defaultValue: UserStore = UserStore.getDefaultInstance()
override suspend fun readFrom(input: InputStream): UserStore {
try {
return UserStore.parseFrom(input)
} catch (exception: InvalidProtocolBufferException) {
throw CorruptionException("Cannot read proto.", exception)
}
}
override suspend fun writeTo(t: UserStore, output: OutputStream) = t.writeTo(output)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment