Skip to content

Commit

Permalink
Add explanatory comments to certain lines in the utils package
Browse files Browse the repository at this point in the history
  • Loading branch information
wingio committed Dec 5, 2023
1 parent ecac4b3 commit b46dbe7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
6 changes: 3 additions & 3 deletions app/src/main/java/xyz/wingio/dimett/utils/CustomTabUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ private val Context.defaultBrowserPackage: String?
.queryIntentActivities(
Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://")),
0
)
.firstOrNull()
?.activityInfo?.packageName
) // All apps that can open `http://` uris (Usually only browsers)
.firstOrNull() // The default browser app is usually first
?.activityInfo?.packageName // We only need the package name

mDefaultBrowserPackage
} else mDefaultBrowserPackage
Expand Down
12 changes: 7 additions & 5 deletions app/src/main/java/xyz/wingio/dimett/utils/EmojiUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ object EmojiUtils : KoinComponent {
* Parses `assets/emoji.json` and returns a map of emoji codepoints to the Twemoji versions resource name located in `res/raw`
*/
val emojis by lazy {
val _json = String(context.assets.open("emoji.json").readBytes())
json.decodeFromString<EmojiJson>(_json).emoji
val _json = String(context.assets.open("emoji.json").readBytes()) // Read emoji.json to a String
json.decodeFromString<EmojiJson>(_json).emoji // Decode the string into a more useful format
}

/**
Expand All @@ -31,9 +31,11 @@ object EmojiUtils : KoinComponent {
*/
val regex by lazy {
"^(${
emojis.keys.sortedByDescending { it.length }.joinToString("|") { emoji ->
Pattern.quote(emoji)
}
emojis.keys
.sortedByDescending { it.length } // Ensure that we match emoji with modifiers (such as skin tone) before the default version of the emoji
.joinToString("|") { emoji ->
Pattern.quote(emoji)
}
})"
}

Expand Down
16 changes: 12 additions & 4 deletions app/src/main/java/xyz/wingio/dimett/utils/TextUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import xyz.wingio.syntakts.compose.rememberRendered
fun inlineContent(
textStyle: TextStyle = LocalTextStyle.current
): Map<String, InlineTextContent> {
val emoteSize = remember(textStyle) { (textStyle.fontSize.value + 2f).sp }
val emoteSize = remember(textStyle) { (textStyle.fontSize.value + 2f).sp } // Make emojis a little bigger than the surrounding text
val ctx = LocalContext.current

return remember(emoteSize, ctx) {
Expand All @@ -61,9 +61,16 @@ fun inlineContent(
placeholderVerticalAlign = PlaceholderVerticalAlign.Center,
),
) { emoji ->
val emojiImage = BitmapDrawable(EmojiUtils.emojis[emoji]?.let {
ctx.getResId(it)
}?.let { ctx.resources.openRawResource(it) }).bitmap.asImageBitmap()
val emojiImage = BitmapDrawable(
/* res = */ ctx.resources,
/* is = */ EmojiUtils.emojis[emoji]
?.let { rawResourceName ->
ctx.getResId(rawResourceName)
}
?.let { rawResId ->
ctx.resources.openRawResource(rawResId)
}
).bitmap.asImageBitmap()

Image(
bitmap = emojiImage,
Expand Down Expand Up @@ -93,6 +100,7 @@ fun getString(
actionHandler: (actionName: String) -> Unit = {}
): AnnotatedString {
val _string = stringResource(string, *args)

return syntakts.rememberRendered(
text = _string,
context = DefaultRenderContext(
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/xyz/wingio/dimett/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@ fun processPostContent(post: Post): String {
val repliedTo = post.mentions.firstOrNull { mention ->
mention.id == post.userRepliedTo
}

return post.content?.plain?.run {
if (repliedTo != null)
this
.replaceFirst("@${repliedTo.username} ", "")
.replaceFirst("@${repliedTo.acct}", "")
.replaceFirst("@${repliedTo.username} ", "") // @username
.replaceFirst("@${repliedTo.acct}", "") // @[email protected]
else
this
} ?: ""
Expand Down

0 comments on commit b46dbe7

Please sign in to comment.