Skip to content

Commit

Permalink
Refactor: Inline PreferenceDefaults now that it's no longer reused
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanghai committed Sep 17, 2024
1 parent 6600b1f commit f3a7e32
Showing 1 changed file with 37 additions and 52 deletions.
89 changes: 37 additions & 52 deletions library/src/main/java/me/zhanghai/compose/preference/Preference.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,67 +81,52 @@ fun Preference(
)
)
) {
PreferenceDefaults.TitleContainer(title = title, enabled = enabled)
PreferenceDefaults.SummaryContainer(summary = summary, enabled = enabled)
CompositionLocalProvider(
LocalContentColor provides
theme.titleColor.let {
if (enabled) it else it.copy(alpha = theme.disabledOpacity)
}
) {
ProvideTextStyle(value = theme.titleTextStyle, content = title)
}
if (summary != null) {
CompositionLocalProvider(
LocalContentColor provides
theme.summaryColor.let {
if (enabled) it else it.copy(alpha = theme.disabledOpacity)
}
) {
ProvideTextStyle(value = theme.summaryTextStyle, content = summary)
}
}
}
},
modifier = modifier,
enabled = enabled,
iconContainer = { PreferenceDefaults.IconContainer(icon = icon, enabled = enabled) },
iconContainer = {
if (icon != null) {
val theme = LocalPreferenceTheme.current
Box(
modifier =
Modifier.widthIn(min = theme.iconContainerMinWidth)
.padding(theme.padding.copy(end = 0.dp)),
contentAlignment = Alignment.CenterStart,
) {
CompositionLocalProvider(
LocalContentColor provides
theme.iconColor.let {
if (enabled) it else it.copy(alpha = theme.disabledOpacity)
},
content = icon,
)
}
}
},
widgetContainer = { widgetContainer?.invoke() },
onClick = onClick,
)
}

internal object PreferenceDefaults {
@Composable
fun IconContainer(icon: @Composable (() -> Unit)?, enabled: Boolean) {
if (icon != null) {
val theme = LocalPreferenceTheme.current
Box(
modifier =
Modifier.widthIn(min = theme.iconContainerMinWidth)
.padding(theme.padding.copy(end = 0.dp)),
contentAlignment = Alignment.CenterStart,
) {
CompositionLocalProvider(
LocalContentColor provides
theme.iconColor.let {
if (enabled) it else it.copy(alpha = theme.disabledOpacity)
},
content = icon,
)
}
}
}

@Composable
fun TitleContainer(title: @Composable () -> Unit, enabled: Boolean) {
val theme = LocalPreferenceTheme.current
CompositionLocalProvider(
LocalContentColor provides
theme.titleColor.let { if (enabled) it else it.copy(alpha = theme.disabledOpacity) }
) {
ProvideTextStyle(value = theme.titleTextStyle, content = title)
}
}

@Composable
fun SummaryContainer(summary: (@Composable () -> Unit)?, enabled: Boolean) {
if (summary != null) {
val theme = LocalPreferenceTheme.current
CompositionLocalProvider(
LocalContentColor provides
theme.summaryColor.let {
if (enabled) it else it.copy(alpha = theme.disabledOpacity)
}
) {
ProvideTextStyle(value = theme.summaryTextStyle, content = summary)
}
}
}
}

@Composable
@Preview(showBackground = true)
private fun PreferencePreview() {
Expand Down

0 comments on commit f3a7e32

Please sign in to comment.