Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dev.hotwire.core.bridge

interface BridgeComponentFragmentLifecycle {
fun onViewCreated()
fun onDestroyView()
}
18 changes: 15 additions & 3 deletions core/src/main/kotlin/dev/hotwire/core/bridge/BridgeDelegate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class BridgeDelegate<D : BridgeDestination>(
) : DefaultLifecycleObserver {
internal var bridge: Bridge? = null
private var destinationIsActive: Boolean = false
private val initializedComponents = hashMapOf<String, BridgeComponent<D>>()
private val resolvedLocation: String
get() = bridge?.webView?.url ?: location

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

Expand Down Expand Up @@ -101,8 +101,20 @@ class BridgeDelegate<D : BridgeDestination>(
return activeComponents.filterIsInstance<C>().firstOrNull()
}

inline fun <reified C> forEachComponent(action: (C) -> Unit) {
activeComponents.filterIsInstance<C>().forEach { action(it) }
inline fun <reified C> forEachInitializedComponent(action: (C) -> Unit) {
initializedComponents.forEach { (_, component) ->
if (component is C) {
action(component)
}
}
}

inline fun <reified C> forEachActiveComponent(action: (C) -> Unit) {
activeComponents.forEach { component ->
if (component is C) {
action(component)
}
}
}

private fun getOrCreateComponent(name: String): BridgeComponent<D>? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.activity.result.ActivityResultLauncher
import dev.hotwire.core.bridge.BridgeComponent
import dev.hotwire.core.bridge.BridgeComponentFragmentLifecycle
import dev.hotwire.core.bridge.BridgeDelegate
import dev.hotwire.core.files.util.HOTWIRE_REQUEST_CODE_FILES
import dev.hotwire.core.files.util.HOTWIRE_REQUEST_CODE_GEOLOCATION_PERMISSION
Expand Down Expand Up @@ -50,12 +52,18 @@ open class HotwireWebBottomSheetFragment : HotwireBottomSheetFragment(), Hotwire
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
webDelegate.onViewCreated()
bridgeDelegate.forEachInitializedComponent<BridgeComponentFragmentLifecycle> {
it.onViewCreated()
}
viewLifecycleOwner.lifecycle.addObserver(bridgeDelegate)
}

override fun onDestroyView() {
super.onDestroyView()
webDelegate.onDestroyView()
bridgeDelegate.forEachInitializedComponent<BridgeComponentFragmentLifecycle> {
it.onDestroyView()
}
viewLifecycleOwner.lifecycle.removeObserver(bridgeDelegate)
}

Expand All @@ -73,6 +81,14 @@ open class HotwireWebBottomSheetFragment : HotwireBottomSheetFragment(), Hotwire
}
}

override fun onBridgeComponentInitialized(component: BridgeComponent<*>) {
super.onBridgeComponentInitialized(component)

if (component is BridgeComponentFragmentLifecycle) {
component.onViewCreated()
}
}

override fun onStart() {
super.onStart()
webDelegate.onStart()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.activity.result.ActivityResultLauncher
import dev.hotwire.core.bridge.BridgeComponent
import dev.hotwire.core.bridge.BridgeComponentFragmentLifecycle
import dev.hotwire.core.bridge.BridgeDelegate
import dev.hotwire.core.files.util.HOTWIRE_REQUEST_CODE_FILES
import dev.hotwire.core.files.util.HOTWIRE_REQUEST_CODE_GEOLOCATION_PERMISSION
Expand Down Expand Up @@ -50,12 +52,18 @@ open class HotwireWebFragment : HotwireFragment(), HotwireWebFragmentCallback {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
webDelegate.onViewCreated()
bridgeDelegate.forEachInitializedComponent<BridgeComponentFragmentLifecycle> {
it.onViewCreated()
}
viewLifecycleOwner.lifecycle.addObserver(bridgeDelegate)
}

override fun onDestroyView() {
super.onDestroyView()
webDelegate.onDestroyView()
bridgeDelegate.forEachInitializedComponent<BridgeComponentFragmentLifecycle> {
it.onDestroyView()
}
viewLifecycleOwner.lifecycle.removeObserver(bridgeDelegate)
}

Expand Down Expand Up @@ -110,6 +118,14 @@ open class HotwireWebFragment : HotwireFragment(), HotwireWebFragmentCallback {
}
}

override fun onBridgeComponentInitialized(component: BridgeComponent<*>) {
super.onBridgeComponentInitialized(component)

if (component is BridgeComponentFragmentLifecycle) {
component.onViewCreated()
}
}

final override fun prepareNavigation(onReady: () -> Unit) {
webDelegate.prepareNavigation(onReady)
}
Expand Down