From 4f3f7c7c663aa44d8ae662d4e5d74bb08fb0e08c Mon Sep 17 00:00:00 2001 From: FunkyMuse Date: Fri, 28 Jul 2023 15:19:58 +0200 Subject: [PATCH] refactor: remove unnecessary code from new built in functions in Compose --- .../compose/extensions/ComposeExtensions.kt | 7 +++-- .../compose/extensions/LifecycleExtensions.kt | 27 ------------------- .../calculator/compose/theme/AppTheme.kt | 6 ++--- 3 files changed, 6 insertions(+), 34 deletions(-) delete mode 100644 app/src/main/kotlin/com/simplemobiletools/calculator/compose/extensions/LifecycleExtensions.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/calculator/compose/extensions/ComposeExtensions.kt b/app/src/main/kotlin/com/simplemobiletools/calculator/compose/extensions/ComposeExtensions.kt index ed088c3..3271b59 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calculator/compose/extensions/ComposeExtensions.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calculator/compose/extensions/ComposeExtensions.kt @@ -7,6 +7,7 @@ import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.runtime.* import androidx.compose.ui.graphics.Color import androidx.lifecycle.Lifecycle +import androidx.lifecycle.compose.LifecycleEventEffect import com.google.accompanist.systemuicontroller.rememberSystemUiController fun Context.getActivity(): Activity { @@ -30,10 +31,8 @@ fun TransparentSystemBars() { fun onEventValue(event: Lifecycle.Event = Lifecycle.Event.ON_START, value: () -> T): T { val rememberLatestUpdateState by rememberUpdatedState(newValue = value) var rememberedValue by remember { mutableStateOf(value()) } - OnLifecycleEvent { lifecycleEvent -> - if (lifecycleEvent == event) { - rememberedValue = rememberLatestUpdateState() - } + LifecycleEventEffect(event = event) { + rememberedValue = rememberLatestUpdateState() } return rememberedValue } diff --git a/app/src/main/kotlin/com/simplemobiletools/calculator/compose/extensions/LifecycleExtensions.kt b/app/src/main/kotlin/com/simplemobiletools/calculator/compose/extensions/LifecycleExtensions.kt deleted file mode 100644 index c5711c9..0000000 --- a/app/src/main/kotlin/com/simplemobiletools/calculator/compose/extensions/LifecycleExtensions.kt +++ /dev/null @@ -1,27 +0,0 @@ -package com.simplemobiletools.calculator.compose.extensions - -import androidx.compose.runtime.Composable -import androidx.compose.runtime.DisposableEffect -import androidx.compose.runtime.getValue -import androidx.compose.runtime.rememberUpdatedState -import androidx.compose.ui.platform.LocalLifecycleOwner -import androidx.lifecycle.Lifecycle -import androidx.lifecycle.LifecycleEventObserver -import androidx.lifecycle.LifecycleOwner - -@Composable -fun OnLifecycleEvent( - lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current, - onEvent: (event: Lifecycle.Event) -> Unit -) { - val currentOnEvent by rememberUpdatedState(onEvent) - DisposableEffect(lifecycleOwner) { - val observer = LifecycleEventObserver { _, event -> - currentOnEvent(event) - } - lifecycleOwner.lifecycle.addObserver(observer) - onDispose { - lifecycleOwner.lifecycle.removeObserver(observer) - } - } -} diff --git a/app/src/main/kotlin/com/simplemobiletools/calculator/compose/theme/AppTheme.kt b/app/src/main/kotlin/com/simplemobiletools/calculator/compose/theme/AppTheme.kt index 9b167e7..f6a4128 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calculator/compose/theme/AppTheme.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calculator/compose/theme/AppTheme.kt @@ -7,7 +7,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalView import androidx.lifecycle.Lifecycle -import com.simplemobiletools.calculator.compose.extensions.OnLifecycleEvent +import androidx.lifecycle.compose.LifecycleEventEffect import com.simplemobiletools.calculator.compose.theme.model.Theme.Companion.systemDefaultMaterialYou @Composable @@ -27,8 +27,8 @@ fun AppThemeSurface( ) ) } - OnLifecycleEvent { event -> - if (event == Lifecycle.Event.ON_START && !view.isInEditMode) { + LifecycleEventEffect(event = Lifecycle.Event.ON_START) { + if (!view.isInEditMode) { currentTheme = getTheme(context = context, materialYouTheme = materialYouTheme) } }