Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0uuuui #521

Closed
wants to merge 12 commits into from
183 changes: 182 additions & 1 deletion app/src/main/java/me/ash/reader/ui/ext/ModifierExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,45 @@ package me.ash.reader.ui.ext
import android.annotation.SuppressLint
import android.view.HapticFeedbackConstants
import android.view.SoundEffectConstants
import androidx.compose.animation.core.snap
import androidx.compose.foundation.*
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.PressInteraction
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.FractionalThreshold
import androidx.compose.material.rememberSwipeableState
import androidx.compose.material.swipeable
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Offset
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.nestedscroll.nestedScroll
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.Velocity
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.lerp
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.PagerScope
import com.google.accompanist.pager.calculateCurrentOffsetForPage
import kotlinx.coroutines.launch
import kotlin.math.absoluteValue

@OptIn(ExperimentalPagerApi::class)
Expand Down Expand Up @@ -126,4 +146,165 @@ fun Modifier.combinedFeedbackClickable(
},
)
}
}
}

enum class ArticleSwipeDirection(val raw: Int) {
Up(1),
Right(2),
Down(3),
Left(4),
Default(0)
}

@OptIn(ExperimentalMaterialApi::class)
fun Modifier.swipeLeftAndRight(onLeft: () -> Unit, onRight: () -> Unit): Modifier = composed {
var screenWidth by rememberSaveable { mutableStateOf(0f) }
var screenHeight by rememberSaveable { mutableStateOf(0f) }
val swipeableState = rememberSwipeableState(
ArticleSwipeDirection.Default,
animationSpec = snap()
)
val connection = remember {
object: NestedScrollConnection {
// Let the children eat first, we consume nothing
override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset { return Offset.Zero }

// Let it scroll...
override suspend fun onPreFling(available: Velocity): Velocity { return Velocity.Zero }

// We consume the rest
override fun onPostScroll(
consumed: Offset,
available: Offset,
source: NestedScrollSource,
): Offset {
// use leftover delta to swipe parent
return Offset(0f, swipeableState.performDrag(available.x))
}

// We fling but with zero speed (needed to trigger the event, but too fast will overshoot)
override suspend fun onPostFling(consumed: Velocity, available: Velocity): Velocity {
// perform fling on parent to trigger state change
swipeableState.performFling(velocity = 0f)
return available
}
}
}
val anchorWeight = remember(screenWidth) {
if (screenWidth == 0f) {
1f
} else {
screenWidth
}
}
val scope = rememberCoroutineScope()
if (swipeableState.isAnimationRunning) {
DisposableEffect(Unit) {
onDispose {
when (swipeableState.currentValue) {
ArticleSwipeDirection.Left -> {
onLeft()
}
ArticleSwipeDirection.Right -> {
onRight()
}
else -> {
return@onDispose
}
}
scope.launch {
swipeableState.snapTo(ArticleSwipeDirection.Default)
}
}
}
}
return@composed Modifier
.onSizeChanged { screenWidth = it.width.toFloat() }
.nestedScroll(connection)
.swipeable(
state = swipeableState,
anchors = mapOf(
0f to ArticleSwipeDirection.Right,
anchorWeight / 2 to ArticleSwipeDirection.Default,
anchorWeight to ArticleSwipeDirection.Left,
),
thresholds = { _, _ -> FractionalThreshold(0.3f) },
orientation = Orientation.Horizontal,
)
}

