refactor: remove unnecessary code from new built in functions in Compose

This commit is contained in:
FunkyMuse 2023-07-28 15:19:58 +02:00
parent 95fd5727d0
commit 4f3f7c7c66
3 changed files with 6 additions and 34 deletions

View File

@ -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 <T : Any> 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
}

View File

@ -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)
}
}
}

View File

@ -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)
}
}