Slide 19
Slide 19 text
@marcoGomier
class Parser private constructor(
private var callFactory: Call.Factory,
private val charset: Charset? = null,
) {
suspend fun getChannel(url: String): Channel = withContext(coroutineContext) {
// If the charset is null, then "null" is saved in the database.
// It's easier for retrieving data afterwards
val charsetString = charset.toString()
val cachedFeed = cacheManager?.getCachedFeed(url, charsetString)
if (cachedFeed != null) {
Log.d(TAG, "Returning object from cache")
return@withContext cachedFeed
} else {
Log.d(TAG, "Returning data from network")
val xml = CoroutineEngine.fetchXML(url, callFactory)
cacheManager?.cacheFeed(
url = url,
channel = channel,
charset = charsetString,
)
return@withContext channel
}
}
}
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Request
import okhttp3.Response
val channel = CoroutineEngine.parseXML(xml, charset)
cacheManager?.cacheFeed(
url = url,
channel = channel,
import org.xmlpull.v1.XmlPullParser
import org.xmlpull.v1.XmlPullParserFactory