Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] kotlin – How can I share info among @Composable function in Android Studio?

Posted on November 11, 2022 By

Solution 1 :

You can do this by creating a class that contains properties as default Composables such as Text, TextField or Button use. A class wraps many properties.

  TextStyle(
        color = textColor,
        fontSize = fontSize,
        fontWeight = fontWeight,
        textAlign = textAlign,
        lineHeight = lineHeight,
        fontFamily = fontFamily,
        textDecoration = textDecoration,
        fontStyle = fontStyle,
        letterSpacing = letterSpacing
    )

this class is used by BasicText

fun BasicText(
    text: String,
    modifier: Modifier = Modifier,
    style: TextStyle = TextStyle.Default,
    onTextLayout: (TextLayoutResult) -> Unit = {},
    overflow: TextOverflow = TextOverflow.Clip,
    softWrap: Boolean = true,
    maxLines: Int = Int.MAX_VALUE,
)

TextFieldColors is another example of this

private class DefaultTextFieldColors(
    private val textColor: Color,
    private val disabledTextColor: Color,
    private val cursorColor: Color,
    private val errorCursorColor: Color,
    private val focusedIndicatorColor: Color,
    private val unfocusedIndicatorColor: Color,
    private val errorIndicatorColor: Color,
    private val disabledIndicatorColor: Color,
    private val leadingIconColor: Color,
    private val disabledLeadingIconColor: Color,
    private val errorLeadingIconColor: Color,
    private val trailingIconColor: Color,
    private val disabledTrailingIconColor: Color,
    private val errorTrailingIconColor: Color,
    private val backgroundColor: Color,
    private val focusedLabelColor: Color,
    private val unfocusedLabelColor: Color,
    private val disabledLabelColor: Color,
    private val errorLabelColor: Color,
    private val placeholderColor: Color,
    private val disabledPlaceholderColor: Color
) 

