Skip to content

Commit

Permalink
Improve logging when the backstack is popped
Browse files Browse the repository at this point in the history
  • Loading branch information
jayohms committed May 3, 2022
1 parent 5dfab8e commit 32feebc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
9 changes: 5 additions & 4 deletions turbo/src/main/kotlin/dev/hotwire/turbo/nav/TurboNavRule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package dev.hotwire.turbo.nav
import android.net.Uri
import android.os.Bundle
import androidx.core.os.bundleOf
import androidx.navigation.*
import androidx.navigation.NavController
import androidx.navigation.NavDestination
import androidx.navigation.NavOptions
import androidx.navigation.fragment.FragmentNavigator
import androidx.navigation.navOptions
import dev.hotwire.turbo.config.*
import dev.hotwire.turbo.session.TurboSessionModalResult
import dev.hotwire.turbo.util.location
import dev.hotwire.turbo.visit.TurboVisitAction
import dev.hotwire.turbo.visit.TurboVisitOptions

Expand Down Expand Up @@ -131,9 +135,6 @@ internal class TurboNavRule(
}
}

private val NavBackStackEntry?.location: String?
get() = this?.arguments?.getString("location")

private fun locationsAreSame(first: String?, second: String?): Boolean {
if (first == null || second == null) {
return false
Expand Down
39 changes: 23 additions & 16 deletions turbo/src/main/kotlin/dev/hotwire/turbo/nav/TurboNavigator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.navigation.NavController
import androidx.navigation.NavOptions
import androidx.navigation.fragment.FragmentNavigator
import androidx.navigation.fragment.findNavController
import dev.hotwire.turbo.util.location
import dev.hotwire.turbo.util.logEvent
import dev.hotwire.turbo.visit.TurboVisitOptions

Expand Down Expand Up @@ -103,10 +104,10 @@ internal class TurboNavigator(private val navDestination: TurboNavDestination) {

when (rule.newPresentation) {
TurboNavPresentation.POP -> onNavigationVisit {
rule.controller.popBackStack()
popBackStack(rule)
}
TurboNavPresentation.REPLACE -> onNavigationVisit {
rule.controller.popBackStack()
popBackStack(rule)
navigateToLocation(rule)
}
TurboNavPresentation.PUSH -> onNavigationVisit {
Expand All @@ -132,7 +133,7 @@ internal class TurboNavigator(private val navDestination: TurboNavDestination) {

when (rule.newPresentation) {
TurboNavPresentation.REPLACE -> onNavigationVisit {
rule.controller.popBackStack()
popBackStack(rule)
navigateToLocation(rule)
}
else -> onNavigationVisit {
Expand All @@ -156,25 +157,31 @@ internal class TurboNavigator(private val navDestination: TurboNavDestination) {
// underlying fragment is still active and will receive the
// result immediately. This allows the modal result flow to
// behave exactly like full screen fragments.
do {
rule.controller.popBackStack()
} while (
rule.controller.currentBackStackEntry.isModalContext
)

popModalsFromBackStack(rule)
sendModalResult(rule)
} else {
sendModalResult(rule)

do {
rule.controller.popBackStack()
} while (
rule.controller.currentBackStackEntry.isModalContext
)
popModalsFromBackStack(rule)
}
}
}

private fun popModalsFromBackStack(rule: TurboNavRule) {
do {
popBackStack(rule)
} while (
rule.controller.currentBackStackEntry.isModalContext
)
}

private fun popBackStack(rule: TurboNavRule) {
logEvent(
"popFromBackStack",
"location" to rule.controller.currentBackStackEntry.location.orEmpty()
)
rule.controller.popBackStack()
}

private fun sendModalResult(rule: TurboNavRule) {
// Save the modal result with VisitOptions so it can be retrieved
// by the previous destination when the backstack is popped.
Expand Down Expand Up @@ -283,7 +290,7 @@ internal class TurboNavigator(private val navDestination: TurboNavDestination) {
private fun logEvent(event: String, vararg params: Pair<String, Any>) {
val attributes = params.toMutableList().apply {
add(0, "session" to session.sessionName)
add("fragment" to fragment.javaClass.simpleName)
add("currentFragment" to fragment.javaClass.simpleName)
}
logEvent(event, attributes)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.animation.ValueAnimator
import android.content.Context
import android.os.Handler
import android.webkit.WebResourceRequest
import androidx.navigation.NavBackStackEntry
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
Expand Down Expand Up @@ -38,6 +39,9 @@ internal fun File.deleteAllFilesInDirectory() {
}
}

internal val NavBackStackEntry?.location: String?
get() = this?.arguments?.getString("location")

internal fun WebResourceRequest.isHttpGetRequest(): Boolean {
return method.equals("GET", ignoreCase = true) &&
url.scheme?.startsWith("HTTP", ignoreCase = true) == true
Expand Down

0 comments on commit 32feebc

Please sign in to comment.