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

Navigation compose #670

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ plugins {
alias(libs.plugins.dokka) apply true
alias(libs.plugins.compose.compiler) apply false
id("com.autonomousapps.dependency-analysis") version "2.0.0"
alias(libs.plugins.android.application) apply false
alias(libs.plugins.kotlin.android) apply false

}

Expand Down
4 changes: 3 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
# (not needed, since no requested libraries are pre-AndroidX)
android.enableJetifier=false
#android.enableJetifier=false
# This is needed for the navigation SDK library. It needs the recycler view.
android.enableJetifier=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official

Expand Down
40 changes: 33 additions & 7 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
[versions]
accompanistPermissions = "0.37.0"
activitycompose = "1.9.3"
agp = "8.7.2"
androidxtest = "1.6.2"
agpVersion = "8.7.2"

Check warning

Code scanning / Android Lint

Obsolete Android Gradle Plugin Version Warning

A newer version of com.android.application than 8.7.2 is available: 8.8.0. (There is also a newer version of 8.7.𝑥 available, if upgrading to 8.8.0 is difficult: 8.7.3)

Check warning

Code scanning / Android Lint

Obsolete Android Gradle Plugin Version Warning

A newer version of com.android.application than 8.7.2 is available: 8.8.0. (There is also a newer version of 8.7.𝑥 available, if upgrading to 8.8.0 is difficult: 8.7.3)

Check warning

Code scanning / Android Lint

Obsolete Android Gradle Plugin Version Warning

A newer version of com.android.application than 8.7.2 is available: 8.8.0. (There is also a newer version of 8.7.𝑥 available, if upgrading to 8.8.0 is difficult: 8.7.3)
androidCore = "1.6.1"
androidx-core = "1.15.0"
androidxtest = "1.6.2"
compose-bom = "2024.11.00"
dokka = "1.9.20"
espresso = "3.6.1"
jacoco-plugin = "0.2.1"
junitktx = "1.2.1"
junit = "4.13.2"
junitVersion = "1.2.1"
junitktx = "1.2.1"
kotlin = "2.0.21"
kotlinxCoroutines = "1.9.0"
mapsktx = "5.1.1"
lifecycleRuntimeKtx = "2.8.7"
mapsecrets = "2.0.1"
mapsktx = "5.1.1"
navigation = "6.0.0"
org-jacoco-core = "0.8.11"
androidx-core = "1.15.0"
places = "4.1.0"
playServicesLocation = "21.3.0"
robolectric = "4.14.1"
screenshot = "0.0.1-alpha08"
secretsGradlePlugin = "2.0.1"
truth = "1.4.4"

[libraries]
# robolectric = { module = "org.robolectric:robolectric", version.ref = "robolectric" }
accompanist-permissions = { module = "com.google.accompanist:accompanist-permissions", version.ref = "accompanistPermissions" }
android-gradle-plugin = { module = "com.android.tools.build:gradle", version.ref = "agp" }
androidx-compose-activity = { module = "androidx.activity:activity-compose", version.ref = "activitycompose" }
androidx-compose-bom = { module = "androidx.compose:compose-bom", version.ref = "compose-bom" }
Expand All @@ -27,25 +39,39 @@
androidx-compose-ui-preview-tooling = { module = "androidx.compose.ui:ui-tooling-preview" }
androidx-compose-ui-tooling = { module = "androidx.compose.ui:ui-tooling" }
androidx-core = { module = "androidx.core:core-ktx", version.ref = "androidx-core" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
androidx-lifecycle-runtime-compose = { module = "androidx.lifecycle:lifecycle-runtime-compose", version.ref = "lifecycleRuntimeKtx" }
androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }
androidx-lifecycle-viewmodel-compose = { module = "androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "lifecycleRuntimeKtx" }
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
androidx-test-compose-ui = { module = "androidx.compose.ui:ui-test-junit4" }
androidx-test-core = { module = "androidx.test:core", version.ref = "androidCore" }
androidx-test-espresso = { module = "androidx.test.espresso:espresso-core", version.ref = "espresso" }
androidx-test-junit-ktx = { module = "androidx.test.ext:junit-ktx", version.ref = "junitktx" }
androidx-test-rules = { module = "androidx.test:rules", version.ref = "androidCore" }
androidx-test-runner = { module = "androidx.test:runner", version.ref = "androidxtest" }
androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" }
androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
dokka-plugin = { module = "org.jetbrains.dokka:dokka-gradle-plugin", version.ref = "dokka" }
jacoco-android-plugin = { module = "com.mxalbert.gradle:jacoco-android", version.ref = "jacoco-plugin", version.require = "0.2.1" }
kotlin = { module = "org.jetbrains.kotlin:kotlin-stdlib-jdk7", version.ref = "kotlin" }
kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinxCoroutines" }
kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "kotlinxCoroutines" }
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinxCoroutines" }
maps-ktx-std = { module = "com.google.maps.android:maps-ktx", version.ref = "mapsktx" }
maps-ktx-utils = { module = "com.google.maps.android:maps-utils-ktx", version.ref = "mapsktx" }
maps-secrets-plugin = { module = "com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin", version.ref = "mapsecrets" }
navigation = { module = "com.google.android.libraries.navigation:navigation", version.ref = "navigation" }
org-jacoco-core = { module = "org.jacoco:org.jacoco.core", version.ref = "org-jacoco-core" }
places = { group = "com.google.android.libraries.places", name = "places", version.ref = "places" }
play-services-location = { module = "com.google.android.gms:play-services-location", version.ref = "playServicesLocation" }
test-junit = { module = "junit:junit", version.ref = "junit" }
truth = { module = "com.google.truth:truth", version.ref = "truth" }

