Yokan / rebuild
Course 1 8624561

ActivityChrome.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/activity/ActivityChrome.kt

implementation123 lines
1package eu.friedcutlet.yokan.ui.screens.activity23import androidx.compose.foundation.layout.Arrangement4import androidx.compose.foundation.layout.Row5import androidx.compose.foundation.layout.fillMaxWidth6import androidx.compose.foundation.layout.offset7import androidx.compose.foundation.layout.padding8import androidx.compose.foundation.pager.PagerState9import androidx.compose.material3.Badge10import androidx.compose.material3.MaterialTheme11import androidx.compose.material3.SecondaryTabRow12import androidx.compose.material3.Tab13import androidx.compose.material3.TabRowDefaults14import androidx.compose.material3.Text15import androidx.compose.runtime.Composable16import androidx.compose.ui.Alignment17import androidx.compose.ui.Modifier18import androidx.compose.ui.graphics.Color19import androidx.compose.ui.unit.dp20import eu.friedcutlet.yokan.domain.ItemType21import eu.friedcutlet.yokan.util.nowMillis22import eu.friedcutlet.yokan.ui.navigation.NavSection23import eu.friedcutlet.yokan.ui.navigation.librarySection2425// ─────────────────────────────────────────────────────────────────────────────26// HistoryScreen + UpdatesScreen27//28// Both screens follow the same chrome from the Dart source (BaseLibraryTabScreen):29// a TopAppBar whose title swaps into an inline search field, a ScrollableTabRow30// of visible ItemTypes (filtered by dbSettings.hideItems), and a LazyColumn of31// rows grouped by date with sticky headers.3233internal fun visibleItemTypes(hidden: Set<NavSection>): List<ItemType> =34    ItemType.entries.filterNot { it.librarySection() in hidden }3536// ─────────────────────────────────────────────────────────────────────────────37// Rows + helpers3839@Composable40internal fun DateGroupHeader(label: String) {41    Text(42        text = label,43        style = MaterialTheme.typography.bodyMedium,44        modifier = Modifier45            .fillMaxWidth()46            .padding(start = 12.dp, bottom = 8.dp),47    )48}4950@Composable51internal fun ItemTypePagerTabRow(52    types: List<ItemType>,53    pagerState: PagerState,54    unreadCounts: Map<ItemType, Int> = emptyMap(),55    onSelect: (Int) -> Unit,56) {57    val selectedIndex = pagerState.currentPage.coerceIn(types.indices)58    SecondaryTabRow(59        selectedTabIndex = selectedIndex,60        modifier = Modifier.fillMaxWidth(),61        containerColor = Color.Transparent,62        indicator = {63            TabRowDefaults.SecondaryIndicator(64                modifier = Modifier65                    .tabIndicatorOffset(66                        selectedTabIndex = selectedIndex,67                        matchContentSize = false,68                    )69                    .offset(y = 1.dp),70                height = 4.dp,71            )72        },73    ) {74        types.forEachIndexed { index, type ->75            val count = unreadCounts[type] ?: 076            Tab(77                selected = index == selectedIndex,78                onClick = { onSelect(index) },79                text = {80                    Row(81                        verticalAlignment = Alignment.CenterVertically,82                        horizontalArrangement = Arrangement.spacedBy(8.dp),83                    ) {84                        Text(type.label)85                        if (count > 0) {86                            Badge(87                                containerColor = MaterialTheme.colorScheme.surfaceVariant,88                                contentColor = MaterialTheme.colorScheme.onSurface,89                            ) {90                                Text(count.toString())91                            }92                        }93                    }94                },95            )96        }97    }98}99100/** Groups entries into Today / Yesterday / Earlier this week / Earlier this month / Older, in that order. */101internal fun <T> groupByDayBucket(entries: List<T>, updatedAt: (T) -> Long): List<Pair<String, List<T>>> {102    if (entries.isEmpty()) return emptyList()103    val now = nowMillis()104    val dayMillis = 24L * 60L * 60L * 1000L105    return entries.groupBy { entry ->106        val deltaDays = ((now / dayMillis) - (updatedAt(entry) / dayMillis)).toInt()107        when {108            deltaDays <= 0 -> "Today"109            deltaDays == 1 -> "Yesterday"110            deltaDays in 2..6 -> "Earlier this week"111            deltaDays in 7..29 -> "Earlier this month"112            else -> "Older"113        }114    }.toList().sortedBy { (label, _) -> dayBucketOrder(label) }115}116117private fun dayBucketOrder(label: String): Int = when (label) {118    "Today" -> 0119    "Yesterday" -> 1120    "Earlier this week" -> 2121    "Earlier this month" -> 3122    else -> 4123}124

Search chapters, reference and source filenames.