Skip to content

Commit

Permalink
SampleApp: make parser a class variable
Browse files Browse the repository at this point in the history
Shows that the parser holds no state, and can be initialized just once.
  • Loading branch information
lytefast committed Nov 4, 2020
1 parent b5f079c commit 7f15c66
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions app/src/main/java/com/discord/simpleast/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,24 @@ class MainActivity : AppCompatActivity() {
private lateinit var resultText: TextView
private lateinit var input: EditText

private lateinit var parser: Parser<RenderContext, Node<RenderContext>, ParseState>

@SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

parser = Parser<RenderContext, Node<RenderContext>, ParseState>()
.addRules(UserMentionRule(), CustomMarkdownRules.createBlockQuoteRule())
.addRules(CustomMarkdownRules.createMarkdownRules(
this@MainActivity,
listOf(R.style.Demo_Header_1, R.style.Demo_Header_2, R.style.Demo_Header_3),
listOf(R.style.Demo_Header_1_Add, R.style.Demo_Header_1_Remove, R.style.Demo_Header_1_Fix)))
.addRules(
CustomMarkdownRules.createCodeRule(this@MainActivity),
CustomMarkdownRules.createCodeInlineRule(this@MainActivity))
.addRules(SimpleMarkdownRules.createSimpleMarkdownRules())

resultText = findViewById(R.id.result_text)
input = findViewById(R.id.input)

Expand Down Expand Up @@ -70,17 +83,6 @@ class MainActivity : AppCompatActivity() {

private fun parseInput() = lifecycleScope.launchWhenStarted {
val renderedText = withContext(Dispatchers.IO) {
val parser = Parser<RenderContext, Node<RenderContext>, ParseState>()
.addRules(UserMentionRule(), CustomMarkdownRules.createBlockQuoteRule())
.addRules(CustomMarkdownRules.createMarkdownRules(
this@MainActivity,
listOf(R.style.Demo_Header_1, R.style.Demo_Header_2, R.style.Demo_Header_3),
listOf(R.style.Demo_Header_1_Add, R.style.Demo_Header_1_Remove, R.style.Demo_Header_1_Fix)))
.addRules(
CustomMarkdownRules.createCodeRule(this@MainActivity),
CustomMarkdownRules.createCodeInlineRule(this@MainActivity))
.addRules(SimpleMarkdownRules.createSimpleMarkdownRules())

SimpleRenderer.render(
source = input.text,
parser = parser,
Expand All @@ -93,7 +95,11 @@ class MainActivity : AppCompatActivity() {

private fun testParse(times: Int) {
for (i in 0 until times) {
SimpleRenderer.renderBasicMarkdown(SampleTexts.ALL)
SimpleRenderer.render(
source = SampleTexts.BENCHMARK_TEXT.trimIndent(),
parser = parser,
initialState = ParseState(false),
renderContext = RenderContext(mapOf(1234 to "User1234")))
}
}

Expand Down

0 comments on commit 7f15c66

Please sign in to comment.