diff options
Diffstat (limited to 'androidApp/src/main/java/mx/trackermap/TrackerMap/android')
-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() |