fix(ui): accessing listState on io thread causes app to crash

This commit is contained in:
junkfood 2024-02-02 02:27:53 +08:00 committed by Ash
parent 1dc906b84b
commit 92f994f069
2 changed files with 19 additions and 10 deletions

View File

@ -4,10 +4,16 @@ import android.util.Log
import androidx.compose.animation.*
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
@ -36,8 +42,11 @@ fun ReadingPage(
val readingUiState = readingViewModel.readingUiState.collectAsStateValue()
val readerState = readingViewModel.readerStateStateFlow.collectAsStateValue()
val homeUiState = homeViewModel.homeUiState.collectAsStateValue()
var isReaderScrollingDown by remember { mutableStateOf(false) }
val isShowToolBar = if (LocalReadingAutoHideToolbar.current.value) {
readingUiState.articleId != null && !readingUiState.listState.isScrollDown()
readingUiState.articleId != null && !isReaderScrollingDown
} else {
true
}
@ -100,9 +109,16 @@ fun ReadingPage(
initialContentExit = ExitTransition.None, sizeTransform = null
)
}
}
}, label = ""
) {
it.run {
val listState = rememberSaveable(
inputs = arrayOf(content),
saver = LazyListState.Saver
) { LazyListState() }
isReaderScrollingDown = listState.isScrollDown()
Content(
content = content.text ?: "",
feedName = feedName,
@ -111,7 +127,7 @@ fun ReadingPage(
link = link,
publishedDate = publishedDate,
isLoading = content is ReaderState.Loading,
listState = readingUiState.listState,
listState = listState,
isShowToolBar = isShowToolBar,
)
}

View File

@ -71,12 +71,6 @@ class ReadingViewModel @Inject constructor(
if (it.isFullContent) internalRenderFullContent()
else renderDescriptionContent()
}
// java.lang.NullPointerException: Attempt to invoke virtual method
// 'boolean androidx.compose.ui.node.LayoutNode.getNeedsOnPositionedDispatch$ui_release()'
// on a null object reference
if (_readingUiState.value.listState.firstVisibleItemIndex != 0) {
_readingUiState.value.listState.scrollToItem(0)
}
}
}
@ -169,7 +163,6 @@ data class ReadingUiState(
val articleId: String? = null,
val isUnread: Boolean = false,
val isStarred: Boolean = false,
val listState: LazyListState = LazyListState(),
val nextArticleId: String? = null,
)