diff options
author | Iván Ávalos <avalos@disroot.org> | 2022-06-08 23:14:35 -0500 |
---|---|---|
committer | Iván Ávalos <avalos@disroot.org> | 2022-06-08 23:14:35 -0500 |
commit | d7790a50599f94422df6851d240e3993fb71aab7 (patch) | |
tree | 292d603264017b5e11644e77b4ef137f27eb77d0 /androidApp/src/main | |
parent | fc3e2a6e316cb8acd6aac0553c92f9dbc6b669b4 (diff) | |
parent | ec157026deea7b72ff233a20c0ed40fa548f75d6 (diff) | |
download | etbsa-trackermap-mobile-d7790a50599f94422df6851d240e3993fb71aab7.tar.gz etbsa-trackermap-mobile-d7790a50599f94422df6851d240e3993fb71aab7.tar.bz2 etbsa-trackermap-mobile-d7790a50599f94422df6851d240e3993fb71aab7.zip |
Merge branch 'main' of https://git.sr.ht/~avalos/trackermap-mobile
Diffstat (limited to 'androidApp/src/main')
3 files changed, 48 insertions, 12 deletions
diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesFragment.kt index e5143cb..6e1b1c6 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesFragment.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesFragment.kt @@ -90,7 +90,7 @@ class DevicesFragment : Fragment() { @DelicateCoroutinesApi private fun setupObservers() { Log.d("DevicesFragment", "setupObservers()") - unitsViewModel.units.observe(viewLifecycleOwner) { units -> + unitsViewModel.displayedUnits.observe(viewLifecycleOwner) { units -> Log.d("DevicesFragment", "Success $units") binding.infoLoading.root.visibility = View.GONE (binding.devicesList.adapter as DevicesAdapter).setData(units) 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() diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsViewModel.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsViewModel.kt index 174d55e..b537c0b 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsViewModel.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsViewModel.kt @@ -54,6 +54,7 @@ class UnitsViewModel( private var _searchQuery = savedStateHandle.getLiveData("searchQuery", "") private var _unitsDisplayMode = MutableLiveData(UnitsDisplayMode.MAP) private var _units = MutableLiveData<List<UnitInformation>>() + private var _displayedUnits = MutableLiveData<List<UnitInformation>>() private var _oldSelectedUnit = MutableLiveData<UnitInformation?>() private var _selectedUnit = MutableLiveData<UnitInformation?>() private var _mapLayerType = MutableLiveData<MapLayer.Type>() @@ -63,6 +64,7 @@ class UnitsViewModel( val searchQuery: LiveData<String> get() = _searchQuery val unitsDisplayMode: LiveData<UnitsDisplayMode> get() = _unitsDisplayMode val units: LiveData<List<UnitInformation>> get() = _units + val displayedUnits: LiveData<List<UnitInformation>> get() = _displayedUnits val oldSelectedUnit: LiveData<UnitInformation?> get() = _oldSelectedUnit val selectedUnit: LiveData<UnitInformation?> get() = _selectedUnit val mapLayerType: LiveData<MapLayer.Type> get() = _mapLayerType @@ -73,16 +75,27 @@ class UnitsViewModel( Log.d("UnitsViewModel", "Initializing Units View Model") unitsController.fetchUnits(viewModelScope) viewModelScope.launch { - setupObservers() + setupUnitsObserver() } viewModelScope.launch { + setupDisplayedUnitsObserver() + } + + viewModelScope.launch { setupGeofenceObserver() } } - private suspend fun setupObservers() { - Log.d("UnitsViewModel", "Setup observers") + private suspend fun setupUnitsObserver() { unitsController.displayedUnitsFlow.collect { units -> + Log.d("UnitsViewModel", "Collecting units") + this._displayedUnits.value = units + } + } + + private suspend fun setupDisplayedUnitsObserver() { + unitsController.unitsFlow.collect { units -> + Log.d("UnitsViewModel", "Collecting displayed units") this._units.value = units updateSelectedUnit() } @@ -90,6 +103,7 @@ class UnitsViewModel( private suspend fun setupGeofenceObserver() { geofenceController.geofencesFlow.collect { + Log.d("UnitsViewModel", "Collecting geofences") this._geofences.postValue(it) } } |