feat(shared): add KMP get_note.php client and delegate Android fetchNoteDetail
This commit is contained in:
@@ -1,5 +1,11 @@
|
|||||||
package app.klottr.net
|
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.Moshi
|
||||||
import com.squareup.moshi.Json
|
import com.squareup.moshi.Json
|
||||||
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
|
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
|
||||||
@@ -13,14 +19,10 @@ import retrofit2.Retrofit
|
|||||||
import retrofit2.converter.moshi.MoshiConverterFactory
|
import retrofit2.converter.moshi.MoshiConverterFactory
|
||||||
import retrofit2.http.*
|
import retrofit2.http.*
|
||||||
import retrofit2.http.Url
|
import retrofit2.http.Url
|
||||||
import app.klottr.util.Auth
|
|
||||||
import app.klottr.BuildConfig
|
|
||||||
import app.klottr.net.KlottrApiShared
|
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
import android.content.Context
|
|
||||||
import android.net.Uri
|
|
||||||
import android.util.Log
|
|
||||||
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
||||||
import okhttp3.MultipartBody
|
import okhttp3.MultipartBody
|
||||||
import okhttp3.RequestBody
|
import okhttp3.RequestBody
|
||||||
@@ -438,23 +440,41 @@ suspend fun deleteNote(id: Long): Boolean {
|
|||||||
|
|
||||||
|
|
||||||
suspend fun fetchNoteDetail(id: Long, lat: Double, lon: Double): NoteDetailDTO? {
|
suspend fun fetchNoteDetail(id: Long, lat: Double, lon: Double): NoteDetailDTO? {
|
||||||
val body = NoteDetailBody(id = id, lat = lat, lon = lon)
|
val sh = try {
|
||||||
return try {
|
KlottrApiShared.getNote(
|
||||||
val resp = Api.svc.getNote(body)
|
baseUrl = ApiConfig.SERVER_API_BASE_URL,
|
||||||
app.klottr.util.AppLog.d { "get_note code=${resp.code()} ok=${resp.isSuccessful}"}
|
apiKey = ApiConfig.SERVER_API_KEY,
|
||||||
if (!resp.isSuccessful) {
|
bearerToken = app.klottr.util.Auth.idToken,
|
||||||
val err = resp.errorBody()?.string()
|
id = id,
|
||||||
app.klottr.util.AppLog.d { "get_note err=$err"}
|
lat = lat,
|
||||||
null
|
lon = lon
|
||||||
} else {
|
)
|
||||||
resp.body()
|
|
||||||
}
|
|
||||||
} catch (t: Throwable) {
|
} catch (t: Throwable) {
|
||||||
app.klottr.util.AppLog.d { "get_note exception: ${t.message}" }
|
app.klottr.util.AppLog.d { "get_note exception: ${t.message}" }
|
||||||
null
|
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 */
|
/** Anropa me.php – null vid nätverksfel/ej 200 */
|
||||||
suspend fun fetchMe(): MeResp? {
|
suspend fun fetchMe(): MeResp? {
|
||||||
val resp = try { Api.svc.me() } catch (_: Throwable) { return null }
|
val resp = try { Api.svc.me() } catch (_: Throwable) { return null }
|
||||||
|
|||||||
@@ -47,6 +47,22 @@ data class NoteDetailBody(
|
|||||||
val lat: Double,
|
val lat: Double,
|
||||||
val lon: 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 ---
|
// --- username_check.php ---
|
||||||
data class UsernameCheckResp(
|
data class UsernameCheckResp(
|
||||||
val ok: Boolean,
|
val ok: Boolean,
|
||||||
|
|||||||
@@ -45,6 +45,13 @@ object KlottrApiShared {
|
|||||||
val limit: Int = 200
|
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/"
|
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)
|
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<JsonObject>() } 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
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user