Skip to content

Commit

Permalink
simplify app logic to initialize the viewmodel
Browse files Browse the repository at this point in the history
add optimization to the initialize method, to initialize only if state is not already initialized
  • Loading branch information
puneet-pdx committed Sep 19, 2024
1 parent 9aeadee commit 3ed8557
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package com.arcgismaps.toolkit.utilitynetworktraceapp
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
Expand All @@ -45,24 +44,22 @@ import androidx.compose.ui.unit.dp
import com.arcgismaps.ArcGISEnvironment
import com.arcgismaps.httpcore.authentication.TokenCredential
import com.arcgismaps.toolkit.utilitynetworktraceapp.screens.MainScreen
import com.arcgismaps.toolkit.utilitynetworktraceapp.screens.TraceViewModel
import com.esri.microappslib.theme.MicroAppTheme

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val traceViewModel: TraceViewModel by viewModels()
setContent {
MicroAppTheme {
UtilityNetworkTraceApp(traceViewModel)
UtilityNetworkTraceApp()
}
}
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun UtilityNetworkTraceApp(traceViewModel: TraceViewModel) {
fun UtilityNetworkTraceApp() {
var initialized by remember { mutableStateOf(false) }

LaunchedEffect(Unit) {
Expand All @@ -81,7 +78,7 @@ fun UtilityNetworkTraceApp(traceViewModel: TraceViewModel) {
topBar = { TopAppBar(title = { Text("UtilityNetworkTraceApp") }) }
) {
Box(Modifier.padding(it)) {
MainScreen(traceViewModel)
MainScreen()
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.arcgismaps.LoadStatus
import com.arcgismaps.toolkit.geoviewcompose.MapView
import com.arcgismaps.toolkit.utilitynetworks.AddStartingPointMode
Expand All @@ -42,7 +43,7 @@ import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MainScreen(viewModel: TraceViewModel) {
fun MainScreen(viewModel: TraceViewModel = viewModel()) {

val loadState by viewModel.arcGISMap.loadStatus.collectAsState()
val coroutineScope = rememberCoroutineScope()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ public class TraceState(
* @since 200.6.0
*/
public suspend fun initialize(): Result<Unit> {
if (_initializationStatus.value is InitializationStatus.Initialized) {
return Result.success(Unit)
}
_initializationStatus.value = InitializationStatus.Initializing
var result = Result.success(Unit)
arcGISMap.load().onSuccess {
Expand Down

0 comments on commit 3ed8557

Please sign in to comment.