diff --git a/app/build.gradle.kts b/app/build.gradle.kts index bc8d9da..9c14ddb 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -7,12 +7,9 @@ plugins { id("org.jetbrains.kotlin.plugin.compose") id("com.android.application") kotlin("android") - kotlin("kapt") + id("com.google.devtools.ksp") version "2.0.21-1.0.26" } - - - // === Läs local.properties utan java.util.Properties === val localProps: Map = run { val file = rootProject.file("local.properties") @@ -115,9 +112,9 @@ dependencies { implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.6") // Room - val room = "2.6.1" + val room = "2.7.2" implementation("androidx.room:room-runtime:$room") - kapt("androidx.room:room-compiler:$room") + ksp("androidx.room:room-compiler:$room") implementation("androidx.room:room-ktx:$room") // Play Services Location diff --git a/build.gradle.kts b/build.gradle.kts index d76d790..80cf384 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -6,4 +6,6 @@ plugins { alias(libs.plugins.kotlin.multiplatform) apply false alias(libs.plugins.android.kotlin.multiplatform.library) apply false alias(libs.plugins.android.lint) apply false + alias(libs.plugins.kotlin.serialization) apply false + alias(libs.plugins.ksp) apply false } \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 3b8181d..044217a 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -12,6 +12,11 @@ kotlinStdlib = "2.0.21" kotlinTest = "2.0.21" runner = "1.5.2" core = "1.5.0" +ktor = "3.2.3" +kotlinxSerialization = "1.7.3" +ksp = "2.0.21-1.0.26" + + [libraries] androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" } @@ -32,6 +37,16 @@ kotlin-stdlib = { group = "org.jetbrains.kotlin", name = "kotlin-stdlib", versio kotlin-test = { group = "org.jetbrains.kotlin", name = "kotlin-test", version.ref = "kotlinTest" } androidx-runner = { group = "androidx.test", name = "runner", version.ref = "runner" } androidx-core = { group = "androidx.test", name = "core", version.ref = "core" } +ktor-client-core = { group = "io.ktor", name = "ktor-client-core", version.ref = "ktor" } +ktor-client-content-negotiation = { group = "io.ktor", name = "ktor-client-content-negotiation", version.ref = "ktor" } +ktor-serialization-kotlinx-json = { group = "io.ktor", name = "ktor-serialization-kotlinx-json", version.ref = "ktor" } +ktor-client-logging = { group = "io.ktor", name = "ktor-client-logging", version.ref = "ktor" } + +ktor-client-okhttp = { group = "io.ktor", name = "ktor-client-okhttp", version.ref = "ktor" } +ktor-client-darwin = { group = "io.ktor", name = "ktor-client-darwin", version.ref = "ktor" } + +kotlinx-serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version.ref = "kotlinxSerialization" } + [plugins] android-application = { id = "com.android.application", version.ref = "agp" } @@ -40,4 +55,6 @@ kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "ko kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } android-kotlin-multiplatform-library = { id = "com.android.kotlin.multiplatform.library", version.ref = "agp" } android-lint = { id = "com.android.lint", version.ref = "agp" } +kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" } +ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" } diff --git a/shared/build.gradle.kts b/shared/build.gradle.kts index dfe4953..67843c1 100644 --- a/shared/build.gradle.kts +++ b/shared/build.gradle.kts @@ -2,6 +2,7 @@ plugins { alias(libs.plugins.kotlin.multiplatform) alias(libs.plugins.android.kotlin.multiplatform.library) alias(libs.plugins.android.lint) + alias(libs.plugins.kotlin.serialization) } kotlin { @@ -60,7 +61,14 @@ kotlin { commonMain { dependencies { implementation(libs.kotlin.stdlib) + implementation(libs.ktor.client.core) // Add KMP dependencies here + + implementation(libs.ktor.client.content.negotiation) + implementation(libs.ktor.serialization.kotlinx.json) + // JSON-runtime explicit (bra för versionssäkring) + implementation(libs.kotlinx.serialization.json) + implementation(libs.ktor.client.logging) } } @@ -75,6 +83,8 @@ kotlin { // Add Android-specific dependencies here. Note that this source set depends on // commonMain by default and will correctly pull the Android artifacts of any KMP // dependencies declared in commonMain. + implementation(libs.ktor.client.okhttp) + } } @@ -93,6 +103,7 @@ kotlin { // part of KMP’s default source set hierarchy. Note that this source set depends // on common by default and will correctly pull the iOS artifacts of any // KMP dependencies declared in commonMain. + implementation(libs.ktor.client.darwin) } } } diff --git a/shared/src/androidMain/kotlin/app/klottr/shared/PlatformHttp.android.kt b/shared/src/androidMain/kotlin/app/klottr/shared/PlatformHttp.android.kt new file mode 100644 index 0000000..8a0d782 --- /dev/null +++ b/shared/src/androidMain/kotlin/app/klottr/shared/PlatformHttp.android.kt @@ -0,0 +1,6 @@ +package app.klottr.net + +import io.ktor.client.engine.HttpClientEngineFactory +import io.ktor.client.engine.okhttp.OkHttp + +internal actual fun platformEngine(): HttpClientEngineFactory<*> = OkHttp diff --git a/shared/src/commonMain/kotlin/app/klottr/net/PlatformHttp.kt b/shared/src/commonMain/kotlin/app/klottr/net/PlatformHttp.kt new file mode 100644 index 0000000..809f50f --- /dev/null +++ b/shared/src/commonMain/kotlin/app/klottr/net/PlatformHttp.kt @@ -0,0 +1,6 @@ +package app.klottr.net + +import io.ktor.client.engine.HttpClientEngineFactory + +// Plattformsabstraktion: vi väljer engine per plattform (OkHttp på Android, Darwin på iOS). +internal expect fun platformEngine(): HttpClientEngineFactory<*> diff --git a/shared/src/commonMain/kotlin/app/klottr/net/WikipediaClient.kt b/shared/src/commonMain/kotlin/app/klottr/net/WikipediaClient.kt new file mode 100644 index 0000000..c4fa52c --- /dev/null +++ b/shared/src/commonMain/kotlin/app/klottr/net/WikipediaClient.kt @@ -0,0 +1,115 @@ +package app.klottr.net + +import io.ktor.client.HttpClient +import io.ktor.client.call.body +import io.ktor.client.plugins.DefaultRequest +import io.ktor.client.plugins.contentnegotiation.ContentNegotiation +import io.ktor.client.plugins.logging.LogLevel +import io.ktor.client.plugins.logging.Logging +import io.ktor.client.request.get +import io.ktor.client.request.parameter +import io.ktor.serialization.kotlinx.json.json +import kotlinx.serialization.Serializable +import kotlinx.serialization.json.Json + +/** + * Delad Wikipedia-klient (KMP) via Ktor + kotlinx.serialization. + * - Håller sig till samma API som din tidigare implementering i app (sv.wikipedia.org, formatversion=2). + * - Returnerar tomma värden/emptyList vid nätfel (kraschar inte). + * - User-Agent sätts per default request. + * + * OBS: Vi byter inte över app-koden till denna direkt. Först lägger vi in den i shared, + * sedan provar vi att byta anrop på Android i ett separat, litet steg. + */ +object WikipediaShared { + + // En HttpClient per process – återanvänds (lazy). + private val client by lazy { + HttpClient(platformEngine()) { + // JSON-konfiguration: tolerant och ignorera okända fält + install(ContentNegotiation) { + json( + Json { + ignoreUnknownKeys = true + isLenient = true + } + ) + } + // Rimlig loggnivå – justeras senare vid behov + install(Logging) { + level = LogLevel.INFO + } + // Sätt en tydlig User-Agent + install(DefaultRequest) { + headers.append("User-Agent", "Klottr/0.3.0 (KMP)") + } + } + } + + // ====== Geosearch (nära) ====== + + @Serializable + private data class GeoSearchItem( + val pageid: Long = 0, + val title: String = "", + val lat: Double = 0.0, + val lon: Double = 0.0 + ) + + @Serializable private data class GeoSearchQuery(val geosearch: List = emptyList()) + @Serializable private data class GeoSearchResponse(val query: GeoSearchQuery? = null) + + /** + * Hämtar närliggande Wikipedia-sidor. Speglar din gamla getNearby(). + * Vid fel: returnerar tom lista. + */ + suspend fun getNearby(lat: Double, lon: Double, radiusMeters: Int): List = try { + val resp: GeoSearchResponse = client.get("https://sv.wikipedia.org/w/api.php") { + parameter("action", "query") + parameter("list", "geosearch") + parameter("gscoord", "$lat|$lon") + parameter("gsradius", radiusMeters) + parameter("gslimit", 50) + parameter("format", "json") + parameter("formatversion", 2) + }.body() + + resp.query?.geosearch + ?.map { WikipediaPage(it.pageid, it.title, it.lat, it.lon) } + ?.orEmpty() + ?: emptyList() + } catch (_: Throwable) { + emptyList() + } + + // ====== Extract (sammanfattning) ====== + + @Serializable private data class ExtractPage( + val pageid: Long? = null, + val title: String? = null, + val extract: String? = null + ) + @Serializable private data class ExtractQuery(val pages: List = emptyList()) + @Serializable private data class ExtractResponse(val query: ExtractQuery? = null) + + /** + * Hämtar extrakt (introtext) för en pageId. Speglar din gamla getExtract(). + * Vid fel/404: returnerar tom WikipediaDetail med rätt pageId. + */ + suspend fun getExtract(pageId: Long): WikipediaDetail = try { + val resp: ExtractResponse = client.get("https://sv.wikipedia.org/w/api.php") { + parameter("action", "query") + parameter("prop", "extracts") + parameter("exintro", 1) + parameter("explaintext", 1) + parameter("format", "json") + parameter("formatversion", 2) + parameter("pageids", pageId) + }.body() + + val p = resp.query?.pages?.firstOrNull() + WikipediaDetail(pageId, p?.title ?: "", p?.extract ?: "") + } catch (_: Throwable) { + WikipediaDetail(pageId, "", "") + } +} diff --git a/shared/src/iosMain/kotlin/PlatformHttp.ios.kt b/shared/src/iosMain/kotlin/PlatformHttp.ios.kt new file mode 100644 index 0000000..1fa840d --- /dev/null +++ b/shared/src/iosMain/kotlin/PlatformHttp.ios.kt @@ -0,0 +1,6 @@ +package app.klottr.net + +import io.ktor.client.engine.HttpClientEngineFactory +import io.ktor.client.engine.darwin.Darwin + +internal actual fun platformEngine(): HttpClientEngineFactory<*> = Darwin