aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/ca/allanwang/kau/xml/FAQ.kt
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/main/kotlin/ca/allanwang/kau/xml/FAQ.kt')
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/xml/FAQ.kt75
1 files changed, 43 insertions, 32 deletions
diff --git a/core/src/main/kotlin/ca/allanwang/kau/xml/FAQ.kt b/core/src/main/kotlin/ca/allanwang/kau/xml/FAQ.kt
index b39540c..07a0287 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/xml/FAQ.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/xml/FAQ.kt
@@ -6,6 +6,8 @@ import android.support.annotation.XmlRes
import android.text.Html
import android.text.Spanned
import ca.allanwang.kau.utils.use
+import org.jetbrains.anko.doAsync
+import org.jetbrains.anko.uiThread
import org.xmlpull.v1.XmlPullParser
/**
@@ -13,42 +15,51 @@ import org.xmlpull.v1.XmlPullParser
*/
/**
- * Parse an xml with two tags, <question>Text</question> and <answer>Text</answer>,
- * and return them as a list of string pairs
+ * Parse an xml asynchronously with two tags, <question>Text</question> and <answer>Text</answer>,
+ * and invoke the [callback] on the ui thread
*/
-fun Context.kauParseFaq(@XmlRes xmlRes: Int, withNumbering: Boolean = true): List<Pair<Spanned, Spanned>> {
- val items = mutableListOf<Pair<Spanned, Spanned>>()
- resources.getXml(xmlRes).use {
- parser: XmlResourceParser ->
- var eventType = parser.eventType
- var question: Spanned? = null
- var flag = -1 //-1, 0, 1 -> invalid, question, answer
- while (eventType != XmlPullParser.END_DOCUMENT) {
- if (eventType == XmlPullParser.START_TAG) {
- flag = when (parser.name) {
- "question" -> 0
- "answer" -> 1
- else -> -1
- }
- } else if (eventType == XmlPullParser.TEXT) {
- when (flag) {
- 0 -> {
- var q = parser.text.replace("\n", "<br/>")
- if (withNumbering) q = "${items.size + 1}. $q"
- question = Html.fromHtml(q)
- flag = -1
+@Suppress("DEPRECATION")
+fun Context.kauParseFaq(
+ @XmlRes xmlRes: Int,
+ /**
+ * If \n is used, it will automatically be converted to </br>
+ */
+ parseNewLine: Boolean = true,
+ callback: (items: List<FaqItem>) -> Unit) {
+ doAsync {
+ val items = mutableListOf<FaqItem>()
+ resources.getXml(xmlRes).use {
+ parser: XmlResourceParser ->
+ var eventType = parser.eventType
+ var question: Spanned? = null
+ var flag = -1 //-1, 0, 1 -> invalid, question, answer
+ while (eventType != XmlPullParser.END_DOCUMENT) {
+ if (eventType == XmlPullParser.START_TAG) {
+ flag = when (parser.name) {
+ "question" -> 0
+ "answer" -> 1
+ else -> -1
}
- 1 -> {
- items.add(Pair(question ?: throw IllegalArgumentException("KAU FAQ answer found without a question"),
- Html.fromHtml(parser.text.replace("\n", "<br/>"))))
- question = null
- flag = -1
+ } else if (eventType == XmlPullParser.TEXT) {
+ when (flag) {
+ 0 -> {
+ question = Html.fromHtml(parser.text.replace("\n", if (parseNewLine) "<br/>" else ""))
+ flag = -1
+ }
+ 1 -> {
+ items.add(FaqItem(items.size + 1,
+ question ?: throw IllegalArgumentException("KAU FAQ answer found without a question"),
+ Html.fromHtml(parser.text.replace("\n", if (parseNewLine) "<br/>" else ""))))
+ question = null
+ flag = -1
+ }
}
}
+ eventType = parser.next()
}
-
- eventType = parser.next()
}
+ uiThread { callback(items) }
}
- return items
-} \ No newline at end of file
+}
+
+data class FaqItem(val number: Int, val question: Spanned, val answer: Spanned) \ No newline at end of file