-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
804 additions
and
121 deletions.
There are no files selected for viewing
67 changes: 8 additions & 59 deletions
67
app/src/main/java/com/capyreader/app/ui/articles/list/ArticleRowSwipeBox.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,26 @@ | ||
package com.capyreader.app.ui.articles.list | ||
|
||
import androidx.compose.animation.animateColorAsState | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.SwipeToDismissBox | ||
import androidx.compose.material3.SwipeToDismissBoxValue | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalLayoutDirection | ||
import androidx.compose.ui.res.painterResource | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.unit.LayoutDirection | ||
import androidx.compose.ui.unit.dp | ||
import com.jocmp.capy.Article | ||
import me.saket.swipe.SwipeableActionsBox | ||
|
||
@Composable | ||
fun ArticleRowSwipeBox( | ||
article: Article, | ||
content: @Composable () -> Unit | ||
) { | ||
val swipeState = rememberArticleRowSwipeState(article = article) | ||
val dismissState = swipeState.state | ||
val action = swipeState.action | ||
|
||
SwipeToDismissBox( | ||
state = dismissState, | ||
enableDismissFromStartToEnd = swipeState.enableStart, | ||
enableDismissFromEndToStart = swipeState.enableEnd, | ||
gesturesEnabled = swipeState.enabled, | ||
backgroundContent = { | ||
val isRtl = LocalLayoutDirection.current == LayoutDirection.Rtl | ||
|
||
val color by animateColorAsState( | ||
when (swipeState.state.targetValue) { | ||
SwipeToDismissBoxValue.Settled -> MaterialTheme.colorScheme.surface | ||
else -> MaterialTheme.colorScheme.surfaceContainerHighest | ||
}, | ||
label = "" | ||
) | ||
Box( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.background(color) | ||
) { | ||
Icon( | ||
painterResource(action.icon), | ||
contentDescription = stringResource(id = action.translationKey), | ||
modifier = Modifier | ||
.padding(24.dp) | ||
.align( | ||
when (dismissState.dismissDirection) { | ||
SwipeToDismissBoxValue.StartToEnd -> if (isRtl) Alignment.CenterEnd else Alignment.CenterStart | ||
else -> if (isRtl) Alignment.CenterStart else Alignment.CenterEnd | ||
} | ||
) | ||
) | ||
} | ||
} | ||
) { | ||
if (swipeState.disabled) { | ||
content() | ||
} | ||
|
||
if (dismissState.currentValue != SwipeToDismissBoxValue.Settled) { | ||
LaunchedEffect(Unit) { | ||
action.commit() | ||
dismissState.reset() | ||
} | ||
SwipeableActionsBox( | ||
startActions = swipeState.start, | ||
endActions = swipeState.end, | ||
backgroundUntilSwipeThreshold = MaterialTheme.colorScheme.surface | ||
) { | ||
content() | ||
} | ||
} |
80 changes: 46 additions & 34 deletions
80
app/src/main/java/com/capyreader/app/ui/articles/list/ArticleRowSwipeState.kt
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
28 changes: 0 additions & 28 deletions
28
app/src/main/java/com/capyreader/app/ui/components/RememberNoFlingSwipeToDismissBoxState.kt
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package me.saket.swipe | ||
|
||
import kotlin.math.abs | ||
|
||
internal data class SwipeActionMeta( | ||
val value: SwipeAction, | ||
val isOnRightSide: Boolean, | ||
) | ||
|
||
internal data class ActionFinder( | ||
val left: List<SwipeAction>, | ||
val right: List<SwipeAction> | ||
) { | ||
|
||
fun actionAt(offset: Float, totalWidth: Int): SwipeActionMeta? { | ||
if (offset == 0f) { | ||
return null | ||
} | ||
|
||
val isOnRightSide = offset < 0f | ||
val actions = if (isOnRightSide) right else left | ||
|
||
val actionAtOffset = actions.actionAt( | ||
offset = abs(offset).coerceAtMost(totalWidth.toFloat()), | ||
totalWidth = totalWidth | ||
) | ||
return actionAtOffset?.let { | ||
SwipeActionMeta( | ||
value = actionAtOffset, | ||
isOnRightSide = isOnRightSide | ||
) | ||
} | ||
} | ||
|
||
private fun List<SwipeAction>.actionAt(offset: Float, totalWidth: Int): SwipeAction? { | ||
if (isEmpty()) { | ||
return null | ||
} | ||
|
||
val totalWeights = this.sumOf { it.weight } | ||
var offsetSoFar = 0.0 | ||
|
||
@Suppress("ReplaceManualRangeWithIndicesCalls") // Avoid allocating an Iterator for every pixel swiped. | ||
for (i in 0 until size) { | ||
val action = this[i] | ||
val actionWidth = (action.weight / totalWeights) * totalWidth | ||
val actionEndX = offsetSoFar + actionWidth | ||
|
||
if (offset <= actionEndX) { | ||
return action | ||
} | ||
offsetSoFar += actionEndX | ||
} | ||
|
||
// Precision error in the above loop maybe? | ||
error("Couldn't find any swipe action. Width=$totalWidth, offset=$offset, actions=$this") | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package me.saket.swipe | ||
|
||
import androidx.compose.foundation.Image | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.painter.Painter | ||
import androidx.compose.ui.unit.dp | ||
|
||
/** | ||
* Represents an action that can be shown in [SwipeableActionsBox]. | ||
* | ||
* @param background Color used as the background of [SwipeableActionsBox] while | ||
* this action is visible. If this action is swiped, its background color is | ||
* also used for drawing a ripple over the content for providing a visual | ||
* feedback to the user. | ||
* | ||
* @param weight The proportional width to give to this element, as related | ||
* to the total of all weighted siblings. [SwipeableActionsBox] will divide its | ||
* horizontal space and distribute it to actions according to their weight. | ||
* | ||
* @param isUndo Determines the direction in which a ripple is drawn when this | ||
* action is swiped. When false, the ripple grows from this action's position | ||
* to consume the entire composable, and vice versa. This can be used for | ||
* actions that can be toggled on and off. | ||
*/ | ||
class SwipeAction( | ||
val onSwipe: () -> Unit, | ||
val icon: @Composable () -> Unit, | ||
val background: Color, | ||
val weight: Double = 1.0, | ||
val isUndo: Boolean = false | ||
) { | ||
init { | ||
require(weight > 0.0) { "invalid weight $weight; must be greater than zero" } | ||
} | ||
|
||
fun copy( | ||
onSwipe: () -> Unit = this.onSwipe, | ||
icon: @Composable () -> Unit = this.icon, | ||
background: Color = this.background, | ||
weight: Double = this.weight, | ||
isUndo: Boolean = this.isUndo, | ||
) = SwipeAction( | ||
onSwipe = onSwipe, | ||
icon = icon, | ||
background = background, | ||
weight = weight, | ||
isUndo = isUndo | ||
) | ||
} | ||
|
||
/** | ||
* See [SwipeAction] for documentation. | ||
*/ | ||
fun SwipeAction( | ||
onSwipe: () -> Unit, | ||
icon: Painter, | ||
background: Color, | ||
weight: Double = 1.0, | ||
isUndo: Boolean = false | ||
): SwipeAction { | ||
return SwipeAction( | ||
icon = { | ||
Image( | ||
modifier = Modifier.padding(16.dp), | ||
painter = icon, | ||
contentDescription = null | ||
) | ||
}, | ||
background = background, | ||
weight = weight, | ||
onSwipe = onSwipe, | ||
isUndo = isUndo | ||
) | ||
} |
Oops, something went wrong.