Skip to content

Commit

Permalink
Linkify summary in TalkingKotlinEpisode.
Browse files Browse the repository at this point in the history
  • Loading branch information
ychescale9 committed Jan 4, 2024
1 parent dbfabde commit 06d14f5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ import androidx.compose.foundation.layout.statusBars
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.ClickableText
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.painter.ColorPainter
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -204,8 +207,18 @@ private fun ContentUi(
)
}
item {
Text(
text = episode.summary,
val linkStyle = SpanStyle(
color = KSTheme.colorScheme.primary,
fontWeight = FontWeight.Bold,
)
val annotatedString = remember(episode.summary) {
episode.summary.linkify(linkStyle)
}
ClickableText(
text = annotatedString,
onClick = { offset ->
annotatedString.findUrl(offset)?.let(onOpenLink)
},
style = KSTheme.typography.bodyMedium,
modifier = Modifier.padding(horizontal = 24.dp),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.github.reactivecircus.kstreamlined.android.feature.talkingkotlinepisode

import android.text.SpannableString
import android.text.style.URLSpan
import android.text.util.Linkify
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString

/**
* Adopted from https://stackoverflow.com/a/73662287
*/
internal fun String.linkify(
linkStyle: SpanStyle
): AnnotatedString = buildAnnotatedString {
append(this@linkify)
val spannable = SpannableString(this@linkify)
Linkify.addLinks(spannable, Linkify.WEB_URLS)
val spans = spannable.getSpans(0, spannable.length, URLSpan::class.java)
for (span in spans) {
val start = spannable.getSpanStart(span)
val end = spannable.getSpanEnd(span)
addStyle(
start = start,
end = end,
style = linkStyle,
)
addStringAnnotation(
tag = UrlTag,
annotation = span.url,
start = start,
end = end,
)
}
}

internal fun AnnotatedString.findUrl(offset: Int): String? =
getStringAnnotations(UrlTag, offset, offset)
.firstOrNull()
?.item

private const val UrlTag = "URL"
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,7 @@ public class FeedRepository(
savedForLater = false,
audioUrl = "https://feeds.soundcloud.com/stream/1689535512-user-38099918-network-resilient-applications-with-store5-talking-kotlin-128.mp3",
thumbnailUrl = "https://i1.sndcdn.com/artworks-f8n8RnHDXyOrojBY-615tKg-t3000x3000.jpg",
summary = "Welcome to another engaging episode of Talking Kotlin! In this edition, we dive into the dynamic world of Android development with Colin White, the creator of the widely acclaimed Coil library. Join us as we discuss the latest developments, insights, and the exciting roadmap for Coil.\n" +
"\n" +
"\uD83D\uDE80 Highlights from this Episode:\n" +
"\n" +
"Learn about Colin's journey in developing the Coil library.\n" +
"Discover the pivotal role Coil plays in simplifying image loading for Android developers.\n" +
"Get an exclusive sneak peek into the upcoming Coil 3.0, featuring multi-platform support and seamless integration with Jetpack Compose.\n" +
"\uD83D\uDD17 Helpful Links:\n" +
"\n" +
"Coil Library GitHub: github.com/coilkt/coil\n" +
"Follow Colin White on Twitter: @colinwhi\n" +
"\n" +
"\uD83C\uDF10 Connect with the Kotlin Community: https://kotlinlang.org/community/\n" +
"\n" +
"Kotlin Foundation: https://kotlinfoundation.org/",
summary = "Welcome to another engaging episode of Talking Kotlin! In this edition, we dive into the dynamic world of Android development with Colin White, the creator of the widely acclaimed Coil library. Join us as we discuss the latest developments, insights, and the exciting roadmap for Coil. \uD83D\uDE80 Highlights from this Episode: Learn about Colin's journey in developing the Coil library. Discover the pivotal role Coil plays in simplifying image loading for Android developers. Get an exclusive sneak peek into the upcoming Coil 3.0, featuring multi-platform support and seamless integration with Jetpack Compose. \uD83D\uDD17 Helpful Links: Coil Library GitHub: github.com/coilkt/coil Follow Colin White on Twitter: @colinwhi \uD83C\uDF10 Connect with the Kotlin Community: https://kotlinlang.org/community/ Kotlin Foundation: https://kotlinfoundation.org/",
duration = "42min.",
)
}
Expand Down

0 comments on commit 06d14f5

Please sign in to comment.