Skip to content

Commit

Permalink
Merge pull request #84 from akiomik/linkable-text
Browse files Browse the repository at this point in the history
Linkable text
  • Loading branch information
akiomik authored Apr 4, 2023
2 parents 97c3a59 + 9ae2f5c commit 13939b7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.github.akiomik.seiun.ui.components

import android.text.method.LinkMovementMethod
import android.text.util.Linkify
import android.widget.TextView
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.text.util.LinkifyCompat
import io.github.akiomik.seiun.R

// https://stackoverflow.com/a/68670583/1918609
@Composable
fun LinkableText(text: String, modifier: Modifier = Modifier) {
val context = LocalContext.current
val customLinkifyTextView = remember { TextView(context) }

AndroidView(modifier = modifier, factory = { customLinkifyTextView }) { textView ->
textView.text = text
LinkifyCompat.addLinks(
textView,
Linkify.WEB_URLS or Linkify.EMAIL_ADDRESSES or Linkify.PHONE_NUMBERS
)
textView.movementMethod = LinkMovementMethod.getInstance()
textView.setTextAppearance(R.style.linkable_text)
}
}
10 changes: 8 additions & 2 deletions app/src/main/java/io/github/akiomik/seiun/ui/feed/FeedPost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import coil.compose.AsyncImage
import io.github.akiomik.seiun.R
import io.github.akiomik.seiun.model.app.bsky.embed.ImagesViewImage
import io.github.akiomik.seiun.model.app.bsky.feed.FeedViewPost
import io.github.akiomik.seiun.ui.components.LinkableText
import io.github.akiomik.seiun.ui.dialog.DeleteDialog
import io.github.akiomik.seiun.ui.dialog.MuteDialog
import io.github.akiomik.seiun.ui.dialog.ReportDialog
Expand Down Expand Up @@ -356,14 +357,19 @@ private fun FeedPostContent(viewPost: FeedViewPost) {

if (viewPost.post.record.text.isNotEmpty()) {
SelectionContainer {
Text(text = viewPost.post.record.text, modifier = Modifier.padding(top = 8.dp))
LinkableText(
text = viewPost.post.record.text,
modifier = Modifier.padding(top = 8.dp)
)
}
}

ImageTile(viewPost)

Row(
modifier = Modifier.padding(top = 8.dp, bottom = 4.dp).fillMaxWidth(),
modifier = Modifier
.padding(top = 8.dp, bottom = 4.dp)
.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import androidx.lifecycle.viewmodel.compose.viewModel
import coil.compose.AsyncImage
import io.github.akiomik.seiun.R
import io.github.akiomik.seiun.model.app.bsky.actor.ProfileViewDetailed
import io.github.akiomik.seiun.ui.components.LinkableText
import io.github.akiomik.seiun.ui.feed.LoadingErrorMessage
import io.github.akiomik.seiun.ui.theme.Indigo800
import io.github.akiomik.seiun.viewmodels.AppViewModel
Expand Down Expand Up @@ -224,7 +225,7 @@ private fun Profile(

NameAndHandle(profile = profile)

Text(profile.description.orEmpty())
LinkableText(profile.description.orEmpty())

StatRow(
profile = profile,
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/values-night/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="linkable_text" parent="TextAppearance.AppCompat">
<item name="android:textColor">@color/white</item>
<item name="android:textSize">16sp</item>
<item name="android:lineHeight">24sp</item>
</style>
</resources>
7 changes: 7 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="linkable_text" parent="TextAppearance.AppCompat">
<item name="android:textSize">16sp</item>
<item name="android:lineHeight">24sp</item>
</style>
</resources>

0 comments on commit 13939b7

Please sign in to comment.