Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update new module script to use geo-compose toolkit component #148

Merged
merged 10 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions display-composable-mapview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,7 @@ dependencies {
implementation "androidx.compose.ui:ui-tooling"
implementation "androidx.compose.ui:ui-tooling-preview"
implementation project(path: ':samples-lib')
// Toolkit dependencies
implementation(platform("com.esri:arcgis-maps-kotlin-toolkit-bom:200.3.0-1"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you update this sample with the flag arcgisToolkitVersion and update the versions.gradle with the build tag of geo-compose:

Suggested change
implementation(platform("com.esri:arcgis-maps-kotlin-toolkit-bom:200.3.0-1"))
implementation(platform("com.esri:arcgis-maps-kotlin-toolkit-bom:$arcgisToolkitVersion"))

implementation('com.esri:arcgis-maps-kotlin-toolkit-geo-compose')
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the new module created when the script is used, but I don't think it should require any sample changes. The DisplayComposableMapView sample should be a basic implementation of the DisplayMap sample, i.e. render a map with a basemap style.

The module script can spin up a sample with touch event implementation, but let's keep this sample similar to DisplayMap with the geo-compose module.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am fine with this change. Although with the previous version of display-composable-mapview sample, we did have the touch event implementation in addition to displaying the map with a particular basemap style. Not sure if there was any particular intention of doing it that way back then? But I too feel it should be similar to the display map sample.

Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ package com.esri.arcgismaps.sample.displaycomposablemapview
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import com.arcgismaps.ApiKey
import com.arcgismaps.ArcGISEnvironment
import com.arcgismaps.mapping.ArcGISMap
import com.arcgismaps.mapping.BasemapStyle
import com.arcgismaps.mapping.Viewpoint
import com.arcgismaps.toolkit.geocompose.MapView
import com.arcgismaps.toolkit.geocompose.MapViewpointOperation
import com.esri.arcgismaps.sample.sampleslib.theme.SampleAppTheme

class MainActivity : ComponentActivity() {
Expand All @@ -47,27 +47,20 @@ class MainActivity : ComponentActivity() {

setContent {
SampleAppTheme {
Column(
// a mutable/immutable state is computed by remember to store its value during
// initial composition, and updates the composition on the state value change
var viewpoint by remember { mutableStateOf(viewpointAmerica) }
val map by remember { mutableStateOf(ArcGISMap(BasemapStyle.ArcGISNavigationNight)) }
MapView(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally
) {
// a mutable/immutable state is computed by remember to store its value during
// initial composition, and updates the composition on the state value change
var viewpoint by remember { mutableStateOf(viewpointAmerica) }
val map by remember { mutableStateOf(ArcGISMap(BasemapStyle.ArcGISNavigationNight)) }
arcGISMap = map,
viewpointOperation = MapViewpointOperation.Set(viewpoint = viewpoint),
onSingleTapConfirmed = {
viewpoint =
if (viewpoint == viewpointAmerica) viewpointAsia else viewpointAmerica
}
)

// Composable function that wraps the MapView
MapViewWithCompose(
arcGISMap = map,
viewpoint = viewpoint,
// lambda to retrieve the MapView's onSingleTapConfirmed
onSingleTap = {
// swap between America and Asia viewpoints
viewpoint =
if (viewpoint == viewpointAmerica) viewpointAsia else viewpointAmerica
}
)
}
}
}
}
Expand Down

This file was deleted.

Binary file modified tools/NewModuleScript.jar
Binary file not shown.
87 changes: 0 additions & 87 deletions tools/NewModuleScript/ComposeMapViewTemplate.kt

This file was deleted.

32 changes: 16 additions & 16 deletions tools/NewModuleScript/MainScreenTemplate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@
package com.esri.arcgismaps.sample.displaycomposablemapview.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.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import com.esri.arcgismaps.sample.displaycomposablemapview.components.ComposeMapView
import com.arcgismaps.mapping.ArcGISMap
import com.arcgismaps.mapping.BasemapStyle
import com.arcgismaps.toolkit.geocompose.MapView
import com.arcgismaps.toolkit.geocompose.MapViewpointOperation
import com.esri.arcgismaps.sample.displaycomposablemapview.components.MapViewModel
import com.esri.arcgismaps.sample.sampleslib.components.SampleTopAppBar

Expand All @@ -34,24 +39,19 @@ import com.esri.arcgismaps.sample.sampleslib.components.SampleTopAppBar
fun MainScreen(sampleName: String, application: Application) {
// create a ViewModel to handle MapView interactions
val mapViewModel = MapViewModel(application)
val arcGISMap by remember { mutableStateOf(ArcGISMap(BasemapStyle.ArcGISNavigationNight)) }

Scaffold(
topBar = { SampleTopAppBar(title = sampleName) },
content = {
Column(
modifier = Modifier
.fillMaxSize()
.padding(it)
) {
// composable function that wraps the MapView
ComposeMapView(
modifier = Modifier.fillMaxSize(),
mapViewModel = mapViewModel,
onSingleTap = {
mapViewModel.changeBasemap()
}
)
}
MapView(
modifier = Modifier.fillMaxSize().padding(it),
arcGISMap = arcGISMap,
viewpointOperation = MapViewpointOperation.Set(viewpoint = mapViewModel.viewpoint.value),
onSingleTapConfirmed = {
mapViewModel.changeBasemap()
}
)
}
)
}
29 changes: 6 additions & 23 deletions tools/NewModuleScript/MapViewModelTemplate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,19 @@
package com.esri.arcgismaps.sample.displaycomposablemapview.components

import android.app.Application
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.AndroidViewModel
import com.arcgismaps.mapping.ArcGISMap
import com.arcgismaps.mapping.BasemapStyle
import com.arcgismaps.mapping.Viewpoint
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update

class MapViewModel(application: Application) : AndroidViewModel(application) {
// set the MapView mutable stateflow
val mapViewState = MutableStateFlow(MapViewState())

private val viewpointAmerica = Viewpoint(39.8, -98.6, 10e7)
private val viewpointAsia = Viewpoint(39.8, 98.6, 10e7)
var viewpoint = mutableStateOf(viewpointAmerica)
/**
* Switch between two basemaps
*/
fun changeBasemap() {
val newArcGISMap: ArcGISMap =
if (mapViewState.value.arcGISMap.basemap.value?.name.equals("ArcGIS:NavigationNight")) {
ArcGISMap(BasemapStyle.ArcGISStreets)
} else {
ArcGISMap(BasemapStyle.ArcGISNavigationNight)
}
mapViewState.update { it.copy(arcGISMap = newArcGISMap) }
viewpoint.value =
if (viewpoint.value == viewpointAmerica) viewpointAsia else viewpointAmerica
}
}


/**
* Data class that represents the MapView state
*/
data class MapViewState( // This would change based on each sample implementation
var arcGISMap: ArcGISMap = ArcGISMap(BasemapStyle.ArcGISNavigationNight),
var viewpoint: Viewpoint = Viewpoint(39.8, -98.6, 10e7)
)
16 changes: 0 additions & 16 deletions tools/NewModuleScript/src/main/java/ScriptMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ private void createFilesAndFolders() {

// Copy Kotlin template files to new sample
File mainActivityTemplate = new File(samplesRepoPath + "/tools/NewModuleScript/MainActivityTemplate.kt");
File composeMapViewTemplate = new File(samplesRepoPath + "/tools/NewModuleScript/ComposeMapViewTemplate.kt");
File mapViewModelTemplate = new File(samplesRepoPath + "/tools/NewModuleScript/MapViewModelTemplate.kt");
File mainScreenTemplate = new File(samplesRepoPath + "/tools/NewModuleScript/MainScreenTemplate.kt");

Expand All @@ -111,9 +110,6 @@ private void createFilesAndFolders() {

File composeComponentsDir = new File(packageDirectory + "/components");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be renamed to componentsDir ?

composeComponentsDir.mkdirs();
FileUtils.copyFileToDirectory(composeMapViewTemplate, composeComponentsDir);
source = Paths.get(composeComponentsDir+"/ComposeMapViewTemplate.kt");
Files.move(source, source.resolveSibling("ComposeMapView.kt"));

FileUtils.copyFileToDirectory(mapViewModelTemplate, composeComponentsDir);
source = Paths.get(composeComponentsDir+"/MapViewModelTemplate.kt");
Expand Down Expand Up @@ -201,18 +197,6 @@ private void updateSampleContent() {
exitProgram(e);
}

//Update ComposeMapView.kt
file = new File(samplesRepoPath + "/" + sampleWithHyphen + "/src/main/java/com/esri/arcgismaps/sample/"+sampleWithoutSpaces+"/components/ComposeMapView.kt");
try {
String fileContent = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
fileContent = fileContent.replace("Copyright 2023", "Copyright " + Calendar.getInstance().get(Calendar.YEAR));
fileContent = fileContent.replace("sample.displaycomposablemapview", "sample." + sampleWithoutSpaces);
FileUtils.write(file,fileContent, StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
exitProgram(e);
}

//Update MapViewModel.kt
file = new File(samplesRepoPath + "/" + sampleWithHyphen + "/src/main/java/com/esri/arcgismaps/sample/"+sampleWithoutSpaces+"/components/MapViewModel.kt");
try {
Expand Down
Loading