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
Expand Up @@ -22,17 +22,21 @@ import com.maptiler.maptilersdk.map.MTMapViewController
import com.maptiler.maptilersdk.map.MTMapViewDelegate
import com.maptiler.maptilersdk.map.style.MTMapReferenceStyle

@Suppress("FunctionName")
@Composable
fun HomeScreen(
navController: NavController,
context: Context,
) {
val mapController = MapController(context)

val options = MTMapOptions()
options.setMapTilerLogoIsVisible(true)

Box(modifier = Modifier.fillMaxSize()) {
MTMapView(
MTMapReferenceStyle.STREETS,
MTMapOptions(),
options,
mapController.controller,
modifier =
Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.maptiler.maptilersdk.MTConfig

class MainActivity : ComponentActivity() {
Expand All @@ -25,22 +21,3 @@ class MainActivity : ComponentActivity() {
}
}
}

@Composable
fun Greeting(
name: String,
modifier: Modifier = Modifier,
) {
Text(
text = "Hello $name!",
modifier = modifier,
)
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
MaterialTheme {
Greeting("Android")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController

@Suppress("FunctionName")
@Composable
fun MainScreen(context: Context) {
val navController = rememberNavController()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ private val LightColorScheme =
*/
)

@Suppress("FunctionName")
@Composable
fun MapTilerMobileDemoTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ package com.maptiler.maptilersdk.bridge
internal typealias JSString = String

internal interface MTCommand {
val isPrimitiveReturnType: Boolean

fun toJS(): JSString
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal interface WebViewExecutorDelegate {
}

internal class WebViewExecutor(
context: Context,
private val context: Context,
) : MTCommandExecutable {
object Constants {
object Error {
Expand All @@ -51,17 +51,21 @@ internal class WebViewExecutor(
}
}

private var webView: WebView? = null
private var _webView: WebView? = null
val webView: WebView
get() {
if (_webView == null) {
initWebViewIfNeeded(context)
}

var delegate: WebViewExecutorDelegate? = null
return _webView!!
}

init {
initWebViewIfNeeded(context)
}
var delegate: WebViewExecutorDelegate? = null

private fun initWebViewIfNeeded(context: Context) {
if (webView == null) {
webView =
if (_webView == null) {
_webView =
WebView(context).apply {
settings.javaScriptEnabled = true
settings.allowFileAccess = true
Expand Down Expand Up @@ -94,7 +98,11 @@ internal class WebViewExecutor(
}
}

fun getWebView(): WebView? = webView
fun setWebView(webView: WebView) {
this._webView = webView
}

fun getAttachableWebView(): WebView? = webView

override suspend fun execute(command: MTCommand): MTBridgeReturnType =
withContext(Dispatchers.Main) {
Expand All @@ -103,35 +111,41 @@ internal class WebViewExecutor(

val deferred = CompletableDeferred<MTBridgeReturnType>()

webView.evaluateJavascript(command.toJS()) { result ->
try {
if (result == null || result == "null") {
if (command.isPrimitiveReturnType) {
webView.evaluateJavascript(command.toJS()) { result ->
try {
if (result == null || result == "null") {
if (isVerbose) {
MTLogger.log("$command completed with unsupported return type.", MTLogType.WARNING)
}

deferred.complete(MTBridgeReturnType.UnsupportedType)
} else {
try {
val parsedResult = MTBridgeReturnType.from(result)
deferred.complete(parsedResult)
} catch (e: Exception) {
deferred.completeExceptionally(MTError.InvalidResultType(result))
}
}
} catch (e: Exception) {
if (isVerbose) {
MTLogger.log("$command completed with unsupported return type.", MTLogType.WARNING)
MTLogger.log("Bridging error occurred for $command: ${e.message}", MTLogType.ERROR)
}

deferred.complete(MTBridgeReturnType.UnsupportedType)
} else {
try {
val parsedResult = MTBridgeReturnType.from(result)
deferred.complete(parsedResult)
} catch (e: Exception) {
deferred.completeExceptionally(MTError.InvalidResultType(result))
}
}
} catch (e: Exception) {
if (isVerbose) {
MTLogger.log("Bridging error occurred for $command: ${e.message}", MTLogType.ERROR)
deferred.completeExceptionally(MTError.Unknown(e.message ?: "Unknown error"))
}

deferred.completeExceptionally(MTError.Unknown(e.message ?: "Unknown error"))
}
} else {
webView.evaluateJavascript(command.toJS(), null)
deferred.complete(MTBridgeReturnType.UnsupportedType)
}

return@withContext deferred.await()
}

fun destroy() {
webView?.destroy()
_webView?.destroy()
_webView = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ internal data class InitializeMap(
val styleVariant: MTMapStyleVariant?,
val isSessionLogicEnabled: Any?,
) : MTCommand {
override val isPrimitiveReturnType: Boolean = false

override fun toJS(): JSString {
val referenceStyleName = referenceStyle.getName()
val styleVariantName = styleVariant?.value?.uppercase()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ import com.maptiler.maptilersdk.bridge.MTBridge
import com.maptiler.maptilersdk.bridge.MTCommand

internal class DragPanDisable : MTCommand {
override val isPrimitiveReturnType: Boolean = false

override fun toJS(): String = "${MTBridge.MAP_OBJECT}.dragPan.disable();"
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import com.maptiler.maptilersdk.map.options.MTDragPanOptions
internal data class DragPanEnable(
val options: MTDragPanOptions?,
) : MTCommand {
override val isPrimitiveReturnType: Boolean = false

override fun toJS(): String {
val optionsString: JSString = JsonConfig.json.encodeToString(options)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ import com.maptiler.maptilersdk.bridge.MTBridge
import com.maptiler.maptilersdk.bridge.MTCommand

internal class PinchRotateAndZoomDisable : MTCommand {
override val isPrimitiveReturnType: Boolean = false

override fun toJS(): String = "${MTBridge.MAP_OBJECT}.touchZoomRotate.disable();"
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ import com.maptiler.maptilersdk.bridge.MTBridge
import com.maptiler.maptilersdk.bridge.MTCommand

internal class PinchRotateAndZoomEnable : MTCommand {
override val isPrimitiveReturnType: Boolean = false

override fun toJS(): String = "${MTBridge.MAP_OBJECT}.touchZoomRotate.enable();"
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ import com.maptiler.maptilersdk.bridge.MTBridge
import com.maptiler.maptilersdk.bridge.MTCommand

internal class TwoFingersDragPitchDisable : MTCommand {
override val isPrimitiveReturnType: Boolean = false

override fun toJS(): String = "${MTBridge.MAP_OBJECT}.touchPitch.disable();"
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ import com.maptiler.maptilersdk.bridge.MTBridge
import com.maptiler.maptilersdk.bridge.MTCommand

internal class TwoFingersDragPitchEnable : MTCommand {
override val isPrimitiveReturnType: Boolean = false

override fun toJS(): String = "${MTBridge.MAP_OBJECT}.touchPitch.enable();"
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ import com.maptiler.maptilersdk.bridge.MTBridge
import com.maptiler.maptilersdk.bridge.MTCommand

internal class GetZoom : MTCommand {
override val isPrimitiveReturnType: Boolean = true

override fun toJS(): String = "${MTBridge.MAP_OBJECT}.getZoom();"
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import com.maptiler.maptilersdk.map.types.MTPoint
internal data class PanBy(
val offset: MTPoint,
) : MTCommand {
override val isPrimitiveReturnType: Boolean = false

override fun toJS(): String {
val offsetString: JSString = JsonConfig.json.encodeToString(offset)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import com.maptiler.maptilersdk.map.LngLat
internal data class PanTo(
val coordinates: LngLat,
) : MTCommand {
override val isPrimitiveReturnType: Boolean = false

override fun toJS(): String {
val coordinatesString: JSString = JsonConfig.json.encodeToString(coordinates)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ import com.maptiler.maptilersdk.bridge.MTCommand
internal data class SetMaxZoom(
val zoom: Double,
) : MTCommand {
override val isPrimitiveReturnType: Boolean = false

override fun toJS(): String = "${MTBridge.MAP_OBJECT}.setMaxZoom($zoom);"
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ import com.maptiler.maptilersdk.bridge.MTCommand
internal data class SetMinZoom(
val zoom: Double,
) : MTCommand {
override val isPrimitiveReturnType: Boolean = false

override fun toJS(): String = "${MTBridge.MAP_OBJECT}.setMinZoom($zoom);"
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ import com.maptiler.maptilersdk.bridge.MTCommand
internal data class SetZoom(
val zoom: Double,
) : MTCommand {
override val isPrimitiveReturnType: Boolean = false

override fun toJS(): String = "${MTBridge.MAP_OBJECT}.setZoom($zoom);"
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ import com.maptiler.maptilersdk.bridge.MTBridge
import com.maptiler.maptilersdk.bridge.MTCommand

internal class ZoomIn : MTCommand {
override val isPrimitiveReturnType: Boolean = false

override fun toJS(): String = "${MTBridge.MAP_OBJECT}.zoomIn();"
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ import com.maptiler.maptilersdk.bridge.MTBridge
import com.maptiler.maptilersdk.bridge.MTCommand

internal class ZoomOut : MTCommand {
override val isPrimitiveReturnType: Boolean = false

override fun toJS(): String = "${MTBridge.MAP_OBJECT}.zoomOut();"
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class MTMapOptions {
/**
* A value representing the position of the MapTiler wordmark on the map.
*/
var logoPosition: MTMapCorner? = MTMapCorner.TOP_RIGHT
var logoPosition: MTMapCorner? = MTMapCorner.TOP_LEFT
private set

/**
Expand Down Expand Up @@ -389,4 +389,10 @@ class MTMapOptions {
constructor() {
// Defaults
}

// Setters

fun setMapTilerLogoIsVisible(isVisible: Boolean) {
maptilerLogoIsVisible = isVisible
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ package com.maptiler.maptilersdk.map
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.key
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.viewinterop.AndroidView
Expand All @@ -29,6 +31,7 @@ fun MTMapView(
styleVariant: MTMapStyleVariant? = null,
) {
val scope = rememberCoroutineScope()
val webView = remember { controller.getAttachableWebView() }

LaunchedEffect(Unit) {
controller.bind(scope)
Expand All @@ -44,8 +47,10 @@ fun MTMapView(
}
}

AndroidView(
factory = { controller.getAttachableWebView() },
modifier = modifier,
)
key(webView) {
AndroidView(
factory = { webView },
modifier = modifier,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class MTMapViewController(
webViewExecutor?.destroy()
}

internal fun getAttachableWebView(): WebView = webViewExecutor?.getWebView()!!
internal fun getAttachableWebView(): WebView = webViewExecutor?.getAttachableWebView()!!

override fun onNavigationFinished(url: String) {
coroutineScope?.launch(Dispatchers.Default) {
Expand Down
Loading