diff options
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.kt | 42 |
1 files changed, 31 insertions, 11 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 bbe9425..cb57216 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/xml/FAQ.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/xml/FAQ.kt @@ -1,10 +1,25 @@ +/* + * Copyright 2018 Allan Wang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package ca.allanwang.kau.xml import android.content.Context import android.content.res.XmlResourceParser -import androidx.annotation.XmlRes import android.text.Html import android.text.Spanned +import androidx.annotation.XmlRes import ca.allanwang.kau.utils.use import org.jetbrains.anko.doAsync import org.jetbrains.anko.uiThread @@ -20,12 +35,13 @@ import org.xmlpull.v1.XmlPullParser */ @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) { + @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 -> @@ -46,10 +62,14 @@ fun Context.kauParseFaq( flag = -1 } 1 -> { - items.add(FaqItem(items.size + 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 "")))) + ?: throw IllegalArgumentException("KAU FAQ answer found without a question"), + Html.fromHtml(parser.text.replace("\n", if (parseNewLine) "<br/>" else "")) + ) + ) question = null flag = -1 } @@ -62,4 +82,4 @@ fun Context.kauParseFaq( } } -data class FaqItem(val number: Int, val question: Spanned, val answer: Spanned)
\ No newline at end of file +data class FaqItem(val number: Int, val question: Spanned, val answer: Spanned) |