shared(ui): add NearbyContent (material-free) and subtitle/list wiring
app(ui): use NearbyContent in NearbyDialog and mark finds before render
This commit is contained in:
@@ -107,6 +107,7 @@ import androidx.work.OneTimeWorkRequestBuilder
|
|||||||
import androidx.work.WorkManager
|
import androidx.work.WorkManager
|
||||||
import app.klottr.data.AppDatabase
|
import app.klottr.data.AppDatabase
|
||||||
import app.klottr.data.NoteRepository
|
import app.klottr.data.NoteRepository
|
||||||
|
import app.klottr.ui.NearbyContent // Delat material-fritt innehåll för Närliggande
|
||||||
import app.klottr.nearby.NearbyEntryShared //Delad radmodell
|
import app.klottr.nearby.NearbyEntryShared //Delad radmodell
|
||||||
import app.klottr.nearby.buildNearbyEntries //Delad builder för “Närliggande”
|
import app.klottr.nearby.buildNearbyEntries //Delad builder för “Närliggande”
|
||||||
import app.klottr.nearby.enrichKlotterLabels // delad etikett-berikning
|
import app.klottr.nearby.enrichKlotterLabels // delad etikett-berikning
|
||||||
@@ -1252,68 +1253,58 @@ private fun NearbyDialog(
|
|||||||
AlertDialog(
|
AlertDialog(
|
||||||
onDismissRequest = onClose,
|
onDismissRequest = onClose,
|
||||||
title = { Text("Närliggande") },
|
title = { Text("Närliggande") },
|
||||||
text = {
|
text = { // Använd delat dialoginnehåll
|
||||||
Column(Modifier.fillMaxWidth().heightIn(max = 420.dp)) {
|
val hasLocation = (lat != null && lon != null) // Flagga för plats
|
||||||
when {
|
// Kända klotteretiketter (cache) — O(1)-lookup
|
||||||
loading -> Text("Söker …")
|
val knownKlotterLabels = FoundStore.getAll(ctx)
|
||||||
error != null -> Text(error)
|
.asSequence()
|
||||||
(lat == null || lon == null) -> Text("Okänt avstånd – försök igen.")
|
.filter { it.layer == FoundStore.LAYER_KLOTTER }
|
||||||
(klotter.isEmpty() && wiki.isEmpty() && dm.isEmpty() && forn.isEmpty()) -> Text("Inga träffar i närheten.")
|
.associate { it.id to it.label }
|
||||||
else -> {
|
|
||||||
val knownKlotterLabels = FoundStore.getAll(ctx) // Hämta allt en gång
|
|
||||||
.asSequence() // Effektiv filtrering
|
|
||||||
.filter { it.layer == FoundStore.LAYER_KLOTTER } // Bara klotter-poster
|
|
||||||
.associate { it.id to it.label } // Map: id -> label
|
|
||||||
|
|
||||||
//Bygg plattformsneutral lista i :shared (ingen Context, inga Android-beroenden)
|
// Bygg delade entries om vi har plats, annars tom lista
|
||||||
val entries: List<NearbyEntryShared> = buildNearbyEntries( // List<> istället för List(...)
|
val entries: List<NearbyEntryShared> =
|
||||||
lat = lat, //Min latitud
|
if (hasLocation) {
|
||||||
lon = lon, //Min longitud
|
buildNearbyEntries(
|
||||||
klotter = klotter, //Klotterposter
|
lat = lat!!,
|
||||||
wiki = wiki, //Wikipedia-poster
|
lon = lon!!,
|
||||||
dm = dm, //DigitaltMuseum-poster
|
klotter = klotter,
|
||||||
forn = forn, //Fornsök-poster
|
wiki = wiki,
|
||||||
wikiRadius = wikiRadius, //Radie för Wikipedia
|
dm = dm,
|
||||||
dmRadius = dmRadius, //Radie för DigitaltMuseum
|
forn = forn,
|
||||||
fornsokRadius = fornsokRadius, //Radie för Fornsök
|
wikiRadius = wikiRadius,
|
||||||
defaultKlotterRadiusM = Config.DEFAULT_KLOTTER_RADIUS_M, //Standardradie för klotter
|
dmRadius = dmRadius,
|
||||||
knownKlotterLabel = { id -> knownKlotterLabels[id] } // Använd förberäknad map (O(1) lookup)
|
fornsokRadius = fornsokRadius,
|
||||||
)
|
defaultKlotterRadiusM = Config.DEFAULT_KLOTTER_RADIUS_M,
|
||||||
|
knownKlotterLabel = { id -> knownKlotterLabels[id] }
|
||||||
|
)
|
||||||
|
} else emptyList()
|
||||||
|
|
||||||
// Markera hittade poster innan vi renderar listan (minskar arbete vid recomposition)
|
// Markera “hittade” innan render (endast när vi inte laddar/har fel och har plats)
|
||||||
app.klottr.nearby.markNewFinds( // Anropa delad hjälpare istället för lokal loop
|
if (hasLocation && !loading && error == null) {
|
||||||
ctx = ctx, // Samma ctx som tidigare
|
app.klottr.nearby.markNewFinds(
|
||||||
entries = entries, // Den delade listan vi just byggde
|
ctx = ctx,
|
||||||
nowMillis = now, // Samma tidsstämpel som tidigare
|
entries = entries,
|
||||||
onMarked = { onMarkedFound() } // Kör din callback om något blev nytt
|
nowMillis = now,
|
||||||
)
|
onMarked = { onMarkedFound() }
|
||||||
|
)
|
||||||
NearbyList( // Delad list-renderare från :shared/ui
|
|
||||||
entries = entries, // Den redan byggda delade listan
|
|
||||||
isClickable = { e -> app.klottr.nearby.isClickable(e, dm, forn) }, // Anropa delade regeln med FQN (slipper import/alias)
|
|
||||||
onEntryClick = { e -> // Mappa klick till dina befintliga callbacks
|
|
||||||
when (e.layer) {
|
|
||||||
FoundStore.LAYER_WIKI -> {
|
|
||||||
val id = e.id; if (id != null) onWikiClick(id.toLong(), e.lat, e.lon)
|
|
||||||
}
|
|
||||||
FoundStore.LAYER_DM -> {
|
|
||||||
val item = dm.firstOrNull { it.id == e.id }; if (item != null) onDmClick(item)
|
|
||||||
}
|
|
||||||
FoundStore.LAYER_FORNSOK -> {
|
|
||||||
val item = forn.firstOrNull { it.id == e.id }; if (item != null) onFornsokClick(item)
|
|
||||||
}
|
|
||||||
FoundStore.LAYER_KLOTTER -> {
|
|
||||||
val id = e.id; if (id != null) onKlotterClick(id, e.lat, e.lon, e.label)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
rowTextStyle = MaterialTheme.typography.bodyMedium.copy( // Tydlig text i både dag/natt
|
|
||||||
color = MaterialTheme.colorScheme.onSurface
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Rendera material-fritt delat innehåll (hanterar själv loading/fel/tom-lista)
|
||||||
|
NearbyContent(
|
||||||
|
loading = loading,
|
||||||
|
error = error,
|
||||||
|
hasLocation = hasLocation,
|
||||||
|
entries = entries,
|
||||||
|
dm = dm,
|
||||||
|
forn = forn,
|
||||||
|
onWikiClick = onWikiClick,
|
||||||
|
onDmClick = onDmClick,
|
||||||
|
onFornsokClick = onFornsokClick,
|
||||||
|
onKlotterClick = onKlotterClick,
|
||||||
|
rowTextStyle = MaterialTheme.typography.bodyMedium.copy( // Säker textfärg i dag/natt
|
||||||
|
color = MaterialTheme.colorScheme.onSurface
|
||||||
|
)
|
||||||
|
)
|
||||||
},
|
},
|
||||||
confirmButton = { Button(onClick = onClose) { Text("Stäng") } }
|
confirmButton = { Button(onClick = onClose) { Text("Stäng") } }
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,72 @@
|
|||||||
|
package app.klottr.ui
|
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.Box
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
|
import androidx.compose.foundation.layout.heightIn
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.text.TextStyle
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import androidx.compose.foundation.text.BasicText
|
||||||
|
|
||||||
|
import app.klottr.nearby.NearbyEntryShared
|
||||||
|
import app.klottr.nearby.isClickable
|
||||||
|
import app.klottr.net.DigitaltMuseumItem
|
||||||
|
import app.klottr.net.FornsokItem
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Material-fri innehålls-komponent för “Närliggande”.
|
||||||
|
* Visar laddning/fel/tomtext och annars listan via NearbyList.
|
||||||
|
*/
|
||||||
|
@Composable
|
||||||
|
fun NearbyContent(
|
||||||
|
loading: Boolean,
|
||||||
|
error: String?,
|
||||||
|
hasLocation: Boolean,
|
||||||
|
entries: List<NearbyEntryShared>,
|
||||||
|
dm: List<DigitaltMuseumItem>,
|
||||||
|
forn: List<FornsokItem>,
|
||||||
|
onWikiClick: (Long, Double, Double) -> Unit,
|
||||||
|
onDmClick: (DigitaltMuseumItem) -> Unit,
|
||||||
|
onFornsokClick: (FornsokItem) -> Unit,
|
||||||
|
onKlotterClick: (String, Double, Double, String) -> Unit,
|
||||||
|
rowTextStyle: TextStyle
|
||||||
|
) {
|
||||||
|
Box(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.heightIn(max = 420.dp)
|
||||||
|
.padding(0.dp)
|
||||||
|
) {
|
||||||
|
when {
|
||||||
|
loading -> BasicText("Söker …", style = rowTextStyle)
|
||||||
|
error != null -> BasicText(error, style = rowTextStyle)
|
||||||
|
!hasLocation -> BasicText("Okänt avstånd – försök igen.", style = rowTextStyle)
|
||||||
|
entries.isEmpty() -> BasicText("Inga träffar i närheten.", style = rowTextStyle)
|
||||||
|
else -> {
|
||||||
|
NearbyList(
|
||||||
|
entries = entries,
|
||||||
|
isClickable = { e -> isClickable(e, dm, forn) },
|
||||||
|
onEntryClick = { e ->
|
||||||
|
when (e.layer) {
|
||||||
|
app.klottr.util.FoundStore.LAYER_WIKI -> {
|
||||||
|
val id = e.id; if (id != null) onWikiClick(id.toLong(), e.lat, e.lon)
|
||||||
|
}
|
||||||
|
app.klottr.util.FoundStore.LAYER_DM -> {
|
||||||
|
val item = dm.firstOrNull { it.id == e.id }; if (item != null) onDmClick(item)
|
||||||
|
}
|
||||||
|
app.klottr.util.FoundStore.LAYER_FORNSOK -> {
|
||||||
|
val item = forn.firstOrNull { it.id == e.id }; if (item != null) onFornsokClick(item)
|
||||||
|
}
|
||||||
|
app.klottr.util.FoundStore.LAYER_KLOTTER -> {
|
||||||
|
val id = e.id; if (id != null) onKlotterClick(id, e.lat, e.lon, e.label)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
rowTextStyle = rowTextStyle
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user