Skip to content

Commit

Permalink
fix(ui): pull to load offset
Browse files Browse the repository at this point in the history
  • Loading branch information
JunkFood02 committed Nov 12, 2024
1 parent 5862cf3 commit 2a23b71
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 47 deletions.
60 changes: 26 additions & 34 deletions app/src/main/java/me/ash/reader/ui/page/home/reading/Content.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
package me.ash.reader.ui.page.home.reading

import androidx.compose.foundation.ScrollState
import androidx.compose.foundation.gestures.scrollable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.calculateEndPadding
import androidx.compose.foundation.layout.calculateStartPadding
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.windowInsetsBottomHeight
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.selection.DisableSelection
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.foundation.verticalScroll
Expand All @@ -25,7 +19,6 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.unit.dp
import me.ash.reader.infrastructure.preference.LocalOpenLink
import me.ash.reader.infrastructure.preference.LocalOpenLinkSpecificBrowser
Expand Down Expand Up @@ -77,36 +70,35 @@ fun Content(
modifier = modifier
.padding(top = contentPadding.calculateTopPadding())
.fillMaxSize()
.verticalScroll(scrollState)

) {
// Top bar height
Spacer(modifier = Modifier.height(64.dp))
// padding
Column(
modifier = Modifier.padding(horizontal = 12.dp)
) {
DisableSelection {
Metadata(
feedName = feedName,
title = title,
author = author,
link = link,
publishedDate = publishedDate,
)
Column(modifier = Modifier.verticalScroll(scrollState)) {
// Top bar height
Spacer(modifier = Modifier.height(64.dp))
// padding
Column(
modifier = Modifier.padding(horizontal = 12.dp)
) {
DisableSelection {
Metadata(
feedName = feedName,
title = title,
author = author,
link = link,
publishedDate = publishedDate,
)
}
}
}

RYWebView(
modifier = Modifier.fillMaxSize(),
content = content,
refererDomain = link.extractDomain(),
onImageClick = onImageClick,
)
Spacer(modifier = Modifier.height(128.dp))
Spacer(modifier = Modifier.height(contentPadding.calculateBottomPadding()))


RYWebView(
modifier = Modifier.fillMaxSize(),
content = content,
refererDomain = link.extractDomain(),
onImageClick = onImageClick,
)
Spacer(modifier = Modifier.height(128.dp))
Spacer(modifier = Modifier.height(contentPadding.calculateBottomPadding()))
}
}

}
Expand Down
18 changes: 14 additions & 4 deletions app/src/main/java/me/ash/reader/ui/page/home/reading/PullToLoad.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import androidx.compose.ui.input.nestedscroll.NestedScrollSource
import androidx.compose.ui.input.nestedscroll.NestedScrollSource.Companion.Drag
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Density
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
Expand Down Expand Up @@ -78,7 +80,10 @@ private class ReaderNestedScrollConnection(
consumed: Offset, available: Offset, source: NestedScrollSource
): Offset = when {
!enabled -> Offset.Zero
source == NestedScrollSource.UserInput -> Offset(0f, onPostScroll(available.y)) // Pull to load
source == NestedScrollSource.UserInput -> Offset(
0f,
onPostScroll(available.y)
) // Pull to load
else -> Offset.Zero
}

Expand Down Expand Up @@ -312,6 +317,7 @@ object PullToLoadDefaults {

fun Modifier.pullToLoad(
state: PullToLoadState,
density: Density,
contentOffsetMultiple: Int = ContentOffsetMultiple,
onScroll: ((Float) -> Unit)? = null,
enabled: Boolean = true,
Expand All @@ -324,7 +330,11 @@ fun Modifier.pullToLoad(
onRelease = state::onRelease,
onScroll = onScroll
)
).run {
if (enabled) offset(x = 0.dp, y = (state.offsetFraction * contentOffsetMultiple).dp)
).then(
if (enabled) offset {
with(density) {
IntOffset(x = 0, y = (state.offsetFraction * contentOffsetMultiple).dp.roundToPx())
}
}
else this
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.platform.rememberNestedScrollInteropConnection
import androidx.compose.ui.unit.TextUnit
Expand Down Expand Up @@ -161,12 +162,8 @@ fun ReadingPage(
val state =
rememberPullToLoadState(
key = content,
onLoadNext = {
readingViewModel.loadNext()
},
onLoadPrevious = {
readingViewModel.loadPrevious()
}
onLoadNext = readingViewModel::loadNext,
onLoadPrevious = readingViewModel::loadPrevious
)

val listState = rememberSaveable(
Expand Down Expand Up @@ -203,15 +200,14 @@ fun ReadingPage(
}
) {
Box(
modifier = Modifier
.fillMaxSize()
.nestedScroll(rememberNestedScrollInteropConnection()),
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Content(
modifier = Modifier
.pullToLoad(
state = state,
density = LocalDensity.current,
onScroll = { f ->
if (abs(f) > 2f)
isReaderScrollingDown = f < 0f
Expand Down

0 comments on commit 2a23b71

Please sign in to comment.