From b3dcb91922be0494e1c814e0e7ec796607e06b6d Mon Sep 17 00:00:00 2001 From: Fabian Norlin Date: Sat, 6 Sep 2025 16:32:17 +0200 Subject: [PATCH] feat(shared): add KMP get_note.php client and delegate Android fetchNoteDetail --- app/src/main/java/app/klottr/net/Api.kt | 56 ++++++++---- .../kotlin/app/klottr/net/ApiModels.kt | 16 ++++ .../kotlin/app/klottr/net/KlottrApiShared.kt | 85 +++++++++++++++++++ 3 files changed, 139 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/app/klottr/net/Api.kt b/app/src/main/java/app/klottr/net/Api.kt index 37d6448..9243130 100644 --- a/app/src/main/java/app/klottr/net/Api.kt +++ b/app/src/main/java/app/klottr/net/Api.kt @@ -1,5 +1,11 @@ package app.klottr.net +import android.content.Context +import android.net.Uri +import android.util.Log +import app.klottr.BuildConfig +import app.klottr.net.KlottrApiShared +import app.klottr.util.Auth import com.squareup.moshi.Moshi import com.squareup.moshi.Json import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory @@ -13,14 +19,10 @@ import retrofit2.Retrofit import retrofit2.converter.moshi.MoshiConverterFactory import retrofit2.http.* import retrofit2.http.Url -import app.klottr.util.Auth -import app.klottr.BuildConfig -import app.klottr.net.KlottrApiShared + import java.io.File import java.util.concurrent.TimeUnit -import android.content.Context -import android.net.Uri -import android.util.Log + import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.MultipartBody import okhttp3.RequestBody @@ -438,23 +440,41 @@ suspend fun deleteNote(id: Long): Boolean { suspend fun fetchNoteDetail(id: Long, lat: Double, lon: Double): NoteDetailDTO? { - val body = NoteDetailBody(id = id, lat = lat, lon = lon) - return try { - val resp = Api.svc.getNote(body) - app.klottr.util.AppLog.d { "get_note code=${resp.code()} ok=${resp.isSuccessful}"} - if (!resp.isSuccessful) { - val err = resp.errorBody()?.string() - app.klottr.util.AppLog.d { "get_note err=$err"} - null - } else { - resp.body() - } + val sh = try { + KlottrApiShared.getNote( + baseUrl = ApiConfig.SERVER_API_BASE_URL, + apiKey = ApiConfig.SERVER_API_KEY, + bearerToken = app.klottr.util.Auth.idToken, + id = id, + lat = lat, + lon = lon + ) } catch (t: Throwable) { - app.klottr.util.AppLog.d { "get_note exception: ${t.message}"} + app.klottr.util.AppLog.d { "get_note exception: ${t.message}" } null } + + if (sh == null) { + app.klottr.util.AppLog.d { "get_note FAIL (null)" } + return null + } + + // Mappa delad modell → Androids Moshi-bundna DTO (behåller nuvarande typer) + return NoteDetailDTO( + id = sh.id, + lat = sh.lat, + lon = sh.lon, + title = sh.title, + body = sh.body, + image = sh.image, + createdAt = sh.createdAt, + updatedAt = sh.updatedAt, + username = sh.username, + userId = sh.userId + ) } + /** Anropa me.php – null vid nätverksfel/ej 200 */ suspend fun fetchMe(): MeResp? { val resp = try { Api.svc.me() } catch (_: Throwable) { return null } diff --git a/shared/src/commonMain/kotlin/app/klottr/net/ApiModels.kt b/shared/src/commonMain/kotlin/app/klottr/net/ApiModels.kt index 36909cc..23df1e6 100644 --- a/shared/src/commonMain/kotlin/app/klottr/net/ApiModels.kt +++ b/shared/src/commonMain/kotlin/app/klottr/net/ApiModels.kt @@ -47,6 +47,22 @@ data class NoteDetailBody( val lat: Double, val lon: Double ) + +/** Delad modell för get_note.php (utan Moshi-beroenden) */ +data class NoteDetailShared( + val id: Long, + val lat: Double, + val lon: Double, + val title: String, + val body: String, + val image: NoteImageDTO? = null, + val createdAt: Long? = null, + val updatedAt: Long? = null, + val username: String? = null, + val userId: Long? = null +) + + // --- username_check.php --- data class UsernameCheckResp( val ok: Boolean, diff --git a/shared/src/commonMain/kotlin/app/klottr/net/KlottrApiShared.kt b/shared/src/commonMain/kotlin/app/klottr/net/KlottrApiShared.kt index 7a99b90..efaefde 100644 --- a/shared/src/commonMain/kotlin/app/klottr/net/KlottrApiShared.kt +++ b/shared/src/commonMain/kotlin/app/klottr/net/KlottrApiShared.kt @@ -45,6 +45,13 @@ object KlottrApiShared { val limit: Int = 200 ) + @kotlinx.serialization.Serializable + private data class NoteDetailBodyShared( + val id: Long, + val lat: Double, + val lon: Double + ) + private fun ensureSlash(u: String): String = if (u.endsWith("/")) u else "$u/" /** @@ -106,4 +113,82 @@ object KlottrApiShared { return NearbyResult(code = code, items = items, etag = newEtag) } + /** + * KMP-variant av get_note.php. Returnerar null vid icke-200 eller undantag. + */ + suspend fun getNote( + baseUrl: String, + apiKey: String, + bearerToken: String?, + id: Long, + lat: Double, + lon: Double + ): NoteDetailShared? { + val url = ensureSlash(baseUrl) + "get_note.php" + val body = NoteDetailBodyShared(id = id, lat = lat, lon = lon) + + val resp = try { + client.post(url) { + contentType(ContentType.Application.Json) + setBody(body) + header("X-API-Key", apiKey) + if (bearerToken != null) header(HttpHeaders.Authorization, "Bearer $bearerToken") + } + } catch (_: Throwable) { + return null + } + + val code = resp.status.value + if (code != 200) return null + + // Läs som JsonObject och mappa manuellt till vår delade modell + val obj = try { resp.body() } catch (_: Throwable) { return null } + + fun JsonElement?.longOrNull(): Long? = this?.jsonPrimitive?.longOrNull + fun JsonElement?.doubleOrNull(): Double? = this?.jsonPrimitive?.doubleOrNull + fun JsonElement?.stringOrNull(): String? = this?.jsonPrimitive?.contentOrNull + + val nid = obj["id"].longOrNull() ?: return null + val nlat = obj["lat"].doubleOrNull() ?: return null + val nlon = obj["lon"].doubleOrNull() ?: return null + val title = obj["title"].stringOrNull() ?: return null + val nbody = obj["body"].stringOrNull() ?: "" + + val createdAt = obj["created_at"].longOrNull() + ?: obj["createdAt"].longOrNull() + ?: obj["created_at"].stringOrNull()?.toLongOrNull() + + val updatedAt = obj["updated_at"].longOrNull() + ?: obj["updatedAt"].longOrNull() + ?: obj["updated_at"].stringOrNull()?.toLongOrNull() + + val username = obj["username"].stringOrNull() + val userId = obj["user_id"].longOrNull() + + val imageObj = obj["image"] as? JsonObject + val image = imageObj?.let { im -> + val iid = im["id"].longOrNull() ?: return@let null + NoteImageDTO( + id = iid, + url = im["url"].stringOrNull(), + width = im["width"].longOrNull()?.toInt(), + height = im["height"].longOrNull()?.toInt(), + etag = im["etag"].stringOrNull() + ) + } + + return NoteDetailShared( + id = nid, + lat = nlat, + lon = nlon, + title = title, + body = nbody, + image = image, + createdAt = createdAt, + updatedAt = updatedAt, + username = username, + userId = userId + ) + } + }