hotwired / turbo-android

Android framework for making Turbo native apps
MIT License
423 stars 51 forks source link

Navigation from native menu buttons or from app schemas links via delegate does not navigate to appropriate turbo fragment #205

Closed luizmota82 closed 2 years ago

luizmota82 commented 2 years ago

Problem

Trying to trigger navigation from either native menu buttons or from app schemas, through TurboActivityDelegate, does not navigate to appropriate turbo fragment as declared in the .json file

Here's my .json file:

{
  "settings": {},
  "rules": [
    {
      "patterns": [
        "/statistics"
      ],
      "properties": {
        "context": "modal",
        "uri": "turbo://fragment/web/modal",
        "pull_to_refresh_enabled": false
      }
    },
    {
      "patterns": [
        "/settings"
      ],
      "properties": {
        "context": "modal",
        "uri": "turbo://fragment/web/modal/session",
        "pull_to_refresh_enabled": false
      }
    },
    {
      "patterns": [
        ".*\/edit"
      ],
      "properties": {
        "context": "modal",
        "uri": "turbo://fragment/web/modal/close",
        "pull_to_refresh_enabled": false
      }
    },{
      "patterns": [
        "/broadcast"
      ],
      "properties": {
        "context": "modal",
        "uri": "turbo://fragment/broadcast",
        "pull_to_refresh_enabled": false
      }
    },
    {
      "patterns": [
        ".*"
      ],
      "properties": {
        "context": "default",
        "uri": "turbo://fragment/web",
        "fallback_uri": "turbo://fragment/web",
        "pull_to_refresh_enabled": true
      }
    }
  ]
}

This is my TurboSessionNavHostFragment

class EventsNavHostFragment : TurboSessionNavHostFragment() {

    companion object {
        const val DASHBOARD_URL = BuildConfig.DASHBOARD_URL
    }

    override val pathConfigurationLocation: TurboPathConfiguration.Location =
        TurboPathConfiguration.Location(
            assetFilePath = "json/dashboard_config_file.json"
        )

    override val registeredActivities: List<KClass<out AppCompatActivity>> =
        emptyList()

    override val registeredFragments: List<KClass<out Fragment>>
        get() = listOf(
            DashboardWebFragment::class,
            WebModalFragment::class,
            WebModelCloseFragment::class,
            SessionAwareWebModalFragment::class,
            WebBottomSheetFragment::class,
            BroadcastFragment::class,
        )

    override val sessionName: String = "EventsSession"

    override val startLocation: String = DASHBOARD_URL

    override fun onCreateWebView(context: Context): TurboWebView = CreatorsTurboWebView(context)

    override fun onSessionCreated() {
        super.onSessionCreated()
        val userAgent = requireContext().generateUserAgent()
        session.webView.settings.userAgentString = userAgent

        if (BuildConfig.DEBUG) {
            session.setDebugLoggingEnabled(true)
            WebView.setWebContentsDebuggingEnabled(true)
        }
    }
}

Triggering the following calls do not navigate to the appropriate fragments, it always loads on the same dashboard

delegate.navigate("${EventsNavHostFragment.DASHBOARD_URL}broadcast", TurboVisitOptions())
val options = TurboVisitOptions(action = TurboVisitAction.REPLACE)
delegate.navigate("${EventsNavHostFragment.DASHBOARD_URL}statistics", options)
delegate.navigate("${EventsNavHostFragment.DASHBOARD_URL}settings", options)
@TurboNavGraphDestination(uri = "turbo://fragment/broadcast")
class BroadcastFragment : WebFragment() {}

@TurboNavGraphDestination(uri = "turbo://fragment/web/modal")
open class WebModalFragment : WebFragment() {

Could someone please clarify what seems to be the issue here?

jayohms commented 2 years ago

Hey @luizmota82, path configuration rules need to get more specific as you get further down the file, since they cascade, like css. So, what's happening, is all urls are picking up the default configuration at the bottom of the file:

{
  "patterns": [
    ".*"
  ],
  "properties": {
    "context": "default",
    "uri": "turbo://fragment/web",
    "fallback_uri": "turbo://fragment/web",
    "pull_to_refresh_enabled": true
  }
}

Move this to the top of the file and you should be good, then. Let me know if that doesn't solve the issues you're seeing, thanks!

See: https://github.com/hotwired/turbo-android/blob/main/docs/PATH-CONFIGURATION.md#rules

luizmota82 commented 2 years ago

Worked like a charm, thank you @jayohms 🚀