Move most of net to shared, use AsyncImage for url loaded images
Introduce BuildKonfig to be able to have KMP build config Preparing DialogShells to be moved to shared (not possible to move yet)
This commit is contained in:
@@ -1,24 +0,0 @@
|
||||
package app.klottr.net
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import java.io.BufferedInputStream
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.URL
|
||||
|
||||
// Enkel (blockerande) hämtning av bitmap från URL. Kör detta på Dispatchers.IO i anroparen.
|
||||
suspend fun fetchBitmapFromUrl(url: String): Bitmap? {
|
||||
return try {
|
||||
val conn = (URL(url).openConnection() as HttpURLConnection).apply {
|
||||
connectTimeout = 7000
|
||||
readTimeout = 7000
|
||||
}
|
||||
conn.inputStream.use { ins ->
|
||||
BufferedInputStream(ins).use { bis ->
|
||||
BitmapFactory.decodeStream(bis)
|
||||
}
|
||||
}
|
||||
} catch (_: Throwable) {
|
||||
null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package app.klottr.ui
|
||||
|
||||
import android.content.pm.PackageManager
|
||||
import androidx.core.content.pm.PackageInfoCompat
|
||||
import app.klottr.BuildKonfig
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.TimeZone
|
||||
|
||||
// Provide your app context via a singleton or DI.
|
||||
// If you already have LocalPlatformContext.current as Context, you can reuse it here.
|
||||
|
||||
//TODO: OH: Handle this app context instance.
|
||||
//private val appContext get() = app.klottr.App.instance // adjust to your app
|
||||
|
||||
actual fun appAboutInfo(): AboutInfo {
|
||||
// val pm = appContext.packageManager
|
||||
// val pi = pm.getPackageInfo(appContext.packageName, 0)
|
||||
// val versionName = pi.versionName ?: "0.0"
|
||||
// val versionCode = PackageInfoCompat.getLongVersionCode(pi)
|
||||
val versionName = "Fix Me"
|
||||
val versionCode = 0L
|
||||
val buildType = BuildKonfig.BUILD_TYPE
|
||||
val buildTime = try {
|
||||
// If you expose BUILD_TIME_UTC in BuildConfig (recommended), use it directly
|
||||
BuildKonfig.BUILD_TIME_UTC
|
||||
} catch (_: Throwable) {
|
||||
// Fallback: unreadable, leave empty or format current
|
||||
SimpleDateFormat("yyyy-MM-dd HH:mm 'UTC'").apply {
|
||||
timeZone = TimeZone.getTimeZone("UTC")
|
||||
}.format(System.currentTimeMillis())
|
||||
}
|
||||
return AboutInfo(
|
||||
versionName = versionName,
|
||||
buildType = buildType,
|
||||
buildNumber = versionCode,
|
||||
buildTimeUtc = buildTime,
|
||||
devBy = "Fabian Norlin",
|
||||
iosLead = "Olof Hedman",
|
||||
contactEmail = "utv@ointressant.se"
|
||||
)
|
||||
}
|
||||
@@ -1,5 +1,10 @@
|
||||
package app.klottr.net
|
||||
|
||||
import app.klottr.BuildKonfig
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.IO
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
data class DigitaltMuseumItem(
|
||||
val id: String,
|
||||
val title: String,
|
||||
@@ -10,3 +15,55 @@ data class DigitaltMuseumItem(
|
||||
val webUrl: String? = null, // publika objektsidan
|
||||
val owner: String? = null // identifier.owner
|
||||
)
|
||||
|
||||
/**
|
||||
* Android-adapter för DigitaltMuseum.
|
||||
* - Läser DM_API_KEY och DM_OWNERS från BuildConfig (hemligheter stannar i :app).
|
||||
* - Delegerar alla anrop till delad KMP-klient (DigitaltMuseumShared) i :shared.
|
||||
* - Ingen nätverkslogik här: endast injicering av nycklar/ägare och tråd (Dispatchers.IO).
|
||||
*/
|
||||
object DigitaltMuseum {
|
||||
|
||||
suspend fun getNearby(
|
||||
lat: Double,
|
||||
lon: Double,
|
||||
radiusMeters: Int
|
||||
): List<DigitaltMuseumItem> = withContext(Dispatchers.IO) {
|
||||
val apiKey = BuildKonfig.DM_API_KEY.orEmpty().trim()
|
||||
if (apiKey.isBlank()) return@withContext emptyList()
|
||||
|
||||
val owners = BuildKonfig.DM_OWNERS
|
||||
.orEmpty().trim()
|
||||
.split(',', ' ', ';')
|
||||
.map { it.trim() }
|
||||
.filter { it.isNotEmpty() }
|
||||
|
||||
DigitaltMuseumShared.getNearby(
|
||||
apiKey = apiKey,
|
||||
owners = owners,
|
||||
lat = lat,
|
||||
lon = lon,
|
||||
radiusMeters = radiusMeters
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun lookupByIdentifier(identifierId: String): DigitaltMuseumItem? = withContext(Dispatchers.IO) {
|
||||
val apiKey = BuildKonfig.DM_API_KEY.orEmpty().trim()
|
||||
if (apiKey.isBlank()) return@withContext null
|
||||
DigitaltMuseumShared.lookupByIdentifier(
|
||||
apiKey = apiKey,
|
||||
identifierId = identifierId
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun getDetailText(uniqueId: String?, owner: String?, identifierId: String): String? = withContext(Dispatchers.IO) {
|
||||
val apiKey = BuildKonfig.DM_API_KEY.orEmpty().trim()
|
||||
if (apiKey.isBlank()) return@withContext null
|
||||
DigitaltMuseumShared.getDetailText(
|
||||
apiKey = apiKey,
|
||||
uniqueId = uniqueId,
|
||||
owner = owner,
|
||||
identifierId = identifierId
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
92
shared/src/commonMain/kotlin/app/klottr/net/WikiThumb.kt
Normal file
92
shared/src/commonMain/kotlin/app/klottr/net/WikiThumb.kt
Normal file
@@ -0,0 +1,92 @@
|
||||
// File: WikiThumb.kt
|
||||
package app.klottr.net
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.produceState
|
||||
import androidx.compose.ui.Modifier
|
||||
import coil3.Image
|
||||
import coil3.ImageLoader
|
||||
import coil3.PlatformContext
|
||||
import coil3.compose.AsyncImage
|
||||
import coil3.request.ImageRequest
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.statement.bodyAsText
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.IO
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.contentOrNull
|
||||
import kotlinx.serialization.json.jsonObject
|
||||
import kotlinx.serialization.json.jsonPrimitive
|
||||
|
||||
private val json = Json { ignoreUnknownKeys = true }
|
||||
|
||||
/**
|
||||
* Composable WikiThumb directly usable in Compose UI
|
||||
*
|
||||
* Use this in UI code.
|
||||
*/
|
||||
@Composable
|
||||
fun WikiThumb(pageId: Long, modifier: Modifier = Modifier) {
|
||||
val url by produceState<String?>(initialValue = null, pageId) {
|
||||
value = fetchWikiThumbUrl(pageId)
|
||||
}
|
||||
AsyncImage(model = url, contentDescription = null, modifier = modifier)
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the Wikipedia thumbnail URL for a page.
|
||||
* Multiplatform-safe (Ktor + kotlinx.serialization).
|
||||
*
|
||||
* @param pageId Wikipedia page id
|
||||
* @param http Ktor HttpClient (provide per platform)
|
||||
* @param thumbSize Requested thumbnail width in px (Wikipedia scales server-side)
|
||||
* @return Direct URL to the thumbnail, or null if none exists.
|
||||
*/
|
||||
suspend fun fetchWikiThumbUrl(
|
||||
pageId: Long,
|
||||
thumbSize: Int = 400,
|
||||
): String? = withContext(Dispatchers.IO) {
|
||||
try {
|
||||
val api =
|
||||
"https://sv.wikipedia.org/w/api.php?action=query&prop=pageimages&format=json&pithumbsize=$thumbSize&pageids=$pageId"
|
||||
val text = WikipediaShared.client.get(api).bodyAsText()
|
||||
val root = json.parseToJsonElement(text).jsonObject
|
||||
val pages = root["query"]?.jsonObject?.get("pages")?.jsonObject ?: return@withContext null
|
||||
val first = pages.values.firstOrNull()?.jsonObject ?: return@withContext null
|
||||
first["thumbnail"]?.jsonObject
|
||||
?.get("source")
|
||||
?.jsonPrimitive
|
||||
?.contentOrNull
|
||||
} catch (_: Throwable) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the thumbnail as a multiplatform [coil3.Image] using Coil 3.
|
||||
*
|
||||
* Use this if an coil3.Image is needed, but prefer to use WikiThumb.
|
||||
*
|
||||
* Pass an [ImageLoader] (recommended: keep a singleton in app code)
|
||||
* and a [PlatformContext] (from Compose via LocalPlatformContext or a platform-provided instance).
|
||||
*/
|
||||
suspend fun fetchWikiThumbImage(
|
||||
pageId: Long,
|
||||
imageLoader: ImageLoader,
|
||||
context: PlatformContext,
|
||||
thumbSize: Int = 400,
|
||||
): Image? = withContext(Dispatchers.IO) {
|
||||
val url = fetchWikiThumbUrl(pageId, thumbSize) ?: return@withContext null
|
||||
val request = ImageRequest.Builder(context)
|
||||
.data(url)
|
||||
.build()
|
||||
try {
|
||||
imageLoader.execute(request).image
|
||||
} catch (_: Throwable) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
16
shared/src/commonMain/kotlin/app/klottr/net/Wikipedia.kt
Normal file
16
shared/src/commonMain/kotlin/app/klottr/net/Wikipedia.kt
Normal file
@@ -0,0 +1,16 @@
|
||||
package app.klottr.net
|
||||
|
||||
/**
|
||||
* Android-adapter som behåller befintligt API men delegerar till delad KMP-klient.
|
||||
* (Stegvis migrering: inga andra anrop behöver ändras.)
|
||||
*/
|
||||
|
||||
// TODO: OH: Denna klass är helt onödig och kan tas bort.
|
||||
object Wikipedia {
|
||||
|
||||
suspend fun getNearby(lat: Double, lon: Double, radiusMeters: Int): List<WikipediaPage> =
|
||||
WikipediaShared.getNearby(lat, lon, radiusMeters)
|
||||
|
||||
suspend fun getExtract(pageId: Long): WikipediaDetail =
|
||||
WikipediaShared.getExtract(pageId)
|
||||
}
|
||||
@@ -24,7 +24,7 @@ import kotlinx.serialization.json.Json
|
||||
object WikipediaShared {
|
||||
|
||||
// En HttpClient per process – återanvänds (lazy).
|
||||
private val client by lazy {
|
||||
val client by lazy {
|
||||
HttpClient(platformEngine()) {
|
||||
// JSON-konfiguration: tolerant och ignorera okända fält
|
||||
install(ContentNegotiation) {
|
||||
|
||||
@@ -10,6 +10,7 @@ import androidx.compose.ui.graphics.ImageBitmap
|
||||
import androidx.compose.ui.text.TextStyle
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.foundation.text.BasicText
|
||||
import coil3.compose.AsyncImage
|
||||
|
||||
@Composable
|
||||
fun DmDetailContent(
|
||||
@@ -17,7 +18,7 @@ fun DmDetailContent(
|
||||
bodyText: String?,
|
||||
distanceText: String,
|
||||
coordText: String?, // Förifylld av Android (t.ex. formatCoords)
|
||||
image: ImageBitmap?, // Miniatyr om finns
|
||||
imageUrl: String?, // Miniatyr om finns
|
||||
titleStyle: TextStyle,
|
||||
bodyStyle: TextStyle,
|
||||
captionStyle: TextStyle,
|
||||
@@ -29,8 +30,8 @@ fun DmDetailContent(
|
||||
.verticalScroll(rememberScrollState()),
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp)
|
||||
) {
|
||||
if (image != null) {
|
||||
Image(bitmap = image, contentDescription = null, modifier = Modifier.fillMaxWidth())
|
||||
if (imageUrl != null) {
|
||||
AsyncImage(model = imageUrl, contentDescription = null, modifier = Modifier.fillMaxWidth())
|
||||
}
|
||||
BasicText(titleText, style = titleStyle)
|
||||
if (!bodyText.isNullOrBlank()) {
|
||||
|
||||
36
shared/src/commonMain/kotlin/app/klottr/ui/Logo.kt
Normal file
36
shared/src/commonMain/kotlin/app/klottr/ui/Logo.kt
Normal file
@@ -0,0 +1,36 @@
|
||||
package app.klottr.ui
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.foundation.Image
|
||||
import org.jetbrains.compose.resources.painterResource
|
||||
//import klottr.shared.generated.resources.Res
|
||||
//import klottr.shared.generated.resources.klottr_logo_v2_line // ← import the symbol too
|
||||
import androidx.compose.foundation.Canvas
|
||||
import androidx.compose.ui.geometry.Offset
|
||||
import androidx.compose.ui.geometry.Size
|
||||
import androidx.compose.ui.graphics.Color
|
||||
|
||||
|
||||
@Composable
|
||||
fun KlottrLogo(modifier: Modifier = Modifier) {
|
||||
// Image(
|
||||
// painter = painterResource(Res.drawable.klottr_logo_v2_line),
|
||||
// contentDescription = null,
|
||||
// modifier = modifier
|
||||
// )
|
||||
|
||||
//TODO: OH: Resource loading currently does not work, just use a pink square for now.
|
||||
Canvas(modifier = modifier) {
|
||||
val side = minOf(size.width, size.height)
|
||||
val topLeft = Offset(
|
||||
x = (size.width - side) / 2f,
|
||||
y = (size.height - side) / 2f
|
||||
)
|
||||
drawRect(
|
||||
color = Color(0xFFFF2D55), // pink
|
||||
topLeft = topLeft,
|
||||
size = Size(side, side)
|
||||
)
|
||||
}
|
||||
}
|
||||
25
shared/src/commonMain/kotlin/app/klottr/ui/PlatformExpect.kt
Normal file
25
shared/src/commonMain/kotlin/app/klottr/ui/PlatformExpect.kt
Normal file
@@ -0,0 +1,25 @@
|
||||
package app.klottr.ui
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
|
||||
// TODO: Samla all platformsintegration på ett och samma ställe.
|
||||
|
||||
/* ----- already introduced earlier ----- */
|
||||
// expect object LocalPlatformContext { val current: Any }
|
||||
// expect fun updateToCurrentLocation(onResult: (Double, Double) -> Unit)
|
||||
// expect interface PlatformIntegration { ... } // unchanged here
|
||||
|
||||
/* ----- new expect surface for About + logo ----- */
|
||||
|
||||
data class AboutInfo(
|
||||
val versionName: String,
|
||||
val buildType: String,
|
||||
val buildNumber: Long,
|
||||
val buildTimeUtc: String,
|
||||
val devBy: String,
|
||||
val iosLead: String,
|
||||
val contactEmail: String
|
||||
)
|
||||
|
||||
/** Platform provides app/build metadata for About dialog. */
|
||||
expect fun appAboutInfo(): AboutInfo
|
||||
@@ -10,6 +10,7 @@ import androidx.compose.ui.graphics.ImageBitmap
|
||||
import androidx.compose.ui.text.TextStyle
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.foundation.text.BasicText
|
||||
import app.klottr.net.WikiThumb
|
||||
|
||||
@Composable
|
||||
fun WikiDetailContent(
|
||||
@@ -17,7 +18,7 @@ fun WikiDetailContent(
|
||||
extractText: String,
|
||||
distanceText: String,
|
||||
coordText: String?,
|
||||
thumb: ImageBitmap?,
|
||||
pageId: Long?,
|
||||
titleStyle: TextStyle,
|
||||
bodyStyle: TextStyle,
|
||||
captionStyle: TextStyle,
|
||||
@@ -29,8 +30,8 @@ fun WikiDetailContent(
|
||||
.verticalScroll(rememberScrollState()),
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp)
|
||||
) {
|
||||
if (thumb != null) {
|
||||
Image(bitmap = thumb, contentDescription = null, modifier = Modifier.fillMaxWidth())
|
||||
if (pageId != null) {
|
||||
WikiThumb(pageId = pageId, modifier = Modifier.fillMaxWidth())
|
||||
}
|
||||
BasicText(if (titleText.isNotBlank()) titleText else "Wikipedia", style = titleStyle)
|
||||
if (extractText.isNotBlank()) {
|
||||
|
||||
Reference in New Issue
Block a user