aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIván Ávalos <avalos@disroot.org>2022-05-20 22:27:59 -0500
committerIván Ávalos <avalos@disroot.org>2022-05-20 22:27:59 -0500
commit22d2b3ed3569925fc3835dd5b53c6aacde86278a (patch)
tree44668ecd692f5c6c26e2a2f98a1692f4b1b69cb5
parented7d22c35d6ddea39284a31e751a2cc9f86332f3 (diff)
downloadetbsa-trackermap-mobile-22d2b3ed3569925fc3835dd5b53c6aacde86278a.tar.gz
etbsa-trackermap-mobile-22d2b3ed3569925fc3835dd5b53c6aacde86278a.tar.bz2
etbsa-trackermap-mobile-22d2b3ed3569925fc3835dd5b53c6aacde86278a.zip
[android] Improved search performance by limiting rate
-rw-r--r--androidApp/build.gradle.kts4
-rw-r--r--androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsActivity.kt38
2 files changed, 32 insertions, 10 deletions
diff --git a/androidApp/build.gradle.kts b/androidApp/build.gradle.kts
index 5bb488c..df33576 100644
--- a/androidApp/build.gradle.kts
+++ b/androidApp/build.gradle.kts
@@ -44,9 +44,9 @@ val googleImplementation by configurations
dependencies {
implementation(project(":shared"))
- implementation("com.google.android.material:material:1.5.0")
+ implementation("com.google.android.material:material:1.6.0")
implementation("androidx.appcompat:appcompat:1.4.1")
- implementation("androidx.constraintlayout:constraintlayout:2.1.3")
+ implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("com.squareup.okhttp3:okhttp:4.9.1")
implementation("com.github.Zhuinden:live-event:1.2.0")
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1")
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()