-
Notifications
You must be signed in to change notification settings - Fork 150
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
Navigation compose #670
base: main
Are you sure you want to change the base?
Navigation compose #670
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ package com.google.maps.android.compose | |
|
||
import android.content.ComponentCallbacks | ||
import android.content.ComponentCallbacks2 | ||
import android.content.Context | ||
import android.content.res.Configuration | ||
import android.location.Location | ||
import android.os.Bundle | ||
|
@@ -35,12 +36,15 @@ import androidx.compose.runtime.rememberCoroutineScope | |
import androidx.compose.runtime.rememberUpdatedState | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.AbstractComposeView | ||
import androidx.compose.ui.platform.ComposeView | ||
import androidx.compose.ui.platform.LocalInspectionMode | ||
import androidx.compose.ui.viewinterop.AndroidView | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.LifecycleEventObserver | ||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.lifecycle.findViewTreeLifecycleOwner | ||
import com.google.android.gms.maps.GoogleMap | ||
import com.google.android.gms.maps.GoogleMapOptions | ||
import com.google.android.gms.maps.LocationSource | ||
import com.google.android.gms.maps.MapView | ||
|
@@ -98,6 +102,7 @@ public fun GoogleMap( | |
onPOIClick: ((PointOfInterest) -> Unit)? = null, | ||
contentPadding: PaddingValues = DefaultMapContentPadding, | ||
mapColorScheme: ComposeMapColorScheme? = null, | ||
mapViewCreator: ((Context, GoogleMapOptions) -> AbstractMapViewDelegate<*>)? = null, | ||
content: @Composable @GoogleMapComposable () -> Unit = {}, | ||
) { | ||
// When in preview, early return a Box with the received modifier preserving layout | ||
|
@@ -145,19 +150,31 @@ public fun GoogleMap( | |
var subcompositionJob by remember { mutableStateOf<Job?>(null) } | ||
val parentCompositionScope = rememberCoroutineScope() | ||
|
||
var delegate by remember { | ||
// TODO: this could leak the view? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My two cents: if the EDIT: Yeah, I think we are holding a reference in this file. |
||
mutableStateOf<AbstractMapViewDelegate<*>?>(null) | ||
} | ||
|
||
AndroidView( | ||
modifier = modifier, | ||
factory = { context -> | ||
MapView(context, googleMapOptionsFactory()).also { mapView -> | ||
if (mapViewCreator != null) { | ||
mapViewCreator(context, googleMapOptionsFactory()) | ||
} else { | ||
MapViewDelegate(MapView(context, googleMapOptionsFactory())) | ||
}.also { mapViewDelegate: AbstractMapViewDelegate<*> -> | ||
delegate = mapViewDelegate | ||
val mapView = mapViewDelegate.mapView | ||
|
||
val componentCallbacks = object : ComponentCallbacks2 { | ||
override fun onConfigurationChanged(newConfig: Configuration) {} | ||
@Deprecated("Deprecated in Java", ReplaceWith("onTrimMemory(level)")) | ||
override fun onLowMemory() { mapView.onLowMemory() } | ||
override fun onTrimMemory(level: Int) { mapView.onLowMemory() } | ||
override fun onLowMemory() { mapViewDelegate.onLowMemory() } | ||
override fun onTrimMemory(level: Int) { mapViewDelegate.onLowMemory() } | ||
} | ||
context.registerComponentCallbacks(componentCallbacks) | ||
|
||
val lifecycleObserver = MapLifecycleEventObserver(mapView) | ||
val lifecycleObserver = MapLifecycleEventObserver(mapViewDelegate) | ||
|
||
mapView.tag = MapTagData(componentCallbacks, lifecycleObserver) | ||
|
||
|
@@ -179,11 +196,15 @@ public fun GoogleMap( | |
} | ||
|
||
mapView.addOnAttachStateChangeListener(onAttachStateListener) | ||
} | ||
}.mapView | ||
}, | ||
onReset = { /* View is detached. */ }, | ||
onRelease = { mapView -> | ||
val (componentCallbacks, lifecycleObserver) = mapView.tagData | ||
val (componentCallbacks, lifecycleObserver) = delegate!!.tagData | ||
// val (componentCallbacks, lifecycleObserver) = when { | ||
// mapView is MapView -> mapView.tagData | ||
// else -> TODO("not yet implemented!") | ||
// } | ||
mapView.context.unregisterComponentCallbacks(componentCallbacks) | ||
lifecycleObserver.moveToDestroyedState() | ||
mapView.tag = null | ||
|
@@ -193,7 +214,7 @@ public fun GoogleMap( | |
subcompositionJob = parentCompositionScope.launchSubcomposition( | ||
mapUpdaterState, | ||
parentComposition, | ||
mapView, | ||
delegate!!, // TODO: not sure about this. Maybe just remember a factory method? | ||
mapClickListeners, | ||
currentContent, | ||
) | ||
|
@@ -202,22 +223,29 @@ public fun GoogleMap( | |
) | ||
} | ||
|
||
private fun View.toDelegate(): MapViewDelegate { | ||
return when (this) { | ||
is MapView -> MapViewDelegate(this) | ||
else -> error("View cannot be used as an AbstractMapViewDelegate") | ||
} | ||
} | ||
|
||
/** | ||
* Create and apply the [content] compositions to the map + | ||
* dispose the [Composition] when the parent composable is disposed. | ||
* */ | ||
private fun CoroutineScope.launchSubcomposition( | ||
mapUpdaterState: MapUpdaterState, | ||
parentComposition: CompositionContext, | ||
mapView: MapView, | ||
mapViewDelegate: AbstractMapViewDelegate<*>, | ||
mapClickListeners: MapClickListeners, | ||
content: @Composable @GoogleMapComposable () -> Unit, | ||
): Job { | ||
// Use [CoroutineStart.UNDISPATCHED] to kick off GoogleMap loading immediately | ||
return launch(start = CoroutineStart.UNDISPATCHED) { | ||
val map = mapView.awaitMap() | ||
val map = mapViewDelegate.awaitMap() | ||
val composition = Composition( | ||
applier = MapApplier(map, mapView, mapClickListeners), | ||
applier = MapApplier(map, mapViewDelegate, mapClickListeners), | ||
parent = parentComposition | ||
) | ||
|
||
|
@@ -308,7 +336,65 @@ public fun googleMapFactory( | |
} | ||
} | ||
|
||
private class MapLifecycleEventObserver(private val mapView: MapView) : LifecycleEventObserver { | ||
public interface AbstractMapViewDelegate<T : View> { | ||
public fun onCreate(savedInstanceState: Bundle?) | ||
public fun onStart() | ||
public fun onResume() | ||
public fun onPause() | ||
public fun onStop() | ||
public fun onLowMemory() | ||
public fun onDestroy() | ||
public suspend fun awaitMap(): GoogleMap | ||
public fun renderComposeViewOnce( | ||
view: AbstractComposeView, | ||
parentContext: CompositionContext, | ||
onAddedToWindow: ((View) -> Unit)? = null, | ||
) | ||
|
||
public fun startRenderingComposeView( | ||
view: AbstractComposeView, | ||
parentContext: CompositionContext, | ||
): ComposeUiViewRenderer.RenderHandle | ||
|
||
public val mapView: T | ||
} | ||
|
||
private val <T : View> AbstractMapViewDelegate<T>.tagData: MapTagData | ||
get() = mapView.tag as MapTagData | ||
|
||
public class MapViewDelegate(override val mapView: MapView) : AbstractMapViewDelegate<MapView> { | ||
override fun onCreate(savedInstanceState: Bundle?): Unit = mapView.onCreate(savedInstanceState) | ||
override fun onStart(): Unit = mapView.onStart() | ||
override fun onResume(): Unit = mapView.onResume() | ||
override fun onPause(): Unit = mapView.onPause() | ||
override fun onStop(): Unit = mapView.onStop() | ||
override fun onLowMemory(): Unit = mapView.onLowMemory() | ||
override fun onDestroy(): Unit = mapView.onDestroy() | ||
override suspend fun awaitMap(): GoogleMap = mapView.awaitMap() | ||
override fun renderComposeViewOnce( | ||
view: AbstractComposeView, | ||
parentContext: CompositionContext, | ||
onAddedToWindow: ((View) -> Unit)? | ||
) { | ||
mapView.renderComposeViewOnce( | ||
view = view, | ||
parentContext = parentContext, | ||
onAddedToWindow = onAddedToWindow | ||
) | ||
} | ||
|
||
override fun startRenderingComposeView( | ||
view: AbstractComposeView, | ||
parentContext: CompositionContext | ||
): ComposeUiViewRenderer.RenderHandle { | ||
return mapView.startRenderingComposeView( | ||
view = view, | ||
parentContext = parentContext, | ||
) | ||
} | ||
} | ||
|
||
private class MapLifecycleEventObserver(private val mapView: AbstractMapViewDelegate<*>) : LifecycleEventObserver { | ||
private var currentLifecycleState: Lifecycle.State = Lifecycle.State.INITIALIZED | ||
|
||
override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) { | ||
|
Check warning
Code scanning / Android Lint
Obsolete Android Gradle Plugin Version Warning