Skip to content

Commit

Permalink
optimize navigation code
Browse files Browse the repository at this point in the history
  • Loading branch information
Jnnshschl committed Dec 30, 2020
1 parent a710c2c commit d1ff6ba
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 148 deletions.
21 changes: 0 additions & 21 deletions app/src/main/java/de/jnns/bmsmonitor/BatteryFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
21 changes: 0 additions & 21 deletions app/src/main/java/de/jnns/bmsmonitor/BikeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
17 changes: 16 additions & 1 deletion app/src/main/java/de/jnns/bmsmonitor/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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()
}
23 changes: 0 additions & 23 deletions app/src/main/java/de/jnns/bmsmonitor/SettingsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
21 changes: 0 additions & 21 deletions app/src/main/java/de/jnns/bmsmonitor/StatsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 12 additions & 12 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,6 @@

</androidx.appcompat.widget.Toolbar>

<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/mainToolbar"
app:navGraph="@navigation/nav_graph" />

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigation"
android:layout_width="0dp"
Expand All @@ -86,4 +74,16 @@
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/main_navigation_menu" />

<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/mainToolbar"
app:navGraph="@navigation/nav_graph" />

</androidx.constraintlayout.widget.ConstraintLayout>
62 changes: 13 additions & 49 deletions app/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">

<fragment
android:id="@+id/batteryFragment"
android:id="@+id/navigation_battery"
android:name="de.jnns.bmsmonitor.BatteryFragment"
android:label="fragment_battery"
tools:layout="@layout/fragment_battery">
<action
android:id="@+id/action_batteryFragment_to_settingsFragment"
app:destination="@id/settingsFragment" />
<action
android:id="@+id/action_batteryFragment_to_bikeFragment"
app:destination="@id/bikeFragment" />
<action
android:id="@+id/action_batteryFragment_to_statsFragment"
app:destination="@id/statsFragment" />
</fragment>
tools:layout="@layout/fragment_battery" />

<fragment
android:id="@+id/settingsFragment"
android:id="@+id/navigation_settings"
android:name="de.jnns.bmsmonitor.SettingsFragment"
android:label="fragment_settings"
tools:layout="@layout/fragment_settings">
<action
android:id="@+id/action_settingsFragment_to_batteryFragment"
app:destination="@id/batteryFragment" />
<action
android:id="@+id/action_settingsFragment_to_bikeFragment"
app:destination="@id/bikeFragment" />
<action
android:id="@+id/action_settingsFragment_to_statsFragment"
app:destination="@id/statsFragment" />
</fragment>
tools:layout="@layout/fragment_settings" />

<fragment
android:id="@+id/bikeFragment"
android:id="@+id/navigation_bike"
android:name="de.jnns.bmsmonitor.BikeFragment"
android:label="fragment_bike"
tools:layout="@layout/fragment_bike">
<action
android:id="@+id/action_bikeFragment_to_batteryFragment"
app:destination="@id/batteryFragment" />
<action
android:id="@+id/action_bikeFragment_to_settingsFragment"
app:destination="@id/settingsFragment" />
<action
android:id="@+id/action_bikeFragment_to_statsFragment"
app:destination="@id/statsFragment" />
</fragment>
tools:layout="@layout/fragment_bike" />

<fragment
android:id="@+id/statsFragment"
android:id="@+id/navigation_stats"
android:name="de.jnns.bmsmonitor.StatsFragment"
android:label="fragment_stats"
tools:layout="@layout/fragment_stats" >
<action
android:id="@+id/action_statsFragment_to_settingsFragment"
app:destination="@id/settingsFragment" />
<action
android:id="@+id/action_statsFragment_to_bikeFragment"
app:destination="@id/bikeFragment" />
<action
android:id="@+id/action_statsFragment_to_batteryFragment"
app:destination="@id/batteryFragment" />
</fragment>
tools:layout="@layout/fragment_stats" />
</navigation>

0 comments on commit d1ff6ba

Please sign in to comment.