Fix: reinstate cross-platform hit notifications via KMP Notifier; trigger on first new item found
This commit is contained in:
@@ -51,6 +51,7 @@ class MainActivity : ComponentActivity() {
|
||||
// val notesQueries = database.notesQueries
|
||||
|
||||
KLog.setEnabled(BuildConfig.KLLOG_DEBUG) // Styr loggnivå via buildflagga
|
||||
app.klottr.platform.PlatformNotifierProvider.init(this)
|
||||
app.klottr.prefs.KvPrefs.init(applicationContext)
|
||||
LocationServiceProvider.init(applicationContext)
|
||||
AndroidAppContext.install(applicationContext)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package app.klottr.platform
|
||||
|
||||
/**
|
||||
* Gemensam "expect"-deklaration som plattformerna implementerar var för sig.
|
||||
*/
|
||||
expect object PlatformNotifierProvider {
|
||||
fun provide(): Notifier
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package app.klottr.platform
|
||||
|
||||
actual object PlatformNotifierProvider {
|
||||
actual fun provide(): Notifier = NotifierIos()
|
||||
}
|
||||
Reference in New Issue
Block a user