From 959a5c26805b7a166bce9c5bde61800def15e3ff Mon Sep 17 00:00:00 2001 From: Marvin Elsen Date: Thu, 24 Oct 2024 21:13:10 +0200 Subject: [PATCH] Refactor SearchController --- .../willow/ui/controllers/SearchController.kt | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) 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) } }