Skip to content

Commit

Permalink
feat(PnX-SI/gn_mobile_occtax#95): allow to override map settings fro…
Browse files Browse the repository at this point in the history
…m existing one
  • Loading branch information
sgrimault committed Aug 15, 2023
1 parent ebcd394 commit 3220b87
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class HomeListFragment : Fragment() {
listOf(
MenuItem(
getString(R.string.home_menu_entry_default),
MapSettings.Builder.newInstance()
MapSettings.Builder()
.minZoomLevel(3.0)
.zoom(6.0)
.editMode(EditFeatureButton.EditMode.SINGLE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class PreferencesFragment : PreferenceFragmentCompat() {

MapSettingsPreferencesUtils.setDefaultPreferences(
context,
MapSettings.Builder.newInstance()
MapSettings.Builder()
.from(appSettings)
.build(),
preferenceScreen
Expand Down
2 changes: 1 addition & 1 deletion maps/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
id 'org.jetbrains.kotlin.android'
}

version = "0.8.1"
version = "0.8.2"

android {
namespace 'fr.geonature.maps'
Expand Down
124 changes: 54 additions & 70 deletions maps/src/main/java/fr/geonature/maps/settings/MapSettings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import org.osmdroid.util.GeoPoint
data class MapSettings(
private val _layersSettings: List<LayerSettings>,
val baseTilesPath: String?,
val useOnlineLayers: Boolean = Builder.newInstance().useOnlineLayers,
val showCompass: Boolean = Builder.newInstance().showCompass,
val showScale: Boolean = Builder.newInstance().showScale,
val showZoom: Boolean = Builder.newInstance().showZoom,
val rotationGesture: Boolean = Builder.newInstance().rotationGesture,
val editMode: EditFeatureButton.EditMode = Builder.newInstance().editMode,
val useOnlineLayers: Boolean = Builder().useOnlineLayers,
val showCompass: Boolean = Builder().showCompass,
val showScale: Boolean = Builder().showScale,
val showZoom: Boolean = Builder().showZoom,
val rotationGesture: Boolean = Builder().rotationGesture,
val editMode: EditFeatureButton.EditMode = Builder().editMode,
val zoom: Double = 0.0,
val minZoomLevel: Double = 0.0,
val maxZoomLevel: Double = 0.0,
Expand Down Expand Up @@ -175,90 +175,74 @@ data class MapSettings(
internal var center: GeoPoint? = null
private set

fun from(mapSettings: MapSettings?) =
apply {
if (mapSettings == null) return@apply

this.layersSettings.addAll(mapSettings._layersSettings)
this.baseTilesPath = mapSettings.baseTilesPath
this.useOnlineLayers = mapSettings.useOnlineLayers
this.showCompass = mapSettings.showCompass
this.showScale = mapSettings.showScale
this.showZoom = mapSettings.showZoom
this.rotationGesture = mapSettings.rotationGesture
this.editMode = mapSettings.editMode
this.zoom = mapSettings.zoom
this.minZoomLevel = mapSettings.minZoomLevel
this.maxZoomLevel = mapSettings.maxZoomLevel
this.minZoomEditing = mapSettings.minZoomEditing
this.maxBounds = mapSettings.maxBounds
this.center = mapSettings.center
}

fun baseTilesPath(baseTilesPath: String) =
apply { this.baseTilesPath = baseTilesPath }
/**
* Makes a copy of given [MapSettings].
*/
fun from(mapSettings: MapSettings?) = apply {
if (mapSettings == null) return@apply

this.layersSettings.addAll(mapSettings._layersSettings)
this.baseTilesPath = mapSettings.baseTilesPath
this.useOnlineLayers = mapSettings.useOnlineLayers
this.showCompass = mapSettings.showCompass
this.showScale = mapSettings.showScale
this.showZoom = mapSettings.showZoom
this.rotationGesture = mapSettings.rotationGesture
this.editMode = mapSettings.editMode
this.zoom = mapSettings.zoom
this.minZoomLevel = mapSettings.minZoomLevel
this.maxZoomLevel = mapSettings.maxZoomLevel
this.minZoomEditing = mapSettings.minZoomEditing
this.maxBounds = mapSettings.maxBounds
this.center = mapSettings.center
}

fun baseTilesPath(baseTilesPath: String) = apply { this.baseTilesPath = baseTilesPath }

fun useOnlineLayers(useOnlineLayers: Boolean) =
apply { this.useOnlineLayers = useOnlineLayers }

fun showCompass(showCompass: Boolean) =
apply { this.showCompass = showCompass }
fun showCompass(showCompass: Boolean) = apply { this.showCompass = showCompass }

fun showScale(showScale: Boolean) =
apply { this.showScale = showScale }
fun showScale(showScale: Boolean) = apply { this.showScale = showScale }

fun showZoom(showZoom: Boolean) =
apply { this.showZoom = showZoom }
fun showZoom(showZoom: Boolean) = apply { this.showZoom = showZoom }

fun rotationGesture(rotateGesture: Boolean) =
apply { this.rotationGesture = rotateGesture }
fun rotationGesture(rotateGesture: Boolean) = apply { this.rotationGesture = rotateGesture }

fun editMode(editMode: EditFeatureButton.EditMode) =
apply { this.editMode = editMode }
fun editMode(editMode: EditFeatureButton.EditMode) = apply { this.editMode = editMode }

fun zoom(zoom: Double) =
apply { this.zoom = zoom }
fun zoom(zoom: Double) = apply { this.zoom = zoom }

fun minZoomLevel(minZoomLevel: Double) =
apply { this.minZoomLevel = minZoomLevel }
fun minZoomLevel(minZoomLevel: Double) = apply { this.minZoomLevel = minZoomLevel }

fun maxZoomLevel(maxZoomLevel: Double) =
apply { this.maxZoomLevel = maxZoomLevel }
fun maxZoomLevel(maxZoomLevel: Double) = apply { this.maxZoomLevel = maxZoomLevel }

fun minZoomEditing(minZoomEditing: Double) =
apply { this.minZoomEditing = minZoomEditing }
fun minZoomEditing(minZoomEditing: Double) = apply { this.minZoomEditing = minZoomEditing }

fun maxBounds(geoPoints: List<GeoPoint>) =
apply { this.maxBounds = BoundingBox.fromGeoPoints(geoPoints) }

fun center(center: GeoPoint?) =
apply { this.center = center }
fun center(center: GeoPoint?) = apply { this.center = center }

fun addLayer(
label: String,
vararg source: String
) =
apply {
addLayer(
LayerSettings.Builder.newInstance()
.label(label)
.sources(source.toList())
.build()
)
}

fun addLayer(layerSettings: LayerSettings) =
apply {
if (!this.layersSettings.any { it.source == layerSettings.source }) this.layersSettings.add(
layerSettings
)
}

fun build() =
MapSettings(this)

companion object {
fun newInstance(): Builder = Builder()
) = apply {
addLayer(
LayerSettings.Builder.newInstance()
.label(label)
.sources(source.toList())
.build()
)
}

fun addLayer(layerSettings: LayerSettings) = apply {
if (!this.layersSettings.any { it.source == layerSettings.source }) this.layersSettings.add(
layerSettings
)
}

fun build() = MapSettings(this)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import java.io.Reader
*
* @author S. Grimault
*/
class MapSettingsReader {
class MapSettingsReader(private val fromExistingMapSettings: MapSettings? = null) {

/**
* parse a `JSON` string to convert as [MapSettings].
Expand Down Expand Up @@ -71,7 +71,7 @@ class MapSettingsReader {
*/
@Throws(Exception::class)
fun read(reader: JsonReader): MapSettings {
val builder = MapSettings.Builder.newInstance()
val builder = MapSettings.Builder().from(fromExistingMapSettings)

reader.beginObject()

Expand Down Expand Up @@ -113,6 +113,7 @@ class MapSettingsReader {

builder.maxBounds(maxBounds)
}

"center" -> {
val tokens = mutableListOf<Double>()
reader.beginArray()
Expand All @@ -132,6 +133,7 @@ class MapSettingsReader {
)
}
}

"start_zoom", "zoom" -> builder.zoom(reader.nextDouble())
"min_zoom" -> builder.minZoomLevel(reader.nextDouble())
"max_zoom" -> builder.maxZoomLevel(reader.nextDouble())
Expand All @@ -140,6 +142,7 @@ class MapSettingsReader {
reader,
builder
)

else -> reader.skipValue()
}
}
Expand All @@ -154,6 +157,10 @@ class MapSettingsReader {
reader: JsonReader,
builder: MapSettings.Builder
) {
if ((fromExistingMapSettings?.layersSettings?: emptyList()).isNotEmpty()) {
builder.layersSettings.clear()
}

reader.beginArray()

while (reader.hasNext()) {
Expand Down Expand Up @@ -200,9 +207,11 @@ class MapSettingsReader {
reader.nextNull()
emptyList()
}

STRING -> {
listOf(reader.nextString())
}

JsonToken.BEGIN_ARRAY -> {
reader.beginArray()
val source = mutableListOf<String>()
Expand All @@ -215,6 +224,7 @@ class MapSettingsReader {

source
}

else -> {
reader.skipValue()
emptyList()
Expand All @@ -228,6 +238,7 @@ class MapSettingsReader {
reader.nextNull()
null
}

BEGIN_OBJECT -> {
reader.beginObject()

Expand All @@ -249,6 +260,7 @@ class MapSettingsReader {

builder.build()
}

else -> throw IOException("Invalid object properties JSON token $jsonToken")
}
}
Expand All @@ -259,6 +271,7 @@ class MapSettingsReader {
reader.nextNull()
null
}

BEGIN_OBJECT -> {
reader.beginObject()

Expand All @@ -273,12 +286,14 @@ class MapSettingsReader {
reader.nextDouble()
.toFloat()
)

"fill" -> builder.fill(reader.nextBoolean())
"fillColor" -> builder.fillColor(reader.nextString())
"fillOpacity" -> builder.fillOpacity(
reader.nextDouble()
.toFloat()
)

else -> reader.skipValue()
}
}
Expand All @@ -287,6 +302,7 @@ class MapSettingsReader {

builder.build()
}

else -> throw IOException("Invalid object properties JSON token $jsonToken")
}
}
Expand Down
2 changes: 1 addition & 1 deletion maps/src/main/java/fr/geonature/maps/ui/MapFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ open class MapFragment : Fragment() {

private fun getMapSettings(context: Context): MapSettings {
// read map settings from arguments or build the default one
val mapSettingsBuilder = MapSettings.Builder.newInstance()
val mapSettingsBuilder = MapSettings.Builder()
.from(arguments?.getParcelableCompat(ARG_MAP_SETTINGS))

setDefaultPreferences(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ object MapSettingsPreferencesUtils {
*/
fun useOnlineLayers(
context: Context,
defaultValue: Boolean = MapSettings.Builder.newInstance().useOnlineLayers
defaultValue: Boolean = MapSettings.Builder().useOnlineLayers
): Boolean {
return getDefaultSharedPreferences(context)
.getBoolean(
Expand Down Expand Up @@ -131,7 +131,7 @@ object MapSettingsPreferencesUtils {
return getDefaultSharedPreferences(context)
.getBoolean(
context.getString(R.string.preference_category_map_show_compass_key),
MapSettings.Builder.newInstance().showCompass
MapSettings.Builder().showCompass
)
}

Expand All @@ -146,7 +146,7 @@ object MapSettingsPreferencesUtils {
return getDefaultSharedPreferences(context)
.getBoolean(
context.getString(R.string.preference_category_map_show_scale_key),
MapSettings.Builder.newInstance().showScale
MapSettings.Builder().showScale
)
}

Expand All @@ -161,7 +161,7 @@ object MapSettingsPreferencesUtils {
return getDefaultSharedPreferences(context)
.getBoolean(
context.getString(R.string.preference_category_map_show_zoom_key),
MapSettings.Builder.newInstance().showZoom
MapSettings.Builder().showZoom
)
}

Expand All @@ -176,7 +176,7 @@ object MapSettingsPreferencesUtils {
return getDefaultSharedPreferences(context)
.getBoolean(
context.getString(R.string.preference_category_map_rotation_key),
MapSettings.Builder.newInstance().rotationGesture
MapSettings.Builder().rotationGesture
)
}
}
Loading

0 comments on commit 3220b87

Please sign in to comment.