Skip to content

Commit

Permalink
Add coverage on map
Browse files Browse the repository at this point in the history
Signed-off-by: Simó Albert i Beltran <[email protected]>
  • Loading branch information
sim6 committed Feb 9, 2025
1 parent 5a1f7f9 commit 51055cf
Show file tree
Hide file tree
Showing 12 changed files with 316 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ object PreferenceKeys {
const val GEOSUBMIT_PATH = "geosubmit_path"
const val GEOSUBMIT_API_KEY = "geosubmit_api_key"

const val COVERAGE_TILE_JSON_URL = "coverage_tile_json_url"

const val MOVEMENT_DETECTOR = "movement_detector"

const val SCANNER_NOTIFICATION_STYLE = "scanner_notification_style"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ fun MLSWarningDialog() {
set(stringPreferencesKey(PreferenceKeys.GEOSUBMIT_PATH), path)

remove(stringPreferencesKey(PreferenceKeys.GEOSUBMIT_API_KEY))

set(stringPreferencesKey(
PreferenceKeys.COVERAGE_TILE_JSON_URL),
defaultServiceParams.value!!.coverageTileJsonUrl
)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package xyz.malkki.neostumbler.ui.composables.settings

import android.util.Patterns
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.material3.AlertDialogDefaults
import androidx.compose.material3.BasicAlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.stringPreferencesKey
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import xyz.malkki.neostumbler.R
import xyz.malkki.neostumbler.StumblerApplication
import xyz.malkki.neostumbler.constants.PreferenceKeys
import xyz.malkki.neostumbler.ui.composables.settings.SettingsItem
import xyz.malkki.neostumbler.ui.composables.settings.geosubmit.SuggestedServicesDialog

private fun DataStore<Preferences>.coverageLayerTileJsonUrl(): Flow<String?> = data
.map { preferences ->
preferences[stringPreferencesKey(PreferenceKeys.COVERAGE_TILE_JSON_URL)]
}
.distinctUntilChanged()

@Composable
fun CoverageLayerSettings() {
val context = LocalContext.current

val coroutineScope = rememberCoroutineScope()

val settingsStore = (context.applicationContext as StumblerApplication).settingsStore
val tileJsonUrl = settingsStore.coverageLayerTileJsonUrl().collectAsState(initial = null)

val dialogOpen = rememberSaveable { mutableStateOf(false) }

if (dialogOpen.value) {
CoverageLayerDialog(
currentTileJsonUrl = tileJsonUrl.value,
onDialogClose = { newTileJsonUrl ->
coroutineScope.launch {
settingsStore.updateData { prefs ->
prefs.toMutablePreferences().apply {
if (newTileJsonUrl.isNullOrEmpty()) {
remove(stringPreferencesKey(PreferenceKeys.COVERAGE_TILE_JSON_URL))
} else {
set(stringPreferencesKey(PreferenceKeys.COVERAGE_TILE_JSON_URL), newTileJsonUrl)
}
}
}
dialogOpen.value = false
}
}
)
}

SettingsItem(
title = stringResource(R.string.coverage_layer),
description = tileJsonUrl.value ?: stringResource(R.string.coverage_layer_no_configured_tile_json_url),
onClick = {
dialogOpen.value = true
}
)
}

@Composable
private fun CoverageLayerDialog(currentTileJsonUrl: String?, onDialogClose: (String?) -> Unit) {
val tileJsonUrl = rememberSaveable {
mutableStateOf(currentTileJsonUrl)
}

val showSuggestedServicesDialog = rememberSaveable {
mutableStateOf(false)
}

if (showSuggestedServicesDialog.value) {
SuggestedServicesDialog(
onServiceSelected = { service ->
if (service != null) {
tileJsonUrl.value = service.coverageTileJsonUrl
}

showSuggestedServicesDialog.value = false
}
)
}

BasicAlertDialog(
onDismissRequest = { onDialogClose(null) }
) {
Surface(
modifier = Modifier
.wrapContentWidth()
.wrapContentHeight(),
shape = MaterialTheme.shapes.large,
tonalElevation = AlertDialogDefaults.TonalElevation
) {
Column(modifier = Modifier.padding(16.dp)) {
Text(
style = MaterialTheme.typography.titleLarge,
text = stringResource(id = R.string.coverage_layer),
)

Spacer(modifier = Modifier.height(16.dp))


TextField(
modifier = Modifier.fillMaxWidth(),
value = tileJsonUrl.value ?: "",
onValueChange = { newTileJsonUrl ->
tileJsonUrl.value = newTileJsonUrl
},
label = { Text(text = stringResource(id = R.string.coverage_layer_tile_json_url)) },
singleLine = true
)

if (!tileJsonUrl.value.isValidUrl) {
Warning(R.string.no_valid_url_warning)
}

if (tileJsonUrl.value.isUnencryptedUrl) {
Warning(R.string.unencrypted_endpoint_warning)
}


Spacer(modifier = Modifier.height(8.dp))

Button(
modifier = Modifier.align(Alignment.CenterHorizontally),
onClick = {
showSuggestedServicesDialog.value = true
}
) {
Text(
text = stringResource(id = R.string.suggested_services_title)
)
}

Row {
Spacer(modifier = Modifier.weight(1.0f))

TextButton(
onClick = { onDialogClose(tileJsonUrl.value) },
enabled = tileJsonUrl.value?.isValidUrl ?: false || tileJsonUrl.value?.isEmpty() ?: false
) {
Text(text = stringResource(id = R.string.save))
}
}
}
}
}
}

private val String?.isValidUrl: Boolean
get() = Patterns.WEB_URL.matcher(this ?: "").matches()

private val String?.isUnencryptedUrl: Boolean
get() = this?.startsWith("http:") ?: false
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xyz.malkki.neostumbler.ui.composables.settings.geosubmit
package xyz.malkki.neostumbler.ui.composables.settings

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
Expand All @@ -21,7 +21,7 @@ import androidx.compose.ui.unit.sp
import xyz.malkki.neostumbler.R

@Composable
fun UnencryptedEndpointWarning() {
fun Warning(stringResource: Int) {
Row(
modifier = Modifier
.wrapContentSize()
Expand All @@ -38,7 +38,7 @@ fun UnencryptedEndpointWarning() {
Text(modifier = Modifier
.fillMaxWidth()
.wrapContentHeight(),
text = stringResource(id = R.string.unencrypted_endpoint_warning),
text = stringResource(id = stringResource),
style = MaterialTheme.typography.labelSmall.copy(fontSize = 14.sp),
color = MaterialTheme.colorScheme.onErrorContainer
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import xyz.malkki.neostumbler.StumblerApplication
import xyz.malkki.neostumbler.constants.PreferenceKeys
import xyz.malkki.neostumbler.geosubmit.GeosubmitParams
import xyz.malkki.neostumbler.ui.composables.settings.SettingsItem
import xyz.malkki.neostumbler.ui.composables.settings.Warning

private fun DataStore<Preferences>.geosubmitParams(): Flow<GeosubmitParams?> = data
.map { preferences ->
Expand Down Expand Up @@ -161,7 +162,7 @@ private fun GeosubmitEndpointDialog(currentParams: GeosubmitParams?, onDialogClo
)

if (endpoint.value.isUnencryptedUrl) {
UnencryptedEndpointWarning()
Warning(R.string.unencrypted_endpoint_warning)
}

Spacer(modifier = Modifier.height(8.dp))
Expand Down
50 changes: 50 additions & 0 deletions app/src/main/java/xyz/malkki/neostumbler/ui/screens/MapScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package xyz.malkki.neostumbler.ui.screens
import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.os.Handler
import android.os.Looper
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
Expand Down Expand Up @@ -65,15 +67,23 @@ import org.maplibre.android.maps.Style
import org.maplibre.android.module.http.HttpRequestUtil
import org.maplibre.android.plugins.annotation.FillManager
import org.maplibre.android.plugins.annotation.FillOptions
import org.maplibre.android.style.layers.FillLayer
import org.maplibre.android.style.layers.PropertyFactory
import org.maplibre.android.style.sources.VectorSource
import xyz.malkki.neostumbler.R
import xyz.malkki.neostumbler.extensions.checkMissingPermissions
import xyz.malkki.neostumbler.ui.composables.KeepScreenOn
import xyz.malkki.neostumbler.ui.composables.PermissionsDialog
import xyz.malkki.neostumbler.ui.viewmodel.MapViewModel
import xyz.malkki.neostumbler.ui.viewmodel.MapViewModel.MapTileSource
import xyz.malkki.neostumbler.utils.getTileJsonLayerIds

private val HEAT_LOW = ColorUtils.setAlphaComponent(0xd278ff, 120)
private val HEAT_HIGH = ColorUtils.setAlphaComponent(0xaa00ff, 120)
private val COVERAGE_SOURCE_ID = "coverage-source"
private val COVERAGE_LAYER_PREFIX = "coverage-layer-"
private val COVERAGE_COLOR = "#ff8000"
private val COVERAGE_OPACITY = 0.4f

@Composable
fun MapScreen(mapViewModel: MapViewModel = viewModel()) {
Expand Down Expand Up @@ -101,6 +111,10 @@ fun MapScreen(mapViewModel: MapViewModel = viewModel()) {

val mapStyle = mapViewModel.mapStyle.collectAsState(initial = null)

val coverageTileJsonUrl = mapViewModel.coverageTileJsonUrl.collectAsState(initial = null)

val coverageTileJsonLayerIds = mapViewModel.coverageTileJsonLayerIds.collectAsState(initial = emptyList<String>())

val latestReportPosition = mapViewModel.latestReportPosition.collectAsState(initial = null)

val heatMapTiles = mapViewModel.heatMapTiles.collectAsState(initial = emptyList())
Expand Down Expand Up @@ -197,6 +211,8 @@ fun MapScreen(mapViewModel: MapViewModel = viewModel()) {

fillManager.value = FillManager(mapView, map, style, LocationComponentConstants.SHADOW_LAYER, null)
}

addCoverage(map, coverageTileJsonUrl.value, coverageTileJsonLayerIds.value)
}

mapView
Expand Down Expand Up @@ -233,6 +249,8 @@ fun MapScreen(mapViewModel: MapViewModel = viewModel()) {
map.setStyle(Style.Builder().fromJson(mapStyle.value!!.styleJson!!))
}
}

addCoverage(map, coverageTileJsonUrl.value, coverageTileJsonLayerIds.value)
}

fillManager.value?.let {
Expand Down Expand Up @@ -284,6 +302,38 @@ fun MapScreen(mapViewModel: MapViewModel = viewModel()) {
}
}

private fun addCoverageLayer(style: Style, layerIds: List<String>) {
for (id in layerIds) {
val layer = style.getLayer(COVERAGE_LAYER_PREFIX + id)
if (layer == null) {
style.addLayer(
FillLayer(COVERAGE_LAYER_PREFIX + id, COVERAGE_SOURCE_ID).apply {
withProperties(
PropertyFactory.fillColor(COVERAGE_COLOR),
PropertyFactory.fillOpacity(COVERAGE_OPACITY)
)
setSourceLayer(id)
}
)
} else {
(layer as? FillLayer)?.setSourceLayer(id)
}
}
}

private fun addCoverage(mapLibreMap: MapLibreMap, tileJsonUrl: String?, layerIds: List<String>) {
if (tileJsonUrl != null) {
mapLibreMap.getStyle { style ->
val vectorSource = style.getSource(COVERAGE_SOURCE_ID) as? VectorSource
if (vectorSource == null || vectorSource.uri != tileJsonUrl) {
style.removeSource(COVERAGE_SOURCE_ID)
style.addSource(VectorSource(COVERAGE_SOURCE_ID, tileJsonUrl))
}
addCoverageLayer(style, layerIds)
}
}
}

private fun createHeatMapFill(tiles: Collection<MapViewModel.HeatMapTile>): List<FillOptions> {
return tiles.map { tile ->
val color = ColorUtils.blendARGB(HEAT_LOW, HEAT_HIGH, tile.heatPct)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import xyz.malkki.neostumbler.ui.composables.SettingsToggle
import xyz.malkki.neostumbler.ui.composables.TroubleshootingSettingsItem
import xyz.malkki.neostumbler.ui.composables.settings.AutoScanToggle
import xyz.malkki.neostumbler.ui.composables.settings.AutoUploadToggle
import xyz.malkki.neostumbler.ui.composables.settings.CoverageLayerSettings
import xyz.malkki.neostumbler.ui.composables.settings.DbPruneSettings
import xyz.malkki.neostumbler.ui.composables.settings.FusedLocationToggle
import xyz.malkki.neostumbler.ui.composables.settings.IgnoreScanThrottlingToggle
Expand All @@ -48,6 +49,7 @@ fun SettingsScreen() {
title = stringResource(id = R.string.settings_group_reports)
) {
GeosubmitEndpointSettings()
CoverageLayerSettings()
AutoUploadToggle()
DbPruneSettings()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import xyz.malkki.neostumbler.extensions.checkMissingPermissions
import xyz.malkki.neostumbler.extensions.get
import xyz.malkki.neostumbler.extensions.parallelMap
import xyz.malkki.neostumbler.location.LocationSourceProvider
import xyz.malkki.neostumbler.utils.getTileJsonLayerIds
import kotlin.math.abs
import kotlin.math.floor
import kotlin.time.Duration.Companion.seconds
Expand Down Expand Up @@ -72,6 +73,14 @@ class MapViewModel(application: Application) : AndroidViewModel(application) {
}
.shareIn(viewModelScope, started = SharingStarted.Eagerly, replay = 1)

val coverageTileJsonUrl: Flow<String?> = settingsStore.data
.map { prefs ->
prefs.get(stringPreferencesKey(PreferenceKeys.COVERAGE_TILE_JSON_URL))
}

private val _coverageTileJsonLayerIds = MutableStateFlow<List<String>>(emptyList<String>())
val coverageTileJsonLayerIds: StateFlow<List<String>> = _coverageTileJsonLayerIds

private val showMyLocation = MutableStateFlow(getApplication<StumblerApplication>().checkMissingPermissions(Manifest.permission.ACCESS_COARSE_LOCATION).isEmpty())

private val _mapCenter = MutableStateFlow<LatLng>(LatLng.ORIGIN)
Expand Down Expand Up @@ -169,6 +178,11 @@ class MapViewModel(application: Application) : AndroidViewModel(application) {
viewModelScope.launch {
val httpClient = (application as StumblerApplication).httpClientProvider.await()
_httpClient.value = httpClient
coverageTileJsonUrl.collect { coverageTileJsonUrl ->
getTileJsonLayerIds(coverageTileJsonUrl, httpClient) { layerIds ->
_coverageTileJsonLayerIds.value = layerIds
}
}
}
}

Expand Down
Loading

0 comments on commit 51055cf

Please sign in to comment.