From d1ff6ba1487e11c3d40daf84e6482d36b1b43ac8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannis=20H=C3=B6schele?= Date: Wed, 30 Dec 2020 17:29:17 +0100 Subject: [PATCH] optimize navigation code --- .../de/jnns/bmsmonitor/BatteryFragment.kt | 21 ------- .../java/de/jnns/bmsmonitor/BikeFragment.kt | 21 ------- .../java/de/jnns/bmsmonitor/MainActivity.kt | 17 ++++- .../de/jnns/bmsmonitor/SettingsFragment.kt | 23 ------- .../java/de/jnns/bmsmonitor/StatsFragment.kt | 21 ------- app/src/main/res/layout/activity_main.xml | 24 +++---- app/src/main/res/navigation/nav_graph.xml | 62 ++++--------------- 7 files changed, 41 insertions(+), 148 deletions(-) diff --git a/app/src/main/java/de/jnns/bmsmonitor/BatteryFragment.kt b/app/src/main/java/de/jnns/bmsmonitor/BatteryFragment.kt index daff1c1..eda9f35 100644 --- a/app/src/main/java/de/jnns/bmsmonitor/BatteryFragment.kt +++ b/app/src/main/java/de/jnns/bmsmonitor/BatteryFragment.kt @@ -81,27 +81,6 @@ class BatteryFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - (requireActivity() as MainActivity).binding.bottomNavigation.setOnNavigationItemSelectedListener { item -> - when (item.itemId) { - R.id.page_bike -> { - requireActivity().title = getString(R.string.app_name) - requireActivity().findNavController(R.id.nav_host_fragment).navigate(R.id.action_batteryFragment_to_bikeFragment) - true - } - R.id.page_settings -> { - requireActivity().title = getString(R.string.appNameSettings) - requireActivity().findNavController(R.id.nav_host_fragment).navigate(R.id.action_batteryFragment_to_settingsFragment) - true - } - R.id.page_stats -> { - requireActivity().title = getString(R.string.appNameStats) - requireActivity().findNavController(R.id.nav_host_fragment).navigate(R.id.action_batteryFragment_to_statsFragment) - true - } - else -> false - } - } - binding.speedViewSpeed.clearSections() binding.speedViewSpeed.addSections( Section(0.00000000f, 0.11111111f, ContextCompat.getColor(requireContext(), R.color.batteryChargeHigh), 72.0f), diff --git a/app/src/main/java/de/jnns/bmsmonitor/BikeFragment.kt b/app/src/main/java/de/jnns/bmsmonitor/BikeFragment.kt index e474775..06cb7b2 100644 --- a/app/src/main/java/de/jnns/bmsmonitor/BikeFragment.kt +++ b/app/src/main/java/de/jnns/bmsmonitor/BikeFragment.kt @@ -51,27 +51,6 @@ class BikeFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - (requireActivity() as MainActivity).binding.bottomNavigation.setOnNavigationItemSelectedListener { item -> - when (item.itemId) { - R.id.page_battery -> { - requireActivity().title = getString(R.string.app_name) - requireActivity().findNavController(R.id.nav_host_fragment).navigate(R.id.action_bikeFragment_to_batteryFragment) - true - } - R.id.page_settings -> { - requireActivity().title = getString(R.string.appNameSettings) - requireActivity().findNavController(R.id.nav_host_fragment).navigate(R.id.action_bikeFragment_to_settingsFragment) - true - } - R.id.page_stats -> { - requireActivity().title = getString(R.string.appNameStats) - requireActivity().findNavController(R.id.nav_host_fragment).navigate(R.id.action_bikeFragment_to_statsFragment) - true - } - else -> false - } - } - binding.speedViewSpeed.clearSections() binding.speedViewSpeed.addSections( Section(0.0f, 0.2f, ContextCompat.getColor(requireContext(), R.color.white), 72.0f), diff --git a/app/src/main/java/de/jnns/bmsmonitor/MainActivity.kt b/app/src/main/java/de/jnns/bmsmonitor/MainActivity.kt index 8fdd58d..f84ae65 100644 --- a/app/src/main/java/de/jnns/bmsmonitor/MainActivity.kt +++ b/app/src/main/java/de/jnns/bmsmonitor/MainActivity.kt @@ -49,6 +49,21 @@ class MainActivity : AppCompatActivity() { Intent(this, BleService::class.java).also { intent -> startService(intent) } } + + val batteryFragment = BatteryFragment() + val bikeFragment = BikeFragment() + val statsFragment = StatsFragment() + val settingsFragment = SettingsFragment() + + binding.bottomNavigation.setOnNavigationItemSelectedListener { + when (it.itemId) { + R.id.page_battery -> supportFragmentManager.beginTransaction().apply { replace(R.id.nav_host_fragment, batteryFragment).commit() } + R.id.page_bike -> supportFragmentManager.beginTransaction().apply { replace(R.id.nav_host_fragment, bikeFragment).commit() } + R.id.page_stats -> supportFragmentManager.beginTransaction().apply { replace(R.id.nav_host_fragment, statsFragment).commit() } + R.id.page_settings -> supportFragmentManager.beginTransaction().apply { replace(R.id.nav_host_fragment, settingsFragment).commit() } + } + true + } } override fun onDestroy() { @@ -70,5 +85,5 @@ class MainActivity : AppCompatActivity() { } } - override fun onSupportNavigateUp(): Boolean = findNavController(R.id.nav_host_fragment).navigateUp() + override fun onSupportNavigateUp(): Boolean = findNavController(R.id.nav_host_fragment).navigateUp() || super.onSupportNavigateUp() } \ No newline at end of file diff --git a/app/src/main/java/de/jnns/bmsmonitor/SettingsFragment.kt b/app/src/main/java/de/jnns/bmsmonitor/SettingsFragment.kt index f984a4f..f4f30cd 100644 --- a/app/src/main/java/de/jnns/bmsmonitor/SettingsFragment.kt +++ b/app/src/main/java/de/jnns/bmsmonitor/SettingsFragment.kt @@ -28,28 +28,5 @@ class SettingsFragment : PreferenceFragmentCompat() { bleAddresses.add("0") btPreference.entryValues = bleAddresses.toTypedArray() btPreferenceBike.entryValues = bleAddresses.toTypedArray() - - val mainActivity = requireActivity() as MainActivity - - mainActivity.binding.bottomNavigation.setOnNavigationItemSelectedListener { item -> - when (item.itemId) { - R.id.page_battery -> { - requireActivity().title = getString(R.string.appNameSettings) - requireActivity().findNavController(R.id.nav_host_fragment).navigate(R.id.action_settingsFragment_to_batteryFragment) - true - } - R.id.page_bike -> { - requireActivity().title = getString(R.string.app_name) - requireActivity().findNavController(R.id.nav_host_fragment).navigate(R.id.action_settingsFragment_to_bikeFragment) - true - } - R.id.page_stats -> { - requireActivity().title = getString(R.string.appNameStats) - requireActivity().findNavController(R.id.nav_host_fragment).navigate(R.id.action_settingsFragment_to_statsFragment) - true - } - else -> false - } - } } } \ No newline at end of file diff --git a/app/src/main/java/de/jnns/bmsmonitor/StatsFragment.kt b/app/src/main/java/de/jnns/bmsmonitor/StatsFragment.kt index 7d41e90..6b834d7 100644 --- a/app/src/main/java/de/jnns/bmsmonitor/StatsFragment.kt +++ b/app/src/main/java/de/jnns/bmsmonitor/StatsFragment.kt @@ -57,27 +57,6 @@ class StatsFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - (requireActivity() as MainActivity).binding.bottomNavigation.setOnNavigationItemSelectedListener { item -> - when (item.itemId) { - R.id.page_battery -> { - requireActivity().title = getString(R.string.appNameSettings) - requireActivity().findNavController(R.id.nav_host_fragment).navigate(R.id.action_statsFragment_to_batteryFragment) - true - } - R.id.page_bike -> { - requireActivity().title = getString(R.string.app_name) - requireActivity().findNavController(R.id.nav_host_fragment).navigate(R.id.action_statsFragment_to_bikeFragment) - true - } - R.id.page_settings -> { - requireActivity().title = getString(R.string.appNameStats) - requireActivity().findNavController(R.id.nav_host_fragment).navigate(R.id.action_statsFragment_to_settingsFragment) - true - } - else -> false - } - } - configureLineChart(binding.linechartVoltage, 2) configureLineChart(binding.linechartCellVoltage, 3) configureLineChart(binding.linechartPower, 0) diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 218d256..5c26e5f 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -61,18 +61,6 @@ - - + + \ No newline at end of file diff --git a/app/src/main/res/navigation/nav_graph.xml b/app/src/main/res/navigation/nav_graph.xml index 39d6ae5..970cb83 100644 --- a/app/src/main/res/navigation/nav_graph.xml +++ b/app/src/main/res/navigation/nav_graph.xml @@ -3,65 +3,29 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/nav_graph" - app:startDestination="@id/batteryFragment"> + app:startDestination="@id/navigation_battery"> + - - - - + tools:layout="@layout/fragment_battery" /> + - - - - + tools:layout="@layout/fragment_settings" /> + - - - - + tools:layout="@layout/fragment_bike" /> + - - - - + tools:layout="@layout/fragment_stats" /> \ No newline at end of file