Second option is to create a class that contains properties and can act a State wrapper for other MutableStates. With this approach can trigger recomposition on any individual property change, pass or observe latest properties unlike first approach.

    class BadgeState(
        var maxNumber: Int = 99,
        var circleShapeThreshold: Int = 1,
        @IntRange(from = 0, to = 99) var roundedRadiusPercent: Int = 50,
        backgroundColor: Color,
        var horizontalPadding: Dp = 4.dp,
        var verticalPadding: Dp = 0.dp,
        textColor: Color,
        var fontSize: TextUnit,
        var fontWeight: FontWeight? = null,
        var fontFamily: FontFamily? = null,
        var fontStyle: FontStyle? = null,
        var textDecoration: TextDecoration? = null,
        var shadow: MaterialShadow? = null,
        var borderStroke: BorderStroke? = null,
        showBadgeThreshold: Int = Int.MIN_VALUE,
    ) {
{
    var backgroundColor by mutableStateOf(backgroundColor)

    /*
        Properties for Text
     */
    var textColor by mutableStateOf(textColor)

    var text by mutableStateOf("0")
        private set

    var numberOnBadge by mutableStateOf(0)
        private set

    var showBadgeThreshold by mutableStateOf(showBadgeThreshold)
    ...
}

And wrap it with remember to not create new instance on each recomposition

fun rememberBadgeState(
    maxNumber: Int = 99,
    circleShapeThreshold: Int = 1,
    @IntRange(from = 0, to = 99) roundedRadiusPercent: Int = 50,
    backgroundColor: Color = Color.Red,
    horizontalPadding: Dp = 4.dp,
    verticalPadding: Dp = 0.dp,
    textColor: Color = Color.White,
    fontSize: TextUnit = 14.sp,
    fontWeight: FontWeight? = null,
    fontFamily: FontFamily? = null,
    fontStyle: FontStyle? = null,
    textDecoration: TextDecoration? = null,
    shadow: MaterialShadow? = null,
    borderStroke: BorderStroke? = null,
    showBadgeThreshold: Int = Int.MIN_VALUE,
): BadgeState {
    return remember {
        BadgeState(
            maxNumber,
            circleShapeThreshold,
            roundedRadiusPercent,
            backgroundColor,
            horizontalPadding,
            verticalPadding,
            textColor,
            fontSize,
            fontWeight,
            fontFamily,
            fontStyle,
            textDecoration,
            shadow,
            borderStroke,
            showBadgeThreshold
        )
    }
}

You can pass this state between you Composables

@Composable
fun Badge(
    modifier: Modifier = Modifier,
    badgeState: BadgeState = rememberBadgeState(),
) {

    val showBadge = remember {
        derivedStateOf {
            badgeState.showBadgeThreshold < badgeState.numberOnBadge
        }
    }

    if (showBadge.value) {
        BadgeComponent(badgeState = badgeState, modifier = modifier)
    }
}

or you can pass to a Modifier you created

private fun Modifier.getBadgeModifier(
    badgeState: BadgeState,
    shape: Shape
) = this
    .materialShadow(badgeState = badgeState)
    .then(
        badgeState.borderStroke?.let { borderStroke ->
            this.border(borderStroke, shape = shape)
        } ?: this
    )
    .background(
        badgeState.backgroundColor,
        shape = shape
    )

or another one

fun Modifier.materialShadow(badgeState: BadgeState) = composed(
    inspectorInfo = {
        name = "shadow"
        value = badgeState.shadow
    },
    factory = {
   // rest of the code
}

Problem :

In order to reduce complex, I need to split a big @Composable function to small parts, and I need to share info such as variables among them.

READ  [FIXED] ios - Google Maps not displaying for Android using Flutter
Powered by Inline Related Posts

At present, I use Code A to do it by using top variables, I don’t think it’s a good.

How can I share info among @Composable function in Android Studio?

Code A

private var paddingXAxis = 80f
private var paddingXLabelMargin = 8f
private var paddingYAxis =50f
private var xAxisLength = 100f
private var yAxisLength = 100f
private var maxPointCount = 40


@Composable
fun ScreenHome_Table(
    modifier: Modifier = Modifier,   
    mViewMode: SoundViewModel
) {
    Box( ) {
    
        Canvas(
            modifier = Modifier
                .fillMaxSize()
                .padding(10.dp)
        ) {
            setParameterTable(this, mContext)
            setProcess(this, uiMSoundDensity.soundList.toList())
            setMainAxis(this)  
            setChildXAxis(this, xTime)
            setChildYAxis(this)
        }
    }
}


fun setParameterTable(drawScope: DrawScope, mContext: Context) {
    with(drawScope) {
        xAxisLength = size.width - paddingXAxis * 2
        yAxisLength = size.height - paddingYAxis * 2
    }
    maxPointCount = mContext.resources.getInteger(R.integer.maxCount)
    divisionUnit = mContext.getString(R.string.divisionUnit)
     ...
}


fun setMainAxis(drawScope: DrawScope) {
    ...
}

Comments

Comment posted by Thracian

You can change your functions to extension of

Comment posted by stackoverflow.com/questions/73162570/…

Thanks! Could you have a look at the topic:

Android Tags:android-jetpack-compose, kotlin

Post navigation

Previous Post: [FIXED] Android ACTION_VIEW intent is not opening urls in non-browser apps like twitter and instagram
Next Post: [FIXED] android – Check if activity is active, and if active pass data from service to activity?

Related Posts

[FIXED] android – Linking to a shared object library (.so) when cross-compiling Android
[FIXED] android – How can I get TextField value from another composable function in Jetpack Compose Android
[FIXED] java – 2 OnClickListeners for one Button cannot resolve the OnClick Listener Android
[FIXED] android – Conveyor with Flutter – Handshake error when running .net web app locally Android
[FIXED] android – Wait for FragmentTransaction animation to finish Android
[FIXED] java – Xamarin.Forms WebView not working with WebRTC Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can VPN be traced by police?
  • Where were Kaiser-Frazer cars built?
  • How do you make gold rose gold paint?
  • What are the newest type of dentures?
  • Can you wear joggers as dress pants?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme