Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify initial app setup with a SessionConfiguration #24

Merged
merged 4 commits into from
Mar 22, 2024
Merged

Conversation

jayohms
Copy link
Contributor

@jayohms jayohms commented Mar 22, 2024

The PR introduces a handful of changes with the goal of reducing the complexity of starting a new app. It allows an app setup to take place across fewer files and removes the need to subclass SessionNavHostFragment for every Session instance in an app.

The HotwireActivity interface has been updated to require the app provide a list of SessionConfiguration instances, which maps to SessionNavHostFragment instances present in the Activity.

Now, to spin up a new app all that's required is to update the activity_main.xml file to include a SessionNavHostFragment in the layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".main.MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/main_nav_host"
        android:name="dev.hotwire.core.navigation.session.SessionNavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="false" />

</androidx.constraintlayout.widget.ConstraintLayout>

And then implement the HotwireActivity interface in your main Activity:

class MainActivity : AppCompatActivity(), HotwireActivity {
    override val delegate by lazy { HotwireActivityDelegate(this) }
    override val appCompatActivity = this

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    override fun sessionConfigurations() = listOf(
        SessionConfiguration(
            name = "main",
            startLocation = Urls.homeUrl,
            navHostFragmentId = R.id.main_nav_host
        )
    )
}

That's it!

This PR also:

  • Removes the Hotwire.appUrl configuration concept. It was flawed, in that each session url should be independent from each other and a global app url would prevent sessions with independent sub/domains.
  • Updates the Route interface to look at the SessionConfiguration instance when determine if a route matches, instead of the global Hotwire.appUrl.
  • Automatically registers all SessionNavHostFragment instances with the HotwireActivityDelegate when they're inflated/created. This previously required manual registration if you had more than one SessionNavHostFragment instance.

@jayohms jayohms changed the title Introduce a SessionConfiguration Simplify initial app setup with a SessionConfiguration Mar 22, 2024
@jayohms jayohms merged commit 9beea0b into main Mar 22, 2024
@jayohms jayohms deleted the session-config branch March 22, 2024 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant