Skip to content

Commit fdb60f3

Browse files
committed
[1.4.0-optimize] 代码优化
1 parent 94f99c6 commit fdb60f3

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

app/src/main/java/org/ninetripods/mq/study/jetpack/datastore/ktx/DataStoreKt.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ val Context.bookDataStorePt: DataStore<BookProto.Book> by dataStore(
3636
context,
3737
BOOK_PREFERENCES_NAME
3838
) { sharedPrefs: SharedPreferencesView, currentData: BookProto.Book ->
39+
//从SP中取出数据
3940
val bookName: String = sharedPrefs.getString(KEY_BOOK_NAME, "") ?: ""
4041
val bookPrice: Float = sharedPrefs.getFloat(KEY_BOOK_PRICE, 0f)
4142

@@ -52,5 +53,4 @@ val Context.bookDataStorePt: DataStore<BookProto.Book> by dataStore(
5253
}
5354
)
5455
}
55-
5656
)
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
package org.ninetripods.mq.study.jetpack.datastore.proto
22

3+
import androidx.datastore.core.CorruptionException
34
import androidx.datastore.core.Serializer
5+
import com.google.protobuf.InvalidProtocolBufferException
46
import org.ninetripods.mq.study.BookProto
57
import java.io.InputStream
68
import java.io.OutputStream
79

810
object BookSerializer : Serializer<BookProto.Book> {
11+
override val defaultValue: BookProto.Book = BookProto.Book.getDefaultInstance()
912

1013
override suspend fun readFrom(input: InputStream): BookProto.Book {
11-
return BookProto.Book.parseFrom(input)
14+
try {
15+
return BookProto.Book.parseFrom(input)
16+
} catch (exception: InvalidProtocolBufferException) {
17+
throw CorruptionException("Cannot read proto.", exception)
18+
}
1219
}
1320

1421
override suspend fun writeTo(t: BookProto.Book, output: OutputStream) {
1522
t.writeTo(output)
1623
}
17-
18-
override val defaultValue: BookProto.Book = BookProto.Book.getDefaultInstance()
19-
2024
}

lib_protobuf/build.gradle

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ android {
3232
//protoc版本参见:https://repo1.maven.org/maven2/com/google/protobuf/protoc/
3333
artifact = "com.google.protobuf:protoc:3.18.0"
3434
}
35+
// Generates the java Protobuf-lite code for the Protobufs in this project. See
36+
// https://github.com/google/protobuf-gradle-plugin#customizing-protobuf-compilation
37+
// for more information.
3538
generateProtoTasks {
3639
all().each { task ->
3740
task.builtins {

0 commit comments

Comments
 (0)