|
| 1 | +/* Copyright 2025 Esri |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + * |
| 15 | + */ |
| 16 | + |
| 17 | +package com.esri.arcgismaps.sample.stylefeatureswithcustomdictionary.components |
| 18 | + |
| 19 | +import android.app.Application |
| 20 | +import androidx.lifecycle.AndroidViewModel |
| 21 | +import androidx.lifecycle.viewModelScope |
| 22 | +import com.arcgismaps.mapping.ArcGISMap |
| 23 | +import com.arcgismaps.mapping.BasemapStyle |
| 24 | +import com.arcgismaps.mapping.Viewpoint |
| 25 | +import com.arcgismaps.mapping.layers.FeatureLayer |
| 26 | +import com.arcgismaps.mapping.symbology.DictionaryRenderer |
| 27 | +import com.arcgismaps.mapping.symbology.DictionarySymbolStyle |
| 28 | +import com.arcgismaps.portal.Portal |
| 29 | +import com.arcgismaps.mapping.PortalItem |
| 30 | +import com.arcgismaps.data.ServiceFeatureTable |
| 31 | +import com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModel |
| 32 | +import com.esri.arcgismaps.sample.stylefeatureswithcustomdictionary.R |
| 33 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 34 | +import kotlinx.coroutines.launch |
| 35 | +import java.io.File |
| 36 | + |
| 37 | +private const val RESTAURANTS_SERVICE_URL = |
| 38 | + "https://services2.arcgis.com/ZQgQTuoyBrtmoGdP/arcgis/rest/services/Redlands_Restaurants/FeatureServer/0" |
| 39 | +private const val WEB_STYLE_ITEM_ID = "adee951477014ec68d7cf0ea0579c800" |
| 40 | + |
| 41 | +class StyleFeaturesWithCustomDictionaryViewModel(app: Application) : AndroidViewModel(app) { |
| 42 | + |
| 43 | + // Feature layer showing restaurant data in Redlands, CA |
| 44 | + private val restaurantFeatureLayer = FeatureLayer.createWithFeatureTable( |
| 45 | + featureTable = ServiceFeatureTable(uri = RESTAURANTS_SERVICE_URL) |
| 46 | + ) |
| 47 | + var arcGISMap = ArcGISMap(BasemapStyle.ArcGISTopographic).apply { |
| 48 | + initialViewpoint = Viewpoint( |
| 49 | + latitude = 34.0543, |
| 50 | + longitude = -117.1963, |
| 51 | + scale = 1e4 |
| 52 | + ) |
| 53 | + } |
| 54 | + private var dictionaryRendererFromStyleFile: DictionaryRenderer? = null |
| 55 | + private var dictionaryRendererFromWebStyle: DictionaryRenderer? = null |
| 56 | + |
| 57 | + private val _selectedStyle = MutableStateFlow(CustomDictionaryStyle.StyleFile) |
| 58 | + |
| 59 | + val messageDialogVM = MessageDialogViewModel() |
| 60 | + private val provisionPath: String by lazy { |
| 61 | + app.getExternalFilesDir(null)?.path.toString() + File.separator + app.getString(R.string.style_features_with_custom_dictionary_app_name) |
| 62 | + } |
| 63 | + |
| 64 | + init { |
| 65 | + viewModelScope.launch { |
| 66 | + // Prepare the dictionary renderer from the style file |
| 67 | + dictionaryRendererFromStyleFile = createDictionaryRendererFromStyleFile() |
| 68 | + // Prepare the dictionary renderer from the web style |
| 69 | + dictionaryRendererFromWebStyle = createDictionaryRendererFromWebStyle().getOrElse { |
| 70 | + messageDialogVM.showMessageDialog(it) |
| 71 | + return@launch |
| 72 | + } |
| 73 | + |
| 74 | + // Apply the renderer for the initially selected style |
| 75 | + applyRendererForSelectedStyle() |
| 76 | + |
| 77 | + // Add the restaurant layer to the map and load the map |
| 78 | + arcGISMap.apply { |
| 79 | + operationalLayers.add(restaurantFeatureLayer) |
| 80 | + }.load().onFailure { |
| 81 | + messageDialogVM.showMessageDialog(it) |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Update the current dictionary style selection and apply the renderer to the feature layer. |
| 88 | + */ |
| 89 | + fun updateSelectedStyle(style: CustomDictionaryStyle) { |
| 90 | + _selectedStyle.value = style |
| 91 | + applyRendererForSelectedStyle() |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Apply the dictionary renderer to the restaurants layer. |
| 96 | + */ |
| 97 | + private fun applyRendererForSelectedStyle() { |
| 98 | + restaurantFeatureLayer.renderer = when (_selectedStyle.value) { |
| 99 | + CustomDictionaryStyle.StyleFile -> dictionaryRendererFromStyleFile |
| 100 | + CustomDictionaryStyle.WebStyle -> dictionaryRendererFromWebStyle |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Create a dictionary renderer from the style file included in the app's assets. |
| 106 | + */ |
| 107 | + private fun createDictionaryRendererFromStyleFile(): DictionaryRenderer { |
| 108 | + val restaurantsStyleFile = File(provisionPath, "Restaurant.stylx") |
| 109 | + check(restaurantsStyleFile.exists()) { |
| 110 | + "Style file not found. Expected at: ${restaurantsStyleFile.canonicalPath}" |
| 111 | + } |
| 112 | + val restaurantStyle = DictionarySymbolStyle.createFromFile(restaurantsStyleFile.path) |
| 113 | + |
| 114 | + return DictionaryRenderer(dictionarySymbolStyle = restaurantStyle) |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Create a dictionary renderer from the web style hosted as a Portal item. |
| 119 | + * Maps the feature layer's field "healthgrade" to the dictionary style's expected field "Inspection". |
| 120 | + */ |
| 121 | + private suspend fun createDictionaryRendererFromWebStyle(): Result<DictionaryRenderer> { |
| 122 | + val portal = Portal(url = "https://www.arcgis.com") |
| 123 | + val portalItem = PortalItem(portal = portal, itemId = WEB_STYLE_ITEM_ID) |
| 124 | + val restaurantSymbolStyle = DictionarySymbolStyle(portalItem).apply { |
| 125 | + load().getOrElse { |
| 126 | + return Result.failure(it) |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return Result.success( |
| 131 | + DictionaryRenderer( |
| 132 | + dictionarySymbolStyle = restaurantSymbolStyle, |
| 133 | + symbologyFieldOverrides = mapOf("healthgrade" to "Inspection") |
| 134 | + ) |
| 135 | + ) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +/** |
| 140 | + * Enum representing the two custom dictionary styles available in this sample. |
| 141 | + */ |
| 142 | +enum class CustomDictionaryStyle { |
| 143 | + StyleFile, |
| 144 | + WebStyle |
| 145 | +} |
0 commit comments