Skip to content

Instantly share code, notes, and snippets.

View Levi-Moreira's full-sized avatar
🏠
Working from home

Levi Levi-Moreira

🏠
Working from home
View GitHub Profile
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
val appBarMaxHeightPx = with(LocalDensity.current) { AppBarHeight.roundToPx() }
val connection = remember(appBarMaxHeightPx) {
CollapsingAppBarNestedScrollConnection(appBarMaxHeightPx)
}
val density = LocalDensity.current
val spaceHeight by remember(density) {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
val appBarMaxHeightPx = with(LocalDensity.current) { AppBarHeight.roundToPx() }
val connection = remember(appBarMaxHeightPx) {
CollapsingAppBarNestedScrollConnection(appBarMaxHeightPx)
}
Box(Modifier.nestedScroll(connection)) {
private class CollapsingAppBarNestedScrollConnection(
val appBarMaxHeight: Int
) : NestedScrollConnection {
var appBarOffset: Int by mutableIntStateOf(0)
private set
override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
val delta = available.y.toInt()
val newOffset = appBarOffset + delta
override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
val delta = available.y.toInt()
val newOffset = appBarOffset + delta
val previousOffset = appBarOffset
appBarOffset = newOffset.coerceIn(-appBarMaxHeight, 0)
val consumed = appBarOffset - previousOffset
return Offset(0f, consumed.toFloat())
}
val appBarOffset by remember { mutableIntStateOf(0) }
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
val connection = remember {
object : NestedScrollConnection {
override fun onPreScroll(
available: Offset,
@Levi-Moreira
Levi-Moreira / ComposeNestedScrollSampleWithAppBarOffset.kt
Last active February 12, 2024 08:09
Modifier Initial Code to include Offsetting of AppBar
val appBarOffset by remember { mutableIntStateOf(0) }
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Box {
LazyColumn(contentPadding = PaddingValues(top = AppBarHeight)) {
items(Contents) {
ListItem(item = it)
@Levi-Moreira
Levi-Moreira / ComposeNestedScrollSampleInitialCode.kt
Last active February 12, 2024 08:08
Initial Code Representing a Screen Without Nested Scrolling
val AppBarHeight = 56.dp
val Purple40 = Color(0xFF6650a4)
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Box {
LazyColumn(contentPadding = PaddingValues(top = AppBarHeight)) {
items(Contents) {
<application
android:name="com.levimoreira.cinemabuff.infrastructure.application.CinemaBuffApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config">
...
</application>
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<debug-overrides>
<trust-anchors>
<certificates src="user" />
</trust-anchors>
</debug-overrides>
</network-security-config>
@RunWith(AndroidJUnit4::class)
class SampleTest {
private lateinit var personDao: PersonDao
private lateinit var db: AppDatabase
@Before
fun createDb() {
db = Room.inMemoryDatabaseBuilder(
InstrumentationRegistry.getContext(), AppDatabase::class.java).build()