Skip to content

Commit

Permalink
Stop using NavigationUI.setupWithNavController() for toolbar setup. I…
Browse files Browse the repository at this point in the history
…t's visually buggy and doesn't allow the back button to display unless the start destination is a unique fragment from other destinations.
  • Loading branch information
jayohms committed Feb 24, 2024
1 parent 262fef1 commit 403e1e6
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import dev.hotwire.turbo.demo.R
import dev.hotwire.turbo.fragments.TurboFragment
import dev.hotwire.turbo.nav.TurboNavGraphDestination
import com.bumptech.glide.Glide
import dev.hotwire.turbo.demo.util.displayBackButtonAsCloseIcon
import dev.hotwire.turbo.util.displayBackButtonAsCloseIcon

@TurboNavGraphDestination(uri = "turbo://fragment/image_viewer")
class ImageViewerFragment : TurboFragment(), NavDestination {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
package dev.hotwire.turbo.demo.features.web

import android.os.Bundle
import android.view.View
import dev.hotwire.turbo.demo.util.displayBackButtonAsCloseIcon
import dev.hotwire.turbo.nav.TurboNavGraphDestination

@TurboNavGraphDestination(uri = "turbo://fragment/web/modal")
class WebModalFragment : WebFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initToolbar()
}

private fun initToolbar() {
toolbarForNavigation()?.displayBackButtonAsCloseIcon()
}
}
class WebModalFragment : WebFragment()
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ import dev.hotwire.turbo.demo.strada.bridgeComponentFactories
val TurboPathConfigurationProperties.description: String?
get() = get("description")

fun Toolbar.displayBackButtonAsCloseIcon() {
navigationIcon = ContextCompat.getDrawable(context, R.drawable.ic_close)
}

@Suppress("DEPRECATION")
fun WebView.initDayNightTheme() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import dev.hotwire.turbo.nav.TurboNavDestination
import dev.hotwire.turbo.nav.TurboNavigator
import dev.hotwire.turbo.session.TurboSessionModalResult
import dev.hotwire.turbo.session.TurboSessionViewModel
import dev.hotwire.turbo.util.displayBackButton
import dev.hotwire.turbo.util.displayBackButtonAsCloseIcon
import dev.hotwire.turbo.util.logEvent

/**
Expand Down Expand Up @@ -95,7 +97,14 @@ class TurboFragmentDelegate(private val navDestination: TurboNavDestination) {

private fun initToolbar() {
navDestination.toolbarForNavigation()?.let {
NavigationUI.setupWithNavController(it, fragment.findNavController())
if (!navigator.isAtStartDestination()) {
if (navDestination.isModal) {
it.displayBackButtonAsCloseIcon()
} else {
it.displayBackButton()
}
}

it.setNavigationOnClickListener {
navDestination.navigateUp()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.navigation.navOptions
import androidx.navigation.ui.R
import dev.hotwire.turbo.config.TurboPathConfiguration
import dev.hotwire.turbo.config.TurboPathConfigurationProperties
import dev.hotwire.turbo.config.context
import dev.hotwire.turbo.delegates.TurboFragmentDelegate
import dev.hotwire.turbo.delegates.TurboNestedFragmentDelegate
import dev.hotwire.turbo.fragments.TurboFragment
Expand Down Expand Up @@ -81,6 +82,12 @@ interface TurboNavDestination {
val isActive: Boolean
get() = fragment.isAdded && !fragment.isDetached

/**
* Specifies whether the destination was presented in a modal context.
*/
val isModal: Boolean
get() = pathProperties.context == TurboNavPresentationContext.MODAL

/**
* Gets the delegate instance that handles the Fragment's lifecycle events.
*/
Expand Down
8 changes: 4 additions & 4 deletions turbo/src/main/kotlin/dev/hotwire/turbo/nav/TurboNavigator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ internal class TurboNavigator(private val navDestination: TurboNavDestination) {
onReady()
}

fun isAtStartDestination(): Boolean {
return currentController().previousBackStackEntry == null
}

fun navigateUp() {
onNavigationVisit {
if (fragment is DialogFragment) {
Expand Down Expand Up @@ -267,10 +271,6 @@ internal class TurboNavigator(private val navDestination: TurboNavDestination) {
return navDestination.navHostForNavigation(location).navController
}

private fun isAtStartDestination(): Boolean {
return currentController().previousBackStackEntry == null
}

private fun shouldNavigate(location: String): Boolean {
val shouldNavigate = navDestination.shouldNavigateTo(location)

Expand Down
12 changes: 12 additions & 0 deletions turbo/src/main/kotlin/dev/hotwire/turbo/util/TurboExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,26 @@ import android.animation.ValueAnimator
import android.content.Context
import android.os.Handler
import android.webkit.WebResourceRequest
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat
import androidx.navigation.NavBackStackEntry
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import dev.hotwire.turbo.R
import dev.hotwire.turbo.visit.TurboVisitAction
import dev.hotwire.turbo.visit.TurboVisitActionAdapter
import java.io.File

fun Toolbar.displayBackButton() {
navigationIcon = ContextCompat.getDrawable(context, R.drawable.ic_back)
}

fun Toolbar.displayBackButtonAsCloseIcon() {
navigationIcon = ContextCompat.getDrawable(context, R.drawable.ic_close)
}


internal fun Context.runOnUiThread(func: () -> Unit) {
when (mainLooper.isCurrentThread) {
true -> func()
Expand Down
9 changes: 9 additions & 0 deletions turbo/src/main/res/drawable/ic_back.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF000000"
android:pathData="M19,11H7.83l4.88,-4.88c0.39,-0.39 0.39,-1.03 0,-1.42 -0.39,-0.39 -1.02,-0.39 -1.41,0l-6.59,6.59c-0.39,0.39 -0.39,1.02 0,1.41l6.59,6.59c0.39,0.39 1.02,0.39 1.41,0 0.39,-0.39 0.39,-1.02 0,-1.41L7.83,13H19c0.55,0 1,-0.45 1,-1s-0.45,-1 -1,-1z"/>
</vector>
9 changes: 9 additions & 0 deletions turbo/src/main/res/drawable/ic_close.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF000000"
android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12 19,6.41z"/>
</vector>

0 comments on commit 403e1e6

Please sign in to comment.