manage 'submit' in the text search field

This commit is contained in:
Adrien Marquès 2018-12-15 12:28:24 +01:00
parent b5b1a5ed4d
commit 68fe52d213
1 changed files with 55 additions and 47 deletions

View File

@ -7,6 +7,7 @@ import android.os.Handler
import android.text.Editable import android.text.Editable
import android.text.TextWatcher import android.text.TextWatcher
import android.util.Log import android.util.Log
import android.view.KeyEvent
import android.view.WindowManager import android.view.WindowManager
import android.widget.Toast import android.widget.Toast
import io.xdrm.lebonprix.anim.HomeFilterAnimation import io.xdrm.lebonprix.anim.HomeFilterAnimation
@ -93,55 +94,14 @@ class HomeActivity : AppCompatActivity() {
// launch search // launch search
search_button.setOnClickListener { search.setOnKeyListener { v, keyCode, event ->
val selectedCategories = getCategories() if( event.action != KeyEvent.ACTION_DOWN || keyCode != KeyEvent.KEYCODE_ENTER )
return@setOnKeyListener false
// (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)
}
launch_search()
return@setOnKeyListener true
} }
search_button.setOnClickListener { launch_search() }
updateCategories(JSONArray("[]")) 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){ private fun updateCategories(categories: JSONArray){
// 1. Update categories // 1. Update categories