Last active
April 16, 2020 14:08
-
-
Save semoro/ea7c07cf3bcbd3bc4cbc4729b82b4140 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.xcodersteam.composesandbox | |
import android.os.Bundle | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.compose.* | |
import androidx.compose.frames.ModelList | |
import androidx.compose.frames.modelListOf | |
import androidx.ui.animation.DpToVectorConverter | |
import androidx.ui.animation.animatedValue | |
import androidx.ui.core.DensityAmbient | |
import androidx.ui.core.Modifier | |
import androidx.ui.core.onPositioned | |
import androidx.ui.core.setContent | |
import androidx.ui.foundation.Box | |
import androidx.ui.foundation.Text | |
import androidx.ui.layout.Column | |
import androidx.ui.layout.Spacer | |
import androidx.ui.layout.fillMaxWidth | |
import androidx.ui.layout.preferredHeight | |
import androidx.ui.material.Button | |
import androidx.ui.material.MaterialTheme | |
import androidx.ui.unit.dp | |
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
MaterialTheme { | |
TestList(modelListOf("A", "B", "C", "D")) | |
} | |
} | |
} | |
} | |
@Composable | |
fun TestList(list: ModelList<String>) { | |
Column { | |
for (item in list) { | |
println("Recomposed outer outer ($item)") | |
RemovalArea(item) { | |
Column { | |
println("Recomposed inner ($item)") | |
Spacer(modifier = Modifier.preferredHeight(5.dp)) | |
Button( | |
onClick = { collapseArea { list.remove(item) } }, | |
modifier = Modifier.fillMaxWidth() | |
) { | |
Text(item) | |
} | |
} | |
} | |
} | |
} | |
} | |
interface RemovalAreaScope { | |
fun collapseArea(onDone: () -> Unit) | |
} | |
@Composable | |
fun RemovalArea(id: Any?, child: @Composable() RemovalAreaScope.() -> Unit) { | |
key(id) { | |
var startedRemoval by state { false } | |
val height = animatedValue(initVal = 0.dp, converter = DpToVectorConverter) | |
val scope = | |
object : RemovalAreaScope { | |
override fun collapseArea(onDone: () -> Unit) { | |
startedRemoval = true | |
height.animateTo(0.dp, | |
onEnd = { _, _ -> | |
onDone() | |
} | |
) | |
} | |
} | |
with(DensityAmbient.current) { | |
Box(modifier = | |
when { | |
!startedRemoval -> Modifier.onPositioned { height.snapTo(it.size.height.toDp()) } | |
else -> Modifier.preferredHeight(height.value) | |
} | |
) { | |
if (!startedRemoval) { | |
scope.child() | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment