Skip to content

Commit 588d69c

Browse files
authored
Merge pull request #184 from hotwired/bridge-delegate
Allow BridgeComponents to receive the destination Fragment view lifecycle events
2 parents bc5a13a + ca313be commit 588d69c

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package dev.hotwire.core.bridge
2+
3+
interface BridgeComponentFragmentLifecycle {
4+
fun onViewCreated()
5+
fun onDestroyView()
6+
}

core/src/main/kotlin/dev/hotwire/core/bridge/BridgeDelegate.kt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ class BridgeDelegate<D : BridgeDestination>(
1414
) : DefaultLifecycleObserver {
1515
internal var bridge: Bridge? = null
1616
private var destinationIsActive: Boolean = false
17-
private val initializedComponents = hashMapOf<String, BridgeComponent<D>>()
1817
private val resolvedLocation: String
1918
get() = bridge?.webView?.url ?: location
2019

20+
val initializedComponents = hashMapOf<String, BridgeComponent<D>>()
2121
val activeComponents: List<BridgeComponent<D>>
2222
get() = initializedComponents.map { it.value }.takeIf { destinationIsActive }.orEmpty()
2323

@@ -101,8 +101,20 @@ class BridgeDelegate<D : BridgeDestination>(
101101
return activeComponents.filterIsInstance<C>().firstOrNull()
102102
}
103103

104-
inline fun <reified C> forEachComponent(action: (C) -> Unit) {
105-
activeComponents.filterIsInstance<C>().forEach { action(it) }
104+
inline fun <reified C> forEachInitializedComponent(action: (C) -> Unit) {
105+
initializedComponents.forEach { (_, component) ->
106+
if (component is C) {
107+
action(component)
108+
}
109+
}
110+
}
111+
112+
inline fun <reified C> forEachActiveComponent(action: (C) -> Unit) {
113+
activeComponents.forEach { component ->
114+
if (component is C) {
115+
action(component)
116+
}
117+
}
106118
}
107119

108120
private fun getOrCreateComponent(name: String): BridgeComponent<D>? {

navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebBottomSheetFragment.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import android.view.View
99
import android.view.ViewGroup
1010
import android.widget.TextView
1111
import androidx.activity.result.ActivityResultLauncher
12+
import dev.hotwire.core.bridge.BridgeComponent
13+
import dev.hotwire.core.bridge.BridgeComponentFragmentLifecycle
1214
import dev.hotwire.core.bridge.BridgeDelegate
1315
import dev.hotwire.core.files.util.HOTWIRE_REQUEST_CODE_FILES
1416
import dev.hotwire.core.files.util.HOTWIRE_REQUEST_CODE_GEOLOCATION_PERMISSION
@@ -50,12 +52,18 @@ open class HotwireWebBottomSheetFragment : HotwireBottomSheetFragment(), Hotwire
5052
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
5153
super.onViewCreated(view, savedInstanceState)
5254
webDelegate.onViewCreated()
55+
bridgeDelegate.forEachInitializedComponent<BridgeComponentFragmentLifecycle> {
56+
it.onViewCreated()
57+
}
5358
viewLifecycleOwner.lifecycle.addObserver(bridgeDelegate)
5459
}
5560

5661
override fun onDestroyView() {
5762
super.onDestroyView()
5863
webDelegate.onDestroyView()
64+
bridgeDelegate.forEachInitializedComponent<BridgeComponentFragmentLifecycle> {
65+
it.onDestroyView()
66+
}
5967
viewLifecycleOwner.lifecycle.removeObserver(bridgeDelegate)
6068
}
6169

@@ -73,6 +81,14 @@ open class HotwireWebBottomSheetFragment : HotwireBottomSheetFragment(), Hotwire
7381
}
7482
}
7583

84+
override fun onBridgeComponentInitialized(component: BridgeComponent<*>) {
85+
super.onBridgeComponentInitialized(component)
86+
87+
if (component is BridgeComponentFragmentLifecycle) {
88+
component.onViewCreated()
89+
}
90+
}
91+
7692
override fun onStart() {
7793
super.onStart()
7894
webDelegate.onStart()

navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebFragment.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import android.view.View
88
import android.view.ViewGroup
99
import android.widget.TextView
1010
import androidx.activity.result.ActivityResultLauncher
11+
import dev.hotwire.core.bridge.BridgeComponent
12+
import dev.hotwire.core.bridge.BridgeComponentFragmentLifecycle
1113
import dev.hotwire.core.bridge.BridgeDelegate
1214
import dev.hotwire.core.files.util.HOTWIRE_REQUEST_CODE_FILES
1315
import dev.hotwire.core.files.util.HOTWIRE_REQUEST_CODE_GEOLOCATION_PERMISSION
@@ -50,12 +52,18 @@ open class HotwireWebFragment : HotwireFragment(), HotwireWebFragmentCallback {
5052
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
5153
super.onViewCreated(view, savedInstanceState)
5254
webDelegate.onViewCreated()
55+
bridgeDelegate.forEachInitializedComponent<BridgeComponentFragmentLifecycle> {
56+
it.onViewCreated()
57+
}
5358
viewLifecycleOwner.lifecycle.addObserver(bridgeDelegate)
5459
}
5560

5661
override fun onDestroyView() {
5762
super.onDestroyView()
5863
webDelegate.onDestroyView()
64+
bridgeDelegate.forEachInitializedComponent<BridgeComponentFragmentLifecycle> {
65+
it.onDestroyView()
66+
}
5967
viewLifecycleOwner.lifecycle.removeObserver(bridgeDelegate)
6068
}
6169

@@ -110,6 +118,14 @@ open class HotwireWebFragment : HotwireFragment(), HotwireWebFragmentCallback {
110118
}
111119
}
112120

121+
override fun onBridgeComponentInitialized(component: BridgeComponent<*>) {
122+
super.onBridgeComponentInitialized(component)
123+
124+
if (component is BridgeComponentFragmentLifecycle) {
125+
component.onViewCreated()
126+
}
127+
}
128+
113129
final override fun prepareNavigation(onReady: () -> Unit) {
114130
webDelegate.prepareNavigation(onReady)
115131
}

0 commit comments

Comments
 (0)