Merge branch 'main' into ios-google-signin

This commit is contained in:
Olof Hedman
2025-09-16 13:37:15 +02:00
42 changed files with 2654 additions and 850 deletions

View File

@@ -42,6 +42,8 @@ class LocationService : Service() {
const val ACTION_CLEAR_FOUND_SESSION = "app.klottr.ACTION_CLEAR_FOUND_SESSION"
const val ACTION_RESCAN_NOW = "app.klottr.ACTION_RESCAN_NOW"
const val ACTION_LOCATION_TICK = "app.klottr.ACTION_LOCATION_TICK"
const val ACTION_SUPPRESS_NOTIFY = "app.klottr.ACTION_SUPPRESS_NOTIFY" // Tysta notiser för ett id
}
@@ -51,6 +53,8 @@ class LocationService : Service() {
// Sessiontillstånd – används för “gå utanför ~200 m → tillåt om-avisering”
private val notifiedWhileInside = mutableMapOf<String, Boolean>() // key(layer:id) -> om vi redan aviserat denna session.
private val lastKnownDistance = mutableMapOf<String, Int>() // senaste uppmätta avståndet (meter).
private val suppressedUntil = mutableMapOf<String, Long>() // key(layer:id) -> tyst till tidsstämpel
private lateinit var callback: LocationCallback
@@ -74,6 +78,14 @@ class LocationService : Service() {
notifiedWhileInside.clear()
lastKnownDistance.clear()
}
ACTION_SUPPRESS_NOTIFY -> {
val layer = intent.getStringExtra("layer")
val id = intent.getStringExtra("id")
val ttl = intent.getLongExtra("ttlMs", 60_000L) // default 60s
if (layer != null && id != null) {
suppressedUntil[key(layer, id)] = System.currentTimeMillis() + ttl // Lägg in tystnad
}
}
ACTION_RESCAN_NOW -> {
scope.launch {
val loc = try {
@@ -243,10 +255,17 @@ class LocationService : Service() {
// Notiser:
// - onlyNew=true → bara för helt nya (inte för återinträde)
// - onlyNew=false → en gång per session ELLER om re-arm via FoundStore (wasAwayStore) eller session-reentry
val nowMs = System.currentTimeMillis() // Nu
val suppressed = (suppressedUntil[k] ?: 0L) > nowMs // Är tystnad aktiv?
val shouldNotify = if (onlyNew) {
!alreadyFound && !wasNotifiedThisSession
!alreadyFound && !wasNotifiedThisSession && !suppressed
} else {
!wasNotifiedThisSession || wasAwayStore || reenteredSession
(!wasNotifiedThisSession || wasAwayStore || reenteredSession) && !suppressed
}
if (suppressed) {
notifiedWhileInside[k] = true // Blockera fler notiser denna session
}
if (shouldNotify) {
val (title, text) = notificationTitleAndText(layer, label, distanceM)
@@ -308,7 +327,7 @@ class LocationService : Service() {
).apply { description = "Bakgrundspositionering" }
mgr.createNotificationChannel(loc)
// NY: Fynd-notiser (synliga)
// Fynd-notiser (synliga)
val hits = NotificationChannel(
CHANNEL_HITS_ID,
"Klottr fynd",

File diff suppressed because it is too large Load Diff

View File

@@ -1,16 +1,19 @@
package app.klottr.work
import android.content.Context
import android.content.Intent
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import app.klottr.LocationService
import app.klottr.net.Geocache
import app.klottr.net.createNote
import app.klottr.net.createNoteDoc
import app.klottr.net.uploadImageFromFile
import app.klottr.util.FoundStore
import app.klottr.util.OutboxStore
import java.io.File
import java.security.MessageDigest
import kotlin.math.abs
/**
* Försöker skicka allt som ligger i utkorgen (offline-klotter).
@@ -33,7 +36,6 @@ class OutboxWorker(
return sb.toString()
}
override suspend fun doWork(): Result {
val ctx = applicationContext
val pending = OutboxStore.all(ctx)
@@ -96,10 +98,46 @@ class OutboxWorker(
} catch (_: Throwable) {
false
}
if (ok) {
try {
// Hämta närliggande från servern för att få rätt id/koordinater
val nearby = Geocache.getNearby(
lat = p.lat, lon = p.lon, radiusMeters = 120, ttlMs = 0L
)
// Välj bästa kandidat: nära i både tid och rum
val match = nearby
.filter { app.klottr.util.distanceMeters(p.lat, p.lon, it.a, it.o) <= 80.0 }
.minByOrNull { abs(it.c - p.createdAt) }
if (match != null) {
// Sätt label på ett rimligt sätt (titel + ev. brödtext, annars fallback)
val label = when {
!p.title.isNullOrBlank() -> p.title!!.trim()
else -> (match.t ?: "Klotter")
}
// Markera som hittad i FoundStore
FoundStore.markFound(
ctx,
FoundStore.LAYER_KLOTTER,
match.i.toString(),
label = label,
lat = match.a,
lon = match.o,
timeMillis = System.currentTimeMillis()
)
// Pinga UI så “Senaste” uppdateras direkt
ctx.sendBroadcast(Intent(LocationService.ACTION_FOUND_UPDATED))
}
} catch (_: Throwable) {
// Tyst fallback – Workern ska inte krascha pga visuell uppdatering
}
}
if (!ok) keep.add(p)
}
OutboxStore.replaceAll(ctx, keep)
return if (keep.isEmpty()) Result.success() else Result.retry()
}

View File

@@ -10,11 +10,11 @@ import kotlin.math.*
//fun distanceMeters(lat1: Double, lon1: Double, lat2: Double, lon2: Double): Double {
// val R = 6371000.0
// val dLat = Math.toRadians(lat2 - lat1)
// val dLon = Math.toRadians(lon2 - lon1)
// val a = sin(dLat/2).pow(2.0) + cos(Math.toRadians(lat1)) * cos(Math.toRadians(lat2)) * sin(dLon/2).pow(2.0)
// return 2 * R * atan2(sqrt(a), sqrt(1 - a))
//val R = 6371000.0
//val dLat = Math.toRadians(lat2 - lat1)
//val dLon = Math.toRadians(lon2 - lon1)
//val a = sin(dLat/2).pow(2.0) + cos(Math.toRadians(lat1)) * cos(Math.toRadians(lat2)) * sin(dLon/2).pow(2.0)
// return 2 * R * atan2(sqrt(a), sqrt(1 - a))
//}
fun roundCoord(v: Double): Double = (v * 1e5).roundToInt() / 1e5
@@ -25,5 +25,19 @@ object MatchBus {
}
object LastLocationStore {
@Volatile var lastKnownLocation: Location? = null
// Adapter som låter befintlig Android-kod använda android.location.Location,
// men under huven lagras värdet i KMP-store (GeoPoint) i :shared.
var lastKnownLocation: Location?
get() {
val gp = app.klottr.location.LastLocationStore.lastKnown // Hämta från delad store
return gp?.let {
Location("klottr").apply { latitude = it.lat; longitude = it.lon }
}
}
set(value) {
app.klottr.location.LastLocationStore.lastKnown = value?.let {
app.klottr.location.GeoPoint(it.latitude, it.longitude)
}
}
}