Skip to content

Commit

Permalink
Big emoji-only messages
Browse files Browse the repository at this point in the history
Adapted from SpiritCroc's SchildNext implementation from
SchildiChat/schildichat-android-next@7eba87f

Fixes: #1438
Signed-off-by: Tobias Büttner <[email protected]>
Signed-off-by: Joe Groocock <[email protected]>
  • Loading branch information
SpiritCroc authored and frebib committed Aug 23, 2024
1 parent 0f14bc1 commit 671def2
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 1 deletion.
1 change: 1 addition & 0 deletions features/messages/impl/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ dependencies {
implementation(libs.androidx.constraintlayout.compose)
implementation(libs.androidx.media3.exoplayer)
implementation(libs.androidx.media3.ui)
implementation(libs.sigpwned.emoji4j)
implementation(libs.vanniktech.blurhash)
implementation(libs.telephoto.zoomableimage)
implementation(libs.matrix.emojibase.bindings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import io.element.android.features.messages.impl.timeline.components.layout.Cont
import io.element.android.features.messages.impl.timeline.components.layout.ContentAvoidingLayoutData
import io.element.android.features.messages.impl.timeline.model.event.TimelineItemTextBasedContent
import io.element.android.features.messages.impl.timeline.model.event.TimelineItemTextBasedContentProvider
import io.element.android.features.messages.impl.utils.containsOnlyEmojis
import io.element.android.libraries.designsystem.preview.ElementPreview
import io.element.android.libraries.designsystem.preview.PreviewsDayNight
import io.element.android.libraries.matrix.api.core.UserId
Expand All @@ -54,9 +55,15 @@ fun TimelineItemTextView(
modifier: Modifier = Modifier,
onContentLayoutChange: (ContentAvoidingLayoutData) -> Unit = {},
) {
val emojiOnly = (content.formattedBody == null || content.formattedBody.toString() == content.body) &&
content.body.replace(" ", "").containsOnlyEmojis()
val textStyle = when {
emojiOnly -> ElementTheme.typography.fontHeadingXlRegular
else -> ElementTheme.typography.fontBodyLgRegular
}
CompositionLocalProvider(
LocalContentColor provides ElementTheme.colors.textPrimary,
LocalTextStyle provides ElementTheme.typography.fontBodyLgRegular
LocalTextStyle provides textStyle
) {
val body = getTextWithResolvedMentions(content)
Box(modifier.semantics { contentDescription = content.plainText }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class TimelineItemEventContentProvider : PreviewParameterProvider<TimelineItemEv
aTimelineItemTextContent(),
aTimelineItemUnknownContent(),
aTimelineItemTextContent().copy(isEdited = true),
aTimelineItemTextContent(body = "😁")
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2024 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
*
* https://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 io.element.android.features.messages.impl.utils

import com.sigpwned.emoji4j.core.Grapheme.Type.EMOJI
import com.sigpwned.emoji4j.core.Grapheme.Type.PICTOGRAPHIC
import com.sigpwned.emoji4j.core.GraphemeMatchResult
import com.sigpwned.emoji4j.core.GraphemeMatcher

/**
* Returns true if the string consists exclusively of "emoji or pictographic graphemes".
*/
fun String.containsOnlyEmojis(): Boolean {
if (isEmpty()) return false

val matcher = GraphemeMatcher(this)
var m: GraphemeMatchResult? = null
var contiguous = true
var previous = 0
while (contiguous && matcher.find()) {
m = matcher.toMatchResult()
// Many non-"emoji" characters are pictographics. We only want to identify this specific range
// https://en.wikipedia.org/wiki/Miscellaneous_Symbols_and_Pictographs
val isEmoji = m!!.grapheme().type == EMOJI || (m.grapheme().type == PICTOGRAPHIC && m.group() in "🌍".."🗺")
contiguous = isEmoji and (m.start() == previous)
previous = m.end()
}

return contiguous and (m?.end() == length)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2024 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
*
* https://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 io.element.android.features.messages.impl.utils

import org.junit.Assert
import org.junit.Test

class EmojiTest {
@Test
fun validEmojis() {
// Simple single/multiple single-codepoint emojis per string
Assert.assertTrue("👍".containsOnlyEmojis())
Assert.assertTrue("😀".containsOnlyEmojis())
Assert.assertTrue("🙂🙁".containsOnlyEmojis())
Assert.assertTrue("👁❤️🍝".containsOnlyEmojis()) // 👁 is a pictographic
Assert.assertTrue("👨‍👩‍👦1️⃣🚀👳🏾‍♂️🪩".containsOnlyEmojis())
Assert.assertTrue("🌍🌎🌏".containsOnlyEmojis())

// Awkward multi-codepoint graphemes
Assert.assertTrue("🧑‍🧑‍🧒‍🧒".containsOnlyEmojis())
Assert.assertTrue("🏴‍☠".containsOnlyEmojis())
Assert.assertTrue("👩🏿‍🔧".containsOnlyEmojis())

Assert.assertFalse("".containsOnlyEmojis())
Assert.assertFalse(" ".containsOnlyEmojis())
Assert.assertFalse("🙂 🙁".containsOnlyEmojis())
Assert.assertFalse(" 🙂 🙁 ".containsOnlyEmojis())
Assert.assertFalse("Hello".containsOnlyEmojis())
Assert.assertFalse("Hello 👋".containsOnlyEmojis())
}
}
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ matrix_analytics_events = "com.github.matrix-org:matrix-analytics-events:0.23.1"

# Emojibase
matrix_emojibase_bindings = "io.element.android:emojibase-bindings:1.1.3"
sigpwned_emoji4j = "com.sigpwned:emoji4j-core:15.1.0"

# Di
inject = "javax.inject:javax.inject:1"
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 671def2

Please sign in to comment.