refactor(shared): move common API models & username helpers to commonMain and restore comments; fix type inference in MainActivity

This commit is contained in:
2025-09-05 20:47:57 +02:00
parent 43daadc3d4
commit 6363cbbc3c
4 changed files with 124 additions and 129 deletions

View File

@@ -705,9 +705,7 @@ private fun KlottrApp(
val idL = f.id.toLongOrNull() ?: return@forEach
if (NoteDetailCache.get(idL) == null) {
// Hämta detalj med NOTISENS koordinater (ger rätt “bevis” på servern)
val detail = withContext(Dispatchers.IO) {
fetchNoteDetail(idL, f.lat, f.lon)
}
val detail = fetchNoteDetail(idL, f.lat, f.lon)
if (detail != null) {
NoteDetailCache.put(idL, detail)
val img = detail.image

View File

@@ -30,67 +30,6 @@ import okhttp3.RequestBody.Companion.toRequestBody
* Läser bas‑URL och API‑nyckel från BuildConfig (hålls i local.properties → buildConfigField).
* Byt inte namn här – resten av appen förväntar sig NoteDTO/CreateBody/KlottrApi.
*/
object ApiConfig {
val SERVER_API_BASE_URL: String get() = ensureTrailingSlash(BuildConfig.SERVER_API_BASE_URL)
val SERVER_API_KEY: String get() = BuildConfig.SERVER_API_KEY
private fun ensureTrailingSlash(u: String): String =
if (u.endsWith("/")) u else "$u/"
}
// v0.1.1: helper för säkra bild-URL:er. Bygger API-URL till get_image.php (kräver aktuell lat/lon för närhetslås)
//fun imageUrl(imageId: Long, noteId: Long, lat: Double, lon: Double): String =
// "${ApiConfig.SERVER_API_BASE_URL}get_image.php?id=$imageId&note=$noteId&lat=$lat&lon=$lon"
/** Kompakta fält enligt nearby.php (behållna som i v0.0.1) */
data class NoteDTO(
val i: Long, // id
val a: Double, // lat
val o: Double, // lon
val t: String, // text
val c: Long, // createdAt (ms)
val u: String // updated_at (ISO)
)
/** Request‑body till create.php – behåller userId för bakåtkompatibilitet */
data class CreateBody(
val lat: Double,
val lon: Double,
val text: String,
val createdAt: Long,
val userId: String? = null, // fylls från Auth.userId (Google sub)
val clientRequestId: String? = null
)
/** Svar från upload_image.php */
data class UploadImageResp(
val ok: Any? = null,
@Json(name = "image_id") val imageId: Long? = null,
val width: Int? = null,
val height: Int? = null
)
/** Ny body till create.php när vi skickar dokument (rubrik/brödtext/bild) */
data class CreateDocBody(
val lat: Double,
val lon: Double,
val title: String,
val body: String,
val imageId: Long? = null,
val radiusM: Int = 20,
val createdAt: Long,
val userId: String? = null, // Google "sub" (bakåtkomp)
val clientRequestId: String? = null
)
/** Svar från get_note.php */
data class NoteImageDTO(
val id: Long,
val url: String? = null,
val width: Int? = null,
val height: Int? = null,
val etag: String? = null
)
data class NoteDetailDTO(
val id: Long,
@@ -98,16 +37,27 @@ data class NoteDetailDTO(
val lon: Double,
val title: String,
val body: String,
val image: NoteImageDTO? = null,
@Json(name = "created_at") val createdAt: Long? = null,
@Json(name = "updated_at") val updatedAt: Long? = null,
@Json(name = "username") val username: String? = null,
@Json(name = "user_id") val userId: Long? = null
val image: NoteImageDTO? = null, @Json(name = "created_at")
val createdAt: Long? = null, @Json(name = "updated_at")
val updatedAt: Long? = null, @Json(name = "username")
val username: String? = null, @Json(name = "user_id")
val userId: Long? = null
)
data class NoteDetailBody(
val id: Long,
val lat: Double,
val lon: Double
object ApiConfig {
val SERVER_API_BASE_URL: String get() = ensureTrailingSlash(BuildConfig.SERVER_API_BASE_URL)
val SERVER_API_KEY: String get() = BuildConfig.SERVER_API_KEY
private fun ensureTrailingSlash(u: String): String =
if (u.endsWith("/")) u else "$u/"
}
/** Svar från upload_image.php */
data class UploadImageResp(
val ok: Any? = null,
@Json(name = "image_id") val imageId: Long? = null,
val width: Int? = null,
val height: Int? = null
)
// --- Användarinfo från me.php ---
@@ -120,29 +70,6 @@ data class MeResp(
@Json(name = "last_login") val lastLogin: String? = null,
@Json(name = "is_suspended") val isSuspended: Int? = 0
)
// --- username_check.php ---
data class UsernameCheckResp(
val ok: Boolean,
val reason: String? = null
)
// --- username_set.php ---
data class UsernameSetResp(
val ok: Boolean,
val username: String? = null,
val reason: String? = null
)
// --- Lokal valideringsresultat (innan servercheck) ---
enum class UsernameLocalStatus { OK, INVALID_CHARS, TOO_FEW_LETTERS, EMPTY }
data class UsernameLocalValidation(
val status: UsernameLocalStatus,
val display: String, // trimmat/komprimerat
val normalized: String // lower-case
)
// Body till nearby.php (POST)
data class NearbyBody(
val lat: Double,
@@ -278,13 +205,6 @@ object Api {
val svc: KlottrApi = retrofit.create(KlottrApi::class.java)
}
/** Resultatcontainer för cache/ETag‑flödet. */
data class NearbyResult(
val code: Int, // HTTP‑kod (200, 304, etc.)
val items: List<NoteDTO>, // tom lista om 304/inga nya
val etag: String? // serverns ETag (om någon)
)
/**
* Hämtar nearby med stöd för ETag och "since".
* Kasta inte exception – returnera bara kod + lista + ny etag.
@@ -552,32 +472,6 @@ suspend fun fetchNoteDetail(id: Long, lat: Double, lon: Double): NoteDetailDTO?
}
}
/** Trim + komprimera whitespace (speglar serverns username_display_clean) */
fun usernameDisplayClean(raw: String): String =
raw.replace(Regex("\\s+"), " ").trim()
/** Normalisering (lower-case) – speglar serverns username_norm_key */
fun usernameNormKey(display: String): String =
display.lowercase()
/** Lokal snabbvalidering – speglar serverns regler */
fun usernameLocalValidate(raw: String): UsernameLocalValidation {
val disp = usernameDisplayClean(raw)
if (disp.isEmpty()) {
return UsernameLocalValidation(UsernameLocalStatus.EMPTY, disp, usernameNormKey(disp))
}
val allowed = Regex("^[\\p{L}\\p{Nd} \\-]+$")
if (!allowed.matches(disp)) {
return UsernameLocalValidation(UsernameLocalStatus.INVALID_CHARS, disp, usernameNormKey(disp))
}
val letters = disp.count { Character.isLetter(it) }
if (letters < 2) {
return UsernameLocalValidation(UsernameLocalStatus.TOO_FEW_LETTERS, disp, usernameNormKey(disp))
}
return UsernameLocalValidation(UsernameLocalStatus.OK, disp, usernameNormKey(disp))
}
/** Anropa me.php – null vid nätverksfel/ej 200 */
suspend fun fetchMe(): MeResp? {
val resp = try { Api.svc.me() } catch (_: Throwable) { return null }