shared(ui): NearbyList fills its space and adds a lightweight, material-free scrollbar thumb; optional LazyListState support
shared(nearby,ui): add resolveNearbyOpenAction for “Nearby” entries and remove 420dp height cap — NearbyContent now fills its space and uses the shared open resolver
This commit is contained in:
@@ -87,3 +87,40 @@ suspend fun resolveFoundOpenAction(
|
|||||||
else -> FoundOpenAction.None
|
else -> FoundOpenAction.None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun resolveNearbyOpenAction(
|
||||||
|
entry: NearbyEntryShared,
|
||||||
|
dmList: List<app.klottr.net.DigitaltMuseumItem>,
|
||||||
|
fornList: List<app.klottr.net.FornsokItem>
|
||||||
|
): FoundOpenAction {
|
||||||
|
return when (entry.layer) {
|
||||||
|
app.klottr.util.FoundStore.LAYER_KLOTTER -> {
|
||||||
|
val id = entry.id ?: return FoundOpenAction.None // Säkerställ id
|
||||||
|
FoundOpenAction.Klotter(
|
||||||
|
id = id,
|
||||||
|
lat = entry.lat,
|
||||||
|
lon = entry.lon,
|
||||||
|
label = entry.label
|
||||||
|
)
|
||||||
|
}
|
||||||
|
app.klottr.util.FoundStore.LAYER_WIKI -> {
|
||||||
|
val id = entry.id?.toLongOrNull() ?: return FoundOpenAction.None // Sida måste kunna tolkas
|
||||||
|
FoundOpenAction.Wiki(
|
||||||
|
pageId = id,
|
||||||
|
lat = entry.lat,
|
||||||
|
lon = entry.lon
|
||||||
|
)
|
||||||
|
}
|
||||||
|
app.klottr.util.FoundStore.LAYER_DM -> {
|
||||||
|
val id = entry.id ?: return FoundOpenAction.None
|
||||||
|
val item = dmList.firstOrNull { it.id == id } ?: return FoundOpenAction.None
|
||||||
|
FoundOpenAction.DigitaltMuseum(item)
|
||||||
|
}
|
||||||
|
app.klottr.util.FoundStore.LAYER_FORNSOK -> {
|
||||||
|
val id = entry.id ?: return FoundOpenAction.None
|
||||||
|
val item = fornList.firstOrNull { it.id == id } ?: return FoundOpenAction.None
|
||||||
|
FoundOpenAction.Fornsok(item)
|
||||||
|
}
|
||||||
|
else -> FoundOpenAction.None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,17 +1,16 @@
|
|||||||
package app.klottr.ui
|
package app.klottr.ui
|
||||||
|
|
||||||
import androidx.compose.foundation.layout.Box
|
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.runtime.Composable
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.text.TextStyle
|
import androidx.compose.ui.text.TextStyle
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.foundation.text.BasicText
|
import androidx.compose.foundation.text.BasicText
|
||||||
|
|
||||||
|
import app.klottr.nearby.FoundOpenAction
|
||||||
import app.klottr.nearby.NearbyEntryShared
|
import app.klottr.nearby.NearbyEntryShared
|
||||||
import app.klottr.nearby.isClickable
|
import app.klottr.nearby.isClickable
|
||||||
|
import app.klottr.nearby.resolveNearbyOpenAction
|
||||||
import app.klottr.net.DigitaltMuseumItem
|
import app.klottr.net.DigitaltMuseumItem
|
||||||
import app.klottr.net.FornsokItem
|
import app.klottr.net.FornsokItem
|
||||||
|
|
||||||
@@ -35,9 +34,7 @@ fun NearbyContent(
|
|||||||
) {
|
) {
|
||||||
Box(
|
Box(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxSize() // Fyll den yta dialogen ger
|
||||||
.heightIn(max = 420.dp)
|
|
||||||
.padding(0.dp)
|
|
||||||
) {
|
) {
|
||||||
when {
|
when {
|
||||||
loading -> BasicText("Söker …", style = rowTextStyle)
|
loading -> BasicText("Söker …", style = rowTextStyle)
|
||||||
@@ -49,19 +46,12 @@ fun NearbyContent(
|
|||||||
entries = entries,
|
entries = entries,
|
||||||
isClickable = { e -> isClickable(e, dm, forn) },
|
isClickable = { e -> isClickable(e, dm, forn) },
|
||||||
onEntryClick = { e ->
|
onEntryClick = { e ->
|
||||||
when (e.layer) {
|
when (val action = resolveNearbyOpenAction(e, dm, forn)) {
|
||||||
app.klottr.util.FoundStore.LAYER_WIKI -> {
|
is FoundOpenAction.Klotter -> onKlotterClick(action.id, action.lat, action.lon, e.label)
|
||||||
val id = e.id; if (id != null) onWikiClick(id.toLong(), e.lat, e.lon)
|
is FoundOpenAction.Wiki -> onWikiClick(action.pageId, action.lat, action.lon)
|
||||||
}
|
is FoundOpenAction.Fornsok -> onFornsokClick(action.item)
|
||||||
app.klottr.util.FoundStore.LAYER_DM -> {
|
is FoundOpenAction.DigitaltMuseum -> onDmClick(action.item)
|
||||||
val item = dm.firstOrNull { it.id == e.id }; if (item != null) onDmClick(item)
|
FoundOpenAction.None -> Unit
|
||||||
}
|
|
||||||
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
|
rowTextStyle = rowTextStyle
|
||||||
@@ -69,4 +59,5 @@ fun NearbyContent(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,29 @@
|
|||||||
package app.klottr.ui // Lägg list-komponenten i samma paket som NearbyEntryRow
|
package app.klottr.ui // Lägg list-komponenten i samma paket som NearbyEntryRow
|
||||||
|
|
||||||
import androidx.compose.foundation.layout.Column //Baslayout
|
import androidx.compose.foundation.background
|
||||||
import androidx.compose.foundation.layout.Spacer //Visuell luft mellan rader
|
import androidx.compose.foundation.layout.Box
|
||||||
import androidx.compose.foundation.layout.height //
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.foundation.lazy.LazyColumn //Foundation-lazy (material-fri)
|
import androidx.compose.foundation.layout.Spacer
|
||||||
import androidx.compose.foundation.lazy.items //
|
import androidx.compose.foundation.layout.fillMaxHeight
|
||||||
import androidx.compose.runtime.Composable //
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.ui.Modifier //Behövs för Modifier i Spacer/Column/…-kedjan
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
import androidx.compose.ui.text.TextStyle //Gör radtexten temastyrbar från Android
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.ui.unit.dp //
|
import androidx.compose.foundation.layout.width
|
||||||
|
import androidx.compose.foundation.lazy.LazyColumn
|
||||||
|
import androidx.compose.foundation.lazy.LazyListState
|
||||||
|
import androidx.compose.foundation.lazy.items
|
||||||
|
import androidx.compose.foundation.lazy.rememberLazyListState
|
||||||
|
import androidx.compose.foundation.text.BasicText
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.derivedStateOf
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.graphics.Color
|
||||||
|
import androidx.compose.ui.graphics.takeOrElse
|
||||||
|
import androidx.compose.ui.text.TextStyle
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
|
||||||
import app.klottr.nearby.NearbyEntryShared // Delad radmodell
|
import app.klottr.nearby.NearbyEntryShared // Delad radmodell
|
||||||
import app.klottr.nearby.subtitleFor // Delad formatterare för undertext
|
import app.klottr.nearby.subtitleFor // Delad formatterare för undertext
|
||||||
@@ -16,21 +31,86 @@ import app.klottr.nearby.subtitleFor //Delad formatterare för
|
|||||||
@Composable
|
@Composable
|
||||||
fun NearbyList( // Delad, material-fri list-renderare
|
fun NearbyList( // Delad, material-fri list-renderare
|
||||||
entries: List<NearbyEntryShared>, // Rader att visa
|
entries: List<NearbyEntryShared>, // Rader att visa
|
||||||
isClickable: (NearbyEntryShared) -> Boolean = { true }, //Låter anroparen styra vilka rader som är klickbara
|
isClickable: (NearbyEntryShared) -> Boolean = { true }, // Låt anroparen styra vilka rader som är klickbara
|
||||||
onEntryClick: (NearbyEntryShared) -> Unit = {}, // Callback vid klick
|
onEntryClick: (NearbyEntryShared) -> Unit = {}, // Callback vid klick
|
||||||
rowTextStyle: TextStyle = TextStyle.Default //Stil (t.ex. färg) injiceras från Android (dag/natt)
|
rowTextStyle: TextStyle = TextStyle.Default, // Stil (t.ex. färg) från Android (dag/natt)
|
||||||
|
modifier: Modifier = Modifier, // Yttre modifier från anroparen
|
||||||
|
listState: LazyListState? = null, // Extern LazyListState kan injiceras; annars skapas internt
|
||||||
|
showScrollbar: Boolean = true // Visa en tunn scrollbar-tumme till höger
|
||||||
) {
|
) {
|
||||||
LazyColumn { //Material-fri lista
|
val state = listState ?: rememberLazyListState() // Skapa eller använd given state
|
||||||
items(entries) { e -> //
|
|
||||||
val clickable = isClickable(e) //Fråga anroparen om raden ska vara klickbar
|
// Härled scrollbar-data från layout-info, robust mot tom/edge-lägen
|
||||||
NearbyEntryRow( //Återanvänd delad rad-komponent
|
val layoutInfo by remember(state) { derivedStateOf { state.layoutInfo } }
|
||||||
label = e.label, //
|
val canScroll by remember(state, entries) {
|
||||||
subtitle = subtitleFor(e), //“<lager> • <avstånd> m”
|
derivedStateOf {
|
||||||
clickable = clickable, //
|
val total = layoutInfo.totalItemsCount
|
||||||
onClick = if (clickable) { { onEntryClick(e) } } else null, //
|
val visible = layoutInfo.visibleItemsInfo.size
|
||||||
textStyle = rowTextStyle //Gör texten läsbar i mörkt läge m.h.a. tema från Android
|
total > 0 && entries.size > visible
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val thumbMetrics by remember(state) {
|
||||||
|
derivedStateOf {
|
||||||
|
val total = layoutInfo.totalItemsCount.coerceAtLeast(1)
|
||||||
|
val visibleCount = layoutInfo.visibleItemsInfo.size.coerceAtLeast(1)
|
||||||
|
|
||||||
|
val fracVisible = (visibleCount.toFloat() / total.toFloat()).coerceIn(0f, 1f)
|
||||||
|
val thumbHeightFrac = fracVisible.coerceIn(0.08f, 0.5f)
|
||||||
|
|
||||||
|
val maxFirstIndex = (total - visibleCount).coerceAtLeast(0)
|
||||||
|
val indexFrac = if (maxFirstIndex > 0) {
|
||||||
|
state.firstVisibleItemIndex.toFloat() / maxFirstIndex.toFloat()
|
||||||
|
} else 0f
|
||||||
|
|
||||||
|
val offsetFrac = (indexFrac * (1f - thumbHeightFrac)).coerceIn(0f, 1f - thumbHeightFrac)
|
||||||
|
offsetFrac to thumbHeightFrac
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Välj tumfärg utifrån textstilens färg, med säker fallback
|
||||||
|
val baseColor = rowTextStyle.color.takeOrElse { Color.LightGray }
|
||||||
|
val thumbColor = baseColor.copy(alpha = 0.45f)
|
||||||
|
|
||||||
|
Box(modifier = modifier.fillMaxSize()) {
|
||||||
|
LazyColumn(
|
||||||
|
state = state,
|
||||||
|
modifier = Modifier.fillMaxSize()
|
||||||
|
) {
|
||||||
|
items(entries) { e ->
|
||||||
|
val clickable = isClickable(e)
|
||||||
|
NearbyEntryRow(
|
||||||
|
label = e.label,
|
||||||
|
subtitle = subtitleFor(e),
|
||||||
|
clickable = clickable,
|
||||||
|
onClick = if (clickable) { { onEntryClick(e) } } else null,
|
||||||
|
textStyle = rowTextStyle
|
||||||
)
|
)
|
||||||
Spacer(Modifier.height(8.dp)) //Enkel separator (material-fri)
|
Spacer(Modifier.padding(top = 8.dp))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tunn, material-fri scrollbar-tumme till höger
|
||||||
|
if (showScrollbar && canScroll) {
|
||||||
|
val (offsetFrac, heightFrac) = thumbMetrics
|
||||||
|
val topWeight = offsetFrac.coerceIn(0f, 1f)
|
||||||
|
val midWeight = heightFrac.coerceIn(0f, 1f)
|
||||||
|
val bottomWeight = (1f - topWeight - midWeight).coerceAtLeast(0f)
|
||||||
|
|
||||||
|
Column(
|
||||||
|
modifier = Modifier
|
||||||
|
.align(Alignment.CenterEnd)
|
||||||
|
.fillMaxHeight()
|
||||||
|
.width(4.dp)
|
||||||
|
) {
|
||||||
|
if (topWeight > 0f) Spacer(Modifier.weight(topWeight, fill = true))
|
||||||
|
Box(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.weight(midWeight.coerceAtLeast(0.001f), fill = true) // Undvik nollvikt
|
||||||
|
.background(thumbColor)
|
||||||
|
)
|
||||||
|
if (bottomWeight > 0f) Spacer(Modifier.weight(bottomWeight, fill = true))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user