Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
25huizengek1 committed Feb 27, 2024
1 parent 92a9dea commit b849222
Show file tree
Hide file tree
Showing 31 changed files with 890 additions and 817 deletions.
14 changes: 0 additions & 14 deletions app/src/main/kotlin/it/vfsfitvnm/vimusic/Dependencies.kt

This file was deleted.

588 changes: 0 additions & 588 deletions app/src/main/kotlin/it/vfsfitvnm/vimusic/MainActivity.kt

This file was deleted.

595 changes: 591 additions & 4 deletions app/src/main/kotlin/it/vfsfitvnm/vimusic/MainApplication.kt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ import it.vfsfitvnm.vimusic.utils.mediaItems
import it.vfsfitvnm.vimusic.utils.shouldBePlaying
import it.vfsfitvnm.vimusic.utils.thumbnail
import it.vfsfitvnm.vimusic.utils.timer
import it.vfsfitvnm.vimusic.utils.toast
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
Expand Down Expand Up @@ -610,7 +611,7 @@ class PlayerService : InvincibleService(), Player.Listener, PlaybackStatsListene
bassBoost?.setStrength(PlayerPreferences.bassBoostLevel.toShort())
bassBoost?.enabled = true
}.onFailure {
Toast.makeText(this, R.string.error_bassboost_init, Toast.LENGTH_SHORT).show()
toast(getString(R.string.error_bassboost_init))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import it.vfsfitvnm.vimusic.Database
import it.vfsfitvnm.vimusic.R
import it.vfsfitvnm.vimusic.transaction
import it.vfsfitvnm.vimusic.utils.intent
import it.vfsfitvnm.vimusic.utils.toast
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
Expand Down Expand Up @@ -213,7 +214,7 @@ class PrecacheService : DownloadService(
/* foreground = */ false
)
}.exceptionOrNull()?.printStackTrace()?.also {
Toast.makeText(context, R.string.error_pre_cache, Toast.LENGTH_SHORT).show()
context.toast(context.getString(R.string.error_pre_cache))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ 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.SpringSpec
import androidx.compose.animation.core.VectorConverter
import androidx.compose.animation.core.spring
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Indication
import androidx.compose.foundation.LocalIndication
Expand All @@ -23,9 +23,10 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.SaverScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -76,7 +77,7 @@ fun BottomSheet(
onDragEnd = {
val velocity = -velocityTracker.calculateVelocity().y
velocityTracker.resetTracking()
state.performFling(velocity, onDismiss)
state.fling(velocity, onDismiss)
}
)
}
Expand Down Expand Up @@ -108,7 +109,7 @@ class BottomSheetState(
draggableState: DraggableState,
private val coroutineScope: CoroutineScope,
private val animatable: Animatable<Dp, AnimationVector1D>,
private val onAnchorChanged: (Int) -> Unit,
private val onAnchorChanged: (Anchor) -> Unit,
val collapsedBound: Dp
) : DraggableState by draggableState {
private val dismissedBound get() = animatable.lowerBound!!
Expand All @@ -123,68 +124,60 @@ class BottomSheetState(
1f - (animatable.upperBound!! - animatable.value) / (animatable.upperBound!! - collapsedBound)
}

fun collapse(animationSpec: AnimationSpec<Dp>) {
onAnchorChanged(COLLAPSED_ANCHOR)
coroutineScope.launch {
animatable.animateTo(collapsedBound, animationSpec)
}
private fun deferAnimateTo(
newValue: Dp,
spec: AnimationSpec<Dp> = spring()
) = coroutineScope.launch {
animatable.animateTo(newValue, spec)
}

fun expand(animationSpec: AnimationSpec<Dp>) {
onAnchorChanged(EXPANDED_ANCHOR)
coroutineScope.launch {
animatable.animateTo(animatable.upperBound!!, animationSpec)
}
fun collapse(spec: AnimationSpec<Dp>) {
onAnchorChanged(Anchor.Collapsed)
deferAnimateTo(collapsedBound, spec)
}

private fun collapse() = collapse(SpringSpec())

private fun expand() = expand(SpringSpec())
fun expand(spec: AnimationSpec<Dp>) {
onAnchorChanged(Anchor.Expanded)
deferAnimateTo(animatable.upperBound!!, spec)
}

private fun collapse() = collapse(spring())
private fun expand() = expand(spring())
fun collapseSoft() = collapse(tween(300))

fun expandSoft() = expand(tween(300))

fun dismiss() {
onAnchorChanged(DISMISSED_ANCHOR)
coroutineScope.launch {
animatable.animateTo(animatable.lowerBound!!)
}
onAnchorChanged(Anchor.Dismissed)
deferAnimateTo(animatable.lowerBound!!)
}

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

fun performFling(velocity: Float, onDismiss: (() -> Unit)?) = when {
fun fling(velocity: Float, onDismiss: (() -> Unit)?) = when {
velocity > 250 -> expand()
velocity < -250 -> {
if (value < collapsedBound && onDismiss != null) {
dismiss()
onDismiss.invoke()
} else {
collapse()
}
} else collapse()
}

else -> {
val l0 = dismissedBound
val l1 = (collapsedBound - dismissedBound) / 2
val l2 = (expandedBound - collapsedBound) / 2
val l3 = expandedBound

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

in l1..l2 -> collapse()
in l2..l3 -> expand()
in l2..expandedBound -> expand()
else -> Unit
}
}
Expand Down Expand Up @@ -218,7 +211,7 @@ class BottomSheetState(

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

available
} else Velocity.Zero
Expand All @@ -228,29 +221,47 @@ class BottomSheetState(
return Velocity.Zero
}
}
}

const val EXPANDED_ANCHOR = 2
const val COLLAPSED_ANCHOR = 1
const val DISMISSED_ANCHOR = 0
@JvmInline
value class Anchor private constructor(internal val value: Int) {
companion object {
val Dismissed = Anchor(value = 0)
val Collapsed = Anchor(value = 1)
val Expanded = Anchor(value = 2)
}

object Saver : androidx.compose.runtime.saveable.Saver<Anchor, Int> {
override fun restore(value: Int) = when (value) {
0 -> Dismissed
1 -> Collapsed
2 -> Expanded
else -> error("Anchor $value does not exist!")
}

override fun SaverScope.save(value: Anchor) = value.value
}
}
}

