|
|
|
@ -29,11 +29,7 @@ import kotlinx.coroutines.withContext
|
|
|
|
|
import kotlinx.serialization.ExperimentalSerializationApi
|
|
|
|
|
import kotlinx.serialization.json.Json
|
|
|
|
|
import kotlinx.serialization.json.decodeFromStream
|
|
|
|
|
import java.io.ByteArrayOutputStream
|
|
|
|
|
import java.io.InputStream
|
|
|
|
|
import java.nio.charset.StandardCharsets
|
|
|
|
|
import javax.inject.Inject
|
|
|
|
|
import kotlin.coroutines.coroutineContext
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* [NiaNetworkDataSource] implementation that provides static news resources to aid development
|
|
|
|
@ -52,7 +48,7 @@ class DemoNiaNetworkDataSource @Inject constructor(
|
|
|
|
|
} else {
|
|
|
|
|
// Use decodeFromString to capability with API 24 below.
|
|
|
|
|
// https://github.com/Kotlin/kotlinx.serialization/issues/2457#issuecomment-1786923342
|
|
|
|
|
val topicsJsonString = convertStreamToString(assets.open(TOPICS_ASSET))
|
|
|
|
|
val topicsJsonString = assets.readText(TOPICS_ASSET)
|
|
|
|
|
networkJson.decodeFromString(topicsJsonString)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -65,7 +61,7 @@ class DemoNiaNetworkDataSource @Inject constructor(
|
|
|
|
|
} else {
|
|
|
|
|
// Use decodeFromString to capability with API 24 below.
|
|
|
|
|
// https://github.com/Kotlin/kotlinx.serialization/issues/2457#issuecomment-1786923342
|
|
|
|
|
val newsJsonString = convertStreamToString(assets.open(NEWS_ASSET))
|
|
|
|
|
val newsJsonString = assets.readText(NEWS_ASSET)
|
|
|
|
|
networkJson.decodeFromString(newsJsonString)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -76,24 +72,6 @@ class DemoNiaNetworkDataSource @Inject constructor(
|
|
|
|
|
override suspend fun getNewsResourceChangeList(after: Int?): List<NetworkChangeList> =
|
|
|
|
|
getNewsResources().mapToChangeList(NetworkNewsResource::id)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert [InputStream] to [String].
|
|
|
|
|
*/
|
|
|
|
|
private suspend fun convertStreamToString(inputStream: InputStream): String = withContext(
|
|
|
|
|
coroutineContext,
|
|
|
|
|
) {
|
|
|
|
|
val result = ByteArrayOutputStream()
|
|
|
|
|
val buffer = ByteArray(1024)
|
|
|
|
|
var length: Int
|
|
|
|
|
while (true) {
|
|
|
|
|
length = inputStream.read(buffer)
|
|
|
|
|
if (length == -1) break
|
|
|
|
|
result.write(buffer, 0, length)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.toString(StandardCharsets.UTF_8.name())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
private const val NEWS_ASSET = "news.json"
|
|
|
|
|
private const val TOPICS_ASSET = "topics.json"
|
|
|
|
|