navigation: use global action for home/settings

This really does not feel right...
This commit is contained in:
dece 2022-01-20 00:16:27 +01:00
parent 6c556c3035
commit 9358c0abca
6 changed files with 49 additions and 10 deletions

View file

@ -4,20 +4,37 @@ import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.google.android.material.navigation.NavigationView
import dev.lowrespalmtree.comet.databinding.ActivityMainBinding
import kotlinx.coroutines.ExperimentalCoroutinesApi
@ExperimentalCoroutinesApi
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private var navHost: NavHostFragment? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
Database.init(applicationContext) // TODO move to App Startup?
supportFragmentManager.findFragmentById(R.id.nav_host_fragment)?.also { navHost ->
findViewById<NavigationView>(R.id.drawer_navigation)?.apply {
setupWithNavController((navHost as NavHostFragment).navController)
}
}
navHost = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment?
navHost?.also { binding.drawerNavigation.setupWithNavController(it.navController) }
}
fun goHome() {
navHost?.navController?.navigate(R.id.action_global_pageViewFragment)
binding.drawerLayout.closeDrawers()
}
fun openHistory() {
binding.drawerLayout.closeDrawers()
// TODO
}
fun openSettings() {
navHost?.navController?.navigate(R.id.action_global_settingsFragment)
binding.drawerLayout.closeDrawers()
}
}

View file

@ -77,7 +77,7 @@ class PageViewFragment : Fragment(), ContentAdapter.ContentAdapterListener {
private fun onBackPressed() {
if (visitedUrls.size >= 2) {
visitedUrls.removeLastOrNull() // Always remove current page first.
openUrl(visitedUrls.removeLastOrNull()!!)
visitedUrls.removeLastOrNull()?.also { openUrl(it) }
}
}

View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawer_layout"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"

View file

@ -1,11 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/home"
android:icon="@android:drawable/ic_menu_myplaces"
android:title="@string/home"
android:onClick="goHome" />
<item
android:id="@+id/history"
android:icon="@android:drawable/ic_menu_recent_history"
android:title="@string/history" />
<item
android:id="@+id/settingsFragment"
android:id="@+id/settings"
android:icon="@android:drawable/ic_menu_preferences"
android:title="@string/settings" />
android:title="@string/settings"
android:onClick="openSettings" />
</menu>

View file

@ -8,8 +8,22 @@
android:id="@+id/pageViewFragment"
android:name="dev.lowrespalmtree.comet.PageViewFragment"
android:label="PageViewFragment" />
<action
android:id="@+id/action_global_pageViewFragment"
app:destination="@id/pageViewFragment"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim" />
<fragment
android:id="@+id/settingsFragment"
android:name="dev.lowrespalmtree.comet.SettingsFragment"
android:label="SettingsFragment" />
<action
android:id="@+id/action_global_settingsFragment"
app:destination="@id/settingsFragment"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim" />
</navigation>

View file

@ -27,6 +27,7 @@
<string name="attachment_summary_on">Automatically download attachments for incoming emails
</string>
<string name="attachment_summary_off">Only download attachments when manually requested</string>
<string name="home">Home</string>
</resources>