Fix NPE in update download dialog when progress is null

The download progress indicator passed a deferred lambda
`progress = { progress!! }` reading UpdateState.downloadProgress
(MutableState<Float?>). Material3 invokes the lambda during
layout/draw, outside the composition snapshot that guarded it, so it
re-read the live state. ApkDownloader sets the value to null when the
response has no content length (total <= 0), and it can also flip while
the screen is being torn down, causing the `!!` to throw NPE.

Capture the value into an immutable local before branching so the
lambda closes over a non-null Float instead of re-reading live state.
This commit is contained in:
世界
2026-07-06 21:16:02 +08:00
parent 34e945088d
commit 258af731f6
2 changed files with 10 additions and 8 deletions
@@ -665,16 +665,17 @@ class MainActivity :
) )
} else { } else {
val progress by UpdateState.downloadProgress val progress by UpdateState.downloadProgress
val progressValue = progress
Column { Column {
if (progress != null) { if (progressValue != null) {
Text("${stringResource(R.string.downloading)} ${(progress!! * 100).toInt()}%") Text("${stringResource(R.string.downloading)} ${(progressValue * 100).toInt()}%")
} else { } else {
Text(stringResource(R.string.downloading)) Text(stringResource(R.string.downloading))
} }
Spacer(modifier = Modifier.height(8.dp)) Spacer(modifier = Modifier.height(8.dp))
if (progress != null) { if (progressValue != null) {
LinearProgressIndicator( LinearProgressIndicator(
progress = { progress!! }, progress = { progressValue },
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
) )
} else { } else {
@@ -281,16 +281,17 @@ fun AppSettingsScreen(
) )
} else { } else {
val progress by UpdateState.downloadProgress val progress by UpdateState.downloadProgress
val progressValue = progress
Column { Column {
if (progress != null) { if (progressValue != null) {
Text("${stringResource(R.string.downloading)} ${(progress!! * 100).toInt()}%") Text("${stringResource(R.string.downloading)} ${(progressValue * 100).toInt()}%")
} else { } else {
Text(stringResource(R.string.downloading)) Text(stringResource(R.string.downloading))
} }
Spacer(modifier = Modifier.height(8.dp)) Spacer(modifier = Modifier.height(8.dp))
if (progress != null) { if (progressValue != null) {
LinearProgressIndicator( LinearProgressIndicator(
progress = { progress!! }, progress = { progressValue },
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
) )
} else { } else {