Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider adding Compose extensions #1

Open
DanteAndroid opened this issue Oct 9, 2023 · 1 comment
Open

Consider adding Compose extensions #1

DanteAndroid opened this issue Oct 9, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@DanteAndroid
Copy link

Here is my snippet:

@Composable
fun Int.dpInPixel() = with(LocalDensity.current) { this@dpInPixel.dp.toPx() }.roundToInt()

@Composable
fun Float.dpInPixel() = with(LocalDensity.current) { this@dpInPixel.dp.toPx() }.roundToInt()

@Composable
fun Int.pxInDp() = with(LocalDensity.current) { this@pxInDp.toDp() }

fun String.asColor(): Color {
    return Color(this.toColorInt())
}

inline fun Modifier.debounceClickable(
    debounceInterval: Long = 400,
    crossinline onClick: () -> Unit,
): Modifier {
    var lastClickTime = 0L
    return clickable {
        val currentTime = System.currentTimeMillis()
        if ((currentTime - lastClickTime) < debounceInterval) return@clickable
        lastClickTime = currentTime
        onClick()
    }
}

@Composable
fun RegisterOnStart(
    key: Any? = null,
    key2: Any? = null,
    function: () -> Unit
) {
    val lifecycleOwner = LocalLifecycleOwner.current
    DisposableEffect(key, key2) {
        val observer = LifecycleEventObserver { _, event ->
            if (event == Lifecycle.Event.ON_START) {
                function.invoke()
            }
        }
        lifecycleOwner.lifecycle.addObserver(observer)
        onDispose {
            lifecycleOwner.lifecycle.removeObserver(observer)
        }
    }
}

fun Fragment.composeView(
    compositionStrategy: ViewCompositionStrategy = ViewCompositionStrategy.DisposeOnDetachedFromWindow,
    context: Context? = getContext(),
    content: @Composable () -> Unit
): ComposeView? {
    context ?: return null
    val view = ComposeView(context)
    view.setViewCompositionStrategy(compositionStrategy)
    view.setContent(content)
    return view
}

fun Activity.composeView(
    compositionStrategy: ViewCompositionStrategy = ViewCompositionStrategy.DisposeOnDetachedFromWindow,
    context: Context? = this,
    content: @Composable () -> Unit
): ComposeView? {
    context ?: return null
    val view = ComposeView(context)
    view.setViewCompositionStrategy(compositionStrategy)
    view.setContent(content)
    return view
}

@SageDroid
Copy link
Member

Thank you so much for your suggestions! I really appreciate it! I already have a few Compose extensions in my projects and a Compose version is definitely something I'm thinking about. Currently my full focus is on the task of releasing the first stable version. After that I will probably work on multiplatform support. This would then also contribute to a possible later compose version. But this is still quite some time in the future. For now I'll work on the stable version. I will keep you informed!

@SageDroid SageDroid reopened this Oct 12, 2023
@SageDroid SageDroid added the enhancement New feature or request label Oct 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants