Skip to content

Commit

Permalink
fix(PnX-SI/gn_mobile_occtax#262): allows the layer to be displayed or…
Browse files Browse the repository at this point in the history
… not by default
  • Loading branch information
sgrimault committed Sep 7, 2024
1 parent 9dffd71 commit 19ae766
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 77 deletions.
17 changes: 9 additions & 8 deletions maps/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,15 @@ Base URLs:

### Layer properties

| Parameter | Type | Default value | Description |
|------------------|--------|---------------|-----------------------------------------------------------------------------------------------------------------------------------------|
| `min_zoom` | Number | -1 | The minimum zoom level |
| `max_zoom` | Number | -1 | The maximum zoom level |
| `tile_size` | Number | 256 | The tile size in pixels (only applicable to tiles layers). |
| `tile_mime_type` | Number | `image/png` | The MIME type used for tiles (only applicable to tiles layers). |
| `attribution` | String | `null` | Describe the layer data and is often a legal obligation towards copyright holders and tile providers (only applicable to tiles layers). |
| `style` | Object | `null` | Define the layer style (only applicable to vector layers). |
| Parameter | Type | Default value | Description |
|--------------------|---------|---------------|-----------------------------------------------------------------------------------------------------------------------------------------|
| `shown_by_default` | Boolean | false | Whether to show this layer by default (default: `false`) |
| `min_zoom` | Number | -1 | The minimum zoom level where the layer is visible |
| `max_zoom` | Number | -1 | The maximum zoom level where the layer is visible |
| `tile_size` | Number | 256 | The tile size in pixels (only applicable to tiles layers). |
| `tile_mime_type` | Number | `image/png` | The MIME type used for tiles (only applicable to tiles layers). |
| `attribution` | String | `null` | Describe the layer data and is often a legal obligation towards copyright holders and tile providers (only applicable to tiles layers). |
| `style` | Object | `null` | Define the layer style (only applicable to vector layers). |

If an online tile layer is active but no attribution is defined, it will automatically be set to
its default value according to this layer:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class LayerSettingsViewModel @Inject constructor(
mapSettings.layersSettings.joinToString(
separator = "\n",
postfix = ",\nusing online layers: ${mapSettings.useOnlineLayers}"
) { "\t'${it.label}': ${it.source}' (active: ${it.properties.active})" }
) { "\t'${it.label}': ${it.source} (active: ${it.properties.active})" }
}..."
}

Expand All @@ -92,27 +92,25 @@ class LayerSettingsViewModel @Inject constructor(
.getOrDefault(emptyList())

// loads selected layers to show on the map or use the first eligible layer
layerRepository.getSelectedLayers()
.getOrDefault(emptyList())
.also {
if (it.isEmpty() && mapSettings.useOnlineLayers) {
listOfNotNull((allLayers.firstOrNull { layer -> layer.isOnline() }
?: allLayers.firstOrNull { layer -> !layer.isOnline() })?.also { layer ->
layerRepository.setSelectedLayers(listOf(layer))
})
}
}
((if (mapSettings.useOnlineLayers) listOfNotNull(allLayers.firstOrNull { layer -> layer.isOnline() && layer.properties.shownByDefault }
?: allLayers.firstOrNull { layer -> layer.isOnline() }) else emptyList()) + (allLayers.filter { layer -> !layer.isOnline() && layer.properties.shownByDefault }
.takeIf { layers -> layers.isNotEmpty() }
?: listOfNotNull(allLayers.firstOrNull { layer -> !layer.isOnline() }))).also { layerSettingsList ->
layerRepository.setSelectedLayers(layerSettingsList)
}
}
}

/**
* Load and show selected layers on the map.
*/
fun load(selectedLayersSettings: List<LayerSettings>) {
Logger.info {
"loading selected layers:\n${
selectedLayersSettings.joinToString("\n") { "\t'${it.label}': ${it.source} (active: ${it.properties.active})" }
}"
if (selectedLayersSettings.isNotEmpty()) {
Logger.info {
"loading selected layers:\n${
selectedLayersSettings.joinToString("\n") { "\t'${it.label}': ${it.source} (active: ${it.properties.active})" }
}"
}
}

viewModelScope.launch {
Expand Down Expand Up @@ -281,8 +279,7 @@ class LayerSettingsViewModel @Inject constructor(
it.second.isNotEmpty()
}
.map {
Triple(
it.first,
Triple(it.first,
it.second.map { file ->
when (file.extension) {
"geojson", "json" -> runCatching {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ class LayerRepositoryImpl(
layerSettings,
basePath
)
}.getOrNull() ?: LayerSettings.Builder()
}.onFailure {
Logger.error {
it.message ?: "failed to load layer from source ${layerSettings.source}"
}
}
.getOrNull() ?: LayerSettings.Builder()
.from(layerSettings)
.properties(
LayerPropertiesSettings.Builder.newInstance()
Expand All @@ -70,14 +75,14 @@ class LayerRepositoryImpl(
}

override suspend fun addLayerFromURI(uri: Uri): Result<LayerSettings> {
Logger.info { "loading layer from URI '${uri}'..." }

return runCatching { localLayerDataSource.buildLocalLayerFromUri(uri) }.onFailure {
Logger.error {
it.message
?: "failed to load local layer from URI '${uri}'"
}
}.onSuccess {
layers.add(it)
Logger.error { it.message ?: "failed to load local layer from URI '${uri}'" }
}
.onSuccess {
layers.add(it)
}
}

override suspend fun getSelectedLayers(): Result<List<LayerSettings>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,52 @@ import kotlinx.parcelize.Parcelize
*/
@Parcelize
data class LayerPropertiesSettings(

/**
* Whether this layer is ready to be displayed on the map (default: `true`).
*/
val active: Boolean = Builder.newInstance().active,

/**
* Whether to show this layer by default (default: `false`).
*/
val shownByDefault: Boolean = Builder.newInstance().shownByDefault,

/**
* The minimum zoom level where the layer is visible (default: -1, ie. no restriction).
*/
val minZoomLevel: Int = Builder.newInstance().minZoomLevel,

/**
* The maximum zoom level where the layer is visible (default: -1, ie. no restriction).
*/
val maxZoomLevel: Int = Builder.newInstance().maxZoomLevel,

/**
* The tile size in pixels (only applicable to tiles layers, default: 256).
*/
val tileSizePixels: Int = Builder.newInstance().tileSizePixels,

/**
* The MIME type used for tiles (only applicable to tiles layers, default: `image/png`).
*/
val tileMimeType: String? = Builder.newInstance().tileMimeType,

/**
* Describe the layer data and is often a legal obligation towards copyright holders and tile
* providers (only applicable to tiles layers).
*/
val attribution: String? = Builder.newInstance().attribution,

/**
* Define the layer style (only applicable to vector layers).
*/
val style: LayerStyleSettings? = Builder.newInstance().style
) : Parcelable {

private constructor(builder: Builder) : this(
builder.active,
builder.shownByDefault,
builder.minZoomLevel,
builder.maxZoomLevel,
builder.tileSizePixels,
Expand All @@ -33,6 +68,9 @@ data class LayerPropertiesSettings(
internal var active: Boolean = true
private set

internal var shownByDefault: Boolean = false
private set

internal var minZoomLevel: Int = -1
private set

Expand All @@ -56,6 +94,7 @@ data class LayerPropertiesSettings(
if (layerPropertiesSettings == null) return@apply

active(layerPropertiesSettings.active)
shownByDefault(layerPropertiesSettings.shownByDefault)
if (layerPropertiesSettings.minZoomLevel >= 0) minZoomLevel(layerPropertiesSettings.minZoomLevel)
if (layerPropertiesSettings.maxZoomLevel > 0) maxZoomLevel(layerPropertiesSettings.maxZoomLevel)
if (layerPropertiesSettings.tileSizePixels > 0) tileSizePixels(layerPropertiesSettings.tileSizePixels)
Expand All @@ -68,6 +107,10 @@ data class LayerPropertiesSettings(
this.active = active
}

fun shownByDefault(shownByDefault: Boolean = false) = apply {
this.shownByDefault = shownByDefault
}

fun minZoomLevel(minZoomLevel: Int = 0) =
apply {
this.minZoomLevel = minZoomLevel.coerceIn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ class MapSettingsReader(private val fromExistingMapSettings: MapSettings? = null

while (reader.hasNext()) {
when (reader.nextName()) {
"shown_by_default" -> builder.shownByDefault(reader.nextBoolean())
"min_zoom" -> builder.minZoomLevel(reader.nextInt())
"max_zoom" -> builder.maxZoomLevel(reader.nextInt())
"tile_size" -> builder.tileSizePixels(reader.nextInt())
Expand Down
7 changes: 3 additions & 4 deletions maps/src/main/java/fr/geonature/maps/ui/MapFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ open class MapFragment : Fragment() {
}

Logger.info { "new layer to add from URI '$uri'" }

layerSettingsViewModel.addLayer(uri)
}
}
Expand Down Expand Up @@ -211,6 +210,7 @@ open class MapFragment : Fragment() {
myLocationFab.onResume()

context?.also { context ->
Logger.debug { "onResume, with context" }
showCompass(context).also {
rotateCompassFab.setMapView(mapView)
rotateCompassFab.visibility = if (it) View.VISIBLE else View.GONE
Expand Down Expand Up @@ -296,9 +296,8 @@ open class MapFragment : Fragment() {
CoroutineScope(Main).launch {
val selectedLayers = layerSettingsViewModel.getSelectedLayers()
.ifEmpty {
if (useOnlineLayers(context)) listOfNotNull(
layerSettingsViewModel.getAllLayers()
.firstOrNull { it.isOnline() }) else emptyList()
if (useOnlineLayers(context)) listOfNotNull(layerSettingsViewModel.getAllLayers()
.firstOrNull { it.isOnline() }) else emptyList()
}

layerSettingsViewModel.load(selectedLayers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fr.geonature.maps.ui.widget

import android.Manifest
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.AnimationDrawable
import android.location.Location
Expand Down Expand Up @@ -61,6 +62,7 @@ class MyLocationButton(
)
drawable?.setTint(Color.DKGRAY)
setImageDrawable(drawable)
imageTintList = ColorStateList.valueOf(Color.DKGRAY)

myLocationState = MyLocationState.ACTIVE
}
Expand Down Expand Up @@ -134,13 +136,15 @@ class MyLocationButton(
location
)
drawable?.setTint(ThemeUtils.getAccentColor(context))
setImageDrawable(drawable)
imageTintList = ColorStateList.valueOf(ThemeUtils.getAccentColor(context))
MyLocationState.ACTIVE_TRACKER
} else {
drawable?.setTint(Color.DKGRAY)
setImageDrawable(drawable)
imageTintList = ColorStateList.valueOf(Color.DKGRAY)
MyLocationState.ACTIVE
}

setImageDrawable(drawable)
}

override fun onLocationOutsideBoundaries(location: Location?) {
Expand Down Expand Up @@ -187,6 +191,7 @@ class MyLocationButton(
)
drawable?.setTint(ThemeUtils.getAccentColor(context))
setImageDrawable(drawable)
imageTintList = ColorStateList.valueOf(ThemeUtils.getAccentColor(context))

myLocationState = MyLocationState.ACTIVE_TRACKER
}
Expand Down
Loading

1 comment on commit 19ae766

@camillemonchicourt
Copy link
Member

Choose a reason for hiding this comment

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

It should be TRUE by default

Please sign in to comment.