Skip to content

Commit

Permalink
Better viewmodel handling
Browse files Browse the repository at this point in the history
  • Loading branch information
darryl-lynch committed Jan 15, 2025
1 parent ebf96ca commit 676d24d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,9 @@ import com.arcgismaps.mapping.view.geometryeditor.GeometryEditor
import com.arcgismaps.mapping.view.geometryeditor.GeometryEditorStyle
import com.arcgismaps.toolkit.geoviewcompose.MapViewProxy
import com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

class CreateAndEditGeometriesViewModel(
application: Application,
private val sampleCoroutineScope: CoroutineScope
) : AndroidViewModel(application) {
class CreateAndEditGeometriesViewModel(application: Application) : AndroidViewModel(application) {
// create a map with the imagery basemap style
val arcGISMap by mutableStateOf(
ArcGISMap(BasemapStyle.ArcGISImagery).apply {
Expand Down Expand Up @@ -126,7 +122,7 @@ class CreateAndEditGeometriesViewModel(
* [singleTapConfirmedEvent].
*/
fun identify(singleTapConfirmedEvent: SingleTapConfirmedEvent) {
sampleCoroutineScope.launch {
viewModelScope.launch {
val graphicsResult = mapViewProxy.identifyGraphicsOverlays(
screenCoordinate = singleTapConfirmedEvent.screenCoordinate,
tolerance = 10.0.dp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@

package com.esri.arcgismaps.sample.createandeditgeometries.screens

import android.app.Application
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.viewmodel.compose.viewModel
import com.arcgismaps.toolkit.geoviewcompose.MapView
import com.esri.arcgismaps.sample.createandeditgeometries.components.CreateAndEditGeometriesViewModel
import com.esri.arcgismaps.sample.sampleslib.components.MessageDialog
Expand All @@ -36,12 +33,8 @@ import com.esri.arcgismaps.sample.sampleslib.components.SampleTopAppBar
*/
@Composable
fun CreateAndEditGeometriesScreen(sampleName: String) {
// coroutineScope that will be cancelled when this call leaves the composition
val sampleCoroutineScope = rememberCoroutineScope()
// get the application property that will be used to construct MapViewModel
val sampleApplication = LocalContext.current.applicationContext as Application
// create a ViewModel to handle MapView interactions
val mapViewModel = remember { CreateAndEditGeometriesViewModel(sampleApplication, sampleCoroutineScope) }
val mapViewModel: CreateAndEditGeometriesViewModel = viewModel()

Scaffold(
topBar = { SampleTopAppBar(title = sampleName) },
Expand Down

0 comments on commit 676d24d

Please sign in to comment.