Fix: reinstate cross-platform hit notifications via KMP Notifier; trigger on first new item found

This commit is contained in:
2025-10-15 14:07:12 +02:00
parent 100a7f3f2d
commit 5b846b634f
10 changed files with 128 additions and 6 deletions

View File

@@ -0,0 +1,38 @@
package app.klottr.platform
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
class NotifierAndroid(private val context: Context) : Notifier {
companion object {
private const val CHANNEL_ID = "klottr_hits"
}
init {
if (Build.VERSION.SDK_INT >= 26) {
val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channel = NotificationChannel(
CHANNEL_ID,
"Klottr-träffar",
NotificationManager.IMPORTANCE_HIGH
)
manager.createNotificationChannel(channel)
}
}
override fun notifyNewHit(title: String, message: String) {
val notification = NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.build()
NotificationManagerCompat.from(context).notify(System.currentTimeMillis().toInt(), notification)
}
}

View File

@@ -0,0 +1,17 @@
package app.klottr.platform
import android.content.Context
actual object PlatformNotifierProvider {
private lateinit var appContext: Context
// Anropas en gång i MainActivity.onCreate
fun init(context: Context) {
appContext = context.applicationContext
}
actual fun provide(): Notifier {
check(::appContext.isInitialized) { "PlatformNotifierProvider.init(context) måste anropas först" }
return NotifierAndroid(appContext)
}
}

View File

@@ -289,6 +289,14 @@ fun KlottrAppShell(
}
}
// Initiera notifieraren på Android
LaunchedEffect(Unit) {
try {
app.klottr.platform.PlatformNotifierProvider.provide()
} catch (e: Exception) {
// I iOS krävs ingen init; Android får context senare via PlatformNotifierProvider.init(context)
}
}
// 🔴 NYTT: Omedelbar UI-uppdatering när FoundStore ändras
// Found updates via shared event bus (no broadcasts)
@@ -659,7 +667,7 @@ fun KlottrAppShell(
// Trigga scanning när fliken blir aktiv (och när lager-inställningar ändras)
LaunchedEffect(selectedTab, showKlotter, showWiki, wikiRadius, showDm, dmRadius, showFornsok, fornsokRadius) {
if (selectedTab != "nearby") return@LaunchedEffect
nearbyRefreshing = true
nearbyRefreshing = true // Visa den grafiska loading-effekten.
nearbyError = null
val gp = LocationServiceProvider.get().current(highAccuracy = false)

View File

@@ -55,13 +55,17 @@ suspend fun scanAndMarkFound(
}
var anyNew = false
var firstNewLabel: String? = null
// Wikipedia
for (w in wikiDef.await()) {
val d = distanceMeters(lat, lon, w.lat, w.lon).roundToInt()
if (d <= config.wikiRadius) {
val added = markFound(FoundStore.LAYER_WIKI, w.pageId.toString(), w.title, w.lat, w.lon, now)
if (added) anyNew = true
if (added) {
anyNew = true
if (firstNewLabel == null) firstNewLabel = w.title
}
}
}
@@ -70,7 +74,10 @@ suspend fun scanAndMarkFound(
val d = distanceMeters(lat, lon, dmi.lat, dmi.lon).roundToInt()
if (d <= config.dmRadius) {
val added = markFound(FoundStore.LAYER_DM, dmi.id, dmi.title, dmi.lat, dmi.lon, now)
if (added) anyNew = true
if (added) {
anyNew = true
if (firstNewLabel == null) firstNewLabel = dmi.title
}
}
}
@@ -79,9 +86,21 @@ suspend fun scanAndMarkFound(
val d = distanceMeters(lat, lon, f.lat, f.lon).roundToInt()
if (d <= config.fornsokRadius) {
val added = markFound(FoundStore.LAYER_FORNSOK, f.id, f.title, f.lat, f.lon, now)
if (added) anyNew = true
if (added) {
anyNew = true
if (firstNewLabel == null) firstNewLabel = f.title
}
}
}
// Skicka notifiering om nya fynd hittats
if (anyNew) {
val notifier = app.klottr.platform.PlatformNotifierProvider.provide()
val title = firstNewLabel ?: "Nytt fynd"
// Dessa lager har inte brödtext här — vi skickar bara titeln.
val message = ""
notifier.notifyNewHit(title, message)
}
// Returnera om något nytt markerades
anyNew
}

View File

@@ -128,11 +128,10 @@ object Geocache {
return@withContext items
}
return@withContext firstItems
}
// Skyddad fetch som aldrig kastar – använder KlottrApiShared.nearby från din KlottrApiShared.kt
// Skyddad fetch som aldrig kastar – använder KlottrApiShared.nearby från KlottrApiShared.kt
private suspend fun safeFetch(
lat: Double,
lon: Double,

View File

@@ -0,0 +1,9 @@
package app.klottr.platform
/**
* Plattformsgemensamt gränssnitt för att skicka lokala notiser.
* Implementeras separat i androidMain och iosMain.
*/
interface Notifier {
fun notifyNewHit(title: String, message: String)
}

View File

@@ -0,0 +1,8 @@
package app.klottr.platform
/**
* Gemensam "expect"-deklaration som plattformerna implementerar var för sig.
*/
expect object PlatformNotifierProvider {
fun provide(): Notifier
}

View File

@@ -0,0 +1,18 @@
package app.klottr.platform
import platform.UserNotifications.*
class NotifierIos : Notifier {
override fun notifyNewHit(title: String, message: String) {
val content = UNMutableNotificationContent().apply {
this.title = title
this.body = message
}
val request = UNNotificationRequest.requestWithIdentifier(
NSUUID().UUIDString,
content,
null
)
UNUserNotificationCenter.currentNotificationCenter().addNotificationRequest(request, null)
}
}

View File

@@ -0,0 +1,5 @@
package app.klottr.platform
actual object PlatformNotifierProvider {
actual fun provide(): Notifier = NotifierIos()
}