HomeCustomizeSheet.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/home/HomeCustomizeSheet.kt
1package eu.friedcutlet.yokan.ui.screens.home23import androidx.compose.foundation.clickable4import androidx.compose.foundation.layout.*5import androidx.compose.foundation.lazy.LazyColumn6import androidx.compose.foundation.lazy.items7import androidx.compose.material.icons.Icons8import androidx.compose.material.icons.filled.ArrowDownward9import androidx.compose.material.icons.filled.ArrowUpward10import androidx.compose.material.icons.filled.Delete11import androidx.compose.material3.*12import androidx.compose.runtime.*13import androidx.compose.ui.Modifier14import androidx.compose.ui.text.input.PasswordVisualTransformation15import androidx.compose.ui.text.style.TextOverflow16import androidx.compose.ui.unit.dp17import eu.friedcutlet.yokan.database.*18import eu.friedcutlet.yokan.ui.components.compactSheetWidth19import eu.friedcutlet.yokan.ui.screens.settings.SwitchTile20import org.jetbrains.compose.resources.stringResource21import yokan.composeapp.generated.resources.*2223@OptIn(ExperimentalMaterial3Api::class)24@Composable25internal fun HomeCustomizeSheet(26 state: HomeState,27 onPreferences: ((DbHomePreferences) -> DbHomePreferences) -> Unit,28 onToken: (String) -> Unit,29 onAddFeed: (DbHomeSourceFeed) -> Unit,30 onRemoveFeed: (String) -> Unit,31 onMoveFeed: (String, Int) -> Unit,32 onDismiss: () -> Unit,33) {34 var tokenDraft by remember { mutableStateOf("") }35 var sourceMenuOpen by remember { mutableStateOf(false) }36 var selectedSourceId by remember { mutableStateOf(state.sources.firstOrNull()?.id) }37 var selectedFeed by remember(selectedSourceId) { mutableStateOf(selectedSourceId?.let { DbHomeSourceFeed(it) }) }38 val source = state.sources.firstOrNull { it.id == selectedSourceId }39 ModalBottomSheet(onDismissRequest = onDismiss, modifier = Modifier.compactSheetWidth()) {40 LazyColumn(Modifier.fillMaxWidth(), contentPadding = PaddingValues(bottom = 32.dp)) {41 item(key = "title") {42 Text(stringResource(Res.string.home_customize), style = MaterialTheme.typography.headlineSmall,43 modifier = Modifier.padding(horizontal = 24.dp, vertical = 12.dp))44 }45 item(key = "featured") { SwitchTile(stringResource(Res.string.home_show_featured), null, state.preferences.showFeatured,46 { value -> onPreferences { it.copy(showFeatured = value) } }) }47 item(key = "motion") { SwitchTile(stringResource(Res.string.home_auto_advance), stringResource(Res.string.home_auto_advance_hint), state.preferences.autoAdvanceFeatured,48 { value -> onPreferences { it.copy(autoAdvanceFeatured = value) } }, enabled = state.preferences.showFeatured) }49 item(key = "library-hero") { SwitchTile(stringResource(Res.string.home_library_featured), null, state.preferences.featuredLibraryOnly,50 { value -> onPreferences { it.copy(featuredLibraryOnly = value) } }, enabled = state.preferences.showFeatured) }51 item(key = "completed") { SwitchTile(stringResource(Res.string.home_hide_completed), null, state.preferences.hideCompleted,52 { value -> onPreferences { it.copy(hideCompleted = value) } }) }53 item(key = "count") {54 Column(Modifier.padding(horizontal = 24.dp, vertical = 8.dp)) {55 Text(stringResource(Res.string.home_shelf_size), style = MaterialTheme.typography.titleMedium)56 Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {57 listOf(6, 12, 24).forEach { count -> FilterChip(selected = state.preferences.itemsPerShelf == count,58 onClick = { onPreferences { it.copy(itemsPerShelf = count) } }, label = { Text(count.toString()) }) }59 }60 }61 }62 item(key = "anilist") { SwitchTile(stringResource(Res.string.home_enable_anilist), null, state.preferences.enableAniList,63 { value -> onPreferences { it.copy(enableAniList = value) } }) }64 item(key = "tmdb") { SwitchTile(stringResource(Res.string.home_enable_tmdb), null, state.preferences.enableTmdb,65 { value -> onPreferences { it.copy(enableTmdb = value) } }) }66 if (state.preferences.enableTmdb) item(key = "token") {67 Column(Modifier.padding(horizontal = 24.dp, vertical = 8.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {68 Text(stringResource(Res.string.home_tmdb_token_hint), style = MaterialTheme.typography.bodyMedium)69 OutlinedTextField(value = tokenDraft, onValueChange = { tokenDraft = it }, singleLine = true,70 label = { Text(stringResource(Res.string.home_tmdb_token)) }, visualTransformation = PasswordVisualTransformation(),71 modifier = Modifier.fillMaxWidth())72 Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {73 Button(enabled = tokenDraft.isNotBlank(), onClick = { onToken(tokenDraft); tokenDraft = "" }) { Text(stringResource(Res.string.home_save_token)) }74 if (state.tmdbConfigured) TextButton(onClick = { onToken(""); tokenDraft = "" }) { Text(stringResource(Res.string.home_remove_token)) }75 }76 if (state.tmdbConfigured) Text(stringResource(Res.string.home_token_saved), style = MaterialTheme.typography.labelLarge)77 }78 }79 item(key = "sections-heading") { Text(stringResource(Res.string.home_visible_sections), style = MaterialTheme.typography.titleLarge,80 modifier = Modifier.padding(horizontal = 24.dp, vertical = 16.dp)) }81 items(HomeSection.entries, key = { "section:${it.name}" }, contentType = { "switch" }) { section ->82 SwitchTile(homeSectionLabel(section), null, section !in state.preferences.hiddenSections, { visible ->83 onPreferences { prefs -> prefs.copy(hiddenSections = if (visible) prefs.hiddenSections - section else prefs.hiddenSections + section) }84 })85 }86 item(key = "feeds-heading") { Text(stringResource(Res.string.home_source_feeds), style = MaterialTheme.typography.titleLarge,87 modifier = Modifier.padding(horizontal = 24.dp, vertical = 16.dp)) }88 items(state.feeds, key = { "feed:${it.key}" }, contentType = { "feed" }) { feed ->89 val name = state.sources.firstOrNull { it.id == feed.sourceId }?.name ?: stringResource(Res.string.home_unavailable_source)90 Column(Modifier.padding(horizontal = 24.dp, vertical = 8.dp)) {91 Text(name, style = MaterialTheme.typography.titleMedium)92 Text(feedLabel(feed, state.presets), color = MaterialTheme.colorScheme.onSurfaceVariant)93 Row {94 IconButton(onClick = { onMoveFeed(feed.key, -1) }, enabled = state.feeds.firstOrNull()?.key != feed.key) {95 Icon(Icons.Filled.ArrowUpward, stringResource(Res.string.home_move_up))96 }97 IconButton(onClick = { onMoveFeed(feed.key, 1) }, enabled = state.feeds.lastOrNull()?.key != feed.key) {98 Icon(Icons.Filled.ArrowDownward, stringResource(Res.string.home_move_down))99 }100 IconButton(onClick = { onRemoveFeed(feed.key) }) { Icon(Icons.Filled.Delete, stringResource(Res.string.home_remove_feed)) }101 }102 }103 }104 item(key = "add-feed") {105 Column(Modifier.fillMaxWidth().padding(horizontal = 24.dp, vertical = 8.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {106 Text(stringResource(Res.string.home_add_feed), style = MaterialTheme.typography.titleMedium)107 if (state.sources.isEmpty()) Text(stringResource(Res.string.home_install_sources)) else {108 Box {109 OutlinedButton(onClick = { sourceMenuOpen = true }) { Text(source?.name ?: stringResource(Res.string.home_select_source)) }110 DropdownMenu(expanded = sourceMenuOpen, onDismissRequest = { sourceMenuOpen = false }, modifier = Modifier.heightIn(max = 320.dp)) {111 state.sources.forEach { candidate -> DropdownMenuItem(text = { Text(candidate.name, maxLines = 1, overflow = TextOverflow.Ellipsis) },112 onClick = { selectedSourceId = candidate.id; sourceMenuOpen = false }) }113 }114 }115 source?.let { selected ->116 val options = listOf(DbHomeSourceFeed(selected.id)) +117 (if (selected.supportLatest == true) listOf(DbHomeSourceFeed(selected.id, HomeFeedMode.Latest)) else emptyList()) +118 state.presets.filter { it.sourceId == selected.id }.map { DbHomeSourceFeed(selected.id, HomeFeedMode.SavedSearch, it.id) }119 options.forEach { option ->120 Row(Modifier.fillMaxWidth().padding(vertical = 4.dp).clickable { selectedFeed = option }, verticalAlignment = androidx.compose.ui.Alignment.CenterVertically) {121 RadioButton(selected = selectedFeed == option, onClick = { selectedFeed = option })122 Text(feedLabel(option, state.presets), modifier = Modifier.weight(1f))123 }124 }125 Button(enabled = state.feeds.size < 8 && selectedFeed != null && state.feeds.none { it == selectedFeed },126 onClick = { selectedFeed?.let(onAddFeed) }) { Text(stringResource(Res.string.home_add_feed)) }127 }128 Text(stringResource(Res.string.home_feed_limit), style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant)129 }130 }131 }132 item(key = "done") { TextButton(onClick = onDismiss, modifier = Modifier.padding(horizontal = 16.dp)) { Text(stringResource(Res.string.home_done)) } }133 }134 }135}136137@Composable138private fun feedLabel(feed: DbHomeSourceFeed, presets: List<DbSavedSearch>): String = when (feed.mode) {139 HomeFeedMode.Popular -> stringResource(Res.string.home_feed_popular)140 HomeFeedMode.Latest -> stringResource(Res.string.home_feed_latest)141 HomeFeedMode.SavedSearch -> presets.firstOrNull { it.id == feed.presetId }?.name ?: stringResource(Res.string.home_feed_saved)142}143