diff options
author | Iván Ávalos <avalos@disroot.org> | 2022-05-20 22:27:59 -0500 |
---|---|---|
committer | Iván Ávalos <avalos@disroot.org> | 2022-05-20 22:27:59 -0500 |
commit | 22d2b3ed3569925fc3835dd5b53c6aacde86278a (patch) | |
tree | 44668ecd692f5c6c26e2a2f98a1692f4b1b69cb5 /androidApp/src/main/java | |
parent | ed7d22c35d6ddea39284a31e751a2cc9f86332f3 (diff) | |
download | etbsa-trackermap-mobile-22d2b3ed3569925fc3835dd5b53c6aacde86278a.tar.gz etbsa-trackermap-mobile-22d2b3ed3569925fc3835dd5b53c6aacde86278a.tar.bz2 etbsa-trackermap-mobile-22d2b3ed3569925fc3835dd5b53c6aacde86278a.zip |
[android] Improved search performance by limiting rate
Diffstat (limited to 'androidApp/src/main/java')
-rw-r--r-- | androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsActivity.kt | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsActivity.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsActivity.kt index 09bedbb..8a161c3 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsActivity.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsActivity.kt @@ -19,11 +19,13 @@ package mx.trackermap.TrackerMap.android.units import android.content.Context import android.content.Intent +import android.os.Build import android.os.Bundle +import android.text.Editable +import android.text.TextWatcher import android.util.Log import android.view.View import android.view.inputmethod.InputMethodManager -import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.TooltipCompat import androidx.core.widget.doAfterTextChanged @@ -36,6 +38,7 @@ import mx.trackermap.TrackerMap.android.map.UnitMapFragment import mx.trackermap.TrackerMap.android.session.UserInformationActivity import mx.trackermap.TrackerMap.android.shared.Utils import org.koin.androidx.viewmodel.ext.android.viewModel +import java.util.* import kotlin.time.ExperimentalTime @DelicateCoroutinesApi @@ -100,13 +103,28 @@ class UnitsActivity : AppCompatActivity() { binding.mapLayerToggle.setOnClickListener { showLayersPopUp(it) } - binding.searchInput.doAfterTextChanged { - Log.d("UnitsActivity", "Search changed to ${it.toString()}") - if (unitsViewModel.unitsDisplayMode.value == UnitsViewModel.UnitsDisplayMode.MAP) { - unitsViewModel.setDisplayMode(UnitsViewModel.UnitsDisplayMode.LIST) + binding.searchInput.addTextChangedListener(object: TextWatcher { + override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {} + override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {} + + var timer = Timer() + val delay = 500L // milliseconds before doing search + + override fun afterTextChanged(p0: Editable?) { + timer.cancel() + timer = Timer() + timer.schedule(object: TimerTask() { + override fun run() { + Log.d("UnitsActivity", "Search changed to $p0") + if (unitsViewModel.unitsDisplayMode.value == UnitsViewModel.UnitsDisplayMode.MAP) { + unitsViewModel.setDisplayMode(UnitsViewModel.UnitsDisplayMode.LIST) + } + unitsViewModel.search(p0.toString()) + } + }, delay) } - unitsViewModel.search(it.toString()) - } + + }) binding.searchInputLayout.setEndIconOnClickListener { unfocusSearch(true) } @@ -119,7 +137,11 @@ class UnitsActivity : AppCompatActivity() { } private fun unfocusSearch(clearText: Boolean = false) { - val manager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager + val manager = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + this.getSystemService(InputMethodManager::class.java) + } else { + this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager + } manager.hideSoftInputFromWindow(binding.searchInput.windowToken, 0) if (clearText) { binding.searchInput.text?.clear() |