Skip to content

Commit

Permalink
refactor: Removed unused UI components and upgraded libraries
Browse files Browse the repository at this point in the history
This commit removes unused UI components, such as BottomSheets, StaticBottomSheet, and CardListItem. Additionally, it introduces a new DraggableBottomSheetState and DraggableBottomSheet composables to provide draggable bottom sheet functionality.

The commit also upgrades several libraries, including AGP to 8.7.3 and Gradle to 8.11.1, improving project maintenance and performance.

Finally, code cleanup and enhancements are implemented in various UI components, such as FadingEdge, OutlinedButtons, MarqueeText, FilledButtons, UtilityCard, VariousChips, Scroll, HomePage, SelectableSurface, and AnimatedCounter, promoting code clarity and consistency.
  • Loading branch information
BobbyESP committed Dec 6, 2024
1 parent f8a6584 commit bf3abbd
Show file tree
Hide file tree
Showing 26 changed files with 349 additions and 633 deletions.
4 changes: 4 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ private fun DropdownMenuContent(
val availableLayoutType = LayoutType.entries.toImmutableList()

Column(
modifier = Modifier.padding(horizontal = 8.dp),
modifier = Modifier,
verticalArrangement = Arrangement.spacedBy(8.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ package com.bobbyesp.ui.components.bottomsheet.draggable
import androidx.activity.compose.BackHandler
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.AnimationSpec
import androidx.compose.animation.core.AnimationVector1D
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.SpringSpec
import androidx.compose.animation.core.VectorConverter
import androidx.compose.animation.core.spring
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.DraggableState
Expand All @@ -23,8 +19,6 @@ import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -33,20 +27,15 @@ import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.input.pointer.util.VelocityTracker
import androidx.compose.ui.input.pointer.util.addPointerInputChange
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.Velocity
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

/**
Expand Down Expand Up @@ -132,172 +121,6 @@ fun DraggableBottomSheet(
}
}

@Stable
class DraggableBottomSheetState(
draggableState: DraggableState,
private val coroutineScope: CoroutineScope,
private val animatable: Animatable<Dp, AnimationVector1D>,
private val onAnchorChanged: (DraggableBottomSheetAnchor) -> Unit,
val collapsedBound: Dp,
) : DraggableState by draggableState {
val dismissedBound: Dp
get() = animatable.lowerBound!!

val expandedBound: Dp
get() = animatable.upperBound!!

val value by animatable.asState()

val isDismissed by derivedStateOf {
value == animatable.lowerBound!!
}

val isCollapsed by derivedStateOf {
value == collapsedBound
}

val isExpanded by derivedStateOf {
value == animatable.upperBound
}

val progress by derivedStateOf {
1f - (animatable.upperBound!! - animatable.value) / (animatable.upperBound!! - collapsedBound)
}

fun collapse(animationSpec: AnimationSpec<Dp>) {
onAnchorChanged(DraggableBottomSheetAnchor.COLLAPSED)
coroutineScope.launch {
animatable.animateTo(collapsedBound, animationSpec)
}
}

fun expand(animationSpec: AnimationSpec<Dp>) {
onAnchorChanged(DraggableBottomSheetAnchor.EXPANDED)
coroutineScope.launch {
animatable.animateTo(animatable.upperBound!!, animationSpec)
}
}

private fun collapse() {
collapse(SpringSpec())
}

private fun expand() {
expand(SpringSpec())
}

fun collapseSoft() {
collapse(spring(stiffness = Spring.StiffnessMediumLow))
}

fun expandSoft() {
expand(spring(stiffness = Spring.StiffnessMediumLow))
}

fun dismiss() {
onAnchorChanged(DraggableBottomSheetAnchor.DISMISSED)
coroutineScope.launch {
animatable.animateTo(animatable.lowerBound!!)
}
}

fun snapTo(value: Dp) {
coroutineScope.launch {
animatable.snapTo(value)
}
}

fun performFling(velocity: Float, onDismiss: (() -> Unit)?) {
if (velocity > 250) {
expand()
} else if (velocity < -250) {
if (value < collapsedBound && onDismiss != null) {
dismiss()
onDismiss.invoke()
} else {
collapse()
}
} else {
val l0 = dismissedBound
val l1 = (collapsedBound - dismissedBound) / 2
val l2 = (expandedBound - collapsedBound) / 2
val l3 = expandedBound

when (value) {
in l0..l1 -> {
if (onDismiss != null) {
dismiss()
onDismiss.invoke()
} else {
collapse()
}
}

in l1..l2 -> collapse()
in l2..l3 -> expand()
else -> Unit
}
}
}

val preUpPostDownNestedScrollConnection
get() = object : NestedScrollConnection {
var isTopReached = false

override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
if (isExpanded && available.y < 0) {
isTopReached = false
}

return if (isTopReached && available.y < 0 && source == NestedScrollSource.UserInput) {
dispatchRawDelta(available.y)
available
} else {
Offset.Zero
}
}

override fun onPostScroll(
consumed: Offset,
available: Offset,
source: NestedScrollSource,
): Offset {
if (!isTopReached) {
isTopReached = consumed.y == 0f && available.y > 0
}

return if (isTopReached && source == NestedScrollSource.UserInput) {
dispatchRawDelta(available.y)
available
} else {
Offset.Zero
}
}

override suspend fun onPreFling(available: Velocity): Velocity {
return if (isTopReached) {
val velocity = -available.y
performFling(velocity, null)

available
} else {
Velocity.Zero
}
}

override suspend fun onPostFling(consumed: Velocity, available: Velocity): Velocity {
isTopReached = false
return Velocity.Zero
}
}
}

enum class DraggableBottomSheetAnchor {
DISMISSED,
COLLAPSED,
EXPANDED
}

@Composable
fun rememberDraggableBottomSheetState(
dismissedBound: Dp,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.bobbyesp.ui.components.bottomsheet.draggable

enum class DraggableBottomSheetAnchor {
DISMISSED,
COLLAPSED,
EXPANDED
}
Loading

0 comments on commit bf3abbd

Please sign in to comment.