diff --git a/src/main/kotlin/com/marvinelsen/willow/ui/controllers/SearchController.kt b/src/main/kotlin/com/marvinelsen/willow/ui/controllers/SearchController.kt index e76f30c..2eb479b 100644 --- a/src/main/kotlin/com/marvinelsen/willow/ui/controllers/SearchController.kt +++ b/src/main/kotlin/com/marvinelsen/willow/ui/controllers/SearchController.kt @@ -16,22 +16,18 @@ class SearchController(private val model: Model) { @FXML @Suppress("UnusedPrivateMember") private fun initialize() { - textFieldSearch.textProperty().addListener { _, _, newValue -> - if (newValue.isNullOrBlank()) { - return@addListener - } + textFieldSearch.textProperty().addListener { _, _, _ -> search() } + searchModeToggleGroup.selectedToggleProperty().addListener { _, _, _ -> search() } + } - val searchMode = searchModeToggleGroup.selectedToggle.userData as SearchMode - model.search(newValue, searchMode) + private fun search() { + val searchQuery = textFieldSearch.text + val searchMode = searchModeToggleGroup.selectedToggle.userData as SearchMode + + if (searchQuery.isNullOrBlank()) { + return } - searchModeToggleGroup.selectedToggleProperty().addListener { _, _, newValue -> - if (textFieldSearch.text.isNullOrBlank()) { - return@addListener - } - - val searchMode = newValue.userData as SearchMode - model.search(textFieldSearch.text, searchMode) - } + model.search(searchQuery, searchMode) } }