Skip to content

Instantly share code, notes, and snippets.

@muthuraj57
Created May 29, 2021 15:33
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 muthuraj57/687ef7063eb5e7a93c7bd461c6984718 to your computer and use it in GitHub Desktop.
Save muthuraj57/687ef7063eb5e7a93c7bd461c6984718 to your computer and use it in GitHub Desktop.
object UserPreferencesSerializer : Serializer<UserPreferences> {
override val defaultValue: UserPreferences = UserPreferences()
override suspend fun readFrom(input: InputStream): UserPreferences {
try {
return UserPreferences.ADAPTER.decode(input)
} catch (exception: IOException) {
throw CorruptionException("Cannot read proto.", exception)
}
}
override suspend fun writeTo(
t: UserPreferences,
output: OutputStream) = t.adapter.encode(output, t)
}
val Context.sserPreferencesDataStore: DataStore<UserPreferences> by dataStore(
fileName = "user_preferences.pb",
serializer = UserPreferencesSerializer
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment