Skip to content

Commit 9d07a19

Browse files
authored
Merge pull request #586 from alexvanyo/av/avoid-mutating-parameter
[Jetsnack] Avoid mutating colors parameter
2 parents fd7a5bb + 399d8cc commit 9d07a19

File tree

1 file changed

+36
-1
lines changed
  • Jetsnack/app/src/main/java/com/example/jetsnack/ui/theme

1 file changed

+36
-1
lines changed

Jetsnack/app/src/main/java/com/example/jetsnack/ui/theme/Theme.kt

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,49 @@ class JetsnackColors(
234234
notificationBadge = other.notificationBadge
235235
isDark = other.isDark
236236
}
237+
238+
fun copy(): JetsnackColors = JetsnackColors(
239+
gradient6_1 = gradient6_1,
240+
gradient6_2 = gradient6_2,
241+
gradient3_1 = gradient3_1,
242+
gradient3_2 = gradient3_2,
243+
gradient2_1 = gradient2_1,
244+
gradient2_2 = gradient2_2,
245+
gradient2_3 = gradient2_3,
246+
brand = brand,
247+
brandSecondary = brandSecondary,
248+
uiBackground = uiBackground,
249+
uiBorder = uiBorder,
250+
uiFloated = uiFloated,
251+
interactivePrimary = interactivePrimary,
252+
interactiveSecondary = interactiveSecondary,
253+
interactiveMask = interactiveMask,
254+
textPrimary = textPrimary,
255+
textSecondary = textSecondary,
256+
textHelp = textHelp,
257+
textInteractive = textInteractive,
258+
textLink = textLink,
259+
tornado1 = tornado1,
260+
iconPrimary = iconPrimary,
261+
iconSecondary = iconSecondary,
262+
iconInteractive = iconInteractive,
263+
iconInteractiveInactive = iconInteractiveInactive,
264+
error = error,
265+
notificationBadge = notificationBadge,
266+
isDark = isDark,
267+
)
237268
}
238269

239270
@Composable
240271
fun ProvideJetsnackColors(
241272
colors: JetsnackColors,
242273
content: @Composable () -> Unit
243274
) {
244-
val colorPalette = remember { colors }
275+
val colorPalette = remember {
276+
// Explicitly creating a new object here so we don't mutate the initial [colors]
277+
// provided, and overwrite the values set in it.
278+
colors.copy()
279+
}
245280
colorPalette.update(colors)
246281
CompositionLocalProvider(LocalJetsnackColors provides colorPalette, content = content)
247282
}

0 commit comments

Comments
 (0)