1- package dev.hotwire.core.turbo.delegates
1+ package dev.hotwire.core.navigation.activities
22
33import android.os.Bundle
44import androidx.activity.OnBackPressedCallback
55import androidx.annotation.IdRes
6- import androidx.appcompat.app.AppCompatActivity
76import androidx.fragment.app.Fragment
87import androidx.navigation.NavController
8+ import dev.hotwire.core.navigation.session.SessionConfiguration
9+ import dev.hotwire.core.navigation.session.SessionNavHostFragment
910import dev.hotwire.core.turbo.nav.HotwireNavDestination
1011import dev.hotwire.core.turbo.observers.HotwireActivityObserver
11- import dev.hotwire.core.turbo.session.SessionNavHostFragment
1212import dev.hotwire.core.turbo.visit.VisitOptions
1313
1414/* *
15- * Initializes the Activity for Turbo navigation and provides all the hooks for an
16- * Activity to communicate with Turbo (and vice versa).
15+ * Initializes the Activity for Hotwire navigation and provides all the hooks for an
16+ * Activity to communicate with Hotwire Native (and vice versa).
1717 *
1818 * @property activity The Activity to bind this delegate to.
1919 * @property currentNavHostFragmentId The resource ID of the [SessionNavHostFragment]
2020 * instance hosted in your Activity's layout resource.
2121 */
2222@Suppress(" unused" , " MemberVisibilityCanBePrivate" )
23- class HotwireActivityDelegate (
24- val activity : AppCompatActivity ,
25- currentNavHostFragmentId : Int
26- ) {
23+ class HotwireActivityDelegate (val activity : HotwireActivity ) {
24+ private val appCompatActivity = activity.appCompatActivity
2725 private val navHostFragments = mutableMapOf<Int , SessionNavHostFragment >()
2826
2927 private val onBackPressedCallback = object : OnBackPressedCallback (enabled = true ) {
@@ -32,22 +30,28 @@ class HotwireActivityDelegate(
3230 }
3331 }
3432
35- /* *
36- * Gets or sets the currently active resource ID of the [SessionNavHostFragment]
37- * instance hosted in your Activity's layout resource. If you use multiple nav host
38- * fragments in your app (such as for bottom tabs), you must update this whenever
39- * the currently active nav host fragment changes.
40- */
41- var currentNavHostFragmentId = currentNavHostFragmentId
33+ private var currentNavHostFragmentId = activity.sessionConfigurations().first().navHostFragmentId
4234 set(value) {
4335 field = value
44- updateOnBackPressedCallback(currentSessionNavHostFragment .navController)
36+ updateOnBackPressedCallback(currentNavHostFragment .navController)
4537 }
4638
39+ /* *
40+ * Initializes the Activity with a BackPressedDispatcher that properly
41+ * handles Fragment navigation with the back button.
42+ */
43+ init {
44+ appCompatActivity.lifecycle.addObserver(HotwireActivityObserver ())
45+ appCompatActivity.onBackPressedDispatcher.addCallback(
46+ owner = appCompatActivity,
47+ onBackPressedCallback = onBackPressedCallback
48+ )
49+ }
50+
4751 /* *
4852 * Gets the Activity's currently active [SessionNavHostFragment].
4953 */
50- val currentSessionNavHostFragment : SessionNavHostFragment
54+ val currentNavHostFragment : SessionNavHostFragment
5155 get() = navHostFragment(currentNavHostFragmentId)
5256
5357 /* *
@@ -58,31 +62,25 @@ class HotwireActivityDelegate(
5862 get() = currentFragment as HotwireNavDestination ?
5963
6064 /* *
61- * Registers the provided nav host fragment and initializes the
62- * Activity with a BackPressedDispatcher that properly handles Fragment
63- * navigation with the back button .
65+ * Sets the currently active session in your Activity. If you use multiple
66+ * [SessionNavHostFragment] instances in your app (such as for bottom tabs),
67+ * you must update this whenever the current session changes .
6468 */
65- init {
66- registerNavHostFragment(currentNavHostFragmentId)
67- activity.lifecycle.addObserver(HotwireActivityObserver ())
68- activity.onBackPressedDispatcher.addCallback(activity, onBackPressedCallback)
69+ fun setCurrentSession (sessionConfiguration : SessionConfiguration ) {
70+ currentNavHostFragmentId = sessionConfiguration.navHostFragmentId
6971 }
7072
71- /* *
72- * Provides the ability to register additional nav host fragments.
73- *
74- * @param navHostFragmentId
75- * @return
76- */
77- fun registerNavHostFragment (@IdRes navHostFragmentId : Int ): SessionNavHostFragment {
78- return findNavHostFragment(navHostFragmentId).also {
79- if (navHostFragments[navHostFragmentId] == null ) {
80- navHostFragments[navHostFragmentId] = it
81- listenToDestinationChanges(it.navController)
82- }
73+ internal fun registerNavHostFragment (navHostFragment : SessionNavHostFragment ) {
74+ if (navHostFragments[navHostFragment.id] == null ) {
75+ navHostFragments[navHostFragment.id] = navHostFragment
76+ listenToDestinationChanges(navHostFragment.navController)
8377 }
8478 }
8579
80+ internal fun unregisterNavHostFragment (navHostFragment : SessionNavHostFragment ) {
81+ navHostFragments.remove(navHostFragment.id)
82+ }
83+
8684 /* *
8785 * Finds the nav host fragment associated with the provided resource ID.
8886 *
@@ -163,22 +161,17 @@ class HotwireActivityDelegate(
163161 }
164162
165163 private fun updateOnBackPressedCallback (navController : NavController ) {
166- if (navController == currentSessionNavHostFragment .navController) {
164+ if (navController == currentNavHostFragment .navController) {
167165 onBackPressedCallback.isEnabled = navController.previousBackStackEntry != null
168166 }
169167 }
170168
171169 private val currentFragment: Fragment ?
172170 get() {
173- return if (currentSessionNavHostFragment .isAdded && ! currentSessionNavHostFragment .isDetached) {
174- currentSessionNavHostFragment .childFragmentManager.primaryNavigationFragment
171+ return if (currentNavHostFragment .isAdded && ! currentNavHostFragment .isDetached) {
172+ currentNavHostFragment .childFragmentManager.primaryNavigationFragment
175173 } else {
176174 null
177175 }
178176 }
179-
180- private fun findNavHostFragment (@IdRes navHostFragmentId : Int ): SessionNavHostFragment {
181- return activity.supportFragmentManager.findFragmentById(navHostFragmentId) as ? SessionNavHostFragment
182- ? : throw IllegalStateException (" No SessionNavHostFragment found with ID: $navHostFragmentId " )
183- }
184177}
0 commit comments