-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
0f14bc1
commit 671def2
Showing
8 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...es/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/utils/Emoji.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
45 changes: 45 additions & 0 deletions
45
...essages/impl/src/test/kotlin/io/element/android/features/messages/impl/utils/EmojiTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
...est/snapshots/images/features.messages.impl.timeline_TimelineView_Day_17_en.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
...t/snapshots/images/features.messages.impl.timeline_TimelineView_Night_17_en.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.