Pills: Daggerization

This commit is contained in:
Benoit Marty 2019-11-28 21:40:02 +01:00
parent 97766404d6
commit f984758d37
4 changed files with 76 additions and 30 deletions

View File

@ -28,7 +28,6 @@ import im.vector.matrix.android.api.session.room.model.relation.ReactionContent
import im.vector.matrix.android.api.session.room.model.relation.ReactionInfo
import im.vector.matrix.android.api.session.room.model.relation.RelationDefaultContent
import im.vector.matrix.android.api.session.room.model.relation.ReplyToContent
import im.vector.matrix.android.api.session.room.send.TextPillsUtils
import im.vector.matrix.android.api.session.room.timeline.TimelineEvent
import im.vector.matrix.android.api.session.room.timeline.getLastMessageContent
import im.vector.matrix.android.internal.database.helper.addSendingEvent
@ -37,6 +36,7 @@ import im.vector.matrix.android.internal.database.query.where
import im.vector.matrix.android.internal.di.UserId
import im.vector.matrix.android.internal.session.content.ThumbnailExtractor
import im.vector.matrix.android.internal.session.room.RoomSummaryUpdater
import im.vector.matrix.android.internal.session.room.send.pills.TextPillsUtils
import im.vector.matrix.android.internal.util.StringProvider
import org.commonmark.parser.Parser
import org.commonmark.renderer.html.HtmlRenderer
@ -51,9 +51,12 @@ import javax.inject.Inject
*
* The transactionID is used as loc
*/
internal class LocalEchoEventFactory @Inject constructor(@UserId private val userId: String,
private val stringProvider: StringProvider,
private val roomSummaryUpdater: RoomSummaryUpdater) {
internal class LocalEchoEventFactory @Inject constructor(
@UserId private val userId: String,
private val stringProvider: StringProvider,
private val roomSummaryUpdater: RoomSummaryUpdater,
private val textPillsUtils: TextPillsUtils
) {
// TODO Inject
private val parser = Parser.builder().build()
// TODO Inject
@ -69,7 +72,7 @@ internal class LocalEchoEventFactory @Inject constructor(@UserId private val use
private fun createTextContent(text: CharSequence, autoMarkdown: Boolean): TextContent {
if (autoMarkdown) {
val source = TextPillsUtils.processSpecialSpansToMarkdown(text)
val source = textPillsUtils.processSpecialSpansToMarkdown(text)
?: text.toString()
val document = parser.parse(source)
val htmlText = renderer.render(document)
@ -79,7 +82,7 @@ internal class LocalEchoEventFactory @Inject constructor(@UserId private val use
}
} else {
// Try to detect pills
TextPillsUtils.processSpecialSpansToHtml(text)?.let {
textPillsUtils.processSpecialSpansToHtml(text)?.let {
return TextContent(text.toString(), it)
}
}

View File

@ -0,0 +1,25 @@
/*
* Copyright 2019 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.matrix.android.internal.session.room.send.pills
import im.vector.matrix.android.api.session.room.send.UserMentionSpan
internal data class MentionLinkSpec(
val span: UserMentionSpan,
val start: Int,
val end: Int
)

View File

@ -0,0 +1,32 @@
/*
* Copyright 2019 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.matrix.android.internal.session.room.send.pills
import javax.inject.Inject
internal class MentionLinkSpecComparator @Inject constructor() : Comparator<MentionLinkSpec> {
override fun compare(o1: MentionLinkSpec, o2: MentionLinkSpec): Int {
return when {
o1.start < o2.start -> -1
o1.start > o2.start -> 1
o1.end < o2.end -> 1
o1.end > o2.end -> -1
else -> 0
}
}
}

View File

@ -13,10 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.matrix.android.api.session.room.send
package im.vector.matrix.android.internal.session.room.send.pills
import android.text.SpannableString
import im.vector.matrix.android.api.session.room.send.UserMentionSpan
import java.util.*
import javax.inject.Inject
/**
* Utility class to detect special span in CharSequence and turn them into
@ -24,13 +26,9 @@ import java.util.*
*
* For now only support UserMentionSpans (TODO rooms, room aliases, etc...)
*/
object TextPillsUtils {
private data class MentionLinkSpec(val span: UserMentionSpan, val start: Int, val end: Int)
private const val MENTION_SPAN_TO_HTML_TEMPLATE = "<a href=\"https://matrix.to/#/%1\$s\">%2\$s</a>"
private const val MENTION_SPAN_TO_MD_TEMPLATE = "[%2\$s](https://matrix.to/#/%1\$s)"
internal class TextPillsUtils @Inject constructor(
private val mentionLinkSpecComparator: MentionLinkSpecComparator
) {
/**
* Detects if transformable spans are present in the text.
@ -72,7 +70,7 @@ object TextPillsUtils {
}
private fun pruneOverlaps(links: MutableList<MentionLinkSpec>) {
Collections.sort(links, COMPARATOR)
Collections.sort(links, mentionLinkSpecComparator)
var len = links.size
var i = 0
while (i < len - 1) {
@ -104,21 +102,9 @@ object TextPillsUtils {
}
}
private val COMPARATOR = Comparator<MentionLinkSpec> { (_, startA, endA), (_, startB, endB) ->
if (startA < startB) {
return@Comparator -1
}
companion object {
private const val MENTION_SPAN_TO_HTML_TEMPLATE = "<a href=\"https://matrix.to/#/%1\$s\">%2\$s</a>"
if (startA > startB) {
return@Comparator 1
}
if (endA < endB) {
return@Comparator 1
}
if (endA > endB) {
-1
} else 0
private const val MENTION_SPAN_TO_MD_TEMPLATE = "[%2\$s](https://matrix.to/#/%1\$s)"
}
}