LocalFolderDialogs.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/settings/downloads/LocalFolderDialogs.kt
1package eu.friedcutlet.yokan.ui.screens.settings.downloads23import androidx.compose.foundation.BorderStroke4import androidx.compose.foundation.layout.Arrangement5import androidx.compose.foundation.layout.Column6import androidx.compose.foundation.layout.PaddingValues7import androidx.compose.foundation.layout.Row8import androidx.compose.foundation.layout.Spacer9import androidx.compose.foundation.layout.fillMaxWidth10import androidx.compose.foundation.layout.height11import androidx.compose.foundation.layout.heightIn12import androidx.compose.foundation.layout.padding13import androidx.compose.foundation.layout.size14import androidx.compose.foundation.rememberScrollState15import androidx.compose.foundation.shape.CircleShape16import androidx.compose.foundation.shape.RoundedCornerShape17import androidx.compose.foundation.verticalScroll18import androidx.compose.material.icons.Icons19import androidx.compose.material.icons.automirrored.filled.ChromeReaderMode20import androidx.compose.material.icons.automirrored.filled.MenuBook21import androidx.compose.material.icons.filled.PlayCircle22import androidx.compose.material.icons.filled.Sync23import androidx.compose.material3.AlertDialog24import androidx.compose.material3.Button25import androidx.compose.material3.Icon26import androidx.compose.material3.MaterialTheme27import androidx.compose.material3.OutlinedTextField28import androidx.compose.material3.Surface29import androidx.compose.material3.Text30import androidx.compose.material3.TextButton31import androidx.compose.runtime.Composable32import androidx.compose.runtime.getValue33import androidx.compose.runtime.mutableStateOf34import androidx.compose.runtime.remember35import androidx.compose.runtime.setValue36import androidx.compose.ui.Alignment37import androidx.compose.ui.Modifier38import androidx.compose.ui.graphics.vector.ImageVector39import androidx.compose.ui.text.font.FontFamily40import androidx.compose.ui.text.font.FontWeight41import androidx.compose.ui.unit.dp42import androidx.compose.ui.unit.sp43import eu.friedcutlet.yokan.database.DbLocalFolder44import eu.friedcutlet.yokan.ui.components.compactDialogWidth45import org.jetbrains.compose.resources.stringResource46import yokan.composeapp.generated.resources.Res47import yokan.composeapp.generated.resources.action_cancel48import yokan.composeapp.generated.resources.action_save49import yokan.composeapp.generated.resources.ui_close5051internal data class LocalFolderEditState(52 val path: String,53 val initialName: String,54 val editing: DbLocalFolder?,55)5657@Composable58internal fun LocalFolderNameDialog(59 initialName: String,60 path: String,61 existingNames: Set<String>,62 onDismiss: () -> Unit,63 onConfirm: (String) -> Unit,64) {65 var name by remember(initialName) { mutableStateOf(initialName) }66 val trimmed = name.trim()67 val collision = trimmed.isNotEmpty() && existingNames.contains(trimmed)68 val empty = trimmed.isEmpty()69 val isReserved = trimmed.equals("local", ignoreCase = true)70 val error = when {71 empty -> "Name cannot be empty"72 isReserved -> "\"local\" is reserved for the default folder"73 collision -> "Name already used"74 else -> null75 }76 AlertDialog(77 onDismissRequest = onDismiss,78 modifier = Modifier.compactDialogWidth(),79 shape = MaterialTheme.shapes.extraLarge,80 title = {81 Text(82 if (existingNames.isEmpty()) "Name folder" else "Edit folder name",83 style = MaterialTheme.typography.titleMedium,84 fontWeight = FontWeight.SemiBold,85 )86 },87 text = {88 Column {89 Text(path, style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant)90 Spacer(Modifier.height(8.dp))91 OutlinedTextField(92 value = name,93 onValueChange = { name = it },94 label = { Text("Folder name") },95 isError = error != null,96 singleLine = true,97 shape = MaterialTheme.shapes.large,98 modifier = Modifier.fillMaxWidth(),99 )100 error?.let { Text(it, color = MaterialTheme.colorScheme.error, style = MaterialTheme.typography.bodySmall) }101 }102 },103 confirmButton = {104 Button(105 onClick = { onConfirm(trimmed) },106 enabled = error == null,107 shape = RoundedCornerShape(50),108 contentPadding = PaddingValues(horizontal = 18.dp, vertical = 8.dp),109 ) { Text(stringResource(Res.string.action_save), style = MaterialTheme.typography.labelLarge) }110 },111 dismissButton = { TextButton(onClick = onDismiss) { Text(stringResource(Res.string.action_cancel)) } },112 )113}114115@Composable116internal fun LocalFolderStructureDialog(onDismiss: () -> Unit) {117 AlertDialog(118 onDismissRequest = onDismiss,119 modifier = Modifier.compactDialogWidth(),120 shape = MaterialTheme.shapes.extraLarge,121 title = {122 Text(123 "Organize local files",124 style = MaterialTheme.typography.titleMedium,125 fontWeight = FontWeight.SemiBold,126 )127 },128 text = {129 Column(130 modifier = Modifier131 .heightIn(max = 560.dp)132 .verticalScroll(rememberScrollState()),133 verticalArrangement = Arrangement.spacedBy(12.dp),134 ) {135 Text(136 "Use a separate root folder for each library type. Include Manga, Anime, or Novel in the root folder name so Yokan can classify it.",137 style = MaterialTheme.typography.bodyMedium,138 color = MaterialTheme.colorScheme.onSurfaceVariant,139 )140 LocalFolderStructureExample(141 title = "Manga",142 detail = "Image folders or CBZ, ZIP, CBT, and TAR archives",143 icon = Icons.AutoMirrored.Filled.MenuBook,144 structure = """145 Manga/146 └── Title name/147 ├── cover.jpg148 ├── Chapter 01/149 │ ├── 001.jpg150 │ └── 002.png151 └── Chapter 02.cbz152 """.trimIndent(),153 )154 LocalFolderStructureExample(155 title = "Anime",156 detail = "Video files with optional matching subtitles",157 icon = Icons.Filled.PlayCircle,158 structure = """159 Anime/160 └── Title name/161 ├── cover.jpg162 ├── Episode 01.mkv163 └── Episode 01_subtitles/164 ├── en.srt165 └── de.srt166 """.trimIndent(),167 )168 LocalFolderStructureExample(169 title = "Novel",170 detail = "EPUB files",171 icon = Icons.AutoMirrored.Filled.ChromeReaderMode,172 structure = """173 Novel/174 └── Title name/175 ├── cover.jpg176 └── Book.epub177 """.trimIndent(),178 )179 Surface(180 color = MaterialTheme.colorScheme.secondaryContainer.copy(alpha = 0.6f),181 shape = MaterialTheme.shapes.large,182 ) {183 Row(184 modifier = Modifier.fillMaxWidth().padding(12.dp),185 horizontalArrangement = Arrangement.spacedBy(10.dp),186 verticalAlignment = Alignment.CenterVertically,187 ) {188 Icon(189 Icons.Filled.Sync,190 contentDescription = null,191 tint = MaterialTheme.colorScheme.onSecondaryContainer,192 modifier = Modifier.size(20.dp),193 )194 Text(195 "After adding or moving files, run Rescan local folders.",196 style = MaterialTheme.typography.bodySmall,197 color = MaterialTheme.colorScheme.onSecondaryContainer,198 )199 }200 }201 }202 },203 confirmButton = {204 Button(205 onClick = onDismiss,206 shape = RoundedCornerShape(50),207 contentPadding = PaddingValues(horizontal = 18.dp, vertical = 8.dp),208 ) { Text(stringResource(Res.string.ui_close), style = MaterialTheme.typography.labelLarge) }209 },210 )211}212213@Composable214private fun LocalFolderStructureExample(215 title: String,216 detail: String,217 icon: ImageVector,218 structure: String,219) {220 Surface(221 modifier = Modifier.fillMaxWidth(),222 color = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.35f),223 shape = MaterialTheme.shapes.large,224 border = BorderStroke(1.dp, MaterialTheme.colorScheme.outlineVariant),225 ) {226 Column(227 modifier = Modifier.padding(14.dp),228 verticalArrangement = Arrangement.spacedBy(10.dp),229 ) {230 Row(231 horizontalArrangement = Arrangement.spacedBy(10.dp),232 verticalAlignment = Alignment.CenterVertically,233 ) {234 Surface(235 color = MaterialTheme.colorScheme.primaryContainer,236 shape = CircleShape,237 ) {238 Icon(239 icon,240 contentDescription = null,241 tint = MaterialTheme.colorScheme.onPrimaryContainer,242 modifier = Modifier.padding(8.dp).size(18.dp),243 )244 }245 Column(modifier = Modifier.weight(1f)) {246 Text(247 title,248 style = MaterialTheme.typography.titleSmall,249 fontWeight = FontWeight.SemiBold,250 )251 Text(252 detail,253 style = MaterialTheme.typography.labelSmall,254 color = MaterialTheme.colorScheme.onSurfaceVariant,255 )256 }257 }258 Text(259 structure,260 fontFamily = FontFamily.Monospace,261 style = MaterialTheme.typography.bodySmall,262 lineHeight = 18.sp,263 )264 }265 }266}267