├── .gitignore ├── README.md ├── build-logic ├── convention │ ├── build.gradle.kts │ └── src │ │ └── main │ │ └── kotlin │ │ ├── JvmLibraryConventionPlugin.kt │ │ ├── MavenConventionPlugin.kt │ │ ├── SigningConventionPlugin.kt │ │ └── com │ │ └── mmoczkowski │ │ └── chart │ │ └── ProjectExt.kt ├── gradle.properties └── settings.gradle.kts ├── build.gradle.kts ├── cache ├── api │ ├── build.gradle.kts │ └── src │ │ └── main │ │ └── kotlin │ │ └── com │ │ └── mmoczkowski │ │ └── chart │ │ └── cache │ │ └── api │ │ └── Cache.kt └── impl │ ├── lru │ ├── build.gradle.kts │ └── src │ │ ├── main │ │ └── kotlin │ │ │ └── com │ │ │ └── mmoczkowski │ │ │ └── chart │ │ │ └── cache │ │ │ └── impl │ │ │ └── lru │ │ │ └── LruCache.kt │ │ └── test │ │ └── kotlin │ │ └── com │ │ └── mmoczkowski │ │ └── chart │ │ └── cache │ │ └── impl │ │ └── lru │ │ └── LruCacheTest.kt │ └── no-op │ ├── build.gradle.kts │ └── src │ └── main │ └── kotlin │ └── com │ └── mmoczkowski │ └── chart │ └── cache │ └── impl │ └── noop │ └── NoOpCache.kt ├── gradle.properties ├── gradle ├── libs.versions.toml └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── img └── chart.gif ├── provider ├── api │ ├── build.gradle.kts │ └── src │ │ └── main │ │ └── kotlin │ │ └── com │ │ └── mmoczkowski │ │ └── chart │ │ └── provider │ │ └── api │ │ ├── Tile.kt │ │ ├── TileCoords.kt │ │ ├── TileProvider.kt │ │ └── TileSizeUnsupportedException.kt └── impl │ ├── debug │ ├── build.gradle.kts │ └── src │ │ └── main │ │ └── kotlin │ │ └── com │ │ └── mmoczkowski │ │ └── chart │ │ └── provider │ │ └── impl │ │ └── debug │ │ └── DebugTileProvider.kt │ ├── google │ ├── build.gradle.kts │ └── src │ │ └── main │ │ └── kotlin │ │ └── com │ │ └── mmoczkowski │ │ └── chart │ │ └── provider │ │ └── impl │ │ └── google │ │ ├── GoogleTileProvider.kt │ │ ├── MapSpec.kt │ │ └── Session.kt │ ├── open-street-map │ ├── build.gradle.kts │ └── src │ │ └── main │ │ └── kotlin │ │ └── com │ │ └── mmoczkowski │ │ └── chart │ │ └── provider │ │ └── impl │ │ └── osm │ │ └── OpenStreetMapTileProvider.kt │ └── url │ ├── build.gradle.kts │ └── src │ └── main │ └── kotlin │ └── com │ └── mmoczkowski │ └── chart │ └── provider │ └── impl │ └── url │ └── UrlTileProvider.kt ├── sample ├── aerial │ ├── build.gradle.kts │ └── src │ │ └── main │ │ └── kotlin │ │ └── com │ │ └── mmoczkowski │ │ └── chart │ │ └── sample │ │ └── aerial │ │ └── Main.kt ├── basic │ ├── build.gradle.kts │ └── src │ │ └── main │ │ └── kotlin │ │ └── com │ │ └── mmoczkowski │ │ └── chart │ │ └── sample │ │ └── basic │ │ └── Main.kt └── mars │ ├── build.gradle.kts │ └── src │ └── main │ └── kotlin │ └── com │ └── mmoczkowski │ └── chart │ └── sample │ └── mars │ ├── Main.kt │ └── data │ ├── MapLayer.kt │ └── PointOfInterest.kt ├── settings.gradle.kts └── ui ├── build.gradle.kts └── src ├── main └── kotlin │ └── com │ └── mmoczkowski │ └── chart │ ├── Chart.kt │ ├── ChartLayer.kt │ ├── ChartState.kt │ ├── LatLng.kt │ ├── MarkerLayer.kt │ ├── MarkerScope.kt │ └── TileState.kt └── test └── kotlin └── com └── mmoczkowski └── chart ├── ChartStateText.kt └── LatLngTest.kt /.gitignore: -------------------------------------------------------------------------------- 1 | # IntelliJ IDEA files 2 | .idea/ 3 | *.iws 4 | *.iml 5 | *.ipr 6 | 7 | # Gradle cache and build directories 8 | .gradle/ 9 | build/ 10 | 11 | # Compiled files 12 | *.class 13 | *.war 14 | *.ear 15 | 16 | # Logs and databases 17 | *.log 18 | *.dat 19 | *.sqlite 20 | 21 | # OS generated files 22 | .DS_Store 23 | .DS_Store? 24 | ._* 25 | .Spotlight-V100 26 | .Trashes 27 | ehthumbs.db 28 | Thumbs.db 29 | 30 | # Temp files 31 | *.swp 32 | *.swo 33 | *.swn 34 | *.bak 35 | *.tmp 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Chart: A JVM Tile Map Library for Compose Desktop 2 | 3 |

4 | 5 |

