From b790c13e5105bd53d35facff4fc09e5a615d7b1c Mon Sep 17 00:00:00 2001 From: zuevval Date: Sat, 5 Jun 2021 21:04:33 +0300 Subject: [PATCH] #296 export steps to xml --- .../learnbraille/data/dsl/DataStorage.kt | 6 +-- .../learnbraille/data/entities/BrailleDots.kt | 8 ++++ .../learnbraille/data/entities/Materials.kt | 9 +++- .../learnbraille/data/entities/StepData.kt | 44 ++++++++++++++----- .../learnbraille/data/entities/XmlAble.kt | 14 ++++++ .../steps/info/AbstractInfoStepFragment.kt | 4 +- .../theory/steps/input/InputDotsFragment.kt | 2 +- .../theory/steps/show/ShowDotsFragment.kt | 2 +- .../learnbraille/data/entities/ToXmlTest.kt | 24 ++++++++++ 9 files changed, 94 insertions(+), 19 deletions(-) create mode 100644 app/src/main/java/com/github/braillesystems/learnbraille/data/entities/XmlAble.kt create mode 100644 app/src/test/java/com/github/braillesystems/learnbraille/data/entities/ToXmlTest.kt 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 23f71f28..e858c3c8 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.text, courseName)) - is LastInfo -> LastInfo(interpolateTextHelper(data.text, courseName)) - is Info -> Info(interpolateTextHelper(data.text, courseName)) + is FirstInfo -> FirstInfo(interpolateTextHelper(data.xmlBody, courseName)) + is LastInfo -> LastInfo(interpolateTextHelper(data.xmlBody, courseName)) + is Info -> Info(interpolateTextHelper(data.xmlBody, 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 e62f0bd0..baa7258f 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,6 +51,14 @@ 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 34482385..98125b8e 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,10 +5,15 @@ 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 ede280b8..2abe3126 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 +sealed class StepData : XmlAble class StepDataConverters { @@ -33,7 +33,8 @@ typealias HtmlText = String */ @Serializable sealed class BaseInfo : StepData() { - abstract val text: HtmlText + override val xmlTag = "text" + override val xmlParams: Map = mapOf("type" to "info") } /** @@ -42,7 +43,7 @@ sealed class BaseInfo : StepData() { @Serializable data class Info( @SerialName("info") - override val text: HtmlText + override val xmlBody: HtmlText ) : BaseInfo() /** @@ -51,7 +52,7 @@ data class Info( @Serializable data class FirstInfo( @SerialName("info") - override val text: HtmlText + override val xmlBody: HtmlText ) : BaseInfo() /** @@ -60,13 +61,14 @@ data class FirstInfo( @Serializable data class LastInfo( @SerialName("info") - override val text: HtmlText + override val xmlBody: HtmlText ) : BaseInfo() @Serializable sealed class BaseInput : StepData() { abstract val brailleDots: BrailleDots + override val xmlTag: String = "practice" } /** @@ -76,6 +78,11 @@ 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) { @@ -91,15 +98,22 @@ data class Input( */ @Serializable data class InputDots( - val text: HtmlText?, + val text: HtmlText, @SerialName("dots") // backward compatibility override val brailleDots: BrailleDots -) : BaseInput() +) : BaseInput() { + override val xmlParams: Map = mapOf( + "type" to "practice", + "title" to text + ) + override val xmlBody: HtmlText = brailleDots.toXml() +} @Serializable sealed class BaseShow : StepData() { abstract val brailleDots: BrailleDots + override val xmlTag = "reading" } /** @@ -109,11 +123,15 @@ sealed class BaseShow : StepData() { data class Show( val material: Material ) : BaseShow() { - override val brailleDots: BrailleDots 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() } /** @@ -124,7 +142,13 @@ data class Show( */ @Serializable data class ShowDots( - val text: HtmlText?, + val text: HtmlText, @SerialName("dots") override val brailleDots: BrailleDots -) : BaseShow() +) : BaseShow() { + override val xmlParams: Map = mapOf( + "type" to "reading", + "title" to text + ) + override val xmlBody: HtmlText = brailleDots.toXml() +} diff --git a/app/src/main/java/com/github/braillesystems/learnbraille/data/entities/XmlAble.kt b/app/src/main/java/com/github/braillesystems/learnbraille/data/entities/XmlAble.kt new file mode 100644 index 00000000..c4674471 --- /dev/null +++ b/app/src/main/java/com/github/braillesystems/learnbraille/data/entities/XmlAble.kt @@ -0,0 +1,14 @@ +package com.github.braillesystems.learnbraille.data.entities + +interface XmlAble { + val xmlTag: String + val xmlParams: Map + val xmlBody: HtmlText + fun toXml(): HtmlText { + var paramsString = "" + for ((key, value) in xmlParams) { + paramsString += " $key=\"$value\"" + } + return "<$xmlTag$paramsString>\n$xmlBody\n" + } +} \ No newline at end of file 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 60c05a0e..f0598ce0 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.text.parseAsHtml() - checkedAnnounce(data.text) + stepBinding.textView?.text = data.xmlBody.parseAsHtml() + checkedAnnounce(data.xmlBody) } } 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 6d50deac..f541ea05 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.text + val text = data.xmlBody ?: 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 94ad8fff..247241c2 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.text + val text = data.xmlBody ?: 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 new file mode 100644 index 00000000..37fcaa83 --- /dev/null +++ b/app/src/test/java/com/github/braillesystems/learnbraille/data/entities/ToXmlTest.kt @@ -0,0 +1,24 @@ +package com.github.braillesystems.learnbraille.data.entities + +import org.junit.Assert.assertEquals +import org.junit.Test + +import com.github.braillesystems.learnbraille.data.entities.BrailleDot.F +import com.github.braillesystems.learnbraille.data.entities.BrailleDot.E + +class ToXmlTest { + @Test + fun stepsToXml() { + val cases = mapOf( + """dummy text""" to Info("dummy text"), + """dummy last text""" to LastInfo("dummy last text"), + """(F, F, F, F, F, F)""" + to ShowDots("look at these dots!", BrailleDots()), + """(F, F, T, F, F, T)""" + to InputDots("type in these dots!", BrailleDots(E, E, F, E, E, F)) + ) + for ((expectedXml, stepData) in cases) { + assertEquals(expectedXml, stepData.toXml().replace("\n", "")) + } + } +}