Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 85872413 authored by Ellen Poe's avatar Ellen Poe
Browse files

refactor: MapView

parent df334952
Loading
Loading
Loading
Loading
+189 −168
Original line number Diff line number Diff line
@@ -167,13 +167,48 @@ fun MapView(
                val savedPlaces by mapViewModel.savedPlacesFlow.collectAsState(FeatureCollection())
                location?.let { LocationPuck(it) }

                // Show user favorites
                FavoritesLayer(savedPlaces, isSystemInDarkTheme())

                OfflineBoundsLayer(selectedOfflineArea)

                RouteLayer(currentRoute)

                TransitLayer(currentTransitItinerary)

                PinsLayer(pinFeatures, isSystemInDarkTheme())
            }
        } else {
            // Handle invalid port - could show an error message
            Box(modifier = Modifier.fillMaxSize()) {
                // Error UI could be added here
            }
        }

        // Handle permission state changes
        LaunchedEffect(hasLocationPermission) {
            mapViewModel.handlePermissionStateChange(hasLocationPermission, cameraState, context)
        }

        MapControls(
            cameraState = cameraState,
            mapViewModel = mapViewModel,
            fabInsets = fabInsets,
            hasLocationPermission = hasLocationPermission,
            onRequestLocationPermission = onRequestLocationPermission,
            appPreferences = appPreferences,
            context = context
        )
    }
}

@Composable
private fun FavoritesLayer(savedPlaces: FeatureCollection, isSystemInDarkTheme: Boolean) {
    val textColor = MaterialTheme.colorScheme.onSurface
    SymbolLayer(
        id = "user_favorites",
        source = rememberGeoJsonSource(GeoJsonData.Features(savedPlaces)),
        iconImage = image(
                        if (isSystemInDarkTheme()) {
            if (isSystemInDarkTheme) {
                painterResource(drawable.ic_stars_dark)
            } else {
                painterResource(drawable.ic_stars_light)
@@ -191,8 +226,10 @@ fun MapView(
        textOffset = offset(0.em, 0.8.em),
        textOptional = const(true),
    )
}

                // Show offline download bounds if an area is selected
@Composable
private fun OfflineBoundsLayer(selectedOfflineArea: OfflineArea?) {
    selectedOfflineArea?.let { area ->
        val boundsPolygon = Polygon(
            listOf(
@@ -222,8 +259,10 @@ fun MapView(
            width = const(3.dp)
        )
    }
}

                // Display route if available
@Composable
private fun RouteLayer(currentRoute: Route?) {
    currentRoute?.let { route ->
        val routePositions = route.geometry.map { coord ->
            Position(coord.lng, coord.lat) // [longitude, latitude]
@@ -263,8 +302,10 @@ fun MapView(
            join = const(LineJoin.Round),
        )
    }
}

                // Display transit itinerary polylines if available
@Composable
private fun TransitLayer(currentTransitItinerary: Itinerary?) {
    currentTransitItinerary?.let { itinerary ->
        itinerary.legs.forEachIndexed { legIndex, leg ->
            leg.legGeometry?.let { geometry ->
@@ -327,12 +368,15 @@ fun MapView(
            }
        }
    }
}

@Composable
private fun PinsLayer(pinFeatures: List<Feature>, isSystemInDarkTheme: Boolean) {
    SymbolLayer(
        id = "map_pins",
        source = rememberGeoJsonSource(GeoJsonData.Features(FeatureCollection(features = pinFeatures))),
        iconImage = image(
                        if (isSystemInDarkTheme()) {
            if (isSystemInDarkTheme) {
                painterResource(drawable.map_pin_dark)
            } else {
                painterResource(drawable.map_pin_light)
@@ -340,29 +384,6 @@ fun MapView(
        ),
    )
}
        } else {
            // Handle invalid port - could show an error message
            Box(modifier = Modifier.fillMaxSize()) {
                // Error UI could be added here
            }
        }

        // Handle permission state changes
        LaunchedEffect(hasLocationPermission) {
            mapViewModel.handlePermissionStateChange(hasLocationPermission, cameraState, context)
        }

        MapControls(
            cameraState = cameraState,
            mapViewModel = mapViewModel,
            fabInsets = fabInsets,
            hasLocationPermission = hasLocationPermission,
            onRequestLocationPermission = onRequestLocationPermission,
            appPreferences = appPreferences,
            context = context
        )
    }
}

/**
 * Get default color for transit mode when route color is not available