diff options
author | Iván Ávalos <avalos@disroot.org> | 2022-01-17 02:48:40 -0600 |
---|---|---|
committer | Iván Ávalos <avalos@disroot.org> | 2022-01-17 02:48:40 -0600 |
commit | 7ca48c7590c4c9702c058f3cecdbf35f618d7d80 (patch) | |
tree | 50fc5a632fd69d725cfccf512a58d113694415d3 | |
parent | 6ed66ecf8444eca49df163de4916391733d29f57 (diff) | |
download | etbsa-trackermap-mobile-7ca48c7590c4c9702c058f3cecdbf35f618d7d80.tar.gz etbsa-trackermap-mobile-7ca48c7590c4c9702c058f3cecdbf35f618d7d80.tar.bz2 etbsa-trackermap-mobile-7ca48c7590c4c9702c058f3cecdbf35f618d7d80.zip |
Improved observer management
3 files changed, 51 insertions, 3 deletions
diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/commands/UnitCommandsFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/commands/UnitCommandsFragment.kt index cd8d7af..9d12608 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/commands/UnitCommandsFragment.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/commands/UnitCommandsFragment.kt @@ -42,10 +42,21 @@ class UnitCommandsFragment: Fragment() { super.onViewCreated(view, savedInstanceState) binding.reportLoading.root.visibility = View.VISIBLE - setupObservers() setupEvents() } + override fun onResume() { + super.onResume() + + setupObservers() + } + + override fun onStop() { + super.onStop() + + removeObservers() + } + override fun onDestroyView() { super.onDestroyView() _binding = null @@ -86,6 +97,10 @@ class UnitCommandsFragment: Fragment() { unitCommandsViewModel.fetchCommands(id) } + private fun removeObservers() { + unitCommandsViewModel.commands.removeObservers(viewLifecycleOwner) + } + private fun setupEvents() { binding.commandsList.setOnItemClickListener { _, _, index, _ -> Log.d("UnitCommandsFragment", "Selected item at index - $index") diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/information/UnitInformationFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/information/UnitInformationFragment.kt index ab94547..f046ccc 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/information/UnitInformationFragment.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/information/UnitInformationFragment.kt @@ -47,10 +47,21 @@ class UnitInformationFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - setupObservers() fetchInformation() } + override fun onResume() { + super.onResume() + + setupObservers() + } + + override fun onStop() { + super.onStop() + + removeObservers() + } + override fun onDestroyView() { super.onDestroyView() _binding = null @@ -66,6 +77,10 @@ class UnitInformationFragment : Fragment() { } } + private fun removeObservers() { + unitInformationViewModel.unit.removeObservers(viewLifecycleOwner) + } + private fun fetchInformation() { val id = arguments?.getInt(UnitDetailsAdapter.DEVICE_ID_ARG) val name = arguments?.getString(UnitDetailsAdapter.DEVICE_NAME_ARG) diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsFragment.kt index d3e008f..600cfe1 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsFragment.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsFragment.kt @@ -60,17 +60,29 @@ class UnitReportsFragment : Fragment() { unitReportsViewModel.setDeviceId( arguments?.getInt(UnitDetailsAdapter.DEVICE_ID_ARG) ?: 0) - initializeMap() setupEvents() + initializeMap() + } + + override fun onResume() { + super.onResume() + setupObservers() } + override fun onStop() { + super.onStop() + + removeObservers() + } + override fun onDestroyView() { super.onDestroyView() _binding = null } private fun initializeMap() { + Log.d("UnitReportsFragment", "initializeMap()") childFragmentManager.commit { replace(R.id.reportsMapContainer, reportsMapFragment) } @@ -140,6 +152,11 @@ class UnitReportsFragment : Fragment() { } } + private fun removeObservers() { + unitReportsViewModel.report.removeObservers(viewLifecycleOwner) + unitReportsViewModel.reportPeriod.removeObservers(viewLifecycleOwner) + } + private fun showPeriodPopUp(view: View) { val popOver = PopupMenu(context, view) popOver.menuInflater.inflate(R.menu.report_period_options, popOver.menu) @@ -262,6 +279,7 @@ class UnitReportsFragment : Fragment() { } private fun showMap(shouldShowMap: Boolean) { + Log.d("UnitReportsFragment", "showMap($shouldShowMap)") binding.reportLoading.root.visibility = View.GONE binding.eventsScroll.visibility = if (shouldShowMap) View.GONE else View.VISIBLE binding.reportsMapContainer.visibility = if (shouldShowMap) View.VISIBLE else View.GONE |