Skip to content

Instantly share code, notes, and snippets.

View belinwu's full-sized avatar

吴上阿吉 belinwu

View GitHub Profile
@belinwu
belinwu / FlipAnimation.kt
Created April 28, 2024 04:32 — forked from vishal2376/FlipAnimation.kt
Card Flip Animation using Jetpack Compose
package com.vishal2376.animations.ui.theme
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.EaseInOut
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import io.reactivex.subjects.PublishSubject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch
import kotlinx.coroutines.newSingleThreadContext
import kotlinx.coroutines.suspendCancellableCoroutine
@belinwu
belinwu / SettingsSection.kt
Created December 15, 2023 12:48 — forked from bmc08gt/SettingsSection.kt
SettingsSectionScope
interface SettingsSectionScope {
fun item(
icon: ImageVector? = null,
title: String,
subtitle: String? = null,
endSlot: @Composable () -> Unit = { },
onClick: (() -> Unit)? = null,
)
@Composable
@belinwu
belinwu / DecomposeUtils.kt
Created November 28, 2023 10:00 — forked from aartikov/DecomposeUtils.kt
Creates CoroutineScope for Decompose component
fun ComponentContext.componentCoroutineScope(): CoroutineScope {
val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
if (lifecycle.state != Lifecycle.State.DESTROYED) {
lifecycle.doOnDestroy {
scope.cancel()
}
} else {
scope.cancel()
}
@belinwu
belinwu / StateMachineDebounceExample.kt
Created November 21, 2023 14:12 — forked from linean/StateMachineDebounceExample.kt
Simple example how events can be debounced on StateMachine side
// Keep in mind this is just simplified example
class Example {
private val scope = CoroutineScope(Job())
private val debouncingChannel = Channel<UserEvent>(
capacity = RENDEZVOUS,
onBufferOverflow = BufferOverflow.DROP_OLDEST,
)
@belinwu
belinwu / ViewModels.kt
Last active October 26, 2023 07:10 — forked from OKatrych/ViewModels.kt
ProvidesCompositionViewModelStoreOwner: Composable scoped viewmodel
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.RememberObserver
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.ViewModelStoreOwner
import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner
import timber.log.Timber
@belinwu
belinwu / VMScope.kt
Created October 25, 2023 05:19 — forked from OKatrych/VMScope.kt
Scope ViewModelStoreOwner to Composable lifecycle
@Composable
internal fun ProvidesViewModelStoreOwner(
ownerKey: String = rememberSaveable { UUID.randomUUID().toString() },
ownerHolder: CompositionScopedVMStoreOwnerHolder = CompositionScopedVMStoreOwnerHolder(),
content: @Composable () -> Unit,
) {
val context = LocalContext.current
remember {
object : RememberObserver {
@belinwu
belinwu / ProvideViewModels.kt
Created October 25, 2023 05:08 — forked from manuelvicnt/ProvideViewModels.kt
Scope ViewModels to Composables
// PLEASE, READ
//
// This is a way to scope ViewModels to the Composition.
// However, this doesn't survive configuration changes or procress death on its own.
// You can handle all config changes in compose by making the activity handle those in the Manifest file
// e.g. android:configChanges="colorMode|density|fontScale|keyboard|keyboardHidden|layoutDirection|locale|mcc|mnc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|touchscreen|uiMode">
//
// This is just an exploration to see what's possible in Compose. We don't encourage developers to copy-paste
// this code if they don't fully understand the implications of it and if this actually solves the use case to solve.
@belinwu
belinwu / ControlledExplosion.kt
Created September 28, 2023 06:01 — forked from omkar-tenkale/ControlledExplosion.kt
Compose Explosion Animation Snippets
@Composable
fun ControlledExplosion() {
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
var progress by remember { mutableStateOf(0f) }
Explosion(progress)