Skip to content

Commit

Permalink
#296 export steps to xml
Browse files Browse the repository at this point in the history
  • Loading branch information
zuevval committed Jun 5, 2021
1 parent 8033a3d commit b790c13
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<BrailleDot>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -33,7 +33,8 @@ typealias HtmlText = String
*/
@Serializable
sealed class BaseInfo : StepData() {
abstract val text: HtmlText
override val xmlTag = "text"
override val xmlParams: Map<String, String> = mapOf("type" to "info")
}

/**
Expand All @@ -42,7 +43,7 @@ sealed class BaseInfo : StepData() {
@Serializable
data class Info(
@SerialName("info")
override val text: HtmlText
override val xmlBody: HtmlText
) : BaseInfo()

/**
Expand All @@ -51,7 +52,7 @@ data class Info(
@Serializable
data class FirstInfo(
@SerialName("info")
override val text: HtmlText
override val xmlBody: HtmlText
) : BaseInfo()

/**
Expand All @@ -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"
}

/**
Expand All @@ -76,6 +78,11 @@ sealed class BaseInput : StepData() {
data class Input(
val material: Material
) : BaseInput() {
override val xmlParams: Map<String, String> = mapOf(
"type" to "practice",
"title" to ""
)
override val xmlBody: HtmlText = material.toXml()

override val brailleDots: BrailleDots
get() = when (material.data) {
Expand All @@ -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<String, String> = 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"
}

/**
Expand All @@ -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<String, String> = mapOf(
"type" to "reading",
"title" to ""
)
override val xmlBody: HtmlText = material.toXml()
}

/**
Expand All @@ -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<String, String> = mapOf(
"type" to "reading",
"title" to text
)
override val xmlBody: HtmlText = brailleDots.toXml()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.braillesystems.learnbraille.data.entities

interface XmlAble {
val xmlTag: String
val xmlParams: Map<String, String>
val xmlBody: HtmlText
fun toXml(): HtmlText {
var paramsString = ""
for ((key, value) in xmlParams) {
paramsString += " $key=\"$value\""
}
return "<$xmlTag$paramsString>\n$xmlBody\n</$xmlTag>"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, StepData>(
"""<text type="info">dummy text</text>""" to Info("dummy text"),
"""<text type="info">dummy last text</text>""" to LastInfo("dummy last text"),
"""<reading type="reading" title="look at these dots!">(F, F, F, F, F, F)</reading>"""
to ShowDots("look at these dots!", BrailleDots()),
"""<practice type="practice" title="type in these dots!">(F, F, T, F, F, T)</practice>"""
to InputDots("type in these dots!", BrailleDots(E, E, F, E, E, F))
)
for ((expectedXml, stepData) in cases) {
assertEquals(expectedXml, stepData.toXml().replace("\n", ""))
}
}
}

0 comments on commit b790c13

Please sign in to comment.