[plugins]
dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }
android-application = { id = "com.android.application", version.ref = "agpVersion" }
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
screenshot = { id = "com.android.compose.screenshot", version.ref = "screenshot"}
dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
screenshot = { id = "com.android.compose.screenshot", version.ref = "screenshot"}
secrets-gradle-plugin = { id = "com.google.android.libraries.mapsplatform.secrets-gradle-plugin", version.ref = "secretsGradlePlugin" }
3 changes: 2 additions & 1 deletion local.defaults.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
MAPS_API_KEY=YOUR_API_KEY
MAPS_API_KEY=YOUR_API_KEY
PLACES_API_KEY=DEFAULT_API_KEY
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package com.google.maps.android.compose
import android.view.View
import androidx.compose.ui.platform.ComposeView
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.MapView
import com.google.android.gms.maps.model.Marker

/**
Expand All @@ -36,7 +35,7 @@ import com.google.android.gms.maps.model.Marker
* implementation should be updated.
*/
internal class ComposeInfoWindowAdapter(
private val mapView: MapView,
private val mapViewDelegate: AbstractMapViewDelegate<*>,
private val markerNodeFinder: (Marker) -> MarkerNode?
) : GoogleMap.InfoWindowAdapter {

Expand All @@ -46,10 +45,10 @@ internal class ComposeInfoWindowAdapter(
if (content == null) {
return null
}
val view = ComposeView(mapView.context).apply {
val view = ComposeView(mapViewDelegate.mapView.context).apply {
setContent { content(marker) }
}
mapView.renderComposeViewOnce(view, parentContext = markerNode.compositionContext)
mapViewDelegate.renderComposeViewOnce(view, parentContext = markerNode.compositionContext)
return view
}

Expand All @@ -59,11 +58,10 @@ internal class ComposeInfoWindowAdapter(
if (infoWindow == null) {
return null
}
val view = ComposeView(mapView.context).apply {
val view = ComposeView(mapViewDelegate.mapView.context).apply {
setContent { infoWindow(marker) }
}
mapView.renderComposeViewOnce(view, parentContext = markerNode.compositionContext)
mapViewDelegate.renderComposeViewOnce(view, parentContext = markerNode.compositionContext)
return view
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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?
Copy link
Collaborator

@kikoso kikoso Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My two cents: if the AbstractMapViewDelegate is holding a reference to the Context it could leak. But I think we are directly not doing it, if we take a look at the mapViewDelegate, right? Not sure if using the java.lang.ref.WeakReference will make sense.

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)

Expand All @@ -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
Expand All @@ -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,
)
Expand All @@ -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
)

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ package com.google.maps.android.compose

import androidx.compose.runtime.AbstractApplier
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.MapView
import com.google.android.gms.maps.model.Circle
import com.google.android.gms.maps.model.GroundOverlay
import com.google.android.gms.maps.model.MapColorScheme
import com.google.android.gms.maps.model.Marker
import com.google.android.gms.maps.model.Polygon
import com.google.android.gms.maps.model.Polyline
Expand All @@ -39,7 +37,7 @@ private object MapNodeRoot : MapNode
// for that particular listener; yet MapClickListeners never actually changes.
internal class MapApplier(
val map: GoogleMap,
internal val mapView: MapView,
internal val mapViewDelegate: AbstractMapViewDelegate<*>,
val mapClickListeners: MapClickListeners,
) : AbstractApplier<MapNode>(MapNodeRoot) {

Expand Down Expand Up @@ -212,7 +210,7 @@ internal class MapApplier(
})
map.setInfoWindowAdapter(
ComposeInfoWindowAdapter(
mapView,
mapViewDelegate,
markerNodeFinder = { marker ->
decorations.firstOrNull { it is MarkerNode && it.marker == marker }
as MarkerNode?
Expand Down
Loading
Loading