@Composable
fun rememberBottomSheetState(
dismissedBound: Dp,
expandedBound: Dp,
collapsedBound: Dp = dismissedBound,
initialAnchor: Int = DISMISSED_ANCHOR
initialAnchor: BottomSheetState.Anchor = BottomSheetState.Anchor.Dismissed
): BottomSheetState {
val density = LocalDensity.current
val coroutineScope = rememberCoroutineScope()

var previousAnchor by rememberSaveable { mutableIntStateOf(initialAnchor) }
var previousAnchor by rememberSaveable(stateSaver = BottomSheetState.Anchor.Saver) {
mutableStateOf(initialAnchor)
}

return remember(dismissedBound, expandedBound, collapsedBound, coroutineScope) {
val initialValue = when (previousAnchor) {
EXPANDED_ANCHOR -> expandedBound
COLLAPSED_ANCHOR -> collapsedBound
DISMISSED_ANCHOR -> dismissedBound
BottomSheetState.Anchor.Dismissed -> dismissedBound
BottomSheetState.Anchor.Collapsed -> collapsedBound
BottomSheetState.Anchor.Expanded -> expandedBound
else -> error("Unknown BottomSheet anchor")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import it.vfsfitvnm.vimusic.LocalPlayerAwareWindowInsets
import it.vfsfitvnm.vimusic.R
import it.vfsfitvnm.vimusic.ui.styling.LocalAppearance
import it.vfsfitvnm.vimusic.utils.align
import it.vfsfitvnm.vimusic.utils.color
import it.vfsfitvnm.vimusic.utils.disabled
import it.vfsfitvnm.vimusic.utils.secondary
import it.vfsfitvnm.vimusic.utils.semiBold

Expand All @@ -27,7 +27,7 @@ fun Attribution(
text: String,
modifier: Modifier = Modifier
) = Column {
val (colorPalette, typography) = LocalAppearance.current
val (_, typography) = LocalAppearance.current
val windowInsets = LocalPlayerAwareWindowInsets.current

val endPaddingValues = windowInsets
Expand Down Expand Up @@ -66,8 +66,7 @@ fun Attribution(

if (attributionsIndex != -1) BasicText(
text = stringResource(R.string.wikipedia_cc_by_sa),
style = typography.xxs.color(colorPalette.textDisabled)
.align(TextAlign.End),
style = typography.xxs.disabled.align(TextAlign.End),
modifier = Modifier
.padding(horizontal = 16.dp)
.padding(bottom = 16.dp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import it.vfsfitvnm.vimusic.ui.styling.LocalAppearance
import it.vfsfitvnm.vimusic.utils.color
import it.vfsfitvnm.vimusic.utils.disabled
import it.vfsfitvnm.vimusic.utils.medium
import it.vfsfitvnm.vimusic.utils.primary

@Composable
fun DialogTextButton(
Expand All @@ -24,15 +25,15 @@ fun DialogTextButton(
) {
val (colorPalette, typography) = LocalAppearance.current

val textColor = when {
!enabled -> colorPalette.textDisabled
primary -> colorPalette.onAccent
else -> colorPalette.text
}

BasicText(
text = text,
style = typography.xs.medium.color(textColor),
style = typography.xs.medium.let {
when {
!enabled -> it.disabled
primary -> it.primary
else -> it
}
},
modifier = modifier
.clip(RoundedCornerShape(36.dp))
.background(if (primary) colorPalette.accent else Color.Transparent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,27 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import it.vfsfitvnm.vimusic.ui.styling.LocalAppearance

@Composable
fun HeaderIconButton(
onClick: () -> Unit,
@DrawableRes icon: Int,
modifier: Modifier = Modifier,
enabled: Boolean = true,
indication: Indication? = rememberRipple(bounded = false)
) {
val (colorPalette) = LocalAppearance.current

HeaderIconButton(
onClick = onClick,
icon = icon,
modifier = modifier,
indication = indication,
enabled = true,
color = if (enabled) colorPalette.text else colorPalette.textDisabled
)
}

@Composable
fun HeaderIconButton(
Expand All @@ -35,6 +56,26 @@ fun HeaderIconButton(
.size(18.dp)
)

@Composable
fun IconButton(
onClick: () -> Unit,
@DrawableRes icon: Int,
modifier: Modifier = Modifier,
enabled: Boolean = true,
indication: Indication? = rememberRipple(bounded = false)
) {
val (colorPalette) = LocalAppearance.current

IconButton(
onClick = onClick,
icon = icon,
modifier = modifier,
indication = indication,
enabled = true,
color = if (enabled) colorPalette.text else colorPalette.textDisabled
)
}

@Composable
fun IconButton(
onClick: () -> Unit,
Expand Down
Loading

0 comments on commit b849222

Please sign in to comment.