Wrap icon and badge parameters of ShapedLauncherIcon in lambda

This commit is contained in:
MM20 2022-09-02 19:10:55 +02:00
parent 9134085088
commit 6e411e9555
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
9 changed files with 54 additions and 39 deletions

View File

@ -54,27 +54,29 @@ import kotlin.math.roundToInt
fun ShapedLauncherIcon( fun ShapedLauncherIcon(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
size: Dp, size: Dp,
icon: LauncherIcon? = null, icon: () -> LauncherIcon? = { null },
badge: Badge? = null, badge: () -> Badge? = { null },
onClick: (() -> Unit)? = null, onClick: (() -> Unit)? = null,
onLongClick: (() -> Unit)? = null, onLongClick: (() -> Unit)? = null,
shape: Shape = LocalIconShape.current shape: Shape = LocalIconShape.current
) { ) {
var currentIcon by remember(icon) { val _icon = icon()
var currentIcon by remember(_icon) {
mutableStateOf( mutableStateOf(
when (icon) { when (_icon) {
is DynamicLauncherIcon -> null is DynamicLauncherIcon -> null
is StaticLauncherIcon -> icon is StaticLauncherIcon -> _icon
else -> null else -> null
} }
) )
} }
if (icon is DynamicLauncherIcon) { if (_icon is DynamicLauncherIcon) {
val time = LocalTime.current val time = LocalTime.current
LaunchedEffect(time) { LaunchedEffect(time) {
currentIcon = icon.getIcon(time) currentIcon = _icon.getIcon(time)
} }
} }
@ -113,7 +115,8 @@ fun ShapedLauncherIcon(
) )
} }
} }
if (badge != null) { val _badge = badge()
if (_badge != null) {
Surface( Surface(
shadowElevation = 1.dp, shadowElevation = 1.dp,
tonalElevation = 1.dp, tonalElevation = 1.dp,
@ -134,7 +137,7 @@ fun ShapedLauncherIcon(
contentAlignment = Alignment.Center contentAlignment = Alignment.Center
) { ) {
badge.progress?.let { _badge.progress?.let {
val progress by animateFloatAsState(it) val progress by animateFloatAsState(it)
CircularProgressIndicator( CircularProgressIndicator(
modifier = Modifier.fillMaxSize(), modifier = Modifier.fillMaxSize(),
@ -143,10 +146,10 @@ fun ShapedLauncherIcon(
color = MaterialTheme.colorScheme.secondaryContainer color = MaterialTheme.colorScheme.secondaryContainer
) )
} }
val badgeIconRes = badge.iconRes val badgeIconRes = _badge.iconRes
val badgeIcon = badge.icon val badgeIcon = _badge.icon
val number = badge.number val number = _badge.number
if (badgeIconRes != null) { if (badgeIconRes != null) {
Image( Image(
modifier = Modifier modifier = Modifier
@ -188,6 +191,13 @@ fun ShapedLauncherIcon(
} }
} }
@Composable
private fun Badge(
badge: () -> Badge?
) {
}
@Composable @Composable
private fun IconLayer( private fun IconLayer(
layer: LauncherIconLayer, layer: LauncherIconLayer,

View File

@ -169,8 +169,8 @@ fun AppItem(
size = 84.dp, size = 84.dp,
modifier = Modifier modifier = Modifier
.padding(16.dp), .padding(16.dp),
badge = badge, badge = { badge },
icon = icon, icon = { icon },
) )
} }

View File

@ -87,10 +87,12 @@ fun CustomizeSearchableSheet(
ShapedLauncherIcon( ShapedLauncherIcon(
size = iconSize, size = iconSize,
icon = icon, icon = { icon },
badge = Badge( badge = {
Badge(
icon = badgeDrawable icon = badgeDrawable
), )
},
onClick = { onClick = {
viewModel.openIconPicker() viewModel.openIconPicker()
} }
@ -239,7 +241,7 @@ fun IconPreview(
) { ) {
ShapedLauncherIcon( ShapedLauncherIcon(
size = iconSize, size = iconSize,
icon = item?.preview, icon = { item?.preview },
onClick = onClick onClick = onClick
) )
} }

View File

@ -82,8 +82,8 @@ fun GridItem(modifier: Modifier = Modifier, item: Searchable, showLabels: Boolea
bounds = it.boundsInWindow() bounds = it.boundsInWindow()
}, },
size = LocalGridIconSize.current, size = LocalGridIconSize.current,
badge = badge, badge = { badge },
icon = icon, icon = { icon },
onClick = { onClick = {
if (!launchOnPress || !viewModel.launch(context, bounds)) { if (!launchOnPress || !viewModel.launch(context, bounds)) {
showPopup = true showPopup = true

View File

@ -70,7 +70,7 @@ fun ContactItem(
size = 48.dp, size = 48.dp,
modifier = Modifier modifier = Modifier
.padding(start = padding, top = padding, bottom = padding), .padding(start = padding, top = padding, bottom = padding),
icon = icon, icon = { icon },
) )
Column( Column(
modifier = Modifier.padding(horizontal = 16.dp) modifier = Modifier.padding(horizontal = 16.dp)

View File

@ -132,8 +132,8 @@ fun FileItem(
size = 48.dp, size = 48.dp,
modifier = Modifier modifier = Modifier
.padding(end = padding, top = padding, bottom = padding), .padding(end = padding, top = padding, bottom = padding),
icon = icon, icon = { icon },
badge = badge badge = { badge }
) )
} }

View File

@ -93,8 +93,8 @@ fun AppShortcutItem(
size = size, size = size,
modifier = Modifier modifier = Modifier
.padding(padding), .padding(padding),
badge = badge, badge = { badge },
icon = icon, icon = { icon },
) )
} }

View File

@ -408,22 +408,25 @@ fun IconShapePreference(
.padding(8.dp), .padding(8.dp),
horizontalAlignment = Alignment.CenterHorizontally horizontalAlignment = Alignment.CenterHorizontally
) { ) {
val context = LocalContext.current
ShapedLauncherIcon( ShapedLauncherIcon(
size = 48.dp, size = 48.dp,
icon = StaticLauncherIcon( icon = {
StaticLauncherIcon(
foregroundLayer = StaticIconLayer( foregroundLayer = StaticIconLayer(
icon = ContextCompat.getDrawable( icon = ContextCompat.getDrawable(
LocalContext.current, context,
R.mipmap.ic_launcher_foreground R.mipmap.ic_launcher_foreground
)!!, )!!,
scale = 1.5f, scale = 1.5f,
), ),
backgroundLayer = StaticIconLayer( backgroundLayer = StaticIconLayer(
icon = ColorDrawable( icon = ColorDrawable(
LocalContext.current.getColor(R.color.ic_launcher_background) context.getColor(R.color.ic_launcher_background)
) )
) )
), )
},
onClick = { onClick = {
onValueChanged(it) onValueChanged(it)
showDialog = false showDialog = false

View File

@ -186,7 +186,7 @@ fun HiddenItem(
) { ) {
ShapedLauncherIcon( ShapedLauncherIcon(
size = 32.dp, size = 32.dp,
icon = icon, icon = { icon },
modifier = Modifier.padding(end = 16.dp) modifier = Modifier.padding(end = 16.dp)
) )
Text( Text(