Skip to content

Commit

Permalink
#296 externify XML machinery
Browse files Browse the repository at this point in the history
  • Loading branch information
zuevval committed Aug 2, 2021
1 parent 6b0fa2c commit 11a30c9
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 64 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.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))
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<BrailleDot>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
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 : XmlAble
sealed class StepData

class StepDataConverters {

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

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

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

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

/**
Expand All @@ -78,11 +76,6 @@ 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 @@ -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<String, String> = 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"
}

/**
Expand All @@ -127,11 +113,6 @@ data class Show(
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 @@ -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<String, String> = mapOf(
"type" to "reading",
"title" to text
)
override val xmlBody: HtmlText = brailleDots.toXml()
}
) : BaseShow()
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.xmlBody.parseAsHtml()
checkedAnnounce(data.xmlBody)
stepBinding.textView?.text = data.text.parseAsHtml()
checkedAnnounce(data.text)
}
}
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.xmlBody
val text = data.text
?: 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.xmlBody
val text = data.text
?: 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 @@ -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<String, String> = 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<String, String> = 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<String, String>
get() = mapOf("name" to lesson.first.name.replace("\"", "'"))
override val xmlParams: Map<String, String> =
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 [... <br>] -> [<p>...</p>]
var stepBuilder: HtmlText = ""
for (step in lesson.second.dropLast(1)) {
stepBuilder += (toXml(step.first.data) + "\n") // TODO [... <br>] -> [<p>...</p>]
}
stepBuilder + lesson.second.takeLast(1)[0].first.data.toXml()
stepBuilder + toXml(lesson.second.takeLast(1)[0].first.data)
}()
}.toXml()
}
Expand All @@ -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", ""))
}
}

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

Expand Down

0 comments on commit 11a30c9

Please sign in to comment.