Create SpanTemplateType and factorize template creation

This commit is contained in:
Arnaud Ringenbach 2022-03-15 16:33:59 +01:00
parent 094ebe6764
commit 56760ecddc
5 changed files with 30 additions and 41 deletions

View File

@ -18,6 +18,7 @@ package org.matrix.android.sdk.internal.session.room.send
import org.matrix.android.sdk.api.session.events.model.Event
import org.matrix.android.sdk.api.session.permalinks.PermalinkService
import org.matrix.android.sdk.api.session.permalinks.PermalinkService.SpanTemplateType.*
class TestPermalinkService : PermalinkService {
override fun createPermalink(event: Event, forceMatrixTo: Boolean): String? {
@ -40,11 +41,10 @@ class TestPermalinkService : PermalinkService {
return null
}
override fun createHtmlMentionSpanTemplate(forceMatrixTo: Boolean): String {
return "<a href=\"https://matrix.to/#/%1\$s\">%2\$s</a>"
}
override fun createMdMentionSpanTemplate(forceMatrixTo: Boolean): String {
return "[%2\$s](https://matrix.to/#/%1\$s)"
override fun createMentionSpanTemplate(type: PermalinkService.SpanTemplateType, forceMatrixTo: Boolean): String {
return when (type) {
HTML -> "<a href=\"https://matrix.to/#/%1\$s\">%2\$s</a>"
MARKDOWN -> "[%2\$s](https://matrix.to/#/%1\$s)"
}
}
}

View File

@ -28,6 +28,11 @@ interface PermalinkService {
const val MATRIX_TO_URL_BASE = "https://matrix.to/#/"
}
enum class SpanTemplateType {
HTML,
MARKDOWN
}
/**
* Creates a permalink for an event.
* Ex: "https://matrix.to/#/!nbzmcXAqpxBXjAdgoX:matrix.org/$1531497316352799BevdV:matrix.org"
@ -82,22 +87,13 @@ interface PermalinkService {
fun getLinkedId(url: String): String?
/**
* Creates a HTML mention span template. Can be used to replace a mention with a permalink to mentioned user.
* Ex: "<a href=\"https://matrix.to/#/%1\$s\">%2\$s</a>"
* Creates a HTML or Markdown mention span template. Can be used to replace a mention with a permalink to mentioned user.
* Ex: "<a href=\"https://matrix.to/#/%1\$s\">%2\$s</a>" or "[%2\$s](https://matrix.to/#/%1\$s)"
*
* @param type: type of template to create
* @param forceMatrixTo whether we should force using matrix.to base URL
*
* @return the HTML template
* @return the created template
*/
fun createHtmlMentionSpanTemplate(forceMatrixTo: Boolean = false): String
/**
* Creates a Markdown mention span template. Can be used to replace a mention with a permalink to mentioned user.
* Ex: "[%2\$s](https://matrix.to/#/%1\$s)"
*
* @param forceMatrixTo whether we should force using matrix.to base URL
*
* @return the Markdown template
*/
fun createMdMentionSpanTemplate(forceMatrixTo: Boolean = false): String
fun createMentionSpanTemplate(type: SpanTemplateType, forceMatrixTo: Boolean = false): String
}

View File

@ -44,11 +44,7 @@ internal class DefaultPermalinkService @Inject constructor(
return permalinkFactory.getLinkedId(url)
}
override fun createHtmlMentionSpanTemplate(forceMatrixTo: Boolean): String {
return permalinkFactory.createHtmlMentionSpanTemplate(forceMatrixTo)
}
override fun createMdMentionSpanTemplate(forceMatrixTo: Boolean): String {
return permalinkFactory.createMdMentionSpanTemplate(forceMatrixTo)
override fun createMentionSpanTemplate(type: PermalinkService.SpanTemplateType, forceMatrixTo: Boolean): String {
return permalinkFactory.createMentionSpanTemplate(type, forceMatrixTo)
}
}

View File

@ -21,7 +21,9 @@ import org.matrix.android.sdk.api.MatrixPatterns
import org.matrix.android.sdk.api.session.events.model.Event
import org.matrix.android.sdk.api.session.permalinks.PermalinkData
import org.matrix.android.sdk.api.session.permalinks.PermalinkParser
import org.matrix.android.sdk.api.session.permalinks.PermalinkService
import org.matrix.android.sdk.api.session.permalinks.PermalinkService.Companion.MATRIX_TO_URL_BASE
import org.matrix.android.sdk.api.session.permalinks.PermalinkService.SpanTemplateType.*
import org.matrix.android.sdk.internal.di.UserId
import javax.inject.Inject
@ -105,25 +107,20 @@ internal class PermalinkFactory @Inject constructor(
?.substringBeforeLast("?")
}
fun createHtmlMentionSpanTemplate(forceMatrixTo: Boolean): String {
fun createMentionSpanTemplate(type: PermalinkService.SpanTemplateType, forceMatrixTo: Boolean): String {
return buildString {
append(MENTION_SPAN_TO_HTML_TEMPLATE_BEGIN)
when (type) {
HTML -> append(MENTION_SPAN_TO_HTML_TEMPLATE_BEGIN)
MARKDOWN -> append(MENTION_SPAN_TO_MD_TEMPLATE_BEGIN)
}
append(baseUrl(forceMatrixTo))
if (useClientFormat(forceMatrixTo)) {
append(USER_PATH)
}
append(MENTION_SPAN_TO_HTML_TEMPLATE_END)
}
}
fun createMdMentionSpanTemplate(forceMatrixTo: Boolean): String {
return buildString {
append(MENTION_SPAN_TO_MD_TEMPLATE_BEGIN)
append(baseUrl(forceMatrixTo))
if (useClientFormat(forceMatrixTo)) {
append(USER_PATH)
when (type) {
HTML -> append(MENTION_SPAN_TO_HTML_TEMPLATE_END)
MARKDOWN -> append(MENTION_SPAN_TO_MD_TEMPLATE_END)
}
append(MENTION_SPAN_TO_MD_TEMPLATE_END)
}
}

View File

@ -38,7 +38,7 @@ internal class TextPillsUtils @Inject constructor(
* @return the transformed String or null if no Span found
*/
fun processSpecialSpansToHtml(text: CharSequence): String? {
return transformPills(text, permalinkService.createHtmlMentionSpanTemplate())
return transformPills(text, permalinkService.createMentionSpanTemplate(PermalinkService.SpanTemplateType.HTML))
}
/**
@ -46,7 +46,7 @@ internal class TextPillsUtils @Inject constructor(
* @return the transformed String or null if no Span found
*/
fun processSpecialSpansToMarkdown(text: CharSequence): String? {
return transformPills(text, permalinkService.createMdMentionSpanTemplate())
return transformPills(text, permalinkService.createMentionSpanTemplate(PermalinkService.SpanTemplateType.MARKDOWN))
}
private fun transformPills(text: CharSequence, template: String): String? {