Skip to content

Commit 3bb5cab

Browse files
committed
Minor Compose audit
Just removing and fixing some weird bits from when I was learning compose
1 parent d8ad856 commit 3bb5cab

10 files changed

Lines changed: 53 additions & 59 deletions

File tree

ui-common/src/main/kotlin/dev/clombardo/dnsnet/ui/common/Dialog.kt

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import androidx.compose.material3.AlertDialog
1212
import androidx.compose.material3.Text
1313
import androidx.compose.material3.TextButton
1414
import androidx.compose.runtime.Composable
15-
import androidx.compose.runtime.remember
1615
import androidx.compose.ui.Modifier
1716

1817
data class DialogButton(
@@ -31,35 +30,31 @@ fun BasicDialog(
3130
tertiaryButton: DialogButton? = null,
3231
onDismissRequest: () -> Unit,
3332
) {
34-
val primaryButtonState = remember { primaryButton }
35-
val secondaryButtonState = remember { secondaryButton }
36-
val tertiaryButtonState = remember { tertiaryButton }
37-
3833
AlertDialog(
3934
modifier = modifier,
4035
onDismissRequest = onDismissRequest,
4136
confirmButton = {
42-
if (tertiaryButtonState != null) {
37+
if (tertiaryButton != null) {
4338
TextButton(
44-
modifier = tertiaryButtonState.modifier,
45-
onClick = tertiaryButtonState.onClick,
39+
modifier = tertiaryButton.modifier,
40+
onClick = tertiaryButton.onClick,
4641
) {
47-
Text(text = tertiaryButtonState.text)
42+
Text(text = tertiaryButton.text)
4843
}
4944
}
50-
if (secondaryButtonState != null) {
45+
if (secondaryButton != null) {
5146
TextButton(
52-
modifier = secondaryButtonState.modifier,
53-
onClick = secondaryButtonState.onClick,
47+
modifier = secondaryButton.modifier,
48+
onClick = secondaryButton.onClick,
5449
) {
55-
Text(text = secondaryButtonState.text)
50+
Text(text = secondaryButton.text)
5651
}
5752
}
5853
TextButton(
59-
modifier = primaryButtonState.modifier,
60-
onClick = primaryButtonState.onClick,
54+
modifier = primaryButton.modifier,
55+
onClick = primaryButton.onClick,
6156
) {
62-
Text(text = primaryButtonState.text)
57+
Text(text = primaryButton.text)
6358
}
6459
},
6560
title = { Text(text = title) },

ui-common/src/main/kotlin/dev/clombardo/dnsnet/ui/common/PaddingUtil.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ import androidx.compose.foundation.layout.PaddingValues
1212
import androidx.compose.foundation.layout.calculateEndPadding
1313
import androidx.compose.foundation.layout.calculateStartPadding
1414
import androidx.compose.runtime.Composable
15+
import androidx.compose.runtime.Stable
1516
import androidx.compose.ui.platform.LocalLayoutDirection
1617
import androidx.compose.ui.unit.Dp
1718
import androidx.compose.ui.unit.dp
1819

20+
@Stable
1921
@Composable
2022
fun PaddingValues.add(
2123
start: Dp = Dp.Unspecified,
@@ -53,6 +55,7 @@ fun PaddingValues.add(
5355
)
5456
}
5557

58+
@Stable
5659
@Composable
5760
operator fun PaddingValues.plus(other: PaddingValues): PaddingValues {
5861
val layoutDirection = LocalLayoutDirection.current

ui-common/src/main/kotlin/dev/clombardo/dnsnet/ui/common/SaveableUtil.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package dev.clombardo.dnsnet.ui.common
1010

1111
import androidx.compose.runtime.Composable
12+
import androidx.compose.runtime.Stable
1213
import androidx.compose.runtime.saveable.listSaver
1314
import androidx.compose.runtime.saveable.rememberSaveable
1415
import androidx.compose.runtime.snapshots.SnapshotStateList
@@ -23,6 +24,7 @@ fun <T: Any> rememberMutableStateListOf(builderAction: MutableList<T>.() -> Unit
2324
}
2425
}
2526

27+
@Stable
2628
private fun <T : Any> snapshotStateListSaver() = listSaver<SnapshotStateList<T>, T>(
2729
save = { stateList -> stateList.toList() },
2830
restore = { it.toMutableStateList() },

ui-common/src/main/kotlin/dev/clombardo/dnsnet/ui/common/ScrollUpIndicator.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ fun BoxScope.ScrollUpIndicator(
7474
exitTransition: ExitTransition = ScrollUpIndicatorDefaults.ExitTransition,
7575
windowInsets: WindowInsets = ScrollUpIndicatorDefaults.windowInsets,
7676
alignment: Alignment = Alignment.BottomEnd,
77-
onClick: suspend CoroutineScope.() -> Unit,
77+
onClick: suspend () -> Unit,
7878
) {
7979
val scope = rememberCoroutineScope()
8080
val scrollUpButtonColor = MaterialTheme.colorScheme.tertiaryContainer
@@ -98,9 +98,7 @@ fun BoxScope.ScrollUpIndicator(
9898
.clickable(
9999
enabled = enabled,
100100
role = Role.Button,
101-
) {
102-
scope.launch(block = onClick)
103-
},
101+
) { scope.launch { onClick() } },
104102
contentAlignment = Alignment.Center,
105103
) {
106104
Icon(

ui-common/src/main/kotlin/dev/clombardo/dnsnet/ui/common/Settings.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ fun ExpandableOptionsItem(
716716
label = "iconRotation",
717717
)
718718
Icon(
719-
modifier = Modifier.rotate(iconRotation),
719+
modifier = Modifier.graphicsLayer { rotationZ = iconRotation },
720720
painter = rememberVectorPainter(Icons.Default.KeyboardArrowDown),
721721
contentDescription = if (expanded) {
722722
stringResource(R.string.collapse)

ui-common/src/main/kotlin/dev/clombardo/dnsnet/ui/common/SplitContentContainer.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ fun SplitContentRowContainer(
145145
targetValue = if (elevated) {
146146
color
147147
} else {
148-
color.copy(alpha = 0f)
148+
Color.Transparent
149149
}
150150
)
151151
val contentColor by animateColorAsState(
@@ -277,7 +277,7 @@ fun SplitContentColumnContainer(
277277
targetValue = if (elevated) {
278278
color
279279
} else {
280-
color.copy(alpha = 0f)
280+
Color.Transparent
281281
}
282282
)
283283
val contentColor by animateColorAsState(

ui-common/src/main/kotlin/dev/clombardo/dnsnet/ui/common/TriStateFab.kt

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi
3232
import androidx.compose.material3.MaterialTheme
3333
import androidx.compose.material3.ripple
3434
import androidx.compose.runtime.Composable
35+
import androidx.compose.runtime.NonRestartableComposable
3536
import androidx.compose.runtime.derivedStateOf
3637
import androidx.compose.runtime.getValue
3738
import androidx.compose.runtime.mutableStateOf
@@ -60,7 +61,26 @@ enum class FabState(internal val progress: Float) {
6061
object TriStateFab {
6162
val size = 128.dp
6263
val safeInsets: WindowInsets
63-
@Composable get() = WindowInsets.displayCutout.union(WindowInsets.systemBars)
64+
@NonRestartableComposable @Composable get() =
65+
WindowInsets.displayCutout.union(WindowInsets.systemBars)
66+
67+
private val inactiveShape = RoundedPolygon(
68+
numVertices = 3,
69+
rounding = CornerRounding(0.2f)
70+
)
71+
private val loadingShape = RoundedPolygon.star(
72+
numVerticesPerRadius = 12,
73+
radius = 2f,
74+
rounding = CornerRounding(0.2f)
75+
)
76+
private val activeShape = RoundedPolygon(
77+
numVertices = 4,
78+
radius = 0.9f,
79+
rounding = CornerRounding(0.2f)
80+
)
81+
82+
val inactiveToLoadingMorph = Morph(inactiveShape, loadingShape)
83+
val loadingToActiveMorph = Morph(loadingShape, activeShape)
6484
}
6585

6686
@OptIn(ExperimentalMaterial3ExpressiveApi::class)
@@ -136,32 +156,6 @@ fun TriStateFab(
136156
label = "contentColor",
137157
)
138158

139-
val inactiveShape = remember {
140-
RoundedPolygon(
141-
numVertices = 3,
142-
rounding = CornerRounding(0.2f)
143-
)
144-
}
145-
val loadingShape = remember {
146-
RoundedPolygon.star(
147-
numVerticesPerRadius = 12,
148-
radius = 2f,
149-
rounding = CornerRounding(0.2f)
150-
)
151-
}
152-
val activeShape = remember {
153-
RoundedPolygon(
154-
numVertices = 4,
155-
radius = 0.9f,
156-
rounding = CornerRounding(0.2f)
157-
)
158-
}
159-
val inactiveToLoadingMorph = remember {
160-
Morph(inactiveShape, loadingShape)
161-
}
162-
val loadingToActiveMorph = remember {
163-
Morph(loadingShape, activeShape)
164-
}
165159
val progress by animateFloatAsState(
166160
targetValue = state.progress,
167161
animationSpec = MaterialTheme.motionScheme.defaultSpatialSpec(),
@@ -191,7 +185,7 @@ fun TriStateFab(
191185
.drawWithCache {
192186
val shape = if (progress < 1f) {
193187
RotatingMorphShape(
194-
morph = inactiveToLoadingMorph,
188+
morph = TriStateFab.inactiveToLoadingMorph,
195189
percentage = progress,
196190
rotation = if (state == FabState.Inactive) {
197191
animatedRotation
@@ -201,7 +195,7 @@ fun TriStateFab(
201195
)
202196
} else if (progress > 1f) {
203197
RotatingMorphShape(
204-
morph = loadingToActiveMorph,
198+
morph = TriStateFab.loadingToActiveMorph,
205199
percentage = progress - 1f,
206200
rotation = if (state == FabState.Active) {
207201
animatedRotation
@@ -211,7 +205,7 @@ fun TriStateFab(
211205
)
212206
} else {
213207
RotatingMorphShape(
214-
morph = inactiveToLoadingMorph,
208+
morph = TriStateFab.inactiveToLoadingMorph,
215209
percentage = 1f,
216210
rotation = infiniteAnimatedRotation,
217211
)

ui-common/src/main/kotlin/dev/clombardo/dnsnet/ui/common/WindowUtil.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ package dev.clombardo.dnsnet.ui.common
1010

1111
import androidx.compose.material3.adaptive.currentWindowAdaptiveInfo
1212
import androidx.compose.runtime.Composable
13+
import androidx.compose.runtime.Stable
1314
import androidx.window.core.layout.WindowSizeClass
1415

16+
@Stable
1517
@Composable
1618
fun isSmallScreen(): Boolean {
1719
return !currentWindowAdaptiveInfo().windowSizeClass

ui-common/src/main/kotlin/dev/clombardo/dnsnet/ui/common/navigation/NavigationScaffold.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ package dev.clombardo.dnsnet.ui.common.navigation
1010

1111
import androidx.compose.animation.EnterTransition
1212
import androidx.compose.animation.ExitTransition
13-
import androidx.compose.animation.core.tween
1413
import androidx.compose.animation.fadeIn
1514
import androidx.compose.animation.fadeOut
1615
import androidx.compose.animation.scaleIn
@@ -38,16 +37,15 @@ import androidx.compose.material3.MaterialTheme
3837
import androidx.compose.material3.Text
3938
import androidx.compose.runtime.Composable
4039
import androidx.compose.runtime.CompositionLocalProvider
40+
import androidx.compose.runtime.NonRestartableComposable
4141
import androidx.compose.runtime.getValue
4242
import androidx.compose.runtime.mutableIntStateOf
4343
import androidx.compose.runtime.remember
4444
import androidx.compose.runtime.setValue
4545
import androidx.compose.ui.Alignment
4646
import androidx.compose.ui.Modifier
47-
import androidx.compose.ui.res.stringResource
4847
import androidx.compose.ui.tooling.preview.Preview
4948
import dev.clombardo.dnsnet.ui.common.plus
50-
import dev.clombardo.dnsnet.ui.common.theme.Animation
5149
import dev.clombardo.dnsnet.ui.common.theme.DnsNetTheme
5250

5351
enum class LayoutType {
@@ -57,7 +55,8 @@ enum class LayoutType {
5755

5856
object NavigationScaffoldDefaults {
5957
val windowInsets: WindowInsets
60-
@Composable get() = WindowInsets.systemBars.union(WindowInsets.displayCutout)
58+
@NonRestartableComposable @Composable get() =
59+
WindowInsets.systemBars.union(WindowInsets.displayCutout)
6160
}
6261

6362
@OptIn(ExperimentalMaterial3ExpressiveApi::class)

ui-common/src/main/kotlin/dev/clombardo/dnsnet/ui/common/theme/Animation.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import androidx.compose.foundation.lazy.LazyListScope
1919
import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi
2020
import androidx.compose.material3.MaterialTheme
2121
import androidx.compose.runtime.Composable
22+
import androidx.compose.runtime.ReadOnlyComposable
2223
import androidx.compose.ui.Alignment
2324
import androidx.compose.ui.Modifier
2425

@@ -28,7 +29,7 @@ object Animation {
2829
val EmphasizedAccelerateEasing by lazy { CubicBezierEasing(0.3f, 0f, 0.8f, 0.15f) }
2930

3031
val ShowSpinnerHorizontal: EnterTransition
31-
@Composable get() {
32+
@ReadOnlyComposable @Composable get() {
3233
return fadeIn(
3334
animationSpec = MaterialTheme.motionScheme.defaultSpatialSpec(),
3435
) + expandHorizontally(
@@ -39,7 +40,7 @@ object Animation {
3940
}
4041

4142
val HideSpinnerHorizontal: ExitTransition
42-
@Composable get() {
43+
@ReadOnlyComposable @Composable get() {
4344
return fadeOut(
4445
animationSpec = MaterialTheme.motionScheme.defaultSpatialSpec(),
4546
) + shrinkHorizontally(

0 commit comments

Comments
 (0)