Skip to content

Commit

Permalink
Update: use keys for items of longer LazyLists
Browse files Browse the repository at this point in the history
  • Loading branch information
machiav3lli committed Aug 15, 2024
1 parent 281fc48 commit 555b51f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListScope
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.pulltorefresh.PullToRefreshContainer
Expand All @@ -20,11 +20,10 @@ import androidx.compose.ui.unit.dp

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun <T> PullToRefreshLazyColumn(
items: List<T>,
fun PullToRefreshLazyColumn(
isRefreshing: Boolean,
onRefresh: () -> Unit,
content: @Composable (T) -> Unit,
content: LazyListScope.() -> Unit,
modifier: Modifier = Modifier,
listState: LazyListState = rememberLazyListState()
) {
Expand All @@ -39,12 +38,9 @@ fun <T> PullToRefreshLazyColumn(
contentPadding = PaddingValues(8.dp),
modifier = Modifier
.fillMaxSize(),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
items(items) {
content(it)
}
}
verticalArrangement = Arrangement.spacedBy(8.dp),
content = content
)

if (pullToRefreshState.isRefreshing) {
LaunchedEffect(true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fun StringSelectionPrefDialogUI(
.padding(vertical = 8.dp)
.weight(1f, false)
) {
items(items = entryPairs) {
items(items = entryPairs, key = { it.first }) {
val isSelected = rememberSaveable(selected) {
mutableStateOf(selected == it.first)
}
Expand Down
46 changes: 23 additions & 23 deletions app/src/main/java/com/saulhdev/feeder/compose/pages/OverlayPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ fun OverlayPage(isOverlay: Boolean = false) {
contentPadding = PaddingValues(8.dp),
modifier = Modifier.padding(paddingValues),
) {
items(bookmarked.value.entries.toList()) { item ->
items(bookmarked.value.entries.toList(), key = { it.key.id }) { item ->
BookmarkItem(
article = item.key,
feed = item.value,
Expand Down Expand Up @@ -288,7 +288,6 @@ fun OverlayPage(isOverlay: Boolean = false) {
}
}
else PullToRefreshLazyColumn(
items = feedList,
isRefreshing = isRefreshing,
onRefresh = {
isRefreshing = true
Expand All @@ -299,29 +298,30 @@ fun OverlayPage(isOverlay: Boolean = false) {
}
},
listState = listState,
content = { item ->
ArticleItem(
article = item,
onBookmark = {
repository.bookmarkArticle(item.id, it)
},
) {
if (prefs.openInBrowser.getValue()) {
context.launchView(item.content.link)
} else {
if (prefs.offlineReader.getValue()) {
//navController.navigate("/${Routes.ARTICLE_VIEW}/${item.id}/")
context.startActivity(
MainActivity.navigateIntent(
content = {
items(feedList, key = { it.id }) { item ->
ArticleItem(
article = item,
onBookmark = {
repository.bookmarkArticle(item.id, it)
},
) {
if (prefs.openInBrowser.getValue()) {
context.launchView(item.content.link)
} else {
if (prefs.offlineReader.getValue()) {
context.startActivity(
MainActivity.navigateIntent(
context,
"${Routes.ARTICLE_VIEW}/${item.id}"
)
)
} else {
openLinkInCustomTab(
context,
"${Routes.ARTICLE_VIEW}/${item.id}"
item.content.link
)
)
} else {
openLinkInCustomTab(
context,
item.content.link
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,21 @@ fun PreferencesPage() {
),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
item {
item(key = R.string.title_service) {
PreferenceGroup(
stringResource(id = R.string.title_service),
prefs = servicePrefs,
onPrefDialog = onPrefDialog
)
}
item {
item(key = R.string.pref_cat_overlay) {
PreferenceGroup(
stringResource(id = R.string.pref_cat_overlay),
prefs = themePrefs,
onPrefDialog = onPrefDialog
)
}
item {
item(key = R.string.title_other) {
PreferenceGroup(
stringResource(id = R.string.title_other),
prefs = debugPrefs,
Expand Down

0 comments on commit 555b51f

Please sign in to comment.