Skip to content

Commit e8d1598

Browse files
authored
Merge pull request #3295 from frebib/feat/big-emoji
Big emoji-only messages
2 parents f9af2b8 + 23001d6 commit e8d1598

File tree

11 files changed

+112
-7
lines changed

11 files changed

+112
-7
lines changed

features/messages/impl/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ dependencies {
7272
implementation(libs.androidx.constraintlayout.compose)
7373
implementation(libs.androidx.media3.exoplayer)
7474
implementation(libs.androidx.media3.ui)
75+
implementation(libs.sigpwned.emoji4j)
7576
implementation(libs.vanniktech.blurhash)
7677
implementation(libs.telephoto.zoomableimage)
7778
implementation(libs.matrix.emojibase.bindings)

features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/timeline/components/event/TimelineItemTextView.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import io.element.android.features.messages.impl.timeline.components.layout.Cont
3535
import io.element.android.features.messages.impl.timeline.components.layout.ContentAvoidingLayoutData
3636
import io.element.android.features.messages.impl.timeline.model.event.TimelineItemTextBasedContent
3737
import io.element.android.features.messages.impl.timeline.model.event.TimelineItemTextBasedContentProvider
38+
import io.element.android.features.messages.impl.utils.containsOnlyEmojis
3839
import io.element.android.libraries.designsystem.preview.ElementPreview
3940
import io.element.android.libraries.designsystem.preview.PreviewsDayNight
4041
import io.element.android.libraries.matrix.api.core.UserId
@@ -54,9 +55,15 @@ fun TimelineItemTextView(
5455
modifier: Modifier = Modifier,
5556
onContentLayoutChange: (ContentAvoidingLayoutData) -> Unit = {},
5657
) {
58+
val emojiOnly = (content.formattedBody == null || content.formattedBody.toString() == content.body) &&
59+
content.body.replace(" ", "").containsOnlyEmojis()
60+
val textStyle = when {
61+
emojiOnly -> ElementTheme.typography.fontHeadingXlRegular
62+
else -> ElementTheme.typography.fontBodyLgRegular
63+
}
5764
CompositionLocalProvider(
5865
LocalContentColor provides ElementTheme.colors.textPrimary,
59-
LocalTextStyle provides ElementTheme.typography.fontBodyLgRegular
66+
LocalTextStyle provides textStyle
6067
) {
6168
val body = getTextWithResolvedMentions(content)
6269
Box(modifier.semantics { contentDescription = content.plainText }) {

features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/timeline/model/AggregatedReactionProvider.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ fun anAggregatedReaction(
3838
count: Int = 1,
3939
isHighlighted: Boolean = false,
4040
): AggregatedReaction {
41+
val timeFormatter = DateFormat.getTimeInstance(DateFormat.SHORT, java.util.Locale.US)
42+
val date = Date(1_689_061_264L)
4143
val senders = buildList {
4244
repeat(count) { index ->
43-
val timeFormatter = DateFormat.getTimeInstance(DateFormat.SHORT)
44-
val date = Date(1_689_061_264L)
4545
add(
4646
AggregatedReactionSender(
4747
senderId = if (isHighlighted && index == 0) userId else UserId("@user$index:server.org"),

features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/timeline/model/event/TimelineItemEventContentProvider.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class TimelineItemEventContentProvider : PreviewParameterProvider<TimelineItemEv
4242
aTimelineItemTextContent(),
4343
aTimelineItemUnknownContent(),
4444
aTimelineItemTextContent().copy(isEdited = true),
45+
aTimelineItemTextContent(body = "😁")
4546
)
4647
}
4748

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2024 New Vector Ltd
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.element.android.features.messages.impl.utils
18+
19+
import com.sigpwned.emoji4j.core.Grapheme.Type.EMOJI
20+
import com.sigpwned.emoji4j.core.Grapheme.Type.PICTOGRAPHIC
21+
import com.sigpwned.emoji4j.core.GraphemeMatchResult
22+
import com.sigpwned.emoji4j.core.GraphemeMatcher
23+
24+
/**
25+
* Returns true if the string consists exclusively of "emoji or pictographic graphemes".
26+
*/
27+
fun String.containsOnlyEmojis(): Boolean {
28+
if (isEmpty()) return false
29+
30+
val matcher = GraphemeMatcher(this)
31+
var m: GraphemeMatchResult? = null
32+
var contiguous = true
33+
var previous = 0
34+
while (contiguous && matcher.find()) {
35+
m = matcher.toMatchResult()
36+
// Many non-"emoji" characters are pictographics. We only want to identify this specific range
37+
// https://en.wikipedia.org/wiki/Miscellaneous_Symbols_and_Pictographs
38+
val isEmoji = m!!.grapheme().type == EMOJI || m.grapheme().type == PICTOGRAPHIC && m.group() in "🌍".."🗺"
39+
contiguous = isEmoji and (m.start() == previous)
40+
previous = m.end()
41+
}
42+
43+
return contiguous and (m?.end() == length)
44+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2024 New Vector Ltd
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.element.android.features.messages.impl.utils
18+
19+
import org.junit.Assert
20+
import org.junit.Test
21+
22+
class EmojiTest {
23+
@Test
24+
fun validEmojis() {
25+
// Simple single/multiple single-codepoint emojis per string
26+
Assert.assertTrue("👍".containsOnlyEmojis())
27+
Assert.assertTrue("😀".containsOnlyEmojis())
28+
Assert.assertTrue("🙂🙁".containsOnlyEmojis())
29+
Assert.assertTrue("👁❤️🍝".containsOnlyEmojis()) // 👁 is a pictographic
30+
Assert.assertTrue("👨‍👩‍👦1️⃣🚀👳🏾‍♂️🪩".containsOnlyEmojis())
31+
Assert.assertTrue("🌍🌎🌏".containsOnlyEmojis())
32+
33+
// Awkward multi-codepoint graphemes
34+
Assert.assertTrue("🧑‍🧑‍🧒‍🧒".containsOnlyEmojis())
35+
Assert.assertTrue("🏴‍☠".containsOnlyEmojis())
36+
Assert.assertTrue("👩🏿‍🔧".containsOnlyEmojis())
37+
38+
Assert.assertFalse("".containsOnlyEmojis())
39+
Assert.assertFalse(" ".containsOnlyEmojis())
40+
Assert.assertFalse("🙂 🙁".containsOnlyEmojis())
41+
Assert.assertFalse(" 🙂 🙁 ".containsOnlyEmojis())
42+
Assert.assertFalse("Hello".containsOnlyEmojis())
43+
Assert.assertFalse("Hello 👋".containsOnlyEmojis())
44+
}
45+
}

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ matrix_analytics_events = "com.github.matrix-org:matrix-analytics-events:0.23.1"
192192

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

196197
# Di
197198
inject = "javax.inject:javax.inject:1"
Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)