From d3a5936d17881d80f229bc1cb4471f5ba1faf270 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Fri, 1 Aug 2025 16:18:55 +0100 Subject: [PATCH] android, desktop: gaurd crashes when item is absent in the list (#6140) --- .../common/views/usersettings/Appearance.android.kt | 4 ++-- .../common/views/helpers/ExposedDropDownSettingRow.kt | 8 +++++--- .../kotlin/chat/simplex/common/views/helpers/Section.kt | 4 ++-- .../views/usersettings/NotificationsSettingsView.kt | 4 ++-- .../usersettings/networkAndServers/NetworkAndServers.kt | 2 +- .../common/views/helpers/CustomTimePicker.desktop.kt | 1 + 6 files changed, 13 insertions(+), 10 deletions(-) diff --git a/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/views/usersettings/Appearance.android.kt b/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/views/usersettings/Appearance.android.kt index 320a8e876a..47506d9532 100644 --- a/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/views/usersettings/Appearance.android.kt +++ b/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/views/usersettings/Appearance.android.kt @@ -159,11 +159,11 @@ fun AppearanceScope.AppearanceLayout( } } -private fun findEnabledIcon(): AppIcon = AppIcon.values().first { icon -> +private fun findEnabledIcon(): AppIcon = AppIcon.values().firstOrNull { icon -> androidAppContext.packageManager.getComponentEnabledSetting( ComponentName(APPLICATION_ID, "chat.simplex.app.MainActivity_${icon.name.lowercase()}") ).let { it == COMPONENT_ENABLED_STATE_DEFAULT || it == COMPONENT_ENABLED_STATE_ENABLED } -} +} ?: AppIcon.DEFAULT @Preview @Composable diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/helpers/ExposedDropDownSettingRow.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/helpers/ExposedDropDownSettingRow.kt index 7ed91adbd9..893ff5a467 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/helpers/ExposedDropDownSettingRow.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/helpers/ExposedDropDownSettingRow.kt @@ -46,7 +46,7 @@ fun ExposedDropDownSetting( horizontalArrangement = Arrangement.End ) { Text( - values.first { it.first == selection.value }.second + (if (label != null) " $label" else ""), + (values.firstOrNull { it.first == selection.value }?.second ?: "") + (if (label != null) " $label" else ""), Modifier.widthIn(max = maxWidth), maxLines = 1, overflow = TextOverflow.Ellipsis, @@ -120,8 +120,10 @@ fun ExposedDropDownSettingWithIcon( ), contentAlignment = Alignment.Center ) { - val choice = values.first { it.first == selection.value } - Icon(painterResource(choice.second), choice.third, Modifier.padding(boxSize * iconPaddingPercent).fillMaxSize(), tint = iconColor) + val choice = values.firstOrNull { it.first == selection.value } + if (choice != null) { + Icon(painterResource(choice.second), choice.third, Modifier.padding(boxSize * iconPaddingPercent).fillMaxSize(), tint = iconColor) + } } DefaultExposedDropdownMenu( modifier = Modifier.widthIn(min = minWidth), diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/helpers/Section.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/helpers/Section.kt index 0d188bb73c..7ee52af784 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/helpers/Section.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/helpers/Section.kt @@ -91,7 +91,7 @@ fun SectionViewSelectable( } } } - SectionTextFooter(values.first { it.value == currentValue.value }.description) + SectionTextFooter(values.firstOrNull { it.value == currentValue.value }?.description ?: AnnotatedString("")) } @Composable @@ -221,7 +221,7 @@ fun SectionItemWithValue( horizontalArrangement = Arrangement.End ) { Text( - values.first { it.value == currentValue.value }.title + (if (label != null) " $label" else ""), + (values.firstOrNull { it.value == currentValue.value }?.title ?: "") + (if (label != null) " $label" else ""), maxLines = 1, overflow = TextOverflow.Ellipsis, color = MaterialTheme.colors.secondary diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/usersettings/NotificationsSettingsView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/usersettings/NotificationsSettingsView.kt index 5af5d5fb90..59b9d596f1 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/usersettings/NotificationsSettingsView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/usersettings/NotificationsSettingsView.kt @@ -62,7 +62,7 @@ fun NotificationsSettingsLayout( if (appPlatform == AppPlatform.ANDROID) { SettingsActionItemWithContent(null, stringResource(MR.strings.settings_notifications_mode_title), { showPage(CurrentPage.NOTIFICATIONS_MODE) }) { Text( - modes.first { it.value == notificationsMode.value }.title, + modes.firstOrNull { it.value == notificationsMode.value }?.title ?: "", maxLines = 1, overflow = TextOverflow.Ellipsis, color = MaterialTheme.colors.secondary @@ -71,7 +71,7 @@ fun NotificationsSettingsLayout( } SettingsActionItemWithContent(null, stringResource(MR.strings.settings_notification_preview_mode_title), { showPage(CurrentPage.NOTIFICATION_PREVIEW_MODE) }) { Text( - previewModes.first { it.value == notificationPreviewMode.value }.title, + previewModes.firstOrNull { it.value == notificationPreviewMode.value }?.title ?: "", maxLines = 1, overflow = TextOverflow.Ellipsis, color = MaterialTheme.colors.secondary diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/usersettings/networkAndServers/NetworkAndServers.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/usersettings/networkAndServers/NetworkAndServers.kt index 98f671ddc4..26ecf151ff 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/usersettings/networkAndServers/NetworkAndServers.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/usersettings/networkAndServers/NetworkAndServers.kt @@ -594,7 +594,7 @@ fun UseOnionHosts( onSelected = {} ) } - SectionTextFooter(values.first { it.value == onionHosts.value }.description) + SectionTextFooter(values.firstOrNull { it.value == onionHosts.value }?.description ?: AnnotatedString("")) } } diff --git a/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/views/helpers/CustomTimePicker.desktop.kt b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/views/helpers/CustomTimePicker.desktop.kt index 03c8e51c55..f1103bc516 100644 --- a/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/views/helpers/CustomTimePicker.desktop.kt +++ b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/views/helpers/CustomTimePicker.desktop.kt @@ -31,6 +31,7 @@ actual fun CustomTimePicker( mutableStateOf(res) } val values = remember(unit.value) { + // TODO replace with firstOrNull val limit = timeUnitsLimits.first { it.timeUnit == unit.value } val res = ArrayList>() for (i in limit.minValue..limit.maxValue) {