From 61b3e37adad7965dbb0440c3930e262c92dbe9f3 Mon Sep 17 00:00:00 2001 From: Anton Yarmolenko Date: Mon, 30 Sep 2024 10:19:47 +0200 Subject: [PATCH] fix: picker crash when no elements (#63) (#517) Co-authored-by: Vadim Kuznetsov --- Core/Core/View/Base/PickerMenu.swift | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Core/Core/View/Base/PickerMenu.swift b/Core/Core/View/Base/PickerMenu.swift index 0de023381..cd3d6c49e 100644 --- a/Core/Core/View/Base/PickerMenu.swift +++ b/Core/Core/View/Base/PickerMenu.swift @@ -33,6 +33,7 @@ public struct PickerMenu: View { private let router: BaseRouter private var idiom: UIUserInterfaceIdiom { UIDevice.current.userInterfaceIdiom } private var selected: ((PickerItem) -> Void) = { _ in } + private let emptyKey: String = "--empty--" public init( items: [PickerItem], @@ -50,18 +51,19 @@ public struct PickerMenu: View { private var filteredItems: [PickerItem] { if search.isEmpty { - return items + return items.isEmpty ? [PickerItem(key: emptyKey, value: "")] : items } else { - return items.filter { $0.value.localizedCaseInsensitiveContains(search) } + let filteredItems = items.filter { $0.value.localizedCaseInsensitiveContains(search) } + return filteredItems.isEmpty ? [PickerItem(key: emptyKey, value: "")] : filteredItems } } private var isSingleSelection: Bool { - return filteredItems.count == 1 + return filteredItems.count == 1 && filteredItems.first?.key != emptyKey } private var isItemSelected: Bool { - return filteredItems.contains(selectedItem) + return filteredItems.contains(selectedItem) && selectedItem.key != emptyKey } private var acceptButtonDisabled: Bool {