Detail access via shared rule: integrate decideDetailAccess in KlotterDialog and fix blockedText nullability
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package app.klottr.nearby
|
||||
|
||||
import app.klottr.util.distanceMeters
|
||||
|
||||
data class DetailAccess(
|
||||
val canAttempt: Boolean, // Om vi får hämta detalj nu
|
||||
val reqLat: Double, // Lat som ska skickas i request (se kommentar nedan)
|
||||
val reqLon: Double, // Lon som ska skickas i request
|
||||
val blockedText: String? // Spärrmeddelande när canAttempt = false (annars null)
|
||||
)
|
||||
|
||||
/**
|
||||
* Beslutar om detalj får hämtas och vilka koordinater som ska skickas:
|
||||
* - Om posten har besökts tidigare: använd postens koordinater (låser upp text).
|
||||
* - Annars: använd besökarens koordinater om kända, annars fall back till postens.
|
||||
* - canAttempt=true om besökt tidigare ELLER inom radius från postens position.
|
||||
*/
|
||||
fun decideDetailAccess(
|
||||
wasFoundBefore: Boolean,
|
||||
userLat: Double?, userLon: Double?,
|
||||
noteLat: Double, noteLon: Double,
|
||||
radiusMeters: Int
|
||||
): DetailAccess {
|
||||
val withinRadius = run {
|
||||
val uLat = userLat
|
||||
val uLon = userLon
|
||||
if (uLat != null && uLon != null) {
|
||||
distanceMeters(uLat, uLon, noteLat, noteLon) <= radiusMeters
|
||||
} else false
|
||||
}
|
||||
|
||||
val can = wasFoundBefore || withinRadius
|
||||
val reqLat = if (wasFoundBefore) noteLat else (userLat ?: noteLat)
|
||||
val reqLon = if (wasFoundBefore) noteLon else (userLon ?: noteLon)
|
||||
|
||||
val blocked = if (!can) "Du måste vara inom ${radiusMeters}\u00A0m för att se det." else null
|
||||
|
||||
return DetailAccess(
|
||||
canAttempt = can,
|
||||
reqLat = reqLat,
|
||||
reqLon = reqLon,
|
||||
blockedText = blocked
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user