Pills: Daggerization
This commit is contained in:
parent
97766404d6
commit
f984758d37
|
@ -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.ReactionInfo
|
||||||
import im.vector.matrix.android.api.session.room.model.relation.RelationDefaultContent
|
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.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.TimelineEvent
|
||||||
import im.vector.matrix.android.api.session.room.timeline.getLastMessageContent
|
import im.vector.matrix.android.api.session.room.timeline.getLastMessageContent
|
||||||
import im.vector.matrix.android.internal.database.helper.addSendingEvent
|
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.di.UserId
|
||||||
import im.vector.matrix.android.internal.session.content.ThumbnailExtractor
|
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.RoomSummaryUpdater
|
||||||
|
import im.vector.matrix.android.internal.session.room.send.pills.TextPillsUtils
|
||||||
import im.vector.matrix.android.internal.util.StringProvider
|
import im.vector.matrix.android.internal.util.StringProvider
|
||||||
import org.commonmark.parser.Parser
|
import org.commonmark.parser.Parser
|
||||||
import org.commonmark.renderer.html.HtmlRenderer
|
import org.commonmark.renderer.html.HtmlRenderer
|
||||||
|
@ -51,9 +51,12 @@ import javax.inject.Inject
|
||||||
*
|
*
|
||||||
* The transactionID is used as loc
|
* The transactionID is used as loc
|
||||||
*/
|
*/
|
||||||
internal class LocalEchoEventFactory @Inject constructor(@UserId private val userId: String,
|
internal class LocalEchoEventFactory @Inject constructor(
|
||||||
private val stringProvider: StringProvider,
|
@UserId private val userId: String,
|
||||||
private val roomSummaryUpdater: RoomSummaryUpdater) {
|
private val stringProvider: StringProvider,
|
||||||
|
private val roomSummaryUpdater: RoomSummaryUpdater,
|
||||||
|
private val textPillsUtils: TextPillsUtils
|
||||||
|
) {
|
||||||
// TODO Inject
|
// TODO Inject
|
||||||
private val parser = Parser.builder().build()
|
private val parser = Parser.builder().build()
|
||||||
// TODO Inject
|
// TODO Inject
|
||||||
|
@ -69,7 +72,7 @@ internal class LocalEchoEventFactory @Inject constructor(@UserId private val use
|
||||||
|
|
||||||
private fun createTextContent(text: CharSequence, autoMarkdown: Boolean): TextContent {
|
private fun createTextContent(text: CharSequence, autoMarkdown: Boolean): TextContent {
|
||||||
if (autoMarkdown) {
|
if (autoMarkdown) {
|
||||||
val source = TextPillsUtils.processSpecialSpansToMarkdown(text)
|
val source = textPillsUtils.processSpecialSpansToMarkdown(text)
|
||||||
?: text.toString()
|
?: text.toString()
|
||||||
val document = parser.parse(source)
|
val document = parser.parse(source)
|
||||||
val htmlText = renderer.render(document)
|
val htmlText = renderer.render(document)
|
||||||
|
@ -79,7 +82,7 @@ internal class LocalEchoEventFactory @Inject constructor(@UserId private val use
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Try to detect pills
|
// Try to detect pills
|
||||||
TextPillsUtils.processSpecialSpansToHtml(text)?.let {
|
textPillsUtils.processSpecialSpansToHtml(text)?.let {
|
||||||
return TextContent(text.toString(), it)
|
return TextContent(text.toString(), it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
)
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -13,10 +13,12 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* 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 android.text.SpannableString
|
||||||
|
import im.vector.matrix.android.api.session.room.send.UserMentionSpan
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility class to detect special span in CharSequence and turn them into
|
* 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...)
|
* For now only support UserMentionSpans (TODO rooms, room aliases, etc...)
|
||||||
*/
|
*/
|
||||||
object TextPillsUtils {
|
internal class TextPillsUtils @Inject constructor(
|
||||||
|
private val mentionLinkSpecComparator: MentionLinkSpecComparator
|
||||||
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)"
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Detects if transformable spans are present in the text.
|
* Detects if transformable spans are present in the text.
|
||||||
|
@ -72,7 +70,7 @@ object TextPillsUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun pruneOverlaps(links: MutableList<MentionLinkSpec>) {
|
private fun pruneOverlaps(links: MutableList<MentionLinkSpec>) {
|
||||||
Collections.sort(links, COMPARATOR)
|
Collections.sort(links, mentionLinkSpecComparator)
|
||||||
var len = links.size
|
var len = links.size
|
||||||
var i = 0
|
var i = 0
|
||||||
while (i < len - 1) {
|
while (i < len - 1) {
|
||||||
|
@ -104,21 +102,9 @@ object TextPillsUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private val COMPARATOR = Comparator<MentionLinkSpec> { (_, startA, endA), (_, startB, endB) ->
|
companion object {
|
||||||
if (startA < startB) {
|
private const val MENTION_SPAN_TO_HTML_TEMPLATE = "<a href=\"https://matrix.to/#/%1\$s\">%2\$s</a>"
|
||||||
return@Comparator -1
|
|
||||||
}
|
|
||||||
|
|
||||||
if (startA > startB) {
|
private const val MENTION_SPAN_TO_MD_TEMPLATE = "[%2\$s](https://matrix.to/#/%1\$s)"
|
||||||
return@Comparator 1
|
|
||||||
}
|
|
||||||
|
|
||||||
if (endA < endB) {
|
|
||||||
return@Comparator 1
|
|
||||||
}
|
|
||||||
|
|
||||||
if (endA > endB) {
|
|
||||||
-1
|
|
||||||
} else 0
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue