Upgrade to Pro — share decks privately, control downloads, hide ads and more …

KtRssReader 的奇幻旅程

Ivan
March 23, 2021
71

KtRssReader 的奇幻旅程

Ivan

March 23, 2021
Tweet

Transcript

  1. <?xml version="1.0"?> <rss version="2.0"> <channel> <image> <link>http://channel.image.link</link> <title>channel image title</title>

    <url>http://channel.image.url</url> <description>channel image description</description> <height>32</height> <width>96</width> </image> </channel> </rss> RSS 2.0 4
  2. <?xml version="1.0" encoding="UTF-8"?> <rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0"> <channel> <itunes:image href="https://channel.image.itunes" />

    <image> <link>http://channel.image.link</link> <title>channel image title</title> <url>http://channel.image.url</url> <description>channel image description</description> <height>32</height> <width>96</width> </image> </channel> </rss> iTunes 5
  3. <?xml version="1.0"?> <rss xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0"> <channel> <googleplay:image href="http://channel.image" />

    <itunes:image href="https://channel.image" /> <image> <link>http://channel.image</link> <title>channel image title</title> <url>http://channel.image.url</url> <description>channel image description</description> <height>32</height> <width>96</width> </image> </channel> </rss> Mixing 6
  4. <?xml version="1.0"?> <rss xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0"> <channel> <itunes:image href="https://channel.image.itunes" />

    <image> <link>http://channel.image.link</link> <title>channel image title</title> <url>http://channel.image.url</url> <description>channel image description</description> <height>32</height> <width>96</width> </image> </channel> </rss> 遇到的情況 7
  5. 8

  6. 9

  7. ◦ Easy-to-use API ◦ Fetches feed for you ◦ Database

    cache and custom cache valid time ◦ Supports RSS 2.0 standard, iTunes, and Google Play tags ◦ Customizes output data with annotations KtRssReader 簡介 11
  8. MainScope().launch { val rssData = withContext(Dispatchers.IO) { Reader.coRead<ITunesChannelData>(url) { useCache

    = true expiredTimeMillis = 3.days.toLongMilliseconds() charset = Charsets.UTF_8 } } print(rssData) } • Reader.read() • Reader.flowRead() 基礎使用 12
  9. annotation class RssTag( val name: String = "", val order:

    Array<OrderType> = [OrderType.RSS_STANDARD, OrderType.ITUNES, OrderType.GOOGLE] annotation class RssRawData(val rawTags: Array<String>) RssTag 14
  10. @RssTag(name = "channel") data class CustomChannelData( @RssTag(name="title",order = [OrderType.GOOGLE, OrderType.ITUNES,

    OrderType.RSS_STANDARD]) val rssTitle: String?, @RssRawData(["itunes:summary", "googleplay:description", "googleplay:summary"]) val description: String?, ) : Serializable Custom RSS Data 15
  11. • Parser 實作面向 ◦ 標準 TAG Parser ▪ RSS 2.0

    ▪ Google ▪ iTunes ▪ Auto Mixed (All together) ◦ 自定義 Parser ▪ Annotation ▪ KAPT 實作方式 16
  12. • Kotlin Parser ◦ 使用 DOM Parser ▪ 方便跨層尋找 TAG

    • 可以處理 TAG 內夾帶 HTML 碼的情況 ▪ RSS 檔案大的時候,比較耗記憶體 ▪ 若有相同 TAG 且多筆資料,需判斷他的父節點 ◦ 實作 ▪ RssStandardParser.kt, GoolgeParser.kt, ITunesParser.kt , AutoMixParser.kt 標準 TAG Parser 17
  13. • Android Parser ◦ 使用 Android 原生 XmlPullParser ▪ 比

    DOM parser 省記憶體 ▪ 速度比 SAX 快 ▪ Event Stream 方式載入 TAG ◦ 實作 ▪ AndroidRssStandardParser.kt, AndroidGoogleParser.kt, AndroidITunesParser.kt, AndroidAutoMixParser.kt 標準 TAG Parser 18
  14. • Annotation Processor ◦ KAPT, Kotlin annotation processing tool ◦

    讓我們可以讀到 annotation 標記的 class、method、variable 等 • Kotlin Poet ◦ 用來產生 Kotlin 程式碼的程式庫 ◦ 用簡單易懂的語法在 building 的時候就可以產生程式碼 ▪ 支援 Control flow ▪ 有各種 Type 和 Class 的表達式 自定義 Parser 22
  15. • 新增一個 module 放 annotation • 新增一個 module 放 processor

    apply plugin: 'kotlin-kapt' dependencies { implementation 'com.squareup:kotlinpoet:1.6.0' implementation "com.google.auto.service:auto-service:1.0-rc6" } Annotation Processor 23
  16. • 開始寫屬於你的 processor @AutoService(Processor::class) class RssAnnotationProcessor : AbstractProcessor() { override

    fun getSupportedAnnotationTypes(): MutableSet<String> { return mutableSetOf( RssTag::class.java.canonicalName, RssRawData::class.java.canonicalName, RssAttribute::class.java.canonicalName, RssValue::class.java.canonicalName ) } override fun process(typeElementSet: MutableSet<out TypeElement>?, roundEnvironment: RoundEnvironment?): Boolean { ... } } https://github.com/ivanisidrowu/KtRssReader/blob/master/processor/src/main/java/tw/ktrssreader/processor/RssAnnotationProcessor.kt Annotation Processor 25
  17. • 如果我要做多個不同用途的 annotation processor ,該怎麼辦? ◦ 創不同的 module ▪ 分包讓使用者

    load 進去 ◦ 有共用邏輯的話,可以用 kapt arguments kapt { arguments { arg("pureKotlinParser", true) } } val isPureKotlinParser = processingEnv.options["pureKotlinParser"]?.toBoolean() ?: false Annotation Processor 26
  18. • 實作各種不同的 generator ◦ Kotlin ▪ KotlinParserGenerator.kt ▪ KotlinExtensionGenerator.kt ◦

    Android ▪ AndroidReaderGenerator.kt ▪ AndroidParserGenerator.kt ▪ AndroidExtensionGenerator.kt Annotation Processor 27
  19. • Processor 給的 javax.lang.model.element.Element ◦ 包含的節點資訊 ▪ Name ▪ Annotation

    ▪ 鄰居節點資訊 ▪ Type ▪ Tag 資訊 (tag, attribute, value 等) Annotation Processor 28
  20. • 我們處理節點資訊的步驟 a. 從 Element 獲取資訊 b. 預處理節點資訊 c. 產生

    Kotlin 檔案 Get Data from Element Pre-process Data Generate Kotlin files Annotation Processor 29
  21. • 很多種方式可以發佈 ◦ bintray、gradle plugin 上 Maven Central 等等 ◦

    JitPack • JitPack ◦ 不麻煩 ◦ 快 • Release 後,請一定要先試過一次 ! ◦ 不然就會遇到問題 程式碼發佈方式 31
  22. • 使用 KSP (Kotlin Symbol Processing) 取代 KAPT ◦ 據說速度可達兩倍之快

    ▪ 因為 KAPT 中間會先生成 Java Stub • Kotlin Fetcher & Cache • 支援 Kotlin 跨平台 KMM 未來方向 32