From 68fe52d2132d24f9696a9444da13600a2b52239d Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 15 Dec 2018 12:28:24 +0100 Subject: [PATCH] manage 'submit' in the text search field --- java/io/xdrm/lebonprix/HomeActivity.kt | 102 +++++++++++++------------ 1 file changed, 55 insertions(+), 47 deletions(-) diff --git a/java/io/xdrm/lebonprix/HomeActivity.kt b/java/io/xdrm/lebonprix/HomeActivity.kt index d710fe1..ebabbc0 100644 --- a/java/io/xdrm/lebonprix/HomeActivity.kt +++ b/java/io/xdrm/lebonprix/HomeActivity.kt @@ -7,6 +7,7 @@ import android.os.Handler import android.text.Editable import android.text.TextWatcher import android.util.Log +import android.view.KeyEvent import android.view.WindowManager import android.widget.Toast import io.xdrm.lebonprix.anim.HomeFilterAnimation @@ -93,55 +94,14 @@ class HomeActivity : AppCompatActivity() { // launch search - search_button.setOnClickListener { - val selectedCategories = getCategories() - - // (1) Loader - if( loader != null && loader!!.isActive ) - loader!!.cancel() - - loader = GlobalScope.launch(Dispatchers.Main) { - // launch animation - load_animator.stage(HomeFilterAnimation.STAGE_CLEAN).during(300).start() - delay(300) - load_animator.stage(HomeFilterAnimation.STAGE_CENTER).during(300).start() - delay(300) - while(true){ - load_animator.stage(HomeFilterAnimation.STAGE_LOAD).during(5000).start() - delay(5000) - } - } - - if( httpJob != null && httpJob!!.isActive ) - httpJob!!.cancel() - - httpJob = GlobalScope.launch { - - // exec request - val prices = apiPrices.fetch(search.text.toString(), selectedCategories.toTypedArray()) - loader?.cancel() - - // stop all if no result - if( prices.isNullOrEmpty() ) { - runOnUiThread { - load_animator.stage(HomeFilterAnimation.CLEAR_ALL).during(0).start() - } - return@launch - } - - // transition - runOnUiThread { - load_animator.stage(HomeFilterAnimation.STAGE_END).during(500).start() - } - delay(500) - - val int = Intent(this@HomeActivity, ResultActivity::class.java) - int.putExtra("prices", prices) - startActivity(int) - overridePendingTransition(0, 0) - } + search.setOnKeyListener { v, keyCode, event -> + if( event.action != KeyEvent.ACTION_DOWN || keyCode != KeyEvent.KEYCODE_ENTER ) + return@setOnKeyListener false + launch_search() + return@setOnKeyListener true } + search_button.setOnClickListener { launch_search() } updateCategories(JSONArray("[]")) @@ -168,6 +128,54 @@ class HomeActivity : AppCompatActivity() { } } + private fun launch_search(){ + val selectedCategories = getCategories() + + // (1) Loader + if( loader != null && loader!!.isActive ) + loader!!.cancel() + + loader = GlobalScope.launch(Dispatchers.Main) { + // launch animation + load_animator.stage(HomeFilterAnimation.STAGE_CLEAN).during(300).start() + delay(300) + load_animator.stage(HomeFilterAnimation.STAGE_CENTER).during(300).start() + delay(300) + while(true){ + load_animator.stage(HomeFilterAnimation.STAGE_LOAD).during(5000).start() + delay(5000) + } + } + + if( httpJob != null && httpJob!!.isActive ) + httpJob!!.cancel() + + httpJob = GlobalScope.launch { + + // exec request + val prices = apiPrices.fetch(search.text.toString(), selectedCategories.toTypedArray()) + loader?.cancel() + + // stop all if no result + if( prices.isNullOrEmpty() ) { + runOnUiThread { + load_animator.stage(HomeFilterAnimation.CLEAR_ALL).during(0).start() + } + return@launch + } + + // transition + runOnUiThread { + load_animator.stage(HomeFilterAnimation.STAGE_END).during(500).start() + } + delay(500) + + val int = Intent(this@HomeActivity, ResultActivity::class.java) + int.putExtra("prices", prices) + startActivity(int) + overridePendingTransition(0, 0) + } + } private fun updateCategories(categories: JSONArray){ // 1. Update categories