Yokan / rebuild
Course 1 8624561

CalendarSettingsScreen.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/settings/calendar/CalendarSettingsScreen.kt

implementation318 lines
1package eu.friedcutlet.yokan.ui.screens.settings.calendar23import androidx.compose.foundation.background4import androidx.compose.foundation.layout.Arrangement5import androidx.compose.foundation.layout.Box6import androidx.compose.foundation.layout.FlowRow7import androidx.compose.foundation.layout.PaddingValues8import androidx.compose.foundation.layout.Row9import androidx.compose.foundation.layout.Spacer10import androidx.compose.foundation.layout.fillMaxWidth11import androidx.compose.foundation.layout.padding12import androidx.compose.foundation.layout.size13import androidx.compose.foundation.layout.width14import androidx.compose.foundation.lazy.rememberLazyListState15import androidx.compose.foundation.shape.CircleShape16import androidx.compose.foundation.shape.RoundedCornerShape17import androidx.compose.material.icons.Icons18import androidx.compose.material.icons.automirrored.filled.KeyboardArrowRight19import androidx.compose.material.icons.filled.CalendarMonth20import androidx.compose.material.icons.filled.CollectionsBookmark21import androidx.compose.material.icons.filled.Sync22import androidx.compose.material.icons.filled.Update23import androidx.compose.material3.Badge24import androidx.compose.material3.FilterChip25import androidx.compose.material3.FilterChipDefaults26import androidx.compose.material3.Icon27import androidx.compose.material3.ListItem28import androidx.compose.material3.ListItemDefaults29import androidx.compose.material3.MaterialTheme30import androidx.compose.material3.OutlinedButton31import androidx.compose.material3.Surface32import androidx.compose.material3.Text33import androidx.compose.runtime.Composable34import androidx.compose.runtime.getValue35import androidx.compose.runtime.mutableStateOf36import androidx.compose.runtime.remember37import androidx.compose.runtime.rememberCoroutineScope38import androidx.compose.runtime.setValue39import androidx.compose.ui.Alignment40import androidx.compose.ui.Modifier41import androidx.compose.ui.draw.clip42import androidx.compose.ui.graphics.Color43import androidx.compose.ui.text.font.FontWeight44import androidx.compose.ui.text.style.TextOverflow45import androidx.compose.ui.unit.dp46import androidx.lifecycle.compose.collectAsStateWithLifecycle47import androidx.lifecycle.viewmodel.compose.viewModel48import eu.friedcutlet.yokan.app.YokanRuntime49import eu.friedcutlet.yokan.domain.ItemType50import eu.friedcutlet.yokan.domain.Manga51import eu.friedcutlet.yokan.database.DbActivityCategoryFilter52import eu.friedcutlet.yokan.ui.screens.activity.ActivityFilterDialog53import kotlinx.datetime.LocalDate54import org.jetbrains.compose.resources.stringResource55import yokan.composeapp.generated.resources.Res56import yokan.composeapp.generated.resources.action_refresh57import yokan.composeapp.generated.resources.activity_filters58import eu.friedcutlet.yokan.ui.components.EmptyState59import eu.friedcutlet.yokan.ui.components.hoverHighlight60import eu.friedcutlet.yokan.ui.screens.settings.SettingsScaffold61import eu.friedcutlet.yokan.ui.screens.settings.SettingsSection62import kotlinx.coroutines.launch6364@Composable65internal fun CalendarSettingsRoute(title: String, initialItemType: ItemType, onBack: () -> Unit, onOpenTitle: (Manga) -> Unit) {66    val model = viewModel { CalendarViewModel(initialItemType, YokanRuntime.repository, YokanRuntime.libraryUpdates) }67    val state by model.state.collectAsStateWithLifecycle()68    CalendarSettingsScreen(69        title = title,70        state = state,71        onBack = onBack,72        onSelectType = model::selectType,73        onRefresh = model::refresh,74        onOpenTitle = onOpenTitle,75        onCategoryFilter = model::updateCategoryFilter,76    )77}7879@Composable80internal fun CalendarSettingsScreen(81    title: String,82    state: CalendarState,83    onBack: () -> Unit,84    onSelectType: (ItemType) -> Unit,85    onRefresh: () -> Unit,86    onOpenTitle: (Manga) -> Unit,87    onCategoryFilter: ((DbActivityCategoryFilter) -> DbActivityCategoryFilter) -> Unit = {},88) {89    val selectedType = state.itemType90    val refreshing = state.refreshing91    val today = state.today92    val events = state.events93    val scope = rememberCoroutineScope()94    var viewYear by remember { mutableStateOf(today.year) }95    var viewMonth by remember { mutableStateOf(today.month.ordinal + 1) }96    var selectedDate by remember { mutableStateOf<LocalDate?>(null) }97    var filtersOpen by remember { mutableStateOf(false) }98    if (filtersOpen) ActivityFilterDialog(state.categories, state.categoryFilter, onCategoryFilter, { filtersOpen = false })99100    val upcoming: List<Pair<Manga, LocalDate>> = remember(events) {101        events.flatMap { (date, mangas) -> mangas.map { it to date } }102            .sortedBy { (_, date) -> date.toString() }103    }104    val groupedByDate = remember(upcoming) {105        upcoming.groupBy { it.second }.toList().sortedBy { it.first.toString() }106    }107    val listState = rememberLazyListState()108109    SettingsScaffold(title, onBack, listState) {110        item {111            OutlinedButton(onClick = { filtersOpen = true }, modifier = Modifier.padding(horizontal = 16.dp)) {112                Text(stringResource(Res.string.activity_filters))113            }114        }115        item {116            FlowRow(117                modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp),118                horizontalArrangement = Arrangement.spacedBy(8.dp),119                verticalArrangement = Arrangement.spacedBy(8.dp),120            ) {121                val pillShape = RoundedCornerShape(50)122                ItemType.entries.forEach { type ->123                    FilterChip(124                        selected = selectedType == type,125                        onClick = { onSelectType(type); selectedDate = null },126                        label = { Text(type.label, style = MaterialTheme.typography.labelLarge) },127                        shape = pillShape,128                        colors = FilterChipDefaults.filterChipColors(129                            selectedContainerColor = MaterialTheme.colorScheme.primary,130                            selectedLabelColor = MaterialTheme.colorScheme.onPrimary,131                            containerColor = MaterialTheme.colorScheme.surfaceContainerHighest,132                            labelColor = MaterialTheme.colorScheme.onSurfaceVariant,133                        ),134                        border = null,135                    )136                }137                OutlinedButton(138                    onClick = onRefresh,139                    enabled = !refreshing,140                    shape = pillShape,141                    contentPadding = PaddingValues(horizontal = 16.dp, vertical = 4.dp),142                ) {143                    Icon(Icons.Filled.Sync, contentDescription = null, modifier = Modifier.size(16.dp))144                    Spacer(Modifier.width(6.dp))145                    Text(146                        if (refreshing) "Refreshing…" else stringResource(Res.string.action_refresh),147                        style = MaterialTheme.typography.labelLarge,148                    )149                }150            }151        }152        item {153            MonthCalendar(154                year = viewYear,155                month = viewMonth,156                today = today,157                selectedDate = selectedDate,158                eventsByDate = events.mapValues { it.value.size },159                onPrev = {160                    if (viewMonth == 1) { viewMonth = 12; viewYear-- } else viewMonth--161                },162                onNext = {163                    if (viewMonth == 12) { viewMonth = 1; viewYear++ } else viewMonth++164                },165                onSelectDate = { date ->166                    selectedDate = date167                    val groupIndex = groupedByDate.indexOfFirst { it.first == date }168                    if (groupIndex >= 0) {169                        val precedingItems = groupedByDate170                            .take(groupIndex)171                            .sumOf { (_, entries) -> 1 + entries.size }172                        scope.launch {173                            listState.animateScrollToItem(index = 3 + precedingItems)174                        }175                    }176                },177            )178        }179        if (upcoming.isEmpty()) {180            item { EmptyState(icon = Icons.Filled.CalendarMonth, title = "Calendar", modifier = Modifier.fillMaxWidth().padding(vertical = 32.dp, horizontal = 16.dp), subtitle = "No favorite ${selectedType.label.lowercase()} entries have update cadence data.") }181        } else {182            groupedByDate.forEach { (date, entries) ->183                item(key = "date:$date", contentType = "date") {184                    Row(185                        modifier = Modifier186                            .fillMaxWidth()187                            .padding(start = 16.dp, end = 16.dp, top = 18.dp, bottom = 4.dp),188                        verticalAlignment = Alignment.CenterVertically,189                    ) {190                        Text(191                            text = if (selectedDate != null) "On $date" else date.toString(),192                            style = MaterialTheme.typography.labelLarge,193                            color = MaterialTheme.colorScheme.primary,194                            fontWeight = FontWeight.Medium,195                            modifier = Modifier.weight(1f),196                        )197                        Badge(198                            containerColor = MaterialTheme.colorScheme.primaryContainer,199                            contentColor = MaterialTheme.colorScheme.onPrimaryContainer,200                        ) {201                            Text("${entries.size}")202                        }203                    }204                }205                entries.forEach { (manga, _) ->206                    item(key = "calendar:${manga.id}", contentType = "entry") {207                        Surface(208                            modifier = Modifier209                                .fillMaxWidth()210                                .padding(horizontal = 12.dp, vertical = 2.dp)211                                .clip(MaterialTheme.shapes.large)212                                .hoverHighlight(onClick = { onOpenTitle(manga) }),213                            color = MaterialTheme.colorScheme.surface,214                        ) {215                            ListItem(216                                headlineContent = {217                                    Text(218                                        manga.name,219                                        style = MaterialTheme.typography.titleSmall,220                                        fontWeight = FontWeight.SemiBold,221                                        maxLines = 1,222                                        overflow = TextOverflow.Ellipsis,223                                    )224                                },225                                supportingContent = {226                                    Text(227                                        manga.source,228                                        style = MaterialTheme.typography.bodySmall,229                                        color = MaterialTheme.colorScheme.onSurfaceVariant,230                                    )231                                },232                                leadingContent = {233                                    Box(234                                        modifier = Modifier235                                            .size(40.dp)236                                            .clip(CircleShape)237                                            .background(MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.55f)),238                                        contentAlignment = Alignment.Center,239                                    ) {240                                        Icon(241                                            Icons.Filled.CollectionsBookmark,242                                            contentDescription = null,243                                            tint = MaterialTheme.colorScheme.onPrimaryContainer,244                                            modifier = Modifier.size(20.dp),245                                        )246                                    }247                                },248                                trailingContent = {249                                    Icon(250                                        Icons.AutoMirrored.Filled.KeyboardArrowRight,251                                        contentDescription = null,252                                        tint = MaterialTheme.colorScheme.onSurfaceVariant,253                                    )254                                },255                                colors = ListItemDefaults.colors(256                                    containerColor = Color.Transparent,257                                ),258                            )259                        }260                    }261                }262            }263        }264        val updates = state.updates265        if (updates.isNotEmpty()) {266            item { SettingsSection("Recent update records") }267            updates.forEach { update ->268                item(key = "update:${update.id}", contentType = "update") {269                    Surface(270                        modifier = Modifier271                            .fillMaxWidth()272                            .padding(horizontal = 12.dp, vertical = 2.dp),273                        shape = MaterialTheme.shapes.large,274                        color = MaterialTheme.colorScheme.surface,275                    ) {276                        ListItem(277                            headlineContent = {278                                Text(279                                    update.title,280                                    style = MaterialTheme.typography.titleSmall,281                                    fontWeight = FontWeight.SemiBold,282                                    maxLines = 1,283                                    overflow = TextOverflow.Ellipsis,284                                )285                            },286                            supportingContent = {287                                Text(288                                    "${update.chapterName} • ${update.date}",289                                    style = MaterialTheme.typography.bodySmall,290                                    color = MaterialTheme.colorScheme.onSurfaceVariant,291                                )292                            },293                            leadingContent = {294                                Box(295                                    modifier = Modifier296                                        .size(40.dp)297                                        .clip(CircleShape)298                                        .background(MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.55f)),299                                    contentAlignment = Alignment.Center,300                                ) {301                                    Icon(302                                        Icons.Filled.Update,303                                        contentDescription = null,304                                        tint = MaterialTheme.colorScheme.onPrimaryContainer,305                                        modifier = Modifier.size(20.dp),306                                    )307                                }308                            },309                            colors = ListItemDefaults.colors(310                                containerColor = Color.Transparent,311                            ),312                        )313                    }314                }315            }316        }317    }318}319

Search chapters, reference and source filenames.