-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Render using yeeted
toAnnotatedString
- Loading branch information
1 parent
bc81a36
commit 9afee20
Showing
6 changed files
with
197 additions
and
12 deletions.
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
118 changes: 118 additions & 0 deletions
118
app/src/main/java/com/imashnake/animite/dev/ext/SpannableExt.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,118 @@ | ||
package com.imashnake.animite.dev.ext | ||
|
||
import android.graphics.Typeface | ||
import android.text.Spanned | ||
import android.text.style.CharacterStyle | ||
import android.text.style.ForegroundColorSpan | ||
import android.text.style.StyleSpan | ||
import android.text.style.URLSpan | ||
import android.text.style.UnderlineSpan | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.text.AnnotatedString | ||
import androidx.compose.ui.text.SpanStyle | ||
import androidx.compose.ui.text.font.FontStyle | ||
import androidx.compose.ui.text.font.FontWeight | ||
import androidx.compose.ui.text.style.TextDecoration | ||
|
||
/** | ||
* [Yeeted](https://stackoverflow.com/a/74100225/11944949). | ||
*/ | ||
fun Spanned.toAnnotatedString(primaryColor: Color): AnnotatedString { | ||
val builder = AnnotatedString.Builder(this.toString()) | ||
val copierContext = CopierContext(primaryColor) | ||
SpanCopier.entries.forEach { copier -> | ||
getSpans(0, length, copier.spanClass).forEach { span -> | ||
copier.copySpan(span, getSpanStart(span), getSpanEnd(span), builder, copierContext) | ||
} | ||
} | ||
return builder.toAnnotatedString() | ||
} | ||
|
||
private data class CopierContext( | ||
val primaryColor: Color, | ||
) | ||
|
||
private enum class SpanCopier { | ||
URL { | ||
override val spanClass = URLSpan::class.java | ||
override fun copySpan( | ||
span: Any, | ||
start: Int, | ||
end: Int, | ||
destination: AnnotatedString.Builder, | ||
context: CopierContext | ||
) { | ||
val urlSpan = span as URLSpan | ||
destination.addStringAnnotation( | ||
tag = name, | ||
annotation = urlSpan.url, | ||
start = start, | ||
end = end, | ||
) | ||
destination.addStyle( | ||
style = SpanStyle(color = context.primaryColor, textDecoration = TextDecoration.Underline), | ||
start = start, | ||
end = end, | ||
) | ||
} | ||
}, | ||
FOREGROUND_COLOR { | ||
override val spanClass = ForegroundColorSpan::class.java | ||
override fun copySpan( | ||
span: Any, | ||
start: Int, | ||
end: Int, | ||
destination: AnnotatedString.Builder, | ||
context: CopierContext | ||
) { | ||
val colorSpan = span as ForegroundColorSpan | ||
destination.addStyle( | ||
style = SpanStyle(color = Color(colorSpan.foregroundColor)), | ||
start = start, | ||
end = end, | ||
) | ||
} | ||
}, | ||
UNDERLINE { | ||
override val spanClass = UnderlineSpan::class.java | ||
override fun copySpan( | ||
span: Any, | ||
start: Int, | ||
end: Int, | ||
destination: AnnotatedString.Builder, | ||
context: CopierContext | ||
) { | ||
destination.addStyle( | ||
style = SpanStyle(textDecoration = TextDecoration.Underline), | ||
start = start, | ||
end = end, | ||
) | ||
} | ||
}, | ||
STYLE { | ||
override val spanClass = StyleSpan::class.java | ||
override fun copySpan( | ||
span: Any, | ||
start: Int, | ||
end: Int, | ||
destination: AnnotatedString.Builder, | ||
context: CopierContext | ||
) { | ||
val styleSpan = span as StyleSpan | ||
|
||
destination.addStyle( | ||
style = when (styleSpan.style) { | ||
Typeface.ITALIC -> SpanStyle(fontStyle = FontStyle.Italic) | ||
Typeface.BOLD -> SpanStyle(fontWeight = FontWeight.Bold) | ||
Typeface.BOLD_ITALIC -> SpanStyle(fontWeight = FontWeight.Bold, fontStyle = FontStyle.Italic) | ||
else -> SpanStyle() | ||
}, | ||
start = start, | ||
end = end, | ||
) | ||
} | ||
}; | ||
|
||
abstract val spanClass: Class<out CharacterStyle> | ||
abstract fun copySpan(span: Any, start: Int, end: Int, destination: AnnotatedString.Builder, context: CopierContext) | ||
} |
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: 2 additions & 1 deletion
3
app/src/main/java/com/imashnake/animite/features/media/MediaUiState.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
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