Untitled

 avatar
unknown
plain_text
9 days ago
24 kB
4
Indexable
class MainScreen {

    private lateinit var lifeCycleOwner: LifecycleOwner

    private val blueOutlineSymbol by lazy {
        SimpleLineSymbol(SimpleLineSymbolStyle.Solid, color = com.arcgismaps.Color.fromRgba(0, 0, 255, 255), 2f)
    }

    private val pointSymbol by lazy {
        SimpleMarkerSymbol(SimpleMarkerSymbolStyle.Circle, color = com.arcgismaps.Color.fromRgba(255, 0, 0, 200), 15f).apply {
            // outline = blueOutlineSymbol
        }
    }

    private val lineSymbol by lazy {
        SimpleLineSymbol(SimpleLineSymbolStyle.Solid, color = com.arcgismaps.Color.fromRgba(0, 0, 0, 255), 3f)
    }

    private val polygonSymbol by lazy {
        SimpleFillSymbol(SimpleFillSymbolStyle.Solid, color = com.arcgismaps.Color.fromRgba(100, 0, 255, 100), blueOutlineSymbol)
    }

    private val graphicsOverlay by lazy {
        GraphicsOverlay()
    }
    private val graphicsStack = mutableListOf<List<Graphic>>()
    private val linePoints = mutableListOf<Point>()
    private val polygonPoints = mutableListOf<Point>()
    private val isMeasuring = mutableStateOf(false)
    private val showActionBar = mutableStateOf(false)
    var distance:Double = 0.0
    var area:Double = 0.0

    var originalArea: Double = 0.0
    var originalDistanceUnit: String = "Meter"
    var originalAreaUnit: String = "Acre"
    var distanceText:MutableState<String> = mutableStateOf("0.0")
    var areaText:MutableState<String> = mutableStateOf("0.0")
    var calloutLocation = mutableStateOf<Point?>(null)
    var selectedGeometryType:String = "Distance"


//    var distanceTextState: MutableLiveData<String> = MutableLiveData() // commented by Ramya

    @SuppressLint("SuspiciousIndentation")
    @Composable
    fun MainScreenComposable() {
        val context = LocalContext.current
        val coroutineScope = rememberCoroutineScope()
        ArcGISEnvironment.applicationContext = context.applicationContext
        val mapType: MutableState<BasemapStyle> = remember { mutableStateOf(BasemapStyle.ArcGISTopographic) }
        lifeCycleOwner = LocalLifecycleOwner.current

        val locationDisplay = rememberLocationDisplay().apply {
            setAutoPanMode(LocationDisplayAutoPanMode.Recenter)
        }

        val map = remember(mapType.value) {
            ArcGISMap(mapType.value).apply {
                initialViewpoint = Viewpoint(
                    latitude = 13.0236, longitude = 80.1780, scale = 72000.0
                )
            }
        }

        var cumulativeDistance by remember { mutableStateOf(0.0) }
        var requestPermissions by remember { mutableStateOf(false) }


        var selectedUnit by remember { mutableStateOf("Meter") }
        var pointsDrawn by remember { mutableStateOf(false) }
        val distanceUnits = mutableListOf("Meter", "Kilometre", "Foot", "Yard")
        val areaUnits = listOf("Acre", "Hectare", "Square Meter", "Square Kilometer", "Square Foot")

        if (selectedGeometryType == "Area" && selectedUnit !in areaUnits) {
            selectedUnit = "Acre"
        }

        if (requestPermissions) {
            RequestPermissions(
                onPermissionGranted = {
                    coroutineScope.launch {
                        locationDisplay.setAutoPanMode(LocationDisplayAutoPanMode.Navigation)
                        locationDisplay.dataSource.start()
                    }
                }
            )
        }

        Box(modifier = Modifier.fillMaxSize()) {
            val mapViewProxy = MapViewProxy()


            MapView(
                modifier = Modifier.fillMaxSize(),
                mapViewProxy = mapViewProxy,
                arcGISMap = map,
                graphicsOverlays = listOf(graphicsOverlay),
                locationDisplay = locationDisplay,
                onSingleTapConfirmed = { screenCoordinate ->
                    if (isMeasuring.value) {
                        val mapPoint: Point? = mapViewProxy.screenToLocationOrNull(screenCoordinate.screenCoordinate)

                        if (mapPoint != null) {
                            pointsDrawn = true
                            val mapPointNotNull: Point = mapPoint
                            val mapPointProjected: Point? = GeometryEngine.projectOrNull(
                                mapPointNotNull,
                                SpatialReference.wgs84()
                            )
                            val latitude = mapPointProjected?.y
                            val longitude = mapPointProjected?.x

                            calloutLocation.value = mapPointProjected

                            Toast.makeText(
                                context,
                                "Map tapped at: Latitude: $latitude, Longitude: $longitude",
                                Toast.LENGTH_SHORT
                            ).show()

                            val relatedGraphics = mutableListOf<Graphic>()

                            when (selectedGeometryType) {
                                "Area" -> {
                                    val pointGraphic = Graphic(mapPoint, pointSymbol)
                                    graphicsOverlay.graphics.add(pointGraphic)
                                    relatedGraphics.add(pointGraphic)

                                    polygonPoints.add(mapPointNotNull)
                                    if (polygonPoints.size > 1) {
                                        val polyline = Polyline(polygonPoints)
                                        val lineGraphic = Graphic(polyline, lineSymbol)
                                        graphicsOverlay.graphics.add(lineGraphic)
                                        relatedGraphics.add(lineGraphic)

                                        // Remove previous text symbols
                                        graphicsOverlay.graphics.removeAll { it.symbol is TextSymbol }

                                        if (polygonPoints.size > 2) {
                                            val polygon = Polygon(polygonPoints)
                                            val polygonGraphic = Graphic(polygon, polygonSymbol)
                                            graphicsOverlay.graphics.add(polygonGraphic)
                                            relatedGraphics.add(polygonGraphic)

                                            val areaValue = GeometryEngine.areaGeodetic(
                                                polygon,
                                                getAreaUnit(selectedUnit),
                                                GeodeticCurveType.Geodesic
                                            )

                                            val centroid = GeometryEngine.labelPointOrNull(polygon)
                                            calloutLocation.value = centroid
//
                                            area =  areaValue
//                                            originalAreaUnit = selectedUnit
                                            areaText.value = formatMeasurementValue(areaValue, selectedUnit) // Update area text
                                            
                                        } else {
                                            val distanceValue = GeometryEngine.lengthGeodetic(
                                                polyline,
                                                getLinearUnit(selectedUnit),
                                                GeodeticCurveType.Geodesic
                                            )
                                            distance = distanceValue
                                            distanceText.value = formatMeasurementValue(distanceValue, selectedUnit) // Update area text
//                                            distanceTextState.postValue(distanceValue.toString())
                                        }
                                    }
                                }

                                "Distance" -> {
                                    val pointGraphic = Graphic(mapPointNotNull, pointSymbol)
                                    graphicsOverlay.graphics.add(pointGraphic)
                                    relatedGraphics.add(pointGraphic)

                                    linePoints.add(mapPointNotNull)

                                    if (linePoints.size > 1) {
                                        val polyline = Polyline(linePoints)
                                        val lineGraphic = Graphic(polyline, lineSymbol)
                                        graphicsOverlay.graphics.add(lineGraphic)
                                        relatedGraphics.add(lineGraphic)

                                        val distanceValue = GeometryEngine.lengthGeodetic(
                                            polyline,
                                            getLinearUnit(selectedUnit),
                                            GeodeticCurveType.Geodesic
                                        )
                                        cumulativeDistance += distanceValue
                                    }

                                    // Remove previous text symbols
                                    graphicsOverlay.graphics.removeAll { it.symbol is TextSymbol }

                                    // Only add the text symbol at the last point
                                    if (linePoints.size > 1) {
                                        linePoints.last()
                                        distance = cumulativeDistance
                                        distanceText.value = formatMeasurementValue(cumulativeDistance, selectedUnit)
//                                        distanceTextState.value = distanceText.value
                                        TextSymbol().apply {
                                            text = distanceText.value
                                            color = com.arcgismaps.Color.fromRgba(0, 0, 0, 255) // Black color
                                            size = 15f
                                        }
                                    }
                                }
                            }
                            graphicsStack.add(relatedGraphics)
                        }
                    }
                }
            )

            {
                calloutLocation.value?.let { location ->

                    Callout(location = location, offset = Offset(0f, -50f)) {
                        Text(
                            text = if (selectedGeometryType == "Area") {

                                "${areaText.value} ${originalAreaUnit}"

                            } else {
                                "${distanceText.value} ${originalDistanceUnit}"
//                                distanceText.value
                            }
                        )
                    }
                }
            }


            ImageButtonWithSpinner(mapType, context)

            IconButton(
                onClick = {
                    if (checkPermissions(context)) {
                        coroutineScope.launch {
                            locationDisplay.setAutoPanMode(LocationDisplayAutoPanMode.Navigation)
                            locationDisplay.dataSource.start()
                        }
                    } else {
                        requestPermissions = true
                    }
                },
                modifier = Modifier
                    .align(Alignment.CenterEnd)
                    .padding(end = 16.dp, top = 200.dp)
            ) {
                Image(
                    painter = painterResource(R.mipmap.ic_location),
                    contentDescription = "My Location"
                )
            }

            IconButton(
                onClick = {
                    showActionBar.value = true
                    isMeasuring.value = true
                },
                modifier = Modifier
                    .align(Alignment.CenterEnd)
                    .padding(end = 16.dp, top = 40.dp)
            ) {
                Image(
                    painter = painterResource(R.mipmap.ic_measure),
                    contentDescription = "Measure"
                )
            }

            if (showActionBar.value) {
                CustomTopBar(
                    selectedGeometryType = selectedGeometryType,
                    onGeometryTypeChange = { selectedGeometryType = it },
                    selectedUnit = selectedUnit,
//                    onUnitChange = { selectedUnit = it },
                    onUnitChange = { unit ->
                        selectedUnit = unit
//                        if (selectedGeometryType == "Distance") {
//                            updateDistanceUnits(unit)
//                        } else if (selectedGeometryType == "Area") {
//                            updateAreaUnits(originalArea, unit)
//                        }
                    },
                    distanceUnits = distanceUnits,
                    areaUnits = areaUnits,
                    onShowActionBarChange = { showActionBar.value = it },
                    onClearGraphics = {
                        clearGraphics()
                        linePoints.clear()
                        polygonPoints.clear()
                        calloutLocation.value = null // Reset callout location
                        distanceText.value = " " // Reset distance text
//                        distanceTextState.value = " "
                        areaText.value = " " // Reset area text
                        cumulativeDistance = 0.0 // Reset cumulative distance
                    },
                    pointsDrawn = pointsDrawn,
                    polygonSymbol = polygonSymbol

                )
            }


            // Call setupMapClickListener here
            setupMapClickListener()
        }

    }



    private fun getAreaUnit(unit: String): AreaUnit? {
        return when (unit) {
            "Square Meter" -> AreaUnit.squareMeters
            "Square Kilometer" -> AreaUnit.squareKilometers
            "Acre" -> AreaUnit(AreaUnitId.Acres)
            "Hectare" -> AreaUnit(AreaUnitId.Hectares)
            "Square Foot" -> AreaUnit.squareFeet
            else -> null
        }
    }

    private fun getLinearUnit(unit: String): LinearUnit? {
        return when (unit) {
            "Meter" -> LinearUnit.meters
            "Kilometre" -> LinearUnit.kilometers
            "Foot" -> LinearUnit.feet
            "Yard" -> LinearUnit(LinearUnitId.Yards)
            else -> null
        }
    }

    private fun formatMeasurementValue(value: Double, unit: String): String {
        var measureVal:String =  String.format("%.2f", value)
        var mutablemeasureVal = mutableStateOf(measureVal)
        return mutablemeasureVal.value
//        return "$value $unit"
    }

    private fun clearGraphics() {
        graphicsOverlay.graphics.clear()
        graphicsStack.clear()
//        linePoints.clear()
//        polygonPoints.clear()
    }
    private fun undoLastGraphic() {


        if (graphicsStack.isNotEmpty()) {
            val lastGraphics = graphicsStack.removeAt(graphicsStack.size - 1)
            lastGraphics.forEach { graphic ->
                graphicsOverlay.graphics.remove(graphic)
            }

//            val selectedGeometryType = "Distance"
            if (selectedGeometryType == "Area" && polygonPoints.isNotEmpty()) {
                polygonPoints.removeAt(polygonPoints.size - 1)
                if (polygonPoints.size < 3) {
                    // If less than 3 points, remove polygon & callout
                     calloutLocation.value = null
                    areaText.value = ""
                } else {
                    // Recalculate area after removing a point
                    convertToArea(polygonSymbol, originalAreaUnit, polygonPoints)
                    val polygon = Polygon(polygonPoints)
                    calloutLocation.value = GeometryEngine.labelPointOrNull(polygon)
                }
            }
            if (selectedGeometryType == "Distance" && linePoints.isNotEmpty()) {
                linePoints.removeAt(linePoints.size - 1)
                if (linePoints.size < 2) {
                    // If less than 2 points, remove polygon & callout
                     calloutLocation.value = null
                    distanceText.value = ""
                } else {
                    // Recalculate distance after removing a point
                    val polyline = Polyline(linePoints)
                    val distanceValue = GeometryEngine.lengthGeodetic(
                        polyline,
                        getLinearUnit(originalDistanceUnit),
                        GeodeticCurveType.Geodesic
                    )
                    distance = distanceValue
                    distanceText.value = formatMeasurementValue(distanceValue, originalDistanceUnit)

                    // Update callout to the last point
                    calloutLocation.value = linePoints.lastOrNull()
                }
            }

            // Hide callout if no graphics remain
            if (graphicsOverlay.graphics.isEmpty()) {
                calloutLocation.value = null
            }
        }
    }


    @Composable
    fun CustomTopBar(
        selectedGeometryType: String,
        onGeometryTypeChange: (String) -> Unit,
        selectedUnit: String,
        onUnitChange: (String) -> Unit,
        distanceUnits: List<String>,
        areaUnits: List<String>,
        onShowActionBarChange: (Boolean) -> Unit,
        onClearGraphics: () -> Unit,
        pointsDrawn: Boolean,
        polygonSymbol : Symbol?


    ) {
        var expanded by remember { mutableStateOf(false) }
        val units = if (selectedGeometryType == "Distance") distanceUnits else areaUnits

        Box(
            modifier = Modifier
                .fillMaxWidth()
                .padding(8.dp)
                .background(color = Color.DarkGray)
        ) {
            Row(
                modifier = Modifier.align(Alignment.CenterStart),
                verticalAlignment = Alignment.CenterVertically
            ) {

                IconButton(onClick = { onShowActionBarChange(false)
                    onClearGraphics()
                    isMeasuring.value = false
                }) {
                    Image(
                        painter = painterResource(R.mipmap.ic_backbutton),
                        contentDescription = "Back"
                    )
                }
            }
            Row(
                modifier = Modifier.align(Alignment.CenterEnd),
                verticalAlignment = Alignment.CenterVertically
            ) {

                if (pointsDrawn) {
                    IconButton(onClick = { undoLastGraphic() }) {
                        Image(
                            painter = painterResource(R.mipmap.ic_undo),
                            contentDescription = "Undo"
                        )
                    }
                    IconButton(onClick = {
                        onClearGraphics()
                        linePoints.clear()
                        polygonPoints.clear()



                    }) {
                        Image(
                            painter = painterResource(R.mipmap.ic_delete),
                            contentDescription = "Delete"
                        )
                    }
                }
                IconButton(onClick = { expanded = true }) {
                    Image(
                        painter = painterResource(R.mipmap.ic_selection),
                        contentDescription = "Settings"
                    )
                }
                DropdownMenu(
                    expanded = expanded,
                    onDismissRequest = { expanded = false },
                    modifier = Modifier.width(150.dp),
                ) {
                    Column(modifier = Modifier.padding(1.dp)) {
                        Text(text = "Select Geometry Type", color = Color.Blue)
                        Row(verticalAlignment = Alignment.CenterVertically) {
                            Checkbox(
                                checked = selectedGeometryType == "Distance",
                                onCheckedChange = {
                                    if (it) onGeometryTypeChange("Distance")
                                }
                            )
                            Text(text = "Distance")
                        }
                        Row(verticalAlignment = Alignment.CenterVertically) {
                            Checkbox(
                                checked = selectedGeometryType == "Area",
                                onCheckedChange = {
                                    if (it) onGeometryTypeChange("Area")
                                }
                            )
                            Text(text = "Area")
                        }

                        Text(text = "Select Unit", color = Color.Blue)
                        units.forEach { unit ->
                            Row(verticalAlignment = Alignment.CenterVertically) {
                                Checkbox(
                                    checked = selectedUnit == unit,
                                    onCheckedChange = { isChecked ->
                                        if (isChecked) {
//                                            selectedUnit = unit
                                            onUnitChange(unit)

                                            if (selectedGeometryType == "Distance" && areaUnits.contains(unit) &&linePoints.size >= 3){
                                                onGeometryTypeChange("Area")
                                                convertToArea(polygonSymbol, unit, linePoints.toList())
                                            }
                                            else {
                                                println("Conditions not met for converting to Area")
                                                 if (selectedGeometryType == "Distance") {
                                                    updateDistanceUnits(unit)
                                                } else if (selectedGeometryType == "Area") {
                                                    updateAreaUnits(unit)
                                                }
                                            }

                                        }
                                    }
                                )
                                Text(text = unit, modifier = Modifier.padding(4.dp))
                            }
                        }
                    }
                }
            }
        }
    }
Editor is loading...
Leave a Comment