@OptIn(ExperimentalMaterialApi::class)
fun Modifier.swipeableUpDown(onUp: () -> Unit, onDown: () -> Unit): Modifier = composed {
var screenHeight by rememberSaveable { mutableStateOf(0f) }
val swipeableState = rememberSwipeableState(
ArticleSwipeDirection.Default,
animationSpec = snap()
)
val connection = remember {
object: NestedScrollConnection {
// Let the children eat first, we consume nothing
override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset { return Offset.Zero }

// Let it scroll...
override suspend fun onPreFling(available: Velocity): Velocity { return Velocity.Zero }

// We consume the rest
override fun onPostScroll(
consumed: Offset,
available: Offset,
source: NestedScrollSource,
): Offset {
// use leftover delta to swipe parent
return Offset(0f, swipeableState.performDrag(available.y))
}

// We fling but with zero speed (needed to trigger the event, but too fast will overshoot)
override suspend fun onPostFling(consumed: Velocity, available: Velocity): Velocity {
// perform fling on parent to trigger state change
swipeableState.performFling(velocity = 0f)
return available
}
}
}
val anchorHeight = remember(screenHeight) {
if (screenHeight == 0f) {
1f
} else {
screenHeight
}
}
val scope = rememberCoroutineScope()
if (swipeableState.isAnimationRunning) {
DisposableEffect(Unit) {
onDispose {
when (swipeableState.currentValue) {
ArticleSwipeDirection.Up -> {
onUp()
}
ArticleSwipeDirection.Down -> {
onDown()
}
else -> {
return@onDispose
}
}
scope.launch {
swipeableState.snapTo(ArticleSwipeDirection.Default)
}
}
}
}
return@composed Modifier
.onSizeChanged { screenHeight = it.height.toFloat() }
.nestedScroll(connection)
.swipeable(
state = swipeableState,
anchors = mapOf(
0f to ArticleSwipeDirection.Up,
anchorHeight / 2 to ArticleSwipeDirection.Default,
anchorHeight to ArticleSwipeDirection.Down,
),
thresholds = { _, _ -> FractionalThreshold(0.3f) },
orientation = Orientation.Vertical,
)
}
62 changes: 62 additions & 0 deletions app/src/main/java/me/ash/reader/ui/page/home/HomeViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class HomeViewModel @Inject constructor(
private val _filterUiState = MutableStateFlow(FilterState())
val filterUiState = _filterUiState.asStateFlow()

private val _readingProgressState = MutableStateFlow(ReadingProgressState())
val readingProgressState = _readingProgressState.asStateFlow()

val syncWorkLiveData = workManager.getWorkInfosByTagLiveData(SyncWorker.WORK_NAME)

fun sync() {
Expand All @@ -57,9 +60,57 @@ class HomeViewModel @Inject constructor(
filter = filterState.filter,
)
}
resetReadingProgress()
fetchArticles()
}

fun resetReadingProgress() {
_readingProgressState.update {
it.copy(
readingCurrentItemNumber = -1,
readingId = "",
readingCurrentNumber = -1,
readingList = emptyList(),
readingNext = "",
readingPrev = "",
)
}
}

fun changeReadingProgressListAndItemNum(list: List<String>, itemNum: Int) {
_readingProgressState.update {
it.copy(
readingList = list,
readingCurrentItemNumber = itemNum
)
}
}

fun recordReadingState(currentNumber: Int,
previous: String,
current: String,
next: String) {
_readingProgressState.update {
it.copy(
readingCurrentNumber = currentNumber,
readingPrev = previous,
readingNext = next,
readingId = current
)
}
}

fun changeReadingCurrentId(id: String) {
_readingProgressState.update {
it.copy(
readingId = id,
readingCurrentItemNumber = -1,
readingPrev = "",
readingNext = ""
)
}
}

fun fetchArticles() {
_homeUiState.update {
it.copy(
Expand Down Expand Up @@ -108,3 +159,14 @@ data class HomeUiState(
val pagingData: Flow<PagingData<ArticleFlowItem>> = emptyFlow(),
val searchContent: String = "",
)

data class ReadingProgressState(
// for flow page scroll usage
val readingCurrentItemNumber: Int = -1,

val readingId: String = "",
val readingCurrentNumber: Int = -1,
val readingList: List<String> = emptyList(),
val readingNext: String = "",
val readingPrev: String = "",
)
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package me.ash.reader.ui.page.home.feeds

import android.util.Log
import androidx.activity.compose.BackHandler
import androidx.compose.animation.core.*
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.KeyboardArrowRight
import androidx.compose.material.icons.outlined.Settings
Expand Down Expand Up @@ -81,6 +83,7 @@ fun FeedsPage(
val newVersion = LocalNewVersionNumber.current
val skipVersion = LocalSkipVersionNumber.current
val currentVersion = remember { context.getCurrentVersion() }
val listState = if (groupWithFeedList.isNotEmpty()) feedsUiState.listState else rememberLazyListState()

val owner = LocalLifecycleOwner.current
var isSyncing by remember { mutableStateOf(false) }
Expand Down Expand Up @@ -157,7 +160,9 @@ fun FeedsPage(
}
},
content = {
LazyColumn {
LazyColumn (
state = listState
) {
item {
DisplayText(
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,17 @@ fun ArticleItem(
@Composable
fun SwipeableArticleItem(
articleWithFeed: ArticleWithFeed,
isFilterUnread: Boolean,
onClick: (ArticleWithFeed) -> Unit = {},
onSwipeOut: (ArticleWithFeed) -> Unit = {},
) {
var isArticleVisible by remember { mutableStateOf(true) }
val dismissState = rememberDismissState(initialValue = DismissValue.Default, confirmStateChange = {
if (it == DismissValue.DismissedToEnd) {
isArticleVisible = false
isArticleVisible = !isFilterUnread
onSwipeOut(articleWithFeed)
}
true
isFilterUnread
})
if (isArticleVisible) {
SwipeToDismiss(
Expand Down
Loading
Loading