Skip to content

Commit

Permalink
[Feat] 스크롤 바 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
YiBeomSeok committed Apr 3, 2024
1 parent 894035c commit c57ef32
Showing 1 changed file with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package org.bmsk.lifemash.feature.topic

import android.content.res.Configuration
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.layout.Arrangement
Expand All @@ -19,6 +21,7 @@ import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FilterChip
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
Expand All @@ -34,10 +37,16 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
Expand Down Expand Up @@ -129,7 +138,8 @@ private fun NewsContent(
state = listState,
modifier = Modifier
.fillMaxSize()
.padding(horizontal = 16.dp),
.padding(horizontal = 16.dp)
.simpleVerticalScrollbar(listState),
verticalArrangement = Arrangement.spacedBy(16.dp),
) {
items(
Expand All @@ -141,6 +151,41 @@ private fun NewsContent(
}
}

fun Modifier.simpleVerticalScrollbar(
state: LazyListState,
width: Dp = 4.dp,
): Modifier = composed {
val targetAlpha = if (state.isScrollInProgress) 1f else 0f
val duration = if (state.isScrollInProgress) 150 else 500

val alpha by animateFloatAsState(
targetValue = targetAlpha,
animationSpec = tween(durationMillis = duration),
label = "",
)

drawWithContent {
drawContent()

val firstVisibleElementIndex =
state.layoutInfo.visibleItemsInfo.firstOrNull()?.index ?: return@drawWithContent
val needDrawScrollbar = state.isScrollInProgress || alpha > 0.0f

if (needDrawScrollbar) {
val elementHeight = this.size.height / state.layoutInfo.totalItemsCount
val scrollbarOffsetY = firstVisibleElementIndex * elementHeight
val scrollbarHeight = state.layoutInfo.visibleItemsInfo.size * elementHeight

drawOval(
color = Color.Cyan.copy(alpha = 0.5f),
topLeft = Offset(this.size.width - width.toPx(), scrollbarOffsetY),
size = Size(width.toPx(), scrollbarHeight),
alpha = alpha,
)
}
}
}

@OptIn(ExperimentalMaterial3Api::class, ExperimentalComposeUiApi::class)
@Composable
private fun SearchBar(
Expand Down

0 comments on commit c57ef32

Please sign in to comment.