From d948e15fc74783824e116200d61e699258b38f99 Mon Sep 17 00:00:00 2001 From: Ashinch Date: Sat, 8 Oct 2022 21:19:43 +0800 Subject: [PATCH] Fix initial filter (#229) --- .../ash/reader/ui/page/home/flow/FlowPage.kt | 23 ++++++++++++++++--- .../reader/ui/page/home/flow/FlowViewModel.kt | 6 ----- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/me/ash/reader/ui/page/home/flow/FlowPage.kt b/app/src/main/java/me/ash/reader/ui/page/home/flow/FlowPage.kt index d29f986..b57a11d 100644 --- a/app/src/main/java/me/ash/reader/ui/page/home/flow/FlowPage.kt +++ b/app/src/main/java/me/ash/reader/ui/page/home/flow/FlowPage.kt @@ -129,7 +129,12 @@ fun FlowPage( }, ) { scope.launch { - flowUiState.listState.scrollToItem(0) + // java.lang.NullPointerException: Attempt to invoke virtual method + // 'boolean androidx.compose.ui.node.LayoutNode.getNeedsOnPositionedDispatch$ui_release()' + // on a null object reference + if (flowUiState.listState.firstVisibleItemIndex != 0) { + flowUiState.listState.scrollToItem(0) + } markAsRead = !markAsRead onSearch = false } @@ -145,7 +150,12 @@ fun FlowPage( }, ) { scope.launch { - flowUiState.listState.scrollToItem(0) + // java.lang.NullPointerException: Attempt to invoke virtual method + // 'boolean androidx.compose.ui.node.LayoutNode.getNeedsOnPositionedDispatch$ui_release()' + // on a null object reference + if (flowUiState.listState.firstVisibleItemIndex != 0) { + flowUiState.listState.scrollToItem(0) + } onSearch = !onSearch } } @@ -249,7 +259,14 @@ fun FlowPage( filterBarPadding = filterBarPadding.dp, filterBarTonalElevation = filterBarTonalElevation.value.dp, ) { - flowViewModel.scrollToItem(0) + scope.launch { + // java.lang.NullPointerException: Attempt to invoke virtual method + // 'boolean androidx.compose.ui.node.LayoutNode.getNeedsOnPositionedDispatch$ui_release()' + // on a null object reference + if (flowUiState.listState.firstVisibleItemIndex != 0) { + flowUiState.listState.scrollToItem(0) + } + } homeViewModel.changeFilter(filterUiState.copy(filter = it)) homeViewModel.fetchArticles() } diff --git a/app/src/main/java/me/ash/reader/ui/page/home/flow/FlowViewModel.kt b/app/src/main/java/me/ash/reader/ui/page/home/flow/FlowViewModel.kt index 1a7ab22..3225d46 100644 --- a/app/src/main/java/me/ash/reader/ui/page/home/flow/FlowViewModel.kt +++ b/app/src/main/java/me/ash/reader/ui/page/home/flow/FlowViewModel.kt @@ -30,12 +30,6 @@ class FlowViewModel @Inject constructor( } } - fun scrollToItem(index: Int) { - viewModelScope.launch { - _flowUiState.value.listState.scrollToItem(index) - } - } - fun markAsRead( groupId: String?, feedId: String?,