package eu.friedcutlet.yokan.ui.screens.home import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.* import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.ArrowDownward import androidx.compose.material.icons.filled.ArrowUpward import androidx.compose.material.icons.filled.Delete import androidx.compose.material3.* import androidx.compose.runtime.* import androidx.compose.ui.Modifier import androidx.compose.ui.text.input.PasswordVisualTransformation import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import eu.friedcutlet.yokan.database.* import eu.friedcutlet.yokan.ui.components.compactSheetWidth import eu.friedcutlet.yokan.ui.screens.settings.SwitchTile import org.jetbrains.compose.resources.stringResource import yokan.composeapp.generated.resources.* @OptIn(ExperimentalMaterial3Api::class) @Composable internal fun HomeCustomizeSheet( state: HomeState, onPreferences: ((DbHomePreferences) -> DbHomePreferences) -> Unit, onToken: (String) -> Unit, onAddFeed: (DbHomeSourceFeed) -> Unit, onRemoveFeed: (String) -> Unit, onMoveFeed: (String, Int) -> Unit, onDismiss: () -> Unit, ) { var tokenDraft by remember { mutableStateOf("") } var sourceMenuOpen by remember { mutableStateOf(false) } var selectedSourceId by remember { mutableStateOf(state.sources.firstOrNull()?.id) } var selectedFeed by remember(selectedSourceId) { mutableStateOf(selectedSourceId?.let { DbHomeSourceFeed(it) }) } val source = state.sources.firstOrNull { it.id == selectedSourceId } ModalBottomSheet(onDismissRequest = onDismiss, modifier = Modifier.compactSheetWidth()) { LazyColumn(Modifier.fillMaxWidth(), contentPadding = PaddingValues(bottom = 32.dp)) { item(key = "title") { Text(stringResource(Res.string.home_customize), style = MaterialTheme.typography.headlineSmall, modifier = Modifier.padding(horizontal = 24.dp, vertical = 12.dp)) } item(key = "featured") { SwitchTile(stringResource(Res.string.home_show_featured), null, state.preferences.showFeatured, { value -> onPreferences { it.copy(showFeatured = value) } }) } item(key = "motion") { SwitchTile(stringResource(Res.string.home_auto_advance), stringResource(Res.string.home_auto_advance_hint), state.preferences.autoAdvanceFeatured, { value -> onPreferences { it.copy(autoAdvanceFeatured = value) } }, enabled = state.preferences.showFeatured) } item(key = "library-hero") { SwitchTile(stringResource(Res.string.home_library_featured), null, state.preferences.featuredLibraryOnly, { value -> onPreferences { it.copy(featuredLibraryOnly = value) } }, enabled = state.preferences.showFeatured) } item(key = "completed") { SwitchTile(stringResource(Res.string.home_hide_completed), null, state.preferences.hideCompleted, { value -> onPreferences { it.copy(hideCompleted = value) } }) } item(key = "count") { Column(Modifier.padding(horizontal = 24.dp, vertical = 8.dp)) { Text(stringResource(Res.string.home_shelf_size), style = MaterialTheme.typography.titleMedium) Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { listOf(6, 12, 24).forEach { count -> FilterChip(selected = state.preferences.itemsPerShelf == count, onClick = { onPreferences { it.copy(itemsPerShelf = count) } }, label = { Text(count.toString()) }) } } } } item(key = "anilist") { SwitchTile(stringResource(Res.string.home_enable_anilist), null, state.preferences.enableAniList, { value -> onPreferences { it.copy(enableAniList = value) } }) } item(key = "tmdb") { SwitchTile(stringResource(Res.string.home_enable_tmdb), null, state.preferences.enableTmdb, { value -> onPreferences { it.copy(enableTmdb = value) } }) } if (state.preferences.enableTmdb) item(key = "token") { Column(Modifier.padding(horizontal = 24.dp, vertical = 8.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) { Text(stringResource(Res.string.home_tmdb_token_hint), style = MaterialTheme.typography.bodyMedium) OutlinedTextField(value = tokenDraft, onValueChange = { tokenDraft = it }, singleLine = true, label = { Text(stringResource(Res.string.home_tmdb_token)) }, visualTransformation = PasswordVisualTransformation(), modifier = Modifier.fillMaxWidth()) Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { Button(enabled = tokenDraft.isNotBlank(), onClick = { onToken(tokenDraft); tokenDraft = "" }) { Text(stringResource(Res.string.home_save_token)) } if (state.tmdbConfigured) TextButton(onClick = { onToken(""); tokenDraft = "" }) { Text(stringResource(Res.string.home_remove_token)) } } if (state.tmdbConfigured) Text(stringResource(Res.string.home_token_saved), style = MaterialTheme.typography.labelLarge) } } item(key = "sections-heading") { Text(stringResource(Res.string.home_visible_sections), style = MaterialTheme.typography.titleLarge, modifier = Modifier.padding(horizontal = 24.dp, vertical = 16.dp)) } items(HomeSection.entries, key = { "section:${it.name}" }, contentType = { "switch" }) { section -> SwitchTile(homeSectionLabel(section), null, section !in state.preferences.hiddenSections, { visible -> onPreferences { prefs -> prefs.copy(hiddenSections = if (visible) prefs.hiddenSections - section else prefs.hiddenSections + section) } }) } item(key = "feeds-heading") { Text(stringResource(Res.string.home_source_feeds), style = MaterialTheme.typography.titleLarge, modifier = Modifier.padding(horizontal = 24.dp, vertical = 16.dp)) } items(state.feeds, key = { "feed:${it.key}" }, contentType = { "feed" }) { feed -> val name = state.sources.firstOrNull { it.id == feed.sourceId }?.name ?: stringResource(Res.string.home_unavailable_source) Column(Modifier.padding(horizontal = 24.dp, vertical = 8.dp)) { Text(name, style = MaterialTheme.typography.titleMedium) Text(feedLabel(feed, state.presets), color = MaterialTheme.colorScheme.onSurfaceVariant) Row { IconButton(onClick = { onMoveFeed(feed.key, -1) }, enabled = state.feeds.firstOrNull()?.key != feed.key) { Icon(Icons.Filled.ArrowUpward, stringResource(Res.string.home_move_up)) } IconButton(onClick = { onMoveFeed(feed.key, 1) }, enabled = state.feeds.lastOrNull()?.key != feed.key) { Icon(Icons.Filled.ArrowDownward, stringResource(Res.string.home_move_down)) } IconButton(onClick = { onRemoveFeed(feed.key) }) { Icon(Icons.Filled.Delete, stringResource(Res.string.home_remove_feed)) } } } } item(key = "add-feed") { Column(Modifier.fillMaxWidth().padding(horizontal = 24.dp, vertical = 8.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) { Text(stringResource(Res.string.home_add_feed), style = MaterialTheme.typography.titleMedium) if (state.sources.isEmpty()) Text(stringResource(Res.string.home_install_sources)) else { Box { OutlinedButton(onClick = { sourceMenuOpen = true }) { Text(source?.name ?: stringResource(Res.string.home_select_source)) } DropdownMenu(expanded = sourceMenuOpen, onDismissRequest = { sourceMenuOpen = false }, modifier = Modifier.heightIn(max = 320.dp)) { state.sources.forEach { candidate -> DropdownMenuItem(text = { Text(candidate.name, maxLines = 1, overflow = TextOverflow.Ellipsis) }, onClick = { selectedSourceId = candidate.id; sourceMenuOpen = false }) } } } source?.let { selected -> val options = listOf(DbHomeSourceFeed(selected.id)) + (if (selected.supportLatest == true) listOf(DbHomeSourceFeed(selected.id, HomeFeedMode.Latest)) else emptyList()) + state.presets.filter { it.sourceId == selected.id }.map { DbHomeSourceFeed(selected.id, HomeFeedMode.SavedSearch, it.id) } options.forEach { option -> Row(Modifier.fillMaxWidth().padding(vertical = 4.dp).clickable { selectedFeed = option }, verticalAlignment = androidx.compose.ui.Alignment.CenterVertically) { RadioButton(selected = selectedFeed == option, onClick = { selectedFeed = option }) Text(feedLabel(option, state.presets), modifier = Modifier.weight(1f)) } } Button(enabled = state.feeds.size < 8 && selectedFeed != null && state.feeds.none { it == selectedFeed }, onClick = { selectedFeed?.let(onAddFeed) }) { Text(stringResource(Res.string.home_add_feed)) } } Text(stringResource(Res.string.home_feed_limit), style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant) } } } item(key = "done") { TextButton(onClick = onDismiss, modifier = Modifier.padding(horizontal = 16.dp)) { Text(stringResource(Res.string.home_done)) } } } } } @Composable private fun feedLabel(feed: DbHomeSourceFeed, presets: List): String = when (feed.mode) { HomeFeedMode.Popular -> stringResource(Res.string.home_feed_popular) HomeFeedMode.Latest -> stringResource(Res.string.home_feed_latest) HomeFeedMode.SavedSearch -> presets.firstOrNull { it.id == feed.presetId }?.name ?: stringResource(Res.string.home_feed_saved) }