Skip to content

Instantly share code, notes, and snippets.

View Skyyo's full-sized avatar
🇺🇦

Denis Rudenko Skyyo

🇺🇦
View GitHub Profile
@Skyyo
Skyyo / collectAsImmutableWithLifecycle.kt
Last active September 19, 2023 10:41
extension to convert collections & 3d party objects into immutable types on UI layer until Compose has a better solution
View collectAsImmutableWithLifecycle.kt
@file:OptIn(ExperimentalLifecycleComposeApi::class)
package com.skyyo.template.utils.extensions
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.State
import androidx.compose.runtime.produceState
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.lifecycle.Lifecycle
@Skyyo
Skyyo / perf_snippet_21.kt
Created October 2, 2022 18:06
perf_snippet_21
View perf_snippet_21.kt
@Composable
private fun HorizontallyMovingButton(scrollProvider: () -> Float) {
Button(modifier = Modifier.graphicsLayer { translationX = scrollProvider() })
}
@Skyyo
Skyyo / perf_snippet_20.kt
Created October 2, 2022 18:00
perf_snippet_20
View perf_snippet_20.kt
@Composable
fun ModifierLambdasScreen() {
val scrollState = rememberScrollState()
BoxWrapper {
ScrollingArea(scrollState)
HorizontallyMovingButton(scrollState.value * 1.5f)
}
}
@Composable
@Skyyo
Skyyo / perf_snippet_19.kt
Last active October 4, 2022 08:57
perf_snippet_19
View perf_snippet_19.kt
@Composable
fun StateReadsLowerStateReadsOptimizedScreen(viewModel: TypicalViewModel = hiltViewModel()) {
ColumnWrapper {
InputsWrapper(
nameStateFlow = viewModel.name,
creditCardNumberStateFlow = viewModel.creditCardNumber,
onNameEntered = viewModel::onNameEntered,
onCreditCardNumberEntered = viewModel::onCardNumberEntered
)
}
@Skyyo
Skyyo / perf_snippet_18.kt
Created October 2, 2022 16:46
perf_snippet_18
View perf_snippet_18.kt
@Composable
@NonRestartableComposable
private fun InputsWrapperAnnotation(
name: String,
creditCardNumber: String,
onNameEntered: (value: String) -> Unit,
onCreditCardNumberEntered: (value: String) -> Unit,
) {
var innerCount by remember { mutableStateOf(0) }
@Skyyo
Skyyo / perf_snippet_17.kt
Created October 2, 2022 16:11
perf_snippet_17
View perf_snippet_17.kt
@Composable
private fun InputsWrapperOptimized(
provideName: () -> String,
provideCreditCardNumber: () -> String,
onNameEntered: (value: String) -> Unit,
onCreditCardNumberEntered: (value: String) -> Unit,
) {
var count by remember { mutableStateOf(0) }
NameTextFieldOptimized(provideName, onNameEntered)
@Skyyo
Skyyo / perf_snippet_16.kt
Created October 2, 2022 15:47
perf_snippet_16
View perf_snippet_16.kt
@Composable
private fun InputsWrapper(
name: String,
creditCardNumber: String,
onNameEntered: (value: String) -> Unit,
onCreditCardNumberEntered: (value: String) -> Unit,
) {
var count by remember {
mutableStateOf(0)
}
@Skyyo
Skyyo / perf_snippet_15.kt
Last active October 4, 2022 08:55
perf_snippet_15
View perf_snippet_15.kt
@Composable
fun StateReadsInputLambdasScreen(viewModel: TypicalViewModel = hiltViewModel()) {
val name by viewModel.name.collectAsStateWithLifecycle()
val creditCardNumber by viewModel.creditCardNumber.collectAsStateWithLifecycle()
Column {
InputsWrapper(
name = name,
creditCardNumber = creditCardNumber,
onNameEntered = viewModel::onNameEntered,
@Skyyo
Skyyo / perf_snippet_14.kt
Created October 2, 2022 15:25
perf_snippet_14
View perf_snippet_14.kt
@Composable
fun StateReadsInlinedComposablesSolution2Screen() {
var count by remember { mutableStateOf(0) }
Column {
TextWrapper(provideCount = { count })
Button(onClick = { count++ }) {
Text(text = "count++")
}
}
}
@Skyyo
Skyyo / perf_snippet_13.kt
Created October 2, 2022 14:45
perf_snippet_13
View perf_snippet_13.kt
@Composable
fun StateReadsInlinedComposablesScreen() {
var count by remember { mutableStateOf(0) }
WrappedColumn {
Text(text = "count: $count")
Button(onClick = { count++ }) {
Text(text = "increase count")
}
}