6 | 7 | ## Overview 8 | Chart is a JVM library designed for loading and displaying map tiles using Compose Desktop. It's structured to be both 9 | modular and adaptable, catering to a range of use-cases. Whether you're building a standard map application or something 10 | more specialized, Chart has got you covered. 11 | 12 | ## Declaring Dependencies 13 | To integrate Chart into your project, include the necessary dependencies: 14 | 15 | ```kotlin 16 | val chartVersion = // Recent version 17 | 18 | // Composable (required) 19 | implementation("com.mmoczkowski:chart-ui:$chartVersion") 20 | 21 | // Cache API (required) 22 | implementation("com.mmoczkowski:chart-cache-api:$chartVersion") 23 | 24 | // Cache implementations (you need at least one) 25 | implementation("com.mmoczkowski:chart-cache-impl-lru:$chartVersion") 26 | implementation("com.mmoczkowski:chart-cache-impl-no-op:$chartVersion") 27 | 28 | // Tile Provider API (required) 29 | implementation("com.mmoczkowski:chart-provider-api:$chartVersion") 30 | 31 | // Tile Provider implementations (you need at least one) 32 | implementation("com.mmoczkowski:chart-provider-impl-google:$chartVersion") 33 | implementation("com.mmoczkowski:chart-provider-impl-open-street-map:$chartVersion") 34 | implementation("com.mmoczkowski:chart-provider-impl-url:$chartVersion") 35 | ``` 36 | 37 | ## Usage 38 | 39 | Using Chart is intuitive and straightforward. Here's a simple guide to help you get started: 40 | 41 | 1. **Initialize a Cache**: 42 | Start by defining the type of cache you want to use. For instance, to use an LRU cache: 43 | ```kotlin 44 | val lruCache = rememberLruCache(maxSize = 150) 45 | ``` 46 | 47 | 2. **Select a Tile Provider**: 48 | Depending on your requirements, choose the tile provider. For using OpenStreetMap: 49 | ```kotlin 50 | val tileProvider = rememberOpenStreetMapTileProvider() 51 | ``` 52 | 53 | 3. **Render the Map**: 54 | Now, simply display the map using the `Chart` composable. Here's a basic setup: 55 | ```kotlin 56 | Chart( 57 | provider = tileProvider, 58 | cache = lruCache, 59 | ) 60 | ``` 61 | 62 | This code sets up a basic map using the OpenStreetMap tile provider and an LRU cache. You can customize this further by 63 | opting for different tile providers, creating custom success/error/loading composables, or even incorporating markers. 64 | 65 | ## Project Structure 66 | - `:build-logic:` - Build configuration and convention plugins. 67 | - `:cache:api` - Cache API for map tile storage. 68 | - `:cache:impl:lru` - In-memory Least Recently Used (LRU) caching implementation. 69 | - `:cache:impl:no-op` - A mock or "no-operation" cache, useful for testing and development. 70 | - `:provider:api` - The blueprint for the Tile Provider API. 71 | - `:provider:impl:debug` - A debug variant of the tile provider for developmental purposes. 72 | - `:provider:impl:google` - Google Map Tiles API tile provider implementation. 73 | - `:provider:impl:open-street-map` - OpenStreetMap tile provider implementation. 74 | - `:provider:impl:url` - A versatile, URL-driven tile provider adaptable to a variety of sources. 75 | - `:ui` - The heart of the user interface, housing the Chart composables. 76 | -------------------------------------------------------------------------------- /build-logic/convention/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import org.jetbrains.kotlin.gradle.tasks.KotlinCompile 18 | 19 | plugins { 20 | `kotlin-dsl` 21 | } 22 | 23 | 24 | dependencies { 25 | implementation(gradleApi()) 26 | } 27 | 28 | gradlePlugin { 29 | plugins { 30 | register("chart.jvm") { 31 | id = "chart.jvm" 32 | implementationClass = "JvmLibraryConventionPlugin" 33 | } 34 | register("chart.maven") { 35 | id = "chart.maven" 36 | implementationClass = "MavenConventionPlugin" 37 | } 38 | register("chart.signing") { 39 | id = "chart.signing" 40 | implementationClass = "SigningConventionPlugin" 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /build-logic/convention/src/main/kotlin/JvmLibraryConventionPlugin.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import com.mmoczkowski.chart.archiveName 18 | import com.mmoczkowski.chart.moduleFullName 19 | import org.gradle.api.Plugin 20 | import org.gradle.api.Project 21 | import org.gradle.api.plugins.JavaPluginExtension 22 | import org.gradle.jvm.tasks.Jar 23 | import org.gradle.kotlin.dsl.configure 24 | 25 | class JvmLibraryConventionPlugin : Plugin { 26 | override fun apply(target: Project) { 27 | with(target) { 28 | with(pluginManager) { 29 | apply("org.jetbrains.kotlin.jvm") 30 | } 31 | extensions.configure { 32 | version = "0.4.0" 33 | group = "com.mmoczkowski.chart.${project.moduleFullName}" 34 | 35 | withJavadocJar() 36 | withSourcesJar() 37 | tasks.withType(Jar::class.java) { 38 | archiveBaseName.set(project.archiveName) 39 | } 40 | } 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /build-logic/convention/src/main/kotlin/MavenConventionPlugin.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import com.mmoczkowski.chart.publicationId 18 | import org.gradle.api.Plugin 19 | import org.gradle.api.Project 20 | import org.gradle.api.publish.PublishingExtension 21 | import org.gradle.api.publish.maven.MavenPublication 22 | import org.gradle.kotlin.dsl.configure 23 | import org.gradle.kotlin.dsl.get 24 | 25 | class MavenConventionPlugin : Plugin { 26 | override fun apply(target: Project) { 27 | with(target) { 28 | with(pluginManager) { 29 | apply("maven-publish") 30 | } 31 | project.extensions.configure { 32 | publications { 33 | create(project.publicationId, MavenPublication::class.java) { 34 | from(components["java"]) 35 | 36 | groupId = "com.mmoczkowski" 37 | artifactId = "chart-${project.path.substring(1).replace(":", "-")}" 38 | version = System.getenv("VERSION") 39 | description = "Compose Desktop Map library" 40 | 41 | pom { 42 | name.set("Chart") 43 | description.set("Compose Desktop Map library") 44 | url.set("http://github.com/mmoczkowski/chart") 45 | licenses { 46 | license { 47 | name.set("The Apache License, Version 2.0") 48 | url.set("http://www.apache.org/licenses/LICENSE-2.0.txt") 49 | } 50 | } 51 | developers { 52 | developer { 53 | id.set("mmoczkowski") 54 | name.set("Miłosz Moczkowski") 55 | email.set("milosz.moczkowski@gmail.com") 56 | } 57 | } 58 | scm { 59 | url.set("https://github.com/mmoczkowski") 60 | } 61 | } 62 | 63 | } 64 | } 65 | repositories { 66 | maven { 67 | val ossrhRepositoryUrl = System.getenv("OSSHR_REPOSITORY") 68 | if (ossrhRepositoryUrl != null) { 69 | url = uri(ossrhRepositoryUrl) 70 | credentials { 71 | username = System.getenv("OSSHR_USERNAME") 72 | password = System.getenv("OSSHR_PASSWORD") 73 | } 74 | } 75 | } 76 | } 77 | } 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /build-logic/convention/src/main/kotlin/SigningConventionPlugin.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import com.mmoczkowski.chart.publicationId 18 | import org.gradle.api.Plugin 19 | import org.gradle.api.Project 20 | import org.gradle.api.publish.PublishingExtension 21 | import org.gradle.kotlin.dsl.configure 22 | import org.gradle.kotlin.dsl.get 23 | import org.gradle.kotlin.dsl.getByType 24 | import org.gradle.plugins.signing.SigningExtension 25 | 26 | class SigningConventionPlugin : Plugin { 27 | override fun apply(target: Project) { 28 | with(target) { 29 | with(pluginManager) { 30 | apply("signing") 31 | } 32 | project.extensions.configure { 33 | val signingKeyId: String? = System.getenv("SIGNING_KEY_ID") 34 | val signingKey: String? = System.getenv("SIGNING_KEY") 35 | val signingPassword: String? = System.getenv("SIGNING_PASSWORD") 36 | useInMemoryPgpKeys(signingKeyId, signingKey, signingPassword) 37 | 38 | val publishing = project.extensions.getByType() 39 | sign(publishing.publications[project.publicationId]) 40 | } 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /build-logic/convention/src/main/kotlin/com/mmoczkowski/chart/ProjectExt.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.mmoczkowski.chart 18 | 19 | import org.gradle.api.Project 20 | import org.gradle.configurationcache.extensions.capitalized 21 | 22 | val Project.publicationId: String 23 | get() { 24 | val processedProjectName: String = 25 | project 26 | .path 27 | .substring(1) 28 | .replace(":", "-") 29 | .split("-") 30 | .joinToString("") { part -> 31 | part.capitalized() 32 | } 33 | return "maven$processedProjectName" 34 | } 35 | 36 | val Project.moduleFullName 37 | get() = path 38 | .substring(1) 39 | .replace(":", ".") 40 | 41 | val Project.archiveName: String 42 | get() = group 43 | .toString() 44 | .replace(".", "-") 45 | -------------------------------------------------------------------------------- /build-logic/gradle.properties: -------------------------------------------------------------------------------- 1 | # Gradle properties are not passed to included builds https://github.com/gradle/gradle/issues/2534 2 | org.gradle.parallel=true 3 | org.gradle.caching=true 4 | org.gradle.configureondemand=true 5 | -------------------------------------------------------------------------------- /build-logic/settings.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | dependencyResolutionManagement { 18 | repositories { 19 | google() 20 | mavenCentral() 21 | } 22 | versionCatalogs { 23 | create("libs") { 24 | from(files("../gradle/libs.versions.toml")) 25 | } 26 | } 27 | } 28 | 29 | rootProject.name = "build-logic" 30 | include(":convention") 31 | -------------------------------------------------------------------------------- /build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | buildscript { 18 | repositories { 19 | gradlePluginPortal() 20 | google() 21 | mavenCentral() 22 | } 23 | dependencies { 24 | classpath(libs.kotlin.gradle.plugin) 25 | } 26 | } 27 | 28 | plugins { 29 | alias(libs.plugins.kotlin.jvm) apply false 30 | alias(libs.plugins.jb.compose) apply false 31 | } 32 | -------------------------------------------------------------------------------- /cache/api/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | plugins { 18 | alias(libs.plugins.jb.compose) 19 | alias(libs.plugins.chart.jvm) 20 | alias(libs.plugins.chart.maven) 21 | alias(libs.plugins.chart.signing) 22 | } 23 | 24 | dependencies { 25 | implementation(libs.compose.desktop) 26 | } 27 | -------------------------------------------------------------------------------- /cache/api/src/main/kotlin/com/mmoczkowski/chart/cache/api/Cache.kt: -------------------------------------------------------------------------------- 1 | package com.mmoczkowski.chart.cache.api 2 | 3 | interface Cache { 4 | suspend fun get(key: K, fetch: suspend (K) -> V): V 5 | } 6 | -------------------------------------------------------------------------------- /cache/impl/lru/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | plugins { 18 | alias(libs.plugins.jb.compose) 19 | alias(libs.plugins.chart.jvm) 20 | alias(libs.plugins.chart.maven) 21 | alias(libs.plugins.chart.signing) 22 | } 23 | 24 | dependencies { 25 | implementation(projects.cache.api) 26 | implementation(libs.compose.desktop) 27 | testImplementation(libs.kotlin.test) 28 | } 29 | -------------------------------------------------------------------------------- /cache/impl/lru/src/main/kotlin/com/mmoczkowski/chart/cache/impl/lru/LruCache.kt: -------------------------------------------------------------------------------- 1 | package com.mmoczkowski.chart.cache.impl.lru 2 | 3 | import androidx.compose.runtime.Composable 4 | import androidx.compose.runtime.remember 5 | import com.mmoczkowski.chart.cache.api.Cache 6 | import kotlinx.coroutines.sync.Mutex 7 | import kotlinx.coroutines.sync.withLock 8 | 9 | class LruCache(private val maxSize: Int) : Cache { 10 | 11 | private val map: LinkedHashMap = LinkedHashMap(maxSize, 0.75f, true) 12 | private val mutex: Mutex = Mutex() 13 | 14 | override suspend fun get(key: K, fetch: suspend (K) -> V): V { 15 | if(key == null) { 16 | throw IllegalArgumentException("Null keys are not allowed") 17 | } 18 | 19 | mutex.withLock { 20 | val cachedValue: V? = map[key] 21 | if (cachedValue != null) { 22 | return cachedValue 23 | } 24 | } 25 | 26 | val fetchedValue: V = fetch(key) ?: throw IllegalArgumentException("Null values are not allowed") 27 | mutex.withLock { 28 | val cachedValue: V? = map[key] 29 | return if(cachedValue == null) { 30 | map[key] = fetchedValue 31 | 32 | if(map.size > maxSize) { 33 | val leastRecentlyUsed: K = map.entries.first().key 34 | map.remove(leastRecentlyUsed) 35 | } 36 | 37 | fetchedValue 38 | } else { 39 | cachedValue 40 | } 41 | } 42 | } 43 | } 44 | 45 | @Composable 46 | fun rememberLruCache(maxSize: Int = 100): Cache = remember(maxSize) { 47 | LruCache(maxSize) 48 | } 49 | -------------------------------------------------------------------------------- /cache/impl/lru/src/test/kotlin/com/mmoczkowski/chart/cache/impl/lru/LruCacheTest.kt: -------------------------------------------------------------------------------- 1 | package com.mmoczkowski.chart.cache.impl.lru 2 | 3 | import kotlin.test.Test 4 | import kotlin.test.assertEquals 5 | import kotlinx.coroutines.runBlocking 6 | 7 | class LruCacheTest { 8 | 9 | @Test 10 | fun `test cache returns fetched value if not present`() = runBlocking { 11 | val lruCache = LruCache(3) 12 | val key = "key1" 13 | val value = 42 14 | 15 | val result = lruCache.get(key) { 16 | value 17 | } 18 | 19 | assertEquals(value, result) 20 | } 21 | 22 | @Test 23 | fun `test cache returns cached value if present`() = runBlocking { 24 | val lruCache = LruCache(3) 25 | val key = "key1" 26 | val value = 42 27 | 28 | lruCache.get(key) { value } 29 | val cachedResult = lruCache.get(key) { 30 | throw Exception("Should not fetch if cached") 31 | } 32 | 33 | assertEquals(value, cachedResult) 34 | } 35 | 36 | @Test 37 | fun `test cache removes LRU item if max size exceeded`() = runBlocking { 38 | val lruCache = LruCache(2) 39 | 40 | lruCache.get("key1") { 1 } 41 | lruCache.get("key2") { 2 } 42 | lruCache.get("key3") { 3 } 43 | 44 | val shouldThrow = try { 45 | lruCache.get("key1") { 46 | throw Exception("key1 should have been evicted") 47 | } 48 | false 49 | } catch (e: Exception) { 50 | true 51 | } 52 | 53 | assertEquals(true, shouldThrow) 54 | } 55 | 56 | @Test 57 | fun `test null key should throw exception`() = runBlocking { 58 | val lruCache = LruCache(2) 59 | 60 | val shouldThrow = try { 61 | lruCache.get(null) { 1 } 62 | false 63 | } catch (e: IllegalArgumentException) { 64 | true 65 | } 66 | 67 | assertEquals(true, shouldThrow) 68 | } 69 | 70 | @Test 71 | fun `test null fetch value should throw exception`() = runBlocking { 72 | val lruCache = LruCache(2) 73 | val shouldThrow = try { 74 | lruCache.get("key") { null } 75 | false 76 | } catch (e: IllegalArgumentException) { 77 | true 78 | } 79 | 80 | assertEquals(true, shouldThrow) 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /cache/impl/no-op/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | plugins { 18 | alias(libs.plugins.jb.compose) 19 | alias(libs.plugins.chart.jvm) 20 | alias(libs.plugins.chart.maven) 21 | alias(libs.plugins.chart.signing) 22 | } 23 | 24 | dependencies { 25 | implementation(projects.cache.api) 26 | implementation(libs.compose.desktop) 27 | } 28 | -------------------------------------------------------------------------------- /cache/impl/no-op/src/main/kotlin/com/mmoczkowski/chart/cache/impl/noop/NoOpCache.kt: -------------------------------------------------------------------------------- 1 | package com.mmoczkowski.chart.cache.impl.noop 2 | 3 | import androidx.compose.runtime.Composable 4 | import androidx.compose.runtime.remember 5 | import com.mmoczkowski.chart.cache.api.Cache 6 | 7 | class NoOpCache : Cache { 8 | override suspend fun get(key: K, fetch: suspend (K) -> V): V = fetch(key) 9 | } 10 | 11 | @Composable 12 | fun rememberNoOpCache(): Cache = remember { NoOpCache() } 13 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | kotlin.code.style=official 2 | kotlin.mpp.androidSourceSetLayoutVersion=2 3 | -------------------------------------------------------------------------------- /gradle/libs.versions.toml: -------------------------------------------------------------------------------- 1 | [versions] 2 | compose = "1.5.1" 3 | kotlin = "1.9.0" 4 | ktor = "2.3.4" 5 | 6 | [libraries] 7 | compose-desktop = { module = "org.jetbrains.compose.desktop:desktop", version.ref = "compose" } 8 | compose-material-icons-extended = { module = "org.jetbrains.compose.material:material-icons-extended-desktop", version.ref = "compose" } 9 | kotlin-gradle-plugin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin", version.ref = "kotlin" } 10 | kotlin-test = {module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" } 11 | kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version = "1.5.1"} 12 | ktor-bom = { group = "io.ktor", name = "ktor-bom", version.ref = "ktor" } 13 | ktor-client-auth = { group = "io.ktor", name = "ktor-client-auth" } 14 | ktor-client-cio = { group = "io.ktor", name = "ktor-client-cio" } 15 | ktor-client-content-negotiation = { group = "io.ktor", name = "ktor-client-content-negotiation" } 16 | ktor-client-core = { group = "io.ktor", name = "ktor-client-core" } 17 | ktor-client-logging = { group = "io.ktor", name = "ktor-client-logging" } 18 | ktor-serialization-json = { group = "io.ktor", name = "ktor-serialization-kotlinx-json" } 19 | 20 | [plugins] 21 | chart-jvm = { id = "chart.jvm", version = "unspecified" } 22 | chart-maven = { id = "chart.maven", version = "unspecified" } 23 | chart-signing = { id = "chart.signing", version = "unspecified" } 24 | jb-compose = { id = "org.jetbrains.compose", version.ref = "compose" } 25 | kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } 26 | kotlinx-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" } 27 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mmoczkowski/Chart/c6e983b4e9b4175a0b42f2755377d38e9651e05c/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.1-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | # 21 | # Gradle start up script for POSIX generated by Gradle. 22 | # 23 | # Important for running: 24 | # 25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 26 | # noncompliant, but you have some other compliant shell such as ksh or 27 | # bash, then to run this script, type that shell name before the whole 28 | # command line, like: 29 | # 30 | # ksh Gradle 31 | # 32 | # Busybox and similar reduced shells will NOT work, because this script 33 | # requires all of these POSIX shell features: 34 | # * functions; 35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 37 | # * compound commands having a testable exit status, especially «case»; 38 | # * various built-in commands including «command», «set», and «ulimit». 39 | # 40 | # Important for patching: 41 | # 42 | # (2) This script targets any POSIX shell, so it avoids extensions provided 43 | # by Bash, Ksh, etc; in particular arrays are avoided. 44 | # 45 | # The "traditional" practice of packing multiple parameters into a 46 | # space-separated string is a well documented source of bugs and security 47 | # problems, so this is (mostly) avoided, by progressively accumulating 48 | # options in "$@", and eventually passing that to Java. 49 | # 50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 52 | # see the in-line comments for details. 53 | # 54 | # There are tweaks for specific operating systems such as AIX, CygWin, 55 | # Darwin, MinGW, and NonStop. 56 | # 57 | # (3) This script is generated from the Groovy template 58 | # https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 59 | # within the Gradle project. 60 | # 61 | # You can find Gradle at https://github.com/gradle/gradle/. 62 | # 63 | ############################################################################## 64 | 65 | # Attempt to set APP_HOME 66 | 67 | # Resolve links: $0 may be a link 68 | app_path=$0 69 | 70 | # Need this for daisy-chained symlinks. 71 | while 72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 73 | [ -h "$app_path" ] 74 | do 75 | ls=$( ls -ld "$app_path" ) 76 | link=${ls#*' -> '} 77 | case $link in #( 78 | /*) app_path=$link ;; #( 79 | *) app_path=$APP_HOME$link ;; 80 | esac 81 | done 82 | 83 | APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit 84 | 85 | APP_NAME="Gradle" 86 | APP_BASE_NAME=${0##*/} 87 | 88 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 89 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 90 | 91 | # Use the maximum available, or set MAX_FD != -1 to use that value. 92 | MAX_FD=maximum 93 | 94 | warn () { 95 | echo "$*" 96 | } >&2 97 | 98 | die () { 99 | echo 100 | echo "$*" 101 | echo 102 | exit 1 103 | } >&2 104 | 105 | # OS specific support (must be 'true' or 'false'). 106 | cygwin=false 107 | msys=false 108 | darwin=false 109 | nonstop=false 110 | case "$( uname )" in #( 111 | CYGWIN* ) cygwin=true ;; #( 112 | Darwin* ) darwin=true ;; #( 113 | MSYS* | MINGW* ) msys=true ;; #( 114 | NONSTOP* ) nonstop=true ;; 115 | esac 116 | 117 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 118 | 119 | 120 | # Determine the Java command to use to start the JVM. 121 | if [ -n "$JAVA_HOME" ] ; then 122 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 123 | # IBM's JDK on AIX uses strange locations for the executables 124 | JAVACMD=$JAVA_HOME/jre/sh/java 125 | else 126 | JAVACMD=$JAVA_HOME/bin/java 127 | fi 128 | if [ ! -x "$JAVACMD" ] ; then 129 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 130 | 131 | Please set the JAVA_HOME variable in your environment to match the 132 | location of your Java installation." 133 | fi 134 | else 135 | JAVACMD=java 136 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 137 | 138 | Please set the JAVA_HOME variable in your environment to match the 139 | location of your Java installation." 140 | fi 141 | 142 | # Increase the maximum file descriptors if we can. 143 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 144 | case $MAX_FD in #( 145 | max*) 146 | MAX_FD=$( ulimit -H -n ) || 147 | warn "Could not query maximum file descriptor limit" 148 | esac 149 | case $MAX_FD in #( 150 | '' | soft) :;; #( 151 | *) 152 | ulimit -n "$MAX_FD" || 153 | warn "Could not set maximum file descriptor limit to $MAX_FD" 154 | esac 155 | fi 156 | 157 | # Collect all arguments for the java command, stacking in reverse order: 158 | # * args from the command line 159 | # * the main class name 160 | # * -classpath 161 | # * -D...appname settings 162 | # * --module-path (only if needed) 163 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 164 | 165 | # For Cygwin or MSYS, switch paths to Windows format before running java 166 | if "$cygwin" || "$msys" ; then 167 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 168 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 169 | 170 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 171 | 172 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 173 | for arg do 174 | if 175 | case $arg in #( 176 | -*) false ;; # don't mess with options #( 177 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 178 | [ -e "$t" ] ;; #( 179 | *) false ;; 180 | esac 181 | then 182 | arg=$( cygpath --path --ignore --mixed "$arg" ) 183 | fi 184 | # Roll the args list around exactly as many times as the number of 185 | # args, so each arg winds up back in the position where it started, but 186 | # possibly modified. 187 | # 188 | # NB: a `for` loop captures its iteration list before it begins, so 189 | # changing the positional parameters here affects neither the number of 190 | # iterations, nor the values presented in `arg`. 191 | shift # remove old arg 192 | set -- "$@" "$arg" # push replacement arg 193 | done 194 | fi 195 | 196 | # Collect all arguments for the java command; 197 | # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of 198 | # shell script including quotes and variable substitutions, so put them in 199 | # double quotes to make sure that they get re-expanded; and 200 | # * put everything else in single quotes, so that it's not re-expanded. 201 | 202 | set -- \ 203 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 204 | -classpath "$CLASSPATH" \ 205 | org.gradle.wrapper.GradleWrapperMain \ 206 | "$@" 207 | 208 | # Use "xargs" to parse quoted args. 209 | # 210 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 211 | # 212 | # In Bash we could simply go: 213 | # 214 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 215 | # set -- "${ARGS[@]}" "$@" 216 | # 217 | # but POSIX shell has neither arrays nor command substitution, so instead we 218 | # post-process each arg (as a line of input to sed) to backslash-escape any 219 | # character that might be a shell metacharacter, then use eval to reverse 220 | # that process (while maintaining the separation between arguments), and wrap 221 | # the whole thing up as a single "set" statement. 222 | # 223 | # This will of course break if any of these variables contains a newline or 224 | # an unmatched quote. 225 | # 226 | 227 | eval "set -- $( 228 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 229 | xargs -n1 | 230 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 231 | tr '\n' ' ' 232 | )" '"$@"' 233 | 234 | exec "$JAVACMD" "$@" 235 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /img/chart.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mmoczkowski/Chart/c6e983b4e9b4175a0b42f2755377d38e9651e05c/img/chart.gif -------------------------------------------------------------------------------- /provider/api/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | plugins { 18 | alias(libs.plugins.jb.compose) 19 | alias(libs.plugins.chart.jvm) 20 | alias(libs.plugins.chart.maven) 21 | alias(libs.plugins.chart.signing) 22 | } 23 | 24 | dependencies { 25 | implementation(libs.compose.desktop) 26 | } 27 | -------------------------------------------------------------------------------- /provider/api/src/main/kotlin/com/mmoczkowski/chart/provider/api/Tile.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.mmoczkowski.chart.provider.api 18 | 19 | import androidx.compose.ui.unit.IntOffset 20 | 21 | data class Tile( 22 | val coords: TileCoords, 23 | val pixelPosition: IntOffset, 24 | ) 25 | -------------------------------------------------------------------------------- /provider/api/src/main/kotlin/com/mmoczkowski/chart/provider/api/TileCoords.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.mmoczkowski.chart.provider.api 18 | 19 | data class TileCoords( 20 | val x: Int, 21 | val y: Int, 22 | val zoom: Int, 23 | ) 24 | -------------------------------------------------------------------------------- /provider/api/src/main/kotlin/com/mmoczkowski/chart/provider/api/TileProvider.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.mmoczkowski.chart.provider.api 18 | 19 | import androidx.compose.ui.graphics.ImageBitmap 20 | 21 | interface TileProvider { 22 | suspend fun getTile(tile: TileCoords, tileSize: Int): ImageBitmap 23 | } 24 | -------------------------------------------------------------------------------- /provider/api/src/main/kotlin/com/mmoczkowski/chart/provider/api/TileSizeUnsupportedException.kt: -------------------------------------------------------------------------------- 1 | package com.mmoczkowski.chart.provider.api 2 | 3 | class TileSizeUnsupportedException(val tileSize: Int) : 4 | Exception("Tile size of $tileSize is not supported") 5 | -------------------------------------------------------------------------------- /provider/impl/debug/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | plugins { 18 | alias(libs.plugins.chart.jvm) 19 | alias(libs.plugins.jb.compose) 20 | } 21 | 22 | dependencies { 23 | implementation(projects.provider.api) 24 | implementation(libs.compose.desktop) 25 | } 26 | -------------------------------------------------------------------------------- /provider/impl/debug/src/main/kotlin/com/mmoczkowski/chart/provider/impl/debug/DebugTileProvider.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.mmoczkowski.chart.provider.impl.debug 18 | 19 | import androidx.compose.runtime.Composable 20 | import androidx.compose.runtime.remember 21 | import androidx.compose.ui.graphics.ImageBitmap 22 | import androidx.compose.ui.graphics.asComposeImageBitmap 23 | import com.mmoczkowski.chart.provider.api.TileCoords 24 | import com.mmoczkowski.chart.provider.api.TileProvider 25 | import org.jetbrains.skia.Bitmap 26 | import org.jetbrains.skia.Canvas 27 | import org.jetbrains.skia.Color4f 28 | import org.jetbrains.skia.Font 29 | import org.jetbrains.skia.Paint 30 | import org.jetbrains.skia.Rect 31 | import org.jetbrains.skia.Typeface 32 | 33 | class DebugTileProvider : TileProvider { 34 | override suspend fun getTile(tile: TileCoords, tileSize: Int): ImageBitmap { 35 | val bitmap = Bitmap() 36 | bitmap.allocN32Pixels(tileSize, tileSize) 37 | val canvas = Canvas(bitmap) 38 | 39 | val bgPaint = Paint().setColor4f(Color4f(0.2f, 0.2f, 0.2f, 1f), null) 40 | canvas.drawRect(Rect(0f, 0f, tileSize.toFloat(), tileSize.toFloat()), bgPaint) 41 | 42 | val textPaint = Paint().setColor4f(Color4f(1f, 0f, 0f, 1f), null) 43 | val font = Font(Typeface.makeDefault()).apply { 44 | size = 20f 45 | } 46 | 47 | val textHeight = font.metrics.descent - font.metrics.ascent 48 | val text = "(${tile.x}, ${tile.y})" 49 | val textWidth = font.measureText(text, textPaint).width 50 | val x = (tileSize - textWidth) / 2 51 | val y = (tileSize + textHeight) / 2 - font.metrics.descent 52 | canvas.drawString(text, x, y, font, textPaint) 53 | return bitmap.asComposeImageBitmap() 54 | } 55 | } 56 | 57 | @Composable 58 | fun rememberDebugTileProvider(): TileProvider = remember { DebugTileProvider() } 59 | -------------------------------------------------------------------------------- /provider/impl/google/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | plugins { 18 | alias(libs.plugins.jb.compose) 19 | alias(libs.plugins.kotlinx.serialization) 20 | alias(libs.plugins.chart.jvm) 21 | alias(libs.plugins.chart.maven) 22 | alias(libs.plugins.chart.signing) 23 | } 24 | 25 | dependencies { 26 | implementation(projects.provider.api) 27 | implementation(libs.compose.desktop) 28 | implementation(libs.kotlinx.serialization.json) 29 | implementation(platform(libs.ktor.bom)) 30 | implementation(libs.ktor.client.auth) 31 | implementation(libs.ktor.client.cio) 32 | implementation(libs.ktor.client.content.negotiation) 33 | implementation(libs.ktor.client.core) 34 | implementation(libs.ktor.serialization.json) 35 | } 36 | -------------------------------------------------------------------------------- /provider/impl/google/src/main/kotlin/com/mmoczkowski/chart/provider/impl/google/GoogleTileProvider.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.mmoczkowski.chart.provider.impl.google 18 | 19 | import androidx.compose.runtime.Composable 20 | import androidx.compose.runtime.remember 21 | import androidx.compose.ui.graphics.ImageBitmap 22 | import androidx.compose.ui.graphics.toComposeImageBitmap 23 | import com.mmoczkowski.chart.provider.api.TileCoords 24 | import com.mmoczkowski.chart.provider.api.TileProvider 25 | import com.mmoczkowski.chart.provider.api.TileSizeUnsupportedException 26 | import io.ktor.client.HttpClient 27 | import io.ktor.client.call.body 28 | import io.ktor.client.engine.cio.CIO 29 | import io.ktor.client.plugins.contentnegotiation.ContentNegotiation 30 | import io.ktor.client.request.get 31 | import io.ktor.client.request.post 32 | import io.ktor.client.request.setBody 33 | import io.ktor.client.statement.HttpResponse 34 | import io.ktor.http.ContentType 35 | import io.ktor.http.HttpStatusCode 36 | import io.ktor.http.contentType 37 | import io.ktor.serialization.kotlinx.json.json 38 | import kotlinx.coroutines.sync.Mutex 39 | import kotlinx.coroutines.sync.withLock 40 | import org.jetbrains.skia.Image 41 | 42 | class GoogleTileProvider internal constructor( 43 | private val apiKey: String, 44 | private val spec: MapSpec, 45 | private val httpClient: HttpClient 46 | ) : TileProvider { 47 | private var session: Session? = null 48 | private val mutex = Mutex() 49 | 50 | override suspend fun getTile(tile: TileCoords, tileSize: Int): ImageBitmap { 51 | mutex.withLock { 52 | if (session == null) { 53 | session = getSession() 54 | } 55 | } 56 | 57 | val bitmap: ImageBitmap = getTileBitmap(tile) 58 | if(bitmap.height != tileSize || bitmap.width != tileSize) { 59 | throw TileSizeUnsupportedException(tileSize) 60 | } 61 | 62 | return bitmap 63 | } 64 | 65 | private suspend fun getSession(): Session = 66 | httpClient.post("https://tile.googleapis.com/v1/createSession?key=$apiKey") { 67 | contentType(ContentType.Application.Json) 68 | setBody(spec) 69 | }.body() 70 | 71 | private suspend fun getTileBitmap(tile: TileCoords): ImageBitmap { 72 | val response: HttpResponse = 73 | httpClient.get( 74 | "https://tile.googleapis.com/v1/2dtiles/" + 75 | "${tile.zoom}/" + 76 | "${tile.x}/" + 77 | "${tile.y}?" + 78 | "session=${session?.token}&" + 79 | "key=$apiKey&" + 80 | "orientation=0" 81 | ) 82 | 83 | mutex.withLock { 84 | if (response.status == HttpStatusCode.Unauthorized) { 85 | session = null 86 | } 87 | } 88 | 89 | val mapTileBytes: ByteArray = response.body() 90 | return Image.makeFromEncoded(mapTileBytes).toComposeImageBitmap() 91 | } 92 | } 93 | 94 | @Composable 95 | fun rememberGoogleTileProvider( 96 | apiKey: String, 97 | mapSpec: MapSpec = MapSpec( 98 | mapType = "roadmap", 99 | language = "en-US", 100 | region = "US" 101 | ) 102 | ): TileProvider { 103 | val httpClient: HttpClient = rememberHttpClient() 104 | return remember(apiKey, mapSpec) { 105 | GoogleTileProvider(apiKey, mapSpec, httpClient) 106 | } 107 | } 108 | 109 | @Composable 110 | private fun rememberHttpClient(): HttpClient = remember { 111 | HttpClient(CIO) { 112 | install(ContentNegotiation) { 113 | json() 114 | } 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /provider/impl/google/src/main/kotlin/com/mmoczkowski/chart/provider/impl/google/MapSpec.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.mmoczkowski.chart.provider.impl.google 18 | 19 | import kotlinx.serialization.SerialName 20 | import kotlinx.serialization.Serializable 21 | 22 | @Serializable 23 | data class MapSpec( 24 | @SerialName("mapType") val mapType: String, 25 | @SerialName("language") val language: String, 26 | @SerialName("region") val region: String 27 | ) 28 | -------------------------------------------------------------------------------- /provider/impl/google/src/main/kotlin/com/mmoczkowski/chart/provider/impl/google/Session.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.mmoczkowski.chart.provider.impl.google 18 | 19 | import kotlinx.serialization.SerialName 20 | import kotlinx.serialization.Serializable 21 | 22 | @Serializable 23 | internal data class Session( 24 | @SerialName("session") val token: String, 25 | @SerialName("expiry") val expiry: String, 26 | @SerialName("tileWidth") val tileWidth: String, 27 | @SerialName("tileHeight") val tileHeight: String, 28 | @SerialName("imageFormat") val imageFormat: String 29 | ) 30 | -------------------------------------------------------------------------------- /provider/impl/open-street-map/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | plugins { 18 | alias(libs.plugins.jb.compose) 19 | alias(libs.plugins.chart.jvm) 20 | alias(libs.plugins.chart.maven) 21 | alias(libs.plugins.chart.signing) 22 | } 23 | 24 | dependencies { 25 | implementation(projects.provider.api) 26 | implementation(projects.provider.impl.url) 27 | implementation(libs.compose.desktop) 28 | } 29 | -------------------------------------------------------------------------------- /provider/impl/open-street-map/src/main/kotlin/com/mmoczkowski/chart/provider/impl/osm/OpenStreetMapTileProvider.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.mmoczkowski.chart.provider.impl.osm 18 | 19 | import androidx.compose.runtime.Composable 20 | import com.mmoczkowski.chart.provider.api.TileProvider 21 | import com.mmoczkowski.chart.provider.api.TileSizeUnsupportedException 22 | import com.mmoczkowski.chart.provider.impl.url.rememberUrlTileProvider 23 | 24 | private const val OSM_TILE_SIZE: Int = 256 25 | 26 | @Composable 27 | fun rememberOpenStreetMapTileProvider(): TileProvider = rememberUrlTileProvider { tileCoords, tileSize -> 28 | if (tileSize != OSM_TILE_SIZE) { 29 | throw TileSizeUnsupportedException(tileSize) 30 | } 31 | 32 | "https://tile.openstreetmap.org/${tileCoords.zoom}/${tileCoords.x}/${tileCoords.y}.png" 33 | } 34 | -------------------------------------------------------------------------------- /provider/impl/url/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | plugins { 18 | alias(libs.plugins.jb.compose) 19 | alias(libs.plugins.chart.jvm) 20 | alias(libs.plugins.chart.maven) 21 | alias(libs.plugins.chart.signing) 22 | } 23 | 24 | dependencies { 25 | implementation(projects.provider.api) 26 | implementation(libs.compose.desktop) 27 | implementation(platform(libs.ktor.bom)) 28 | implementation(libs.ktor.client.core) 29 | implementation(libs.ktor.client.cio) 30 | } 31 | -------------------------------------------------------------------------------- /provider/impl/url/src/main/kotlin/com/mmoczkowski/chart/provider/impl/url/UrlTileProvider.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.mmoczkowski.chart.provider.impl.url 18 | 19 | import androidx.compose.runtime.Composable 20 | import androidx.compose.runtime.remember 21 | import androidx.compose.ui.graphics.ImageBitmap 22 | import androidx.compose.ui.graphics.toComposeImageBitmap 23 | import com.mmoczkowski.chart.provider.api.TileCoords 24 | import com.mmoczkowski.chart.provider.api.TileProvider 25 | import io.ktor.client.HttpClient 26 | import io.ktor.client.call.body 27 | import io.ktor.client.engine.cio.CIO 28 | import io.ktor.client.request.get 29 | import org.jetbrains.skia.Image 30 | 31 | class UrlTileProvider internal constructor( 32 | private val httpClient: HttpClient, 33 | private val urlBuilder: (TileCoords, Int) -> String, 34 | ) : TileProvider { 35 | override suspend fun getTile(tile: TileCoords, tileSize: Int): ImageBitmap { 36 | val url: String = urlBuilder(tile, tileSize) 37 | val mapTileBytes: ByteArray = httpClient.get(url).body() 38 | return Image.makeFromEncoded(mapTileBytes).toComposeImageBitmap() 39 | } 40 | } 41 | 42 | @Composable 43 | fun rememberUrlTileProvider(urlBuilder: (TileCoords, Int) -> String): TileProvider { 44 | val httpClient: HttpClient = rememberHttpClient() 45 | return remember(urlBuilder) { UrlTileProvider(httpClient, urlBuilder) } 46 | } 47 | 48 | @Composable 49 | private fun rememberHttpClient(): HttpClient = remember { 50 | HttpClient(CIO) 51 | } 52 | -------------------------------------------------------------------------------- /sample/aerial/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import org.jetbrains.compose.desktop.application.dsl.TargetFormat 18 | 19 | plugins { 20 | alias(libs.plugins.jb.compose) 21 | alias(libs.plugins.chart.jvm) 22 | } 23 | 24 | val sampleVersion = "1.0.0" 25 | group = "com.mmoczkowski.chart.sample.aerial" 26 | version = sampleVersion 27 | 28 | dependencies { 29 | implementation(projects.cache.api) 30 | implementation(projects.cache.impl.lru) 31 | implementation(projects.provider.api) 32 | implementation(projects.provider.impl.url) 33 | implementation(projects.ui) 34 | implementation(compose.desktop.currentOs) 35 | implementation(libs.compose.desktop) 36 | implementation(libs.compose.material.icons.extended) 37 | } 38 | 39 | compose.desktop { 40 | application { 41 | mainClass = "com.mmoczkowski.chart.sample.aerial.MainKt" 42 | 43 | nativeDistributions { 44 | targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb) 45 | packageName = "com.mmoczkowski.chart.sample" 46 | packageVersion = sampleVersion 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /sample/aerial/src/main/kotlin/com/mmoczkowski/chart/sample/aerial/Main.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.mmoczkowski.chart.sample.aerial 18 | 19 | import androidx.compose.foundation.layout.Box 20 | import androidx.compose.foundation.layout.Column 21 | import androidx.compose.foundation.layout.fillMaxSize 22 | import androidx.compose.foundation.layout.padding 23 | import androidx.compose.foundation.layout.wrapContentSize 24 | import androidx.compose.material.Icon 25 | import androidx.compose.material.IconButton 26 | import androidx.compose.material.MaterialTheme 27 | import androidx.compose.material.Surface 28 | import androidx.compose.material.darkColors 29 | import androidx.compose.material.icons.Icons 30 | import androidx.compose.material.icons.filled.ZoomIn 31 | import androidx.compose.material.icons.filled.ZoomOut 32 | import androidx.compose.runtime.Composable 33 | import androidx.compose.ui.Alignment 34 | import androidx.compose.ui.Modifier 35 | import androidx.compose.ui.unit.DpSize 36 | import androidx.compose.ui.unit.dp 37 | import androidx.compose.ui.window.Window 38 | import androidx.compose.ui.window.application 39 | import androidx.compose.ui.window.rememberWindowState 40 | import com.mmoczkowski.chart.Chart 41 | import com.mmoczkowski.chart.ChartState 42 | import com.mmoczkowski.chart.LatLng 43 | import com.mmoczkowski.chart.cache.impl.lru.rememberLruCache 44 | import com.mmoczkowski.chart.provider.impl.url.rememberUrlTileProvider 45 | import com.mmoczkowski.chart.rememberChartState 46 | 47 | fun main() = application { 48 | Window( 49 | title = "Chart: Aerial Sample", 50 | state = rememberWindowState(size = DpSize(800.dp, 1024.dp)), 51 | onCloseRequest = ::exitApplication 52 | ) { 53 | MaterialTheme(colors = darkColors()) { 54 | val state: ChartState = rememberChartState( 55 | minZoom = 7, 56 | maxZoom = 12, 57 | focus = LatLng(52.2297f, 21.0122f) 58 | ) 59 | 60 | Chart( 61 | state = state, 62 | layers = listOf( 63 | rememberBaseLayer(), 64 | rememberAeroLayer() 65 | ), 66 | cache = rememberLruCache() 67 | ) 68 | 69 | MapControls( 70 | onZoomIn = state::zoomIn, 71 | onZoomOut = state::zoomOut 72 | ) 73 | } 74 | } 75 | } 76 | 77 | @Composable 78 | private fun MapControls( 79 | modifier: Modifier = Modifier, 80 | onZoomIn: () -> Unit, 81 | onZoomOut: () -> Unit, 82 | ) { 83 | Box( 84 | modifier = modifier 85 | .fillMaxSize() 86 | .padding(16.dp) 87 | ) { 88 | Surface(modifier = Modifier.align(Alignment.BottomEnd).wrapContentSize()) { 89 | Column( 90 | modifier = Modifier.padding(8.dp) 91 | ) { 92 | IconButton( 93 | onClick = onZoomIn 94 | ) { 95 | Icon(imageVector = Icons.Default.ZoomIn, contentDescription = null) 96 | } 97 | IconButton( 98 | onClick = onZoomOut 99 | ) { 100 | Icon(imageVector = Icons.Default.ZoomOut, contentDescription = null) 101 | } 102 | } 103 | } 104 | } 105 | } 106 | 107 | @Composable 108 | private fun rememberAeroLayer() = rememberUrlTileProvider { tileCoords, _ -> 109 | "https://nwy-tiles-api.prod.newaydata.com/tiles/" + 110 | "${tileCoords.zoom}/" + 111 | "${tileCoords.x}/" + 112 | "${tileCoords.y}.png?path=2310/aero/latest" 113 | } 114 | 115 | @Composable 116 | private fun rememberBaseLayer() = rememberUrlTileProvider { tileCoords, _ -> 117 | "https://nwy-tiles-api.prod.newaydata.com/tiles/" + 118 | "${tileCoords.zoom}/" + 119 | "${tileCoords.x}/" + 120 | "${tileCoords.y}.jpg?path=2310/base/latest" 121 | } 122 | -------------------------------------------------------------------------------- /sample/basic/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import org.jetbrains.compose.desktop.application.dsl.TargetFormat 18 | 19 | plugins { 20 | alias(libs.plugins.jb.compose) 21 | alias(libs.plugins.chart.jvm) 22 | } 23 | 24 | val sampleVersion = "1.0.0" 25 | group = "com.mmoczkowski.chart.sample.basic" 26 | version = sampleVersion 27 | 28 | dependencies { 29 | implementation(projects.cache.api) 30 | implementation(projects.cache.impl.lru) 31 | implementation(projects.provider.api) 32 | implementation(projects.provider.impl.openStreetMap) 33 | implementation(projects.ui) 34 | implementation(compose.desktop.currentOs) 35 | implementation(libs.compose.desktop) 36 | implementation(libs.compose.material.icons.extended) 37 | 38 | } 39 | 40 | compose.desktop { 41 | application { 42 | mainClass = "com.mmoczkowski.chart.sample.basic.MainKt" 43 | 44 | nativeDistributions { 45 | targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb) 46 | packageName = "com.mmoczkowski.chart.sample" 47 | packageVersion = sampleVersion 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /sample/basic/src/main/kotlin/com/mmoczkowski/chart/sample/basic/Main.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.mmoczkowski.chart.sample.basic 18 | 19 | import androidx.compose.foundation.layout.Box 20 | import androidx.compose.foundation.layout.BoxScope 21 | import androidx.compose.foundation.layout.Column 22 | import androidx.compose.foundation.layout.padding 23 | import androidx.compose.material.Icon 24 | import androidx.compose.material.IconButton 25 | import androidx.compose.material.MaterialTheme 26 | import androidx.compose.material.icons.Icons 27 | import androidx.compose.material.icons.filled.ZoomIn 28 | import androidx.compose.material.icons.filled.ZoomOut 29 | import androidx.compose.runtime.Composable 30 | import androidx.compose.ui.Alignment 31 | import androidx.compose.ui.Modifier 32 | import androidx.compose.ui.unit.DpSize 33 | import androidx.compose.ui.unit.dp 34 | import androidx.compose.ui.window.Window 35 | import androidx.compose.ui.window.application 36 | import androidx.compose.ui.window.rememberWindowState 37 | import com.mmoczkowski.chart.Chart 38 | import com.mmoczkowski.chart.ChartState 39 | import com.mmoczkowski.chart.LatLng 40 | import com.mmoczkowski.chart.cache.impl.lru.rememberLruCache 41 | import com.mmoczkowski.chart.provider.impl.osm.rememberOpenStreetMapTileProvider 42 | import com.mmoczkowski.chart.rememberChartState 43 | 44 | fun main() = application { 45 | Window( 46 | title = "Chart: Basic Sample", 47 | state = rememberWindowState(size = DpSize(800.dp, 1024.dp)), 48 | onCloseRequest = ::exitApplication 49 | ) { 50 | MaterialTheme { 51 | Box { 52 | val state: ChartState = rememberChartState( 53 | zoom = 10, 54 | focus = LatLng(52.2297f, 21.0122f) 55 | ) 56 | Chart( 57 | state = state, 58 | provider = rememberOpenStreetMapTileProvider(), 59 | cache = rememberLruCache(maxSize = 256) 60 | ) 61 | MapControls( 62 | onZoomIn = state::zoomIn, 63 | onZoomOut = state::zoomOut 64 | ) 65 | } 66 | } 67 | } 68 | } 69 | 70 | @Composable 71 | private fun BoxScope.MapControls( 72 | onZoomIn: () -> Unit, 73 | onZoomOut: () -> Unit 74 | ) { 75 | Column( 76 | modifier = Modifier 77 | .align(Alignment.BottomEnd) 78 | .padding(16.dp) 79 | ) { 80 | IconButton( 81 | onClick = onZoomIn 82 | ) { 83 | Icon(imageVector = Icons.Default.ZoomIn, contentDescription = null) 84 | } 85 | IconButton( 86 | onClick = onZoomOut 87 | ) { 88 | Icon(imageVector = Icons.Default.ZoomOut, contentDescription = null) 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /sample/mars/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import org.jetbrains.compose.desktop.application.dsl.TargetFormat 18 | 19 | plugins { 20 | alias(libs.plugins.jb.compose) 21 | alias(libs.plugins.chart.jvm) 22 | } 23 | 24 | val sampleVersion = "1.0.0" 25 | group = "com.mmoczkowski.chart.sample.mars" 26 | version = sampleVersion 27 | 28 | dependencies { 29 | implementation(projects.cache.api) 30 | implementation(projects.cache.impl.lru) 31 | implementation(projects.provider.api) 32 | implementation(projects.provider.impl.url) 33 | implementation(projects.ui) 34 | implementation(compose.desktop.currentOs) 35 | implementation(libs.compose.desktop) 36 | implementation(libs.compose.material.icons.extended) 37 | } 38 | 39 | compose.desktop { 40 | application { 41 | mainClass = "com.mmoczkowski.chart.sample.mars.MainKt" 42 | 43 | nativeDistributions { 44 | targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb) 45 | packageName = "com.mmoczkowski.chart.sample" 46 | packageVersion = sampleVersion 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /sample/mars/src/main/kotlin/com/mmoczkowski/chart/sample/mars/Main.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.mmoczkowski.chart.sample.mars 18 | 19 | import androidx.compose.foundation.ExperimentalFoundationApi 20 | import androidx.compose.foundation.TooltipArea 21 | import androidx.compose.foundation.layout.Box 22 | import androidx.compose.foundation.layout.Column 23 | import androidx.compose.foundation.layout.Row 24 | import androidx.compose.foundation.layout.fillMaxSize 25 | import androidx.compose.foundation.layout.padding 26 | import androidx.compose.foundation.layout.requiredSize 27 | import androidx.compose.foundation.layout.wrapContentSize 28 | import androidx.compose.foundation.selection.selectable 29 | import androidx.compose.foundation.shape.RoundedCornerShape 30 | import androidx.compose.material.Icon 31 | import androidx.compose.material.IconButton 32 | import androidx.compose.material.MaterialTheme 33 | import androidx.compose.material.Surface 34 | import androidx.compose.material.Text 35 | import androidx.compose.material.darkColors 36 | import androidx.compose.material.icons.Icons 37 | import androidx.compose.material.icons.filled.Place 38 | import androidx.compose.material.icons.filled.ZoomIn 39 | import androidx.compose.material.icons.filled.ZoomOut 40 | import androidx.compose.runtime.Composable 41 | import androidx.compose.runtime.getValue 42 | import androidx.compose.runtime.mutableStateOf 43 | import androidx.compose.runtime.remember 44 | import androidx.compose.runtime.setValue 45 | import androidx.compose.ui.Alignment 46 | import androidx.compose.ui.Modifier 47 | import androidx.compose.ui.draw.shadow 48 | import androidx.compose.ui.geometry.Offset 49 | import androidx.compose.ui.unit.DpSize 50 | import androidx.compose.ui.unit.dp 51 | import androidx.compose.ui.window.Window 52 | import androidx.compose.ui.window.application 53 | import androidx.compose.ui.window.rememberWindowState 54 | import com.mmoczkowski.chart.Chart 55 | import com.mmoczkowski.chart.LatLng 56 | import com.mmoczkowski.chart.MarkerScope.Companion.relativeOffset 57 | import com.mmoczkowski.chart.cache.impl.lru.rememberLruCache 58 | import com.mmoczkowski.chart.provider.impl.url.rememberUrlTileProvider 59 | import com.mmoczkowski.chart.rememberChartState 60 | import com.mmoczkowski.chart.sample.mars.data.MapLayer 61 | import com.mmoczkowski.chart.sample.mars.data.PointOfInterest 62 | import com.mmoczkowski.chart.sample.mars.data.pointsOfInterests 63 | import kotlin.math.pow 64 | 65 | fun main() = application { 66 | Window( 67 | title = "Chart: Mars Sample", 68 | state = rememberWindowState(size = DpSize(800.dp, 1024.dp)), 69 | onCloseRequest = ::exitApplication 70 | ) { 71 | MaterialTheme(colors = darkColors()) { 72 | val chartState = rememberChartState( 73 | zoom = 3, 74 | tilesRowCount = { zoom -> (2f.pow(zoom) * 2).toInt() } 75 | ) 76 | var selectedLayerIndex: Int by remember { mutableStateOf(0) } 77 | val providers = MapLayer.entries.map { layer -> 78 | rememberUrlTileProvider { tileCoords, _ -> 79 | "https://api.nasa.gov/mars-wmts/catalog/${layer.id}/1.0.0/default/default028mm/" + 80 | "${tileCoords.zoom}/" + 81 | "${tileCoords.y}/" + 82 | "${tileCoords.x}.png" 83 | } 84 | } 85 | 86 | Chart( 87 | state = chartState, 88 | markers = remember { pointsOfInterests }, 89 | marker = { poi -> PoiMarker(poi) }, 90 | markerPosition = { poi -> poi.position }, 91 | layers = providers, 92 | isLayerVisible = { layerIndex -> selectedLayerIndex == layerIndex }, 93 | cache = rememberLruCache() 94 | ) 95 | 96 | MapControls( 97 | focus = chartState.focus, 98 | selectedLayer = selectedLayerIndex, 99 | onLayerSelected = { layer -> selectedLayerIndex = layer }, 100 | onZoomIn = chartState::zoomIn, 101 | onZoomOut = chartState::zoomOut 102 | ) 103 | } 104 | } 105 | } 106 | 107 | @Composable 108 | private fun MapControls( 109 | modifier: Modifier = Modifier, 110 | focus: LatLng, 111 | selectedLayer: Int, 112 | onLayerSelected: (Int) -> Unit, 113 | onZoomIn: () -> Unit, 114 | onZoomOut: () -> Unit, 115 | ) { 116 | Box( 117 | modifier = modifier 118 | .fillMaxSize() 119 | .padding(16.dp) 120 | ) { 121 | Surface { 122 | Text( 123 | text = focus.toString(), 124 | modifier = Modifier 125 | .align(Alignment.TopStart) 126 | .padding(8.dp) 127 | ) 128 | } 129 | 130 | Row(modifier = Modifier.align(Alignment.TopEnd)) { 131 | MapLayer.entries.forEachIndexed { index, layer -> 132 | LayerButton( 133 | isSelected = index == selectedLayer, 134 | name = layer.name 135 | ) { 136 | onLayerSelected(index) 137 | } 138 | } 139 | } 140 | 141 | Surface(modifier = Modifier.align(Alignment.BottomEnd)) { 142 | Column( 143 | modifier = Modifier.padding(8.dp) 144 | ) { 145 | IconButton( 146 | onClick = onZoomIn 147 | ) { 148 | Icon(imageVector = Icons.Default.ZoomIn, contentDescription = null) 149 | } 150 | IconButton( 151 | onClick = onZoomOut 152 | ) { 153 | Icon(imageVector = Icons.Default.ZoomOut, contentDescription = null) 154 | } 155 | } 156 | } 157 | } 158 | } 159 | 160 | @Composable 161 | private fun LayerButton( 162 | modifier: Modifier = Modifier, 163 | isSelected: Boolean, 164 | name: String, 165 | onClick: () -> Unit, 166 | ) { 167 | Surface( 168 | color = if (isSelected) { 169 | MaterialTheme.colors.surface 170 | } else { 171 | MaterialTheme.colors.onSurface 172 | }, 173 | modifier = modifier.selectable(selected = isSelected, onClick = onClick) 174 | ) { 175 | Text(name, modifier = Modifier.padding(8.dp)) 176 | } 177 | } 178 | 179 | @OptIn(ExperimentalFoundationApi::class) 180 | @Composable 181 | private fun PoiMarker(poi: PointOfInterest, modifier: Modifier = Modifier) { 182 | TooltipArea( 183 | tooltip = { 184 | Surface( 185 | modifier = Modifier.shadow(8.dp), 186 | shape = RoundedCornerShape(8.dp) 187 | ) { 188 | Text( 189 | text = poi.title, 190 | modifier = Modifier.padding(10.dp) 191 | ) 192 | } 193 | }, 194 | modifier = modifier 195 | .wrapContentSize() 196 | .relativeOffset(Offset(0f, -0.5f)) 197 | ) { 198 | Icon( 199 | imageVector = Icons.Default.Place, 200 | contentDescription = null, 201 | modifier = Modifier.requiredSize(32.dp) 202 | ) 203 | } 204 | } 205 | -------------------------------------------------------------------------------- /sample/mars/src/main/kotlin/com/mmoczkowski/chart/sample/mars/data/MapLayer.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.mmoczkowski.chart.sample.mars.data 18 | 19 | enum class MapLayer( 20 | val id: String 21 | ) { 22 | Color( 23 | id = "Mars_Viking_MDIM21_ClrMosaic_global_232m", 24 | ), 25 | Elevation( 26 | id = "Mars_MGS_MOLA_ClrShade_merge_global_463m", 27 | ), 28 | Albedo( 29 | id = "msss_atlas_simp_clon", 30 | ) 31 | } 32 | -------------------------------------------------------------------------------- /sample/mars/src/main/kotlin/com/mmoczkowski/chart/sample/mars/data/PointOfInterest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.mmoczkowski.chart.sample.mars.data 18 | 19 | import com.mmoczkowski.chart.LatLng 20 | 21 | data class PointOfInterest( 22 | val title: String, 23 | val country: String, 24 | val date: String, 25 | val position: LatLng 26 | ) 27 | 28 | internal val pointsOfInterests: List = 29 | listOf( 30 | PointOfInterest( 31 | title = "Viking 1", 32 | country = "US", 33 | date = "1976-07-20", 34 | position = LatLng(latitude = 22.697f, longitude = -49.97f) 35 | ), 36 | PointOfInterest( 37 | title = "Viking 2", 38 | country = "US", 39 | date = "1976-09-03", 40 | position = LatLng(latitude = 48.269f, longitude = -225.99f) 41 | ), 42 | PointOfInterest( 43 | title = "Pathfinder", 44 | country = "US", 45 | date = "1997-07-04", 46 | position = LatLng(latitude = 19.33f, longitude = -33.55f) 47 | ), 48 | PointOfInterest( 49 | title = "Spirit", 50 | country = "US", 51 | date = "2004-01-04", 52 | position = LatLng(latitude = -14.568f, longitude = 175.472f) 53 | ), 54 | PointOfInterest( 55 | title = "Opportunity", 56 | country = "US", 57 | date = "2004-01-25", 58 | position = LatLng(latitude = -1.946f, longitude = -5.537f) 59 | ), 60 | PointOfInterest( 61 | title = "Phoenix", 62 | country = "US", 63 | date = "2008-05-25", 64 | position = LatLng(latitude = 68.218f, longitude = -125.749f) 65 | ), 66 | PointOfInterest( 67 | title = "Curiosity", 68 | country = "US", 69 | date = "2012-08-06", 70 | position = LatLng(latitude = -4.589f, longitude = 137.441f) 71 | ), 72 | PointOfInterest( 73 | title = "Perseverance", 74 | country = "US", 75 | date = "2021-02-18", 76 | position = LatLng(latitude = 18.444f, longitude = 77.450f) 77 | ), 78 | PointOfInterest( 79 | title = "Zhurong", 80 | country = "China", 81 | date = "2021-05-14", 82 | position = LatLng(latitude = 25.1f, longitude = 109.9f) 83 | ) 84 | ) -------------------------------------------------------------------------------- /settings.gradle.kts: -------------------------------------------------------------------------------- 1 | enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS") 2 | 3 | pluginManagement { 4 | includeBuild("build-logic") 5 | repositories { 6 | google() 7 | gradlePluginPortal() 8 | mavenCentral() 9 | maven("https://maven.pkg.jetbrains.space/public/p/compose/dev") 10 | } 11 | } 12 | 13 | dependencyResolutionManagement { 14 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) 15 | repositories { 16 | mavenCentral() 17 | } 18 | } 19 | 20 | rootProject.name = "Chart" 21 | 22 | include(":cache:api") 23 | include(":cache:impl:lru") 24 | include(":cache:impl:no-op") 25 | include(":provider:api") 26 | include(":provider:impl:debug") 27 | include(":provider:impl:google") 28 | include(":provider:impl:open-street-map") 29 | include(":provider:impl:url") 30 | include(":sample:aerial") 31 | include(":sample:basic") 32 | include(":sample:mars") 33 | include(":ui") 34 | -------------------------------------------------------------------------------- /ui/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | plugins { 18 | alias(libs.plugins.jb.compose) 19 | alias(libs.plugins.chart.jvm) 20 | alias(libs.plugins.chart.maven) 21 | alias(libs.plugins.chart.signing) 22 | } 23 | 24 | dependencies { 25 | implementation(projects.cache.api) 26 | implementation(projects.provider.api) 27 | implementation(libs.compose.desktop) 28 | testImplementation(libs.kotlin.test) 29 | } 30 | -------------------------------------------------------------------------------- /ui/src/main/kotlin/com/mmoczkowski/chart/Chart.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.mmoczkowski.chart 18 | 19 | import androidx.compose.foundation.Image 20 | import androidx.compose.foundation.gestures.detectTapGestures 21 | import androidx.compose.foundation.gestures.rememberTransformableState 22 | import androidx.compose.foundation.gestures.transformable 23 | import androidx.compose.foundation.layout.BoxWithConstraints 24 | import androidx.compose.foundation.layout.fillMaxSize 25 | import androidx.compose.foundation.layout.requiredSize 26 | import androidx.compose.foundation.layout.wrapContentSize 27 | import androidx.compose.material.CircularProgressIndicator 28 | import androidx.compose.material.Icon 29 | import androidx.compose.material.Surface 30 | import androidx.compose.material.icons.Icons 31 | import androidx.compose.material.icons.filled.Place 32 | import androidx.compose.material.icons.filled.Warning 33 | import androidx.compose.runtime.Composable 34 | import androidx.compose.runtime.remember 35 | import androidx.compose.ui.Modifier 36 | import androidx.compose.ui.draw.clipToBounds 37 | import androidx.compose.ui.geometry.Offset 38 | import androidx.compose.ui.graphics.ImageBitmap 39 | import androidx.compose.ui.input.pointer.pointerInput 40 | import androidx.compose.ui.layout.ContentScale 41 | import androidx.compose.ui.unit.IntSize 42 | import androidx.compose.ui.unit.dp 43 | import com.mmoczkowski.chart.MarkerScope.Companion.relativeOffset 44 | import com.mmoczkowski.chart.cache.api.Cache 45 | import com.mmoczkowski.chart.provider.api.TileCoords 46 | import com.mmoczkowski.chart.provider.api.TileProvider 47 | 48 | @Composable 49 | fun Chart( 50 | modifier: Modifier = Modifier, 51 | state: ChartState = rememberChartState(), 52 | success: @Composable (TileCoords, ImageBitmap) -> Unit = { _, image -> 53 | DefaultSuccessTile(image) 54 | }, 55 | error: @Composable () -> Unit = { 56 | DefaultErrorTile() 57 | }, 58 | loading: @Composable () -> Unit = { 59 | DefaultLoadingTile() 60 | }, 61 | background: @Composable () -> Unit = { 62 | DefaultBackground() 63 | }, 64 | provider: TileProvider, 65 | cache: Cache, ImageBitmap> 66 | ) { 67 | Chart( 68 | modifier = modifier, 69 | state = state, 70 | success = success, 71 | error = error, 72 | loading = loading, 73 | markers = emptyList(), 74 | markerPosition = { LatLng.NullIsland }, 75 | marker = { /* no-op */ }, 76 | background = background, 77 | layers = remember(provider) { listOf(provider) }, 78 | cache = cache 79 | ) 80 | } 81 | 82 | @Composable 83 | fun Chart( 84 | modifier: Modifier = Modifier, 85 | state: ChartState = rememberChartState(), 86 | success: @Composable (TileCoords, ImageBitmap) -> Unit = { _, image -> 87 | DefaultSuccessTile(image) 88 | }, 89 | error: @Composable () -> Unit = { 90 | DefaultErrorTile() 91 | }, 92 | loading: @Composable () -> Unit = { 93 | DefaultLoadingTile() 94 | }, 95 | background: @Composable () -> Unit = { 96 | DefaultBackground() 97 | }, 98 | layers: List, 99 | isLayerVisible: (Int) -> Boolean = { true }, 100 | cache: Cache, ImageBitmap> 101 | ) { 102 | Chart( 103 | modifier = modifier, 104 | state = state, 105 | success = success, 106 | error = error, 107 | loading = loading, 108 | markers = emptyList(), 109 | markerPosition = { LatLng.NullIsland }, 110 | marker = { /* no-op */ }, 111 | background = background, 112 | layers = layers, 113 | isLayerVisible = isLayerVisible, 114 | cache = cache 115 | ) 116 | } 117 | 118 | @Composable 119 | fun Chart( 120 | modifier: Modifier = Modifier, 121 | state: ChartState = rememberChartState(), 122 | success: @Composable (TileCoords, ImageBitmap) -> Unit = { _, image -> 123 | DefaultSuccessTile(image) 124 | }, 125 | error: @Composable () -> Unit = { 126 | DefaultErrorTile() 127 | }, 128 | loading: @Composable () -> Unit = { 129 | DefaultLoadingTile() 130 | }, 131 | markers: List, 132 | markerPosition: (M) -> LatLng, 133 | marker: @Composable MarkerScope.(M) -> Unit = { DefaultMarker() }, 134 | background: @Composable () -> Unit = { 135 | DefaultBackground() 136 | }, 137 | layers: List, 138 | isLayerVisible: (Int) -> Boolean = { true }, 139 | cache: Cache, ImageBitmap> 140 | ) { 141 | val transformableState = rememberTransformableState { _, offsetDelta, _ -> 142 | state.shiftFocus(offsetDelta = offsetDelta) 143 | } 144 | 145 | BoxWithConstraints( 146 | modifier = modifier 147 | .fillMaxSize() 148 | .clipToBounds() 149 | .transformable(transformableState) 150 | .pointerInput(Unit) { 151 | detectTapGestures( 152 | onDoubleTap = { _ -> 153 | state.zoomIn() 154 | } 155 | ) 156 | } 157 | ) { 158 | background() 159 | 160 | state.viewportSize = IntSize(constraints.maxWidth, constraints.maxHeight) 161 | 162 | ChartLayer( 163 | tiles = state.visibleTiles, 164 | tileSize = state.tileSize, 165 | offset = state.translationOffset, 166 | success = success, 167 | error = error, 168 | loading = loading, 169 | layers = layers, 170 | isLayerVisible = isLayerVisible, 171 | cache = cache 172 | ) 173 | 174 | MarkerLayer( 175 | state = state, 176 | markers = markers, 177 | markerPosition = markerPosition, 178 | marker = marker, 179 | ) 180 | } 181 | } 182 | 183 | @Composable 184 | fun DefaultSuccessTile(image: ImageBitmap) { 185 | Image( 186 | bitmap = image, 187 | contentScale = ContentScale.FillBounds, 188 | contentDescription = null, 189 | modifier = Modifier.fillMaxSize() 190 | ) 191 | } 192 | 193 | @Composable 194 | fun DefaultErrorTile() { 195 | Surface(modifier = Modifier.fillMaxSize()) { 196 | Icon( 197 | imageVector = Icons.Default.Warning, 198 | contentDescription = null, 199 | modifier = Modifier.requiredSize(32.dp) 200 | ) 201 | } 202 | } 203 | 204 | @Composable 205 | fun DefaultLoadingTile() { 206 | Surface(modifier = Modifier.fillMaxSize()) { 207 | CircularProgressIndicator( 208 | modifier = Modifier.wrapContentSize() 209 | ) 210 | } 211 | } 212 | 213 | @Composable 214 | fun DefaultBackground() { 215 | Surface(modifier = Modifier.fillMaxSize()) { 216 | /* no-op */ 217 | } 218 | } 219 | 220 | @Composable 221 | fun DefaultMarker() { 222 | Icon( 223 | imageVector = Icons.Default.Place, 224 | contentDescription = null, 225 | modifier = Modifier 226 | .requiredSize(32.dp) 227 | .relativeOffset(Offset(0f, -0.5f)) 228 | ) 229 | } 230 | -------------------------------------------------------------------------------- /ui/src/main/kotlin/com/mmoczkowski/chart/ChartLayer.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.mmoczkowski.chart 18 | 19 | import androidx.compose.foundation.layout.Box 20 | import androidx.compose.foundation.layout.fillMaxSize 21 | import androidx.compose.foundation.layout.requiredSize 22 | import androidx.compose.runtime.Composable 23 | import androidx.compose.runtime.LaunchedEffect 24 | import androidx.compose.runtime.getValue 25 | import androidx.compose.runtime.key 26 | import androidx.compose.runtime.mutableStateOf 27 | import androidx.compose.runtime.remember 28 | import androidx.compose.runtime.setValue 29 | import androidx.compose.ui.Modifier 30 | import androidx.compose.ui.geometry.Offset 31 | import androidx.compose.ui.graphics.ImageBitmap 32 | import androidx.compose.ui.layout.Layout 33 | import androidx.compose.ui.platform.LocalDensity 34 | import com.mmoczkowski.chart.cache.api.Cache 35 | import com.mmoczkowski.chart.provider.api.Tile 36 | import com.mmoczkowski.chart.provider.api.TileCoords 37 | import com.mmoczkowski.chart.provider.api.TileProvider 38 | 39 | @Composable 40 | internal fun ChartLayer( 41 | modifier: Modifier = Modifier, 42 | tiles: List, 43 | tileSize: Int, 44 | offset: Offset, 45 | layers: List, 46 | isLayerVisible: (Int) -> Boolean, 47 | success: @Composable (TileCoords, ImageBitmap) -> Unit, 48 | error: @Composable () -> Unit, 49 | loading: @Composable () -> Unit, 50 | cache: Cache, ImageBitmap>, 51 | ) { 52 | Layout( 53 | content = { 54 | layers.forEachIndexed { layerIndex, provider -> 55 | tiles.forEach { tile -> 56 | key(layerIndex, tile.coords) { 57 | var tileState: TileState by remember { mutableStateOf(TileState.Loading) } 58 | LaunchedEffect(tile.coords, provider) { 59 | tileState = TileState.Loading 60 | tileState = try { 61 | val image: ImageBitmap = cache.get(layerIndex to tile.coords) { (_, coords) -> 62 | provider.getTile(coords, tileSize) 63 | } 64 | TileState.Success(tile.coords, image = image) 65 | } catch (exception: Exception) { 66 | TileState.Error 67 | } 68 | } 69 | val tileSizeDp = with(LocalDensity.current) { tileSize.toDp() } 70 | 71 | TileContent( 72 | state = tileState, 73 | success = success, 74 | error = error, 75 | loading = loading, 76 | modifier = Modifier.requiredSize(tileSizeDp) 77 | ) 78 | } 79 | } 80 | } 81 | 82 | }, 83 | modifier = modifier.fillMaxSize() 84 | ) { measurables, constraints -> 85 | val placeables = measurables.map { measurable -> 86 | measurable.measure(constraints) 87 | } 88 | 89 | layout(constraints.maxWidth, constraints.maxHeight) { 90 | placeables.forEachIndexed { index, placeable -> 91 | val layerIndex: Int = index / tiles.size 92 | if(isLayerVisible(layerIndex)) { 93 | val tileIndex: Int = index % tiles.size 94 | val tile: Tile = tiles[tileIndex] 95 | 96 | placeable.placeRelativeWithLayer( 97 | position = tile.pixelPosition, 98 | zIndex = layerIndex.toFloat(), 99 | ) { 100 | translationX = offset.x 101 | translationY = offset.y 102 | } 103 | } 104 | } 105 | } 106 | } 107 | } 108 | 109 | @Composable 110 | private fun TileContent( 111 | state: TileState, 112 | success: @Composable (TileCoords, ImageBitmap) -> Unit, 113 | error: @Composable () -> Unit, 114 | loading: @Composable () -> Unit, 115 | modifier: Modifier = Modifier 116 | ) { 117 | Box(modifier) { 118 | when (state) { 119 | is TileState.Success -> success(state.coords, state.image) 120 | TileState.Error -> error() 121 | TileState.Loading -> loading() 122 | } 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /ui/src/main/kotlin/com/mmoczkowski/chart/ChartState.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.mmoczkowski.chart 18 | 19 | import androidx.compose.runtime.Composable 20 | import androidx.compose.runtime.derivedStateOf 21 | import androidx.compose.runtime.getValue 22 | import androidx.compose.runtime.mutableStateOf 23 | import androidx.compose.runtime.remember 24 | import androidx.compose.runtime.setValue 25 | import androidx.compose.ui.geometry.Offset 26 | import androidx.compose.ui.unit.IntOffset 27 | import androidx.compose.ui.unit.IntRect 28 | import androidx.compose.ui.unit.IntSize 29 | import com.mmoczkowski.chart.provider.api.Tile 30 | import com.mmoczkowski.chart.provider.api.TileCoords 31 | import kotlin.math.PI 32 | import kotlin.math.atan 33 | import kotlin.math.exp 34 | import kotlin.math.ln 35 | import kotlin.math.max 36 | import kotlin.math.min 37 | import kotlin.math.pow 38 | import kotlin.math.tan 39 | 40 | class ChartState( 41 | val tileSize: Int, 42 | initialFocus: LatLng, 43 | initialZoom: Int, 44 | private val minZoom: Int, 45 | private val maxZoom: Int, 46 | private val tilesRowCount: (Int) -> Int, 47 | private val tilesColCount: (Int) -> Int 48 | ) { 49 | internal var zoom: Int by mutableStateOf(minZoom) 50 | private set 51 | 52 | var focus: LatLng by mutableStateOf(initialFocus) 53 | internal set 54 | 55 | internal var viewportSize: IntSize by mutableStateOf(IntSize.Zero) 56 | 57 | private val tilesCount: IntSize by derivedStateOf { 58 | IntSize( 59 | width = tilesRowCount(zoom), 60 | height = tilesColCount(zoom) 61 | ) 62 | } 63 | 64 | private val chartSize: IntSize by derivedStateOf { 65 | IntSize( 66 | width = tilesCount.width * tileSize, 67 | height = tilesCount.height * tileSize 68 | ) 69 | } 70 | 71 | private val rawOffset: IntOffset by derivedStateOf { 72 | getPixelCoords(focus) 73 | } 74 | 75 | private val boundedOffset: IntOffset by derivedStateOf { 76 | val x = rawOffset.x - (viewportSize.width / 2) 77 | IntOffset( 78 | x = if (x < 0) { 79 | chartSize.width + (x % chartSize.width) 80 | } else { 81 | x 82 | }.toInt() % chartSize.width, 83 | y = rawOffset.y - viewportSize.height / 2 84 | ) 85 | } 86 | 87 | private val viewport: IntRect by derivedStateOf { 88 | IntRect( 89 | topLeft = boundedOffset, 90 | bottomRight = IntOffset( 91 | x = boundedOffset.x + viewportSize.width + tileSize, 92 | y = min( 93 | boundedOffset.y + viewportSize.height + tileSize, 94 | chartSize.height 95 | ) 96 | ) 97 | ) 98 | } 99 | 100 | val translationOffset: Offset by derivedStateOf { 101 | Offset( 102 | x = -((viewportSize.width - tileSize) / 2f) - boundedOffset.x, 103 | y = -((viewportSize.height - tileSize) / 2f) - boundedOffset.y 104 | ) 105 | } 106 | 107 | val visibleTiles: List by derivedStateOf { 108 | val firstVisibleX: Int = viewport.left / tileSize 109 | val lastVisibleX: Int = firstVisibleX + (viewport.width / tileSize) 110 | 111 | (firstVisibleX..lastVisibleX).flatMap { x -> 112 | val firstVisibleY: Int = max( 113 | 0, 114 | viewport.top / tileSize 115 | ) 116 | val lastVisibleY: Int = min( 117 | firstVisibleY + (viewport.height / tileSize), 118 | tilesCount.height - 1 119 | ) 120 | 121 | (firstVisibleY..lastVisibleY).map { y -> 122 | Tile( 123 | coords = TileCoords( 124 | x = x % tilesCount.width, 125 | y = y % tilesCount.height, 126 | zoom = zoom 127 | ), 128 | pixelPosition = IntOffset(x = x * tileSize, y = y * tileSize) 129 | ) 130 | } 131 | } 132 | } 133 | 134 | init { 135 | setZoomLevel(initialZoom) 136 | } 137 | 138 | fun setZoomLevel(newZoom: Int) { 139 | zoom = newZoom.coerceIn( 140 | minimumValue = minZoom, 141 | maximumValue = maxZoom 142 | ) 143 | } 144 | 145 | fun zoomIn() { 146 | setZoomLevel(zoom + 1) 147 | } 148 | 149 | fun zoomOut() { 150 | setZoomLevel(zoom - 1) 151 | } 152 | 153 | fun shiftFocus(offsetDelta: Offset) { 154 | focus = Offset( 155 | x = rawOffset.x - offsetDelta.x, 156 | y = (rawOffset.y - offsetDelta.y).coerceIn( 157 | minimumValue = 0f, 158 | maximumValue = (chartSize.height - 1).toFloat() 159 | ) 160 | ).getLatLng() 161 | } 162 | 163 | private fun Offset.getLatLng(): LatLng { 164 | val worldX = (x / chartSize.width * (2 * PI) - PI) 165 | val worldY = PI - (y / chartSize.height * (2 * PI)) 166 | 167 | val lat = Math.toDegrees(2 * atan(exp(worldY)) - PI / 2) 168 | val lon = Math.toDegrees(worldX) 169 | return LatLng( 170 | lat.toFloat(), 171 | lon.toFloat() 172 | ) 173 | } 174 | 175 | fun getPixelCoords(position: LatLng): IntOffset { 176 | val latRad = Math.toRadians(position.latitude.toDouble()).toFloat() 177 | val lonRad = Math.toRadians(position.longitude.toDouble()).toFloat() 178 | val worldX: Float = tileSize / (2 * PI.toFloat()) * (lonRad + PI.toFloat()) 179 | val worldY: Float = tileSize / (2 * PI.toFloat()) * (PI.toFloat() - ln(tan(PI.toFloat() / 4 + latRad / 2))) 180 | return IntOffset( 181 | x = (worldX * tilesCount.width).toInt(), 182 | y = (worldY * tilesCount.height).toInt(), 183 | ) 184 | } 185 | } 186 | 187 | @Composable 188 | fun rememberChartState( 189 | tileSize: Int = 256, 190 | focus: LatLng = LatLng.NullIsland, 191 | zoom: Int = 0, 192 | minZoom: Int = 0, 193 | maxZoom: Int = 22, 194 | tilesRowCount: (Int) -> Int = { z -> 2f.pow(z).toInt() }, 195 | tilesColCount: (Int) -> Int = { z -> 2f.pow(z).toInt() } 196 | ): ChartState = remember(zoom, focus) { 197 | ChartState( 198 | tileSize = tileSize, 199 | initialZoom = zoom, 200 | initialFocus = focus, 201 | minZoom = minZoom, 202 | maxZoom = maxZoom, 203 | tilesRowCount = tilesRowCount, 204 | tilesColCount = tilesColCount 205 | ) 206 | } 207 | -------------------------------------------------------------------------------- /ui/src/main/kotlin/com/mmoczkowski/chart/LatLng.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.mmoczkowski.chart 18 | 19 | import kotlin.math.abs 20 | 21 | class LatLng( 22 | latitude: Float, 23 | longitude: Float 24 | ) { 25 | val latitude: Float = latitude.coerceIn(-90f, 90f) 26 | val longitude: Float = ((longitude - 180.0f) % 360.0f + 360.0f) % 360.0f - 180.0f 27 | 28 | override fun toString(): String { 29 | val latDirection = if (latitude >= 0) "N" else "S" 30 | val lonDirection = if (longitude >= 0) "E" else "W" 31 | 32 | return String.format( 33 | "%.3f° %s, %.3f° %s", 34 | abs(latitude), latDirection, 35 | abs(longitude), lonDirection 36 | ) 37 | } 38 | 39 | companion object { 40 | val NullIsland = LatLng(latitude = 0f, longitude = 0f) 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /ui/src/main/kotlin/com/mmoczkowski/chart/MarkerLayer.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.mmoczkowski.chart 18 | 19 | import androidx.compose.foundation.layout.fillMaxSize 20 | import androidx.compose.runtime.Composable 21 | import androidx.compose.ui.Modifier 22 | import androidx.compose.ui.layout.Layout 23 | 24 | @Composable 25 | internal fun MarkerLayer( 26 | modifier: Modifier = Modifier, 27 | markers: List, 28 | markerPosition: (T) -> LatLng, 29 | marker: @Composable MarkerScope.(T) -> Unit, 30 | state: ChartState, 31 | ) { 32 | Layout( 33 | content = { 34 | markers.forEach { item -> 35 | MarkerScope.marker(item) 36 | } 37 | }, 38 | modifier = modifier.fillMaxSize() 39 | ) { measurables, constraints -> 40 | val placeables = measurables.map { measurable -> 41 | measurable.measure(constraints) 42 | } 43 | 44 | layout(constraints.maxWidth, constraints.maxHeight) { 45 | placeables.forEachIndexed { index, placeable -> 46 | val item: T = markers[index] 47 | val position: LatLng = markerPosition(item) 48 | 49 | placeable.placeRelativeWithLayer( 50 | position = state.getPixelCoords(position), 51 | zIndex = 0f, 52 | ) { 53 | translationX = state.translationOffset.x 54 | translationY = state.translationOffset.y 55 | } 56 | } 57 | } 58 | } 59 | } -------------------------------------------------------------------------------- /ui/src/main/kotlin/com/mmoczkowski/chart/MarkerScope.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.mmoczkowski.chart 18 | 19 | import androidx.compose.runtime.Stable 20 | import androidx.compose.ui.Modifier 21 | import androidx.compose.ui.geometry.Offset 22 | import androidx.compose.ui.layout.layout 23 | 24 | interface MarkerScope { 25 | 26 | @Stable 27 | fun Modifier.relativeOffset(offset: Offset) = this.then(layout { measurable, constraints -> 28 | val placeable = measurable.measure(constraints) 29 | layout(placeable.width, placeable.height) { 30 | placeable.placeRelative( 31 | x = (placeable.width * offset.x).toInt(), 32 | y = (placeable.height * offset.y).toInt() 33 | ) 34 | } 35 | }) 36 | 37 | companion object : MarkerScope 38 | } 39 | -------------------------------------------------------------------------------- /ui/src/main/kotlin/com/mmoczkowski/chart/TileState.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2023 Miłosz Moczkowski 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.mmoczkowski.chart 18 | 19 | import androidx.compose.ui.graphics.ImageBitmap 20 | import com.mmoczkowski.chart.provider.api.TileCoords 21 | 22 | internal sealed interface TileState { 23 | data class Success(val coords: TileCoords, val image: ImageBitmap) : TileState 24 | data object Error : TileState 25 | data object Loading : TileState 26 | } 27 | -------------------------------------------------------------------------------- /ui/src/test/kotlin/com/mmoczkowski/chart/ChartStateText.kt: -------------------------------------------------------------------------------- 1 | package com.mmoczkowski.chart 2 | 3 | import androidx.compose.runtime.snapshotFlow 4 | import kotlin.test.Test 5 | import kotlin.test.assertEquals 6 | import kotlinx.coroutines.flow.first 7 | import kotlinx.coroutines.runBlocking 8 | 9 | class ChartStateTest { 10 | 11 | @Test 12 | fun `Given initial zoom less then min, when ChartState is constructed, then zoom should be min`() = runBlocking { 13 | val minZoom = 10 14 | val maxZoom = 20 15 | val chartState = ChartState( 16 | tileSize = 256, 17 | initialFocus = LatLng(0f, 0f), 18 | initialZoom = minZoom - 1, 19 | minZoom = minZoom, 20 | maxZoom = maxZoom, 21 | tilesRowCount = { 1 }, 22 | tilesColCount = { 1 } 23 | ) 24 | assertEquals(minZoom, snapshotFlow { chartState.zoom }.first()) 25 | } 26 | 27 | @Test 28 | fun `Given initial zoom larger then max, when ChartState is constructed, then zoom should be max`() = runBlocking { 29 | val minZoom = 10 30 | val maxZoom = 20 31 | val chartState = ChartState( 32 | tileSize = 256, 33 | initialFocus = LatLng(0f, 0f), 34 | initialZoom = maxZoom + 1, 35 | minZoom = minZoom, 36 | maxZoom = maxZoom, 37 | tilesRowCount = { 1 }, 38 | tilesColCount = { 1 } 39 | ) 40 | assertEquals(maxZoom, snapshotFlow { chartState.zoom }.first()) 41 | } 42 | 43 | @Test 44 | fun `Given out-of-bounds zoom, when setZoomLevel is called, then zoom should be within min and max`() = runBlocking { 45 | val chartState = ChartState( 46 | tileSize = 256, 47 | initialFocus = LatLng(0f, 0f), 48 | initialZoom = 5, 49 | minZoom = 0, 50 | maxZoom = 10, 51 | tilesRowCount = { 1 }, 52 | tilesColCount = { 1 } 53 | ) 54 | chartState.setZoomLevel(11) 55 | assertEquals(10, snapshotFlow { chartState.zoom }.first()) 56 | chartState.setZoomLevel(-1) 57 | assertEquals(0, snapshotFlow { chartState.zoom }.first()) 58 | } 59 | 60 | @Test 61 | fun `Given a zoom level, when zoomIn or zoomOut is called, then zoom should increment or decrement by 1`() = runBlocking { 62 | val chartState = ChartState( 63 | tileSize = 256, 64 | initialFocus = LatLng(0f, 0f), 65 | initialZoom = 5, 66 | minZoom = 0, 67 | maxZoom = 10, 68 | tilesRowCount = { 1 }, 69 | tilesColCount = { 1 } 70 | ) 71 | chartState.zoomIn() 72 | assertEquals(6, snapshotFlow { chartState.zoom }.first()) 73 | chartState.zoomOut() 74 | assertEquals(5, snapshotFlow { chartState.zoom }.first()) 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /ui/src/test/kotlin/com/mmoczkowski/chart/LatLngTest.kt: -------------------------------------------------------------------------------- 1 | package com.mmoczkowski.chart 2 | 3 | import kotlin.test.Test 4 | import kotlin.test.assertEquals 5 | 6 | class LatLngTest { 7 | 8 | @Test 9 | fun `latitude should be coerced between -90 and 90`() { 10 | assertEquals(-90f, LatLng(-100f, 0f).latitude) 11 | assertEquals(90f, LatLng(100f, 0f).latitude) 12 | assertEquals(45f, LatLng(45f, 0f).latitude) 13 | } 14 | 15 | @Test 16 | fun `longitude should be normalized between -180 and 180`() { 17 | assertEquals(-180f, LatLng(0f, -180f).longitude) 18 | assertEquals(-179f, LatLng(0f, -179f).longitude) 19 | assertEquals(0f, LatLng(0f, 360f).longitude) 20 | assertEquals(-180f, LatLng(0f, 540f).longitude) 21 | } 22 | 23 | @Test 24 | fun `NullIsland should be at 0,0`() { 25 | assertEquals(0f, LatLng.NullIsland.latitude) 26 | assertEquals(0f, LatLng.NullIsland.longitude) 27 | } 28 | } 29 | --------------------------------------------------------------------------------