Skip to content

Commit 29e514f

Browse files
committed
Take capability below API 24, and add comment.
Change-Id: I86d99fa9e74a8475c0b2bad202cfb4697ab1016b
1 parent a928bf1 commit 29e514f

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

core/network/src/main/kotlin/com/google/samples/apps/nowinandroid/core/network/demo/DemoNiaNetworkDataSource.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,26 @@ class DemoNiaNetworkDataSource @Inject constructor(
4848
@OptIn(ExperimentalSerializationApi::class)
4949
override suspend fun getTopics(ids: List<String>?): List<NetworkTopic> =
5050
withContext(ioDispatcher) {
51-
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
51+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
5252
assets.open(TOPICS_ASSET).use(networkJson::decodeFromStream)
5353
} else {
54-
networkJson.decodeFromString(assets.open(TOPICS_ASSET).toString())
54+
// Use decodeFromString to capability with API 24 below.
55+
// https://github.com/Kotlin/kotlinx.serialization/issues/2457#issuecomment-1786923342
56+
val topicsJsonString = convertStreamToString(assets.open(TOPICS_ASSET))
57+
networkJson.decodeFromString(topicsJsonString)
5558
}
5659
}
5760

5861
@OptIn(ExperimentalSerializationApi::class)
5962
override suspend fun getNewsResources(ids: List<String>?): List<NetworkNewsResource> =
6063
withContext(ioDispatcher) {
61-
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
64+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
6265
assets.open(NEWS_ASSET).use(networkJson::decodeFromStream)
6366
} else {
64-
networkJson.decodeFromString(assets.open(TOPICS_ASSET).toString())
67+
// Use decodeFromString to capability with API 24 below.
68+
// https://github.com/Kotlin/kotlinx.serialization/issues/2457#issuecomment-1786923342
69+
val newsJsonString = convertStreamToString(assets.open(NEWS_ASSET))
70+
networkJson.decodeFromString(newsJsonString)
6571
}
6672
}
6773

0 commit comments

Comments
 (0)