Skip to content

Commit

Permalink
[Android] Allow the VMDTextStyleProvider to use Composable functions (#…
Browse files Browse the repository at this point in the history
…222)

* Make VMDTextStyleProvider functions Composable
* API build
* changelog
  • Loading branch information
amartellino authored Jun 21, 2024
1 parent 248e0b1 commit 3e87cd9
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

- [VMD] Change parameters order for some Compose components
- [VMD] Change VMDTextField's signature on Android
- [VMD] Change VMDTextStyleProvider::textStyleForResource signature (now Composable)

### Updates

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public final class com/mirego/trikot/viewmodels/declarative/compose/extensions/M
}

public final class com/mirego/trikot/viewmodels/declarative/compose/extensions/TextStyleResourceExtensionsKt {
public static final fun textStyle (Lcom/mirego/trikot/viewmodels/declarative/properties/VMDTextStyleResource;)Landroidx/compose/ui/text/TextStyle;
public static final fun toAnnotatedString (Lcom/mirego/trikot/viewmodels/declarative/components/VMDTextViewModel;)Landroidx/compose/ui/text/AnnotatedString;
public static final fun textStyle (Lcom/mirego/trikot/viewmodels/declarative/properties/VMDTextStyleResource;Landroidx/compose/runtime/Composer;I)Landroidx/compose/ui/text/TextStyle;
public static final fun toAnnotatedString (Lcom/mirego/trikot/viewmodels/declarative/components/VMDTextViewModel;Landroidx/compose/runtime/Composer;I)Landroidx/compose/ui/text/AnnotatedString;
}

public final class com/mirego/trikot/viewmodels/declarative/compose/extensions/VMDAnimatedPropertyChange {
Expand Down Expand Up @@ -244,7 +244,7 @@ public final class com/mirego/trikot/viewmodels/declarative/configuration/Defaul
public final class com/mirego/trikot/viewmodels/declarative/configuration/DefaultTextStyleProvider : com/mirego/trikot/viewmodels/declarative/configuration/VMDTextStyleProvider {
public static final field $stable I
public fun <init> ()V
public fun textStyleForResource (Lcom/mirego/trikot/viewmodels/declarative/properties/VMDTextStyleResource;)Landroidx/compose/ui/text/TextStyle;
public fun textStyleForResource (Lcom/mirego/trikot/viewmodels/declarative/properties/VMDTextStyleResource;Landroidx/compose/runtime/Composer;I)Landroidx/compose/ui/text/TextStyle;
}

public final class com/mirego/trikot/viewmodels/declarative/configuration/TrikotViewModelDeclarative {
Expand All @@ -261,6 +261,6 @@ public abstract interface class com/mirego/trikot/viewmodels/declarative/configu
}

public abstract interface class com/mirego/trikot/viewmodels/declarative/configuration/VMDTextStyleProvider {
public abstract fun textStyleForResource (Lcom/mirego/trikot/viewmodels/declarative/properties/VMDTextStyleResource;)Landroidx/compose/ui/text/TextStyle;
public abstract fun textStyleForResource (Lcom/mirego/trikot/viewmodels/declarative/properties/VMDTextStyleResource;Landroidx/compose/runtime/Composer;I)Landroidx/compose/ui/text/TextStyle;
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mirego.trikot.viewmodels.declarative.compose.extensions

import androidx.compose.runtime.Composable
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextStyle
Expand All @@ -10,14 +11,17 @@ import com.mirego.trikot.viewmodels.declarative.properties.VMDRichTextSpan
import com.mirego.trikot.viewmodels.declarative.properties.VMDSpanStyleResourceTransform
import com.mirego.trikot.viewmodels.declarative.properties.VMDTextStyleResource

@Composable
fun VMDTextStyleResource.textStyle(): TextStyle? =
TrikotViewModelDeclarative.textStyleProvider.textStyleForResource(this)

@Composable
fun VMDTextViewModel.toAnnotatedString() = buildAnnotatedString {
append(text)
spans.forEach { addSpanStyle(it) }
}

@Composable
private fun AnnotatedString.Builder.addSpanStyle(spanStyle: VMDRichTextSpan) {
spanStyle.toComposeSpanStyle()?.let {
addStyle(
Expand All @@ -28,6 +32,7 @@ private fun AnnotatedString.Builder.addSpanStyle(spanStyle: VMDRichTextSpan) {
}
}

@Composable
private fun VMDRichTextSpan.toComposeSpanStyle(): SpanStyle? =
when (val currentTransform = transform) {
is VMDSpanStyleResourceTransform -> currentTransform.textStyleResource.textStyle()?.toSpanStyle()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.mirego.trikot.viewmodels.declarative.configuration

import androidx.compose.runtime.Composable
import androidx.compose.ui.text.TextStyle
import com.mirego.trikot.viewmodels.declarative.properties.VMDTextStyleResource

interface VMDTextStyleProvider {
@Composable
fun textStyleForResource(resource: VMDTextStyleResource): TextStyle?
}

class DefaultTextStyleProvider : VMDTextStyleProvider {
@Composable
override fun textStyleForResource(resource: VMDTextStyleResource): TextStyle? = null
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.mirego.sample.resource

import androidx.compose.runtime.Composable
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import com.mirego.sample.resources.SampleTextStyleResource
Expand All @@ -8,16 +10,19 @@ import com.mirego.trikot.viewmodels.declarative.properties.VMDNoTextStyleResourc
import com.mirego.trikot.viewmodels.declarative.properties.VMDTextStyleResource

class SampleTextStyleProvider : VMDTextStyleProvider {
@Composable
override fun textStyleForResource(resource: VMDTextStyleResource): TextStyle? =
when (resource) {
is VMDNoTextStyleResource -> null
is SampleTextStyleResource -> mapSampleStyleResource(resource)
else -> null
}

private fun mapSampleStyleResource(resource: VMDTextStyleResource): TextStyle? =
@Composable
private fun mapSampleStyleResource(resource: SampleTextStyleResource): TextStyle =
when (resource) {
SampleTextStyleResource.HIGHLIGHTED -> TextStyle(background = Color.Yellow)
else -> null
SampleTextStyleResource.HIGHLIGHTED -> TextStyle(background = LocalHighlightColor.current)
}
}

private val LocalHighlightColor = compositionLocalOf { Color.Yellow }

0 comments on commit 3e87cd9

Please sign in to comment.