From 11a30c9fcb43f23c2190f1b821aeb9f445c9f7fa Mon Sep 17 00:00:00 2001 From: zuevval Date: Mon, 2 Aug 2021 11:45:29 +0300 Subject: [PATCH] #296 externify XML machinery --- .../learnbraille/data/dsl/DataStorage.kt | 6 +- .../learnbraille/data/entities/BrailleDots.kt | 8 --- .../learnbraille/data/entities/Materials.kt | 9 +-- .../learnbraille/data/entities/StepData.kt | 43 +++--------- .../steps/info/AbstractInfoStepFragment.kt | 4 +- .../theory/steps/input/InputDotsFragment.kt | 2 +- .../theory/steps/show/ShowDotsFragment.kt | 2 +- .../learnbraille/data/entities/ToXmlTest.kt | 66 ++++++++++++++++--- 8 files changed, 76 insertions(+), 64 deletions(-) diff --git a/app/src/main/java/com/github/braillesystems/learnbraille/data/dsl/DataStorage.kt b/app/src/main/java/com/github/braillesystems/learnbraille/data/dsl/DataStorage.kt index e858c3c8..23f71f28 100644 --- a/app/src/main/java/com/github/braillesystems/learnbraille/data/dsl/DataStorage.kt +++ b/app/src/main/java/com/github/braillesystems/learnbraille/data/dsl/DataStorage.kt @@ -177,9 +177,9 @@ private fun Step.interpolateText(courseName: CourseName): Step = if (data !is BaseInfo) data else { when (data) { - is FirstInfo -> FirstInfo(interpolateTextHelper(data.xmlBody, courseName)) - is LastInfo -> LastInfo(interpolateTextHelper(data.xmlBody, courseName)) - is Info -> Info(interpolateTextHelper(data.xmlBody, courseName)) + is FirstInfo -> FirstInfo(interpolateTextHelper(data.text, courseName)) + is LastInfo -> LastInfo(interpolateTextHelper(data.text, courseName)) + is Info -> Info(interpolateTextHelper(data.text, courseName)) } } ) diff --git a/app/src/main/java/com/github/braillesystems/learnbraille/data/entities/BrailleDots.kt b/app/src/main/java/com/github/braillesystems/learnbraille/data/entities/BrailleDots.kt index baa7258f..e62f0bd0 100644 --- a/app/src/main/java/com/github/braillesystems/learnbraille/data/entities/BrailleDots.kt +++ b/app/src/main/java/com/github/braillesystems/learnbraille/data/entities/BrailleDots.kt @@ -51,14 +51,6 @@ data class BrailleDots( ) override fun toString() = "$b1$b2$b3$b4$b5$b6" - - fun toXml(): String { - val dotsSymbols = this.toString().replace("F", "T").replace("E", "F") - var result = "" - dotsSymbols.dropLast(1).forEach { result += "$it, " } - result += dotsSymbols.takeLast(1) - return "($result)" - } } val BrailleDots.list: List diff --git a/app/src/main/java/com/github/braillesystems/learnbraille/data/entities/Materials.kt b/app/src/main/java/com/github/braillesystems/learnbraille/data/entities/Materials.kt index 98125b8e..34482385 100644 --- a/app/src/main/java/com/github/braillesystems/learnbraille/data/entities/Materials.kt +++ b/app/src/main/java/com/github/braillesystems/learnbraille/data/entities/Materials.kt @@ -5,15 +5,10 @@ import kotlinx.serialization.Serializable @Entity(tableName = "materials") @Serializable -data class Material ( +data class Material( @PrimaryKey val id: DBid, val data: MaterialData -) { - fun toXml() : String { - // TODO implement (it's not easy) - return "TODO" - } -} +) @Dao interface MaterialDao { diff --git a/app/src/main/java/com/github/braillesystems/learnbraille/data/entities/StepData.kt b/app/src/main/java/com/github/braillesystems/learnbraille/data/entities/StepData.kt index 2abe3126..1e70430d 100644 --- a/app/src/main/java/com/github/braillesystems/learnbraille/data/entities/StepData.kt +++ b/app/src/main/java/com/github/braillesystems/learnbraille/data/entities/StepData.kt @@ -13,7 +13,7 @@ import kotlinx.serialization.json.Json * All texts are in html format. Use .parseAsHtml android extension function. */ @Serializable -sealed class StepData : XmlAble +sealed class StepData class StepDataConverters { @@ -33,8 +33,7 @@ typealias HtmlText = String */ @Serializable sealed class BaseInfo : StepData() { - override val xmlTag = "text" - override val xmlParams: Map = mapOf("type" to "info") + abstract val text: HtmlText } /** @@ -43,7 +42,7 @@ sealed class BaseInfo : StepData() { @Serializable data class Info( @SerialName("info") - override val xmlBody: HtmlText + override val text: HtmlText ) : BaseInfo() /** @@ -52,7 +51,7 @@ data class Info( @Serializable data class FirstInfo( @SerialName("info") - override val xmlBody: HtmlText + override val text: HtmlText ) : BaseInfo() /** @@ -61,14 +60,13 @@ data class FirstInfo( @Serializable data class LastInfo( @SerialName("info") - override val xmlBody: HtmlText + override val text: HtmlText ) : BaseInfo() @Serializable sealed class BaseInput : StepData() { abstract val brailleDots: BrailleDots - override val xmlTag: String = "practice" } /** @@ -78,11 +76,6 @@ sealed class BaseInput : StepData() { data class Input( val material: Material ) : BaseInput() { - override val xmlParams: Map = mapOf( - "type" to "practice", - "title" to "" - ) - override val xmlBody: HtmlText = material.toXml() override val brailleDots: BrailleDots get() = when (material.data) { @@ -98,22 +91,15 @@ data class Input( */ @Serializable data class InputDots( - val text: HtmlText, + val text: HtmlText?, @SerialName("dots") // backward compatibility override val brailleDots: BrailleDots -) : BaseInput() { - override val xmlParams: Map = mapOf( - "type" to "practice", - "title" to text - ) - override val xmlBody: HtmlText = brailleDots.toXml() -} +) : BaseInput() @Serializable sealed class BaseShow : StepData() { abstract val brailleDots: BrailleDots - override val xmlTag = "reading" } /** @@ -127,11 +113,6 @@ data class Show( get() = when (material.data) { is OneBrailleSymbol -> material.data.brailleDots } - override val xmlParams: Map = mapOf( - "type" to "reading", - "title" to "" - ) - override val xmlBody: HtmlText = material.toXml() } /** @@ -142,13 +123,7 @@ data class Show( */ @Serializable data class ShowDots( - val text: HtmlText, + val text: HtmlText?, @SerialName("dots") override val brailleDots: BrailleDots -) : BaseShow() { - override val xmlParams: Map = mapOf( - "type" to "reading", - "title" to text - ) - override val xmlBody: HtmlText = brailleDots.toXml() -} +) : BaseShow() diff --git a/app/src/main/java/com/github/braillesystems/learnbraille/ui/screens/theory/steps/info/AbstractInfoStepFragment.kt b/app/src/main/java/com/github/braillesystems/learnbraille/ui/screens/theory/steps/info/AbstractInfoStepFragment.kt index f0598ce0..60c05a0e 100644 --- a/app/src/main/java/com/github/braillesystems/learnbraille/ui/screens/theory/steps/info/AbstractInfoStepFragment.kt +++ b/app/src/main/java/com/github/braillesystems/learnbraille/ui/screens/theory/steps/info/AbstractInfoStepFragment.kt @@ -11,7 +11,7 @@ abstract class AbstractInfoStepFragment(helpMsgId: HelpMsgId) : AbstractStepFrag override fun iniStepHelper() { val data = step.data require(data is BaseInfo) - stepBinding.textView?.text = data.xmlBody.parseAsHtml() - checkedAnnounce(data.xmlBody) + stepBinding.textView?.text = data.text.parseAsHtml() + checkedAnnounce(data.text) } } diff --git a/app/src/main/java/com/github/braillesystems/learnbraille/ui/screens/theory/steps/input/InputDotsFragment.kt b/app/src/main/java/com/github/braillesystems/learnbraille/ui/screens/theory/steps/input/InputDotsFragment.kt index f541ea05..6d50deac 100644 --- a/app/src/main/java/com/github/braillesystems/learnbraille/ui/screens/theory/steps/input/InputDotsFragment.kt +++ b/app/src/main/java/com/github/braillesystems/learnbraille/ui/screens/theory/steps/input/InputDotsFragment.kt @@ -52,7 +52,7 @@ class InputDotsFragment : AbstractInputStepFragment(R.string.lessons_help_input_ val data = step.data require(data is InputDots) - val text = data.xmlBody + val text = data.text ?: getString(R.string.lessons_show_dots_info_template).format(data.brailleDots.spelling) infoTextView.text = text.parseAsHtml() checkedAnnounce(text.removeHtmlMarkup()) diff --git a/app/src/main/java/com/github/braillesystems/learnbraille/ui/screens/theory/steps/show/ShowDotsFragment.kt b/app/src/main/java/com/github/braillesystems/learnbraille/ui/screens/theory/steps/show/ShowDotsFragment.kt index 247241c2..94ad8fff 100644 --- a/app/src/main/java/com/github/braillesystems/learnbraille/ui/screens/theory/steps/show/ShowDotsFragment.kt +++ b/app/src/main/java/com/github/braillesystems/learnbraille/ui/screens/theory/steps/show/ShowDotsFragment.kt @@ -44,7 +44,7 @@ class ShowDotsFragment : AbstractShowStepFragment(R.string.lessons_help_show_dot val data = step.data require(data is ShowDots) - val text = data.xmlBody + val text = data.text ?: getString(R.string.lessons_show_dots_info_template).format(data.brailleDots.spelling) infoTextView.text = text.parseAsHtml() checkedAnnounce(text.removeHtmlMarkup()) diff --git a/app/src/test/java/com/github/braillesystems/learnbraille/data/entities/ToXmlTest.kt b/app/src/test/java/com/github/braillesystems/learnbraille/data/entities/ToXmlTest.kt index 1c4b17e4..fc3b03c5 100644 --- a/app/src/test/java/com/github/braillesystems/learnbraille/data/entities/ToXmlTest.kt +++ b/app/src/test/java/com/github/braillesystems/learnbraille/data/entities/ToXmlTest.kt @@ -16,20 +16,70 @@ internal val testLessons by lessons { } } +internal fun toXml(brailleDots: BrailleDots): HtmlText { + val dotsSymbols = brailleDots.toString().replace("F", "T").replace("E", "F") + var result = "" + dotsSymbols.dropLast(1).forEach { result += "$it, " } + result += dotsSymbols.takeLast(1) + return "($result)" +} + +internal fun toXml(material: Material): HtmlText { + return "TODO" // TODO (not easy) +} + +internal fun toXml(stepData: StepData): HtmlText = + when (stepData) { + is BaseInfo -> object : XmlAble { + override val xmlTag: String = "text" + override val xmlParams: Map = mapOf("type" to "info") + override val xmlBody: HtmlText = stepData.text + } + is BaseInput -> object : XmlAble { + override val xmlTag: String = "practice" + override val xmlParams = mapOf( + "type" to "practice", + "title" to if (stepData is InputDots) stepData.text ?: "" else "" + ) + override val xmlBody: HtmlText = when (stepData) { + is InputDots -> toXml(stepData.brailleDots) + is Input -> toXml(stepData.material) + else -> "toXml not implemented for this class" + } + } + is BaseShow -> object : XmlAble { + override val xmlTag: String = "reading" + override val xmlParams = mapOf( + "type" to "reading", + "title" to if (stepData is ShowDots) stepData.text ?: "" else "" + ) + override val xmlBody: HtmlText = when (stepData) { + is ShowDots -> toXml(stepData.brailleDots) + is Input -> toXml(stepData.material) + else -> "toXml not implemented for this class" + } + } + else -> object : XmlAble { + override val xmlTag: String = stepData.toString() + override val xmlParams: Map = mapOf() + override val xmlBody: HtmlText = "toXml not implemented for this class" + } + }.toXml() + internal fun toXml(lesson: LessonWithSteps): HtmlText { return object : XmlAble { override val xmlTag: String = "lesson" - override val xmlParams: Map - get() = mapOf("name" to lesson.first.name.replace("\"", "'")) + override val xmlParams: Map = + mapOf("name" to lesson.first.name.replace("\"", "'")) override val xmlBody: HtmlText get() = { - var stepBuilder: HtmlText = "" - for(step in lesson.second.dropLast(1)){ - stepBuilder += (step.first.data.toXml() + "\n") // TODO [...
] -> [

...

] + var stepBuilder: HtmlText = "" + for (step in lesson.second.dropLast(1)) { + stepBuilder += (toXml(step.first.data) + "\n") // TODO [...
] -> [

...

] } - stepBuilder + lesson.second.takeLast(1)[0].first.data.toXml() + stepBuilder + toXml(lesson.second.takeLast(1)[0].first.data) }() }.toXml() } @@ -46,7 +96,7 @@ class ToXmlTest { to InputDots("type in these dots!", BrailleDots(E, E, F, E, E, F)) ) for ((expectedXml, stepData) in cases) { - assertEquals(expectedXml, stepData.toXml().replace("\n", "")) + assertEquals(expectedXml, toXml(stepData).replace("\n", "")) } } @@ -55,7 +105,7 @@ class ToXmlTest { println(toXml(testLessons.lessons[0])) var xmlText = "" - for(lessons in golubinaIntroLessons.lessons) { + for (lessons in golubinaIntroLessons.lessons) { xmlText += (toXml(lessons) + "\n") }