dc09-sensors #4
3 changed files with 28 additions and 4 deletions
|
@ -119,6 +119,11 @@ dependencies {
|
|||
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0")
|
||||
|
||||
// Ktor
|
||||
val ktor_version = "2.3.1"
|
||||
implementation("io.ktor:ktor-client-core:$ktor_version")
|
||||
implementation("io.ktor:ktor-client-cio:$ktor_version")
|
||||
|
||||
// Map Compose library
|
||||
implementation("ovh.plrapps:mapcompose:2.7.1")
|
||||
}
|
|
@ -3,6 +3,7 @@ package ru.nm17.narodmon.ui.pages
|
|||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.FilterChip
|
||||
|
@ -13,6 +14,7 @@ import androidx.compose.runtime.mutableStateOf
|
|||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalConfiguration
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.navigation.NavController
|
||||
|
@ -31,11 +33,16 @@ fun SensorsPage(navController: NavController) {
|
|||
val mapVM by remember { mutableStateOf(MapViewModel()) }
|
||||
var filter by remember { mutableStateOf(SensorsFilter.All) }
|
||||
|
||||
val scrConfig = LocalConfiguration.current
|
||||
val scrHalfHeight = scrConfig.screenHeightDp / 2
|
||||
|
||||
GenericNavScaffold(
|
||||
title = { Text(text = stringResource(R.string.sensors_page_title)) }
|
||||
) {
|
||||
Column(modifier = Modifier.padding(it)) {
|
||||
MapUI(state = mapVM.state)
|
||||
|
||||
MapUI(state = mapVM.state, modifier = Modifier.height(scrHalfHeight.dp))
|
||||
|
||||
Row(
|
||||
modifier = Modifier.padding(horizontal = 8.dp),
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp),
|
||||
|
|
|
@ -3,20 +3,32 @@ package ru.nm17.narodmon.ui.viewmodel
|
|||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.lifecycle.ViewModel
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.engine.cio.CIO
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.statement.bodyAsChannel
|
||||
import io.ktor.utils.io.jvm.javaio.toInputStream
|
||||
import ovh.plrapps.mapcompose.api.addLayer
|
||||
import ovh.plrapps.mapcompose.core.TileStreamProvider
|
||||
import ovh.plrapps.mapcompose.ui.state.MapState
|
||||
import java.io.InputStream
|
||||
import java.net.URL
|
||||
|
||||
class MapViewModel : ViewModel() {
|
||||
private val client = HttpClient(CIO)
|
||||
|
||||
private val tileStreamProvider = TileStreamProvider { row, col, zoom ->
|
||||
URL("https://tile.openstreetmap.org/${zoom}/${row}/${col}.png").openStream()
|
||||
//URL("https://tile2.maps.2gis.com/poi?x=${row}&y=${col}&z=${zoom}&v=1&ts=online_sd").openStream()
|
||||
requestTile(row, col, zoom)
|
||||
}
|
||||
|
||||
val state: MapState by mutableStateOf(
|
||||
MapState(4, 4, 4096).apply {
|
||||
MapState(4, 4096, 4096).apply {
|
||||
addLayer(tileStreamProvider)
|
||||
}
|
||||
)
|
||||
|
||||
private suspend fun requestTile(row: Int, col: Int, zoom: Int): InputStream {
|
||||
val response = client.get("https://tile.openstreetmap.org/${zoom}/${col}/${row}.png")
|
||||
return response.bodyAsChannel().toInputStream()
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue