Skip to content

Commit

Permalink
Move string handling to UI
Browse files Browse the repository at this point in the history
  • Loading branch information
darryl-lynch committed Dec 27, 2024
1 parent b872835 commit de792be
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ import com.arcgismaps.toolkit.geoviewcompose.MapViewProxy
import com.esri.arcgismaps.sample.projectgeometry.R
import com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import java.util.Locale

data class PointProjection (val original: Point, val projection: Point)

class ProjectGeometryViewModel(val app: Application) : AndroidViewModel(app) {
// create a map with a navigation night basemap style
Expand All @@ -55,6 +56,10 @@ class ProjectGeometryViewModel(val app: Application) : AndroidViewModel(app) {
// Create a message dialog view model for handling error messages
val messageDialogVM = MessageDialogViewModel()

// state flow of a point and its projection for presentation in UI
private val _pointProjectionFlow = MutableStateFlow<PointProjection?>(null)
val pointFlow = _pointProjectionFlow.asStateFlow()

// setup the red pin marker image as bitmap drawable
private val markerDrawable: BitmapDrawable by lazy {
// load the bitmap from resources and create a drawable
Expand All @@ -80,9 +85,6 @@ class ProjectGeometryViewModel(val app: Application) : AndroidViewModel(app) {
graphics.add(markerGraphic)
}

private val _infoText = MutableStateFlow(app.resources.getString(R.string.tap_to_begin))
val infoText: StateFlow<String> = _infoText

init {
viewModelScope.launch {
arcGISMap.load().onFailure { error ->
Expand All @@ -109,18 +111,9 @@ class ProjectGeometryViewModel(val app: Application) : AndroidViewModel(app) {
geometry = point,
spatialReference = SpatialReference.wgs84()
)
_infoText.value = app.resources.getString(
R.string.projection_info_text,
point.toDisplayFormat(),
projectedPoint?.toDisplayFormat()
)
projectedPoint?.let { projection ->
_pointProjectionFlow.value = PointProjection(point, projection)
}
}
}
}

/**
* Extension function for the Point type that returns
* a float-precision formatted string suitable for display
*/
private fun Point.toDisplayFormat() =
"${String.format(Locale.getDefault(),"%.5f", x)}, ${String.format(Locale.getDefault(),"%.5f", y)}"
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,34 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.arcgismaps.geometry.Point
import com.arcgismaps.toolkit.geoviewcompose.MapView
import com.esri.arcgismaps.sample.projectgeometry.R
import com.esri.arcgismaps.sample.projectgeometry.components.ProjectGeometryViewModel
import com.esri.arcgismaps.sample.sampleslib.components.MessageDialog
import com.esri.arcgismaps.sample.sampleslib.components.SampleTopAppBar
import java.util.Locale

/**
* Main screen layout for the sample app
*/
@Composable
fun ProjectGeometryScreen(sampleName: String) {
val mapViewModel: ProjectGeometryViewModel = viewModel()
val infoText by mapViewModel.infoText.collectAsState()
val projectionInfo by mapViewModel.pointFlow.collectAsState()

val infoText =
if (projectionInfo == null) {
"Tap to begin"
}
else {
stringResource(
R.string.projection_info_text,
projectionInfo?.original?.toDisplayFormat()!!,
projectionInfo?.projection?.toDisplayFormat()!!
)
}

Scaffold(
topBar = { SampleTopAppBar(title = sampleName) },
content = {
Expand Down Expand Up @@ -88,3 +103,10 @@ fun ProjectGeometryScreen(sampleName: String) {
}
)
}

/**
* Extension function for the Point type that returns
* a float-precision formatted string suitable for display
*/
private fun Point.toDisplayFormat() =
"${String.format(Locale.getDefault(),"%.5f", x)}, ${String.format(Locale.getDefault(),"%.5f", y)}"

0 comments on commit de792be

Please sign in to comment.