-
Notifications
You must be signed in to change notification settings - Fork 4
/
Serializers.kt
47 lines (39 loc) · 1.91 KB
/
Serializers.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.grishko188.data.features.profile.datastore
import androidx.datastore.core.CorruptionException
import androidx.datastore.core.Serializer
import com.google.protobuf.InvalidProtocolBufferException
import java.io.InputStream
import java.io.OutputStream
import javax.inject.Inject
class UserProfileSerializer @Inject constructor() : Serializer<UserProfileProto> {
override val defaultValue: UserProfileProto = UserProfileProto.getDefaultInstance()
override suspend fun readFrom(input: InputStream): UserProfileProto =
try {
// readFrom is already called on the data store background thread
@Suppress("BlockingMethodInNonBlockingContext")
UserProfileProto.parseFrom(input)
} catch (exception: InvalidProtocolBufferException) {
throw CorruptionException("Cannot read proto.", exception)
}
override suspend fun writeTo(t: UserProfileProto, output: OutputStream) {
// writeTo is already called on the data store background thread
@Suppress("BlockingMethodInNonBlockingContext")
t.writeTo(output)
}
}
class UserSettingsSerializer @Inject constructor() : Serializer<UserSettingsProto> {
override val defaultValue: UserSettingsProto = UserSettingsProto.getDefaultInstance()
override suspend fun readFrom(input: InputStream): UserSettingsProto =
try {
// readFrom is already called on the data store background thread
@Suppress("BlockingMethodInNonBlockingContext")
UserSettingsProto.parseFrom(input)
} catch (exception: InvalidProtocolBufferException) {
throw CorruptionException("Cannot read proto.", exception)
}
override suspend fun writeTo(t: UserSettingsProto, output: OutputStream) {
// writeTo is already called on the data store background thread
@Suppress("BlockingMethodInNonBlockingContext")
t.writeTo(output)
}
}