MassMigrationPreviewScreen.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/migration/MassMigrationPreviewScreen.kt
1package eu.friedcutlet.yokan.ui.screens.migration23import androidx.compose.foundation.ExperimentalFoundationApi4import androidx.compose.foundation.background5import androidx.compose.foundation.combinedClickable6import androidx.compose.foundation.layout.Arrangement7import androidx.compose.foundation.layout.Box8import androidx.compose.foundation.layout.Column9import androidx.compose.foundation.layout.Row10import androidx.compose.foundation.layout.Spacer11import androidx.compose.foundation.layout.fillMaxSize12import androidx.compose.foundation.layout.fillMaxWidth13import androidx.compose.foundation.layout.height14import androidx.compose.foundation.layout.padding15import androidx.compose.foundation.layout.size16import androidx.compose.foundation.layout.width17import androidx.compose.foundation.lazy.itemsIndexed18import androidx.compose.foundation.shape.RoundedCornerShape19import androidx.compose.material.icons.Icons20import androidx.compose.material.icons.automirrored.filled.ArrowForward21import androidx.compose.material.icons.automirrored.filled.MenuBook22import androidx.compose.material.icons.filled.ExpandLess23import androidx.compose.material.icons.filled.ExpandMore24import androidx.compose.material.icons.filled.Extension25import androidx.compose.material3.Button26import androidx.compose.material3.Checkbox27import androidx.compose.material3.Icon28import androidx.compose.material3.ListItem29import androidx.compose.material3.MaterialTheme30import androidx.compose.material3.Text31import androidx.compose.material3.TextButton32import androidx.compose.runtime.Composable33import androidx.compose.runtime.getValue34import androidx.compose.runtime.mutableStateOf35import androidx.compose.runtime.remember36import androidx.compose.runtime.setValue37import androidx.compose.ui.Alignment38import androidx.compose.ui.Modifier39import androidx.compose.ui.draw.clip40import androidx.compose.ui.semantics.Role41import androidx.compose.ui.semantics.contentDescription42import androidx.compose.ui.semantics.role43import androidx.compose.ui.semantics.selected44import androidx.compose.ui.semantics.semantics45import androidx.compose.ui.semantics.stateDescription46import androidx.compose.ui.text.font.FontWeight47import androidx.compose.ui.text.style.TextOverflow48import androidx.compose.ui.unit.dp49import androidx.lifecycle.compose.collectAsStateWithLifecycle50import androidx.lifecycle.viewmodel.compose.viewModel51import eu.friedcutlet.yokan.app.YokanRuntime52import eu.friedcutlet.yokan.domain.Source53import eu.friedcutlet.yokan.ui.components.RouteScaffold54import eu.friedcutlet.yokan.ui.components.hoverHighlight55import eu.friedcutlet.yokan.ui.navigation.MissingRoute56import eu.friedcutlet.yokan.ui.navigation.Navigator57import eu.friedcutlet.yokan.ui.navigation.Route58import eu.friedcutlet.yokan.services.extensions.languageName59import org.jetbrains.compose.resources.stringResource60import yokan.composeapp.generated.resources.Res61import yokan.composeapp.generated.resources.accessibility_collapsed62import yokan.composeapp.generated.resources.accessibility_expanded63import yokan.composeapp.generated.resources.mass_migration_collapse_source_chapters64import yokan.composeapp.generated.resources.mass_migration_expand_source_chapters6566@Composable67internal fun MassMigrationPreviewRoute(route: Route.MassMigrationPreview, nav: Navigator) {68 val vm = viewModel {69 MassMigrationPreviewViewModel(route.sourceId, YokanRuntime.repository, YokanRuntime.settings, YokanRuntime.libraryIndex)70 }71 val state by vm.state.collectAsStateWithLifecycle()72 val source = state.source ?: return MissingRoute("Mass Migration Preview", nav::back)73 MassMigrationPreviewScreen(74 source = source,75 state = state,76 onToggle = vm::toggle,77 onLongPress = vm::selectRangeTo,78 onToggleAll = vm::toggleAll,79 onContinue = { nav.navigate(Route.MassMigrationDestination(source.id, state.selectedMangaIds)) },80 onBack = nav::back,81 )82}8384@OptIn(ExperimentalFoundationApi::class)85@Composable86internal fun MassMigrationPreviewScreen(87 source: Source,88 state: MassMigrationPreviewState,89 onToggle: (index: Int) -> Unit,90 onLongPress: (index: Int) -> Unit,91 onToggleAll: () -> Unit,92 onContinue: () -> Unit,93 onBack: () -> Unit,94) {95 val items = state.items.orEmpty()96 val totalCount = items.size97 val selectedCount = state.selectedCount98 val allSelected = state.allSelected99100 RouteScaffold(101 title = "Mass Migration Preview",102 onBack = onBack,103 actions = {104 if (totalCount > 0) {105 TextButton(onClick = onToggleAll) {106 Text(107 if (allSelected) "Deselect all" else "Select all",108 style = MaterialTheme.typography.labelLarge,109 fontWeight = FontWeight.SemiBold,110 )111 }112 }113 },114 ) {115 // Header source tile — same rounded-surface idiom as the rest of the116 // ported screens; no clickable so it reads as a banner, not nav row.117 item {118 androidx.compose.material3.Surface(119 modifier = Modifier120 .fillMaxWidth()121 .padding(horizontal = 8.dp, vertical = 2.dp),122 shape = MaterialTheme.shapes.large,123 color = MaterialTheme.colorScheme.surfaceContainer,124 border = androidx.compose.foundation.BorderStroke(1.dp, MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.4f)),125 ) {126 ListItem(127 headlineContent = {128 Text(129 source.name,130 style = MaterialTheme.typography.titleSmall,131 fontWeight = FontWeight.SemiBold,132 )133 },134 supportingContent = {135 Text(136 "${languageName(source.lang)} • $selectedCount/$totalCount selected",137 style = MaterialTheme.typography.bodySmall,138 color = MaterialTheme.colorScheme.onSurfaceVariant,139 )140 },141 leadingContent = {142 Box(143 modifier = Modifier144 .size(40.dp)145 .clip(androidx.compose.foundation.shape.CircleShape)146 .background(MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.55f)),147 contentAlignment = Alignment.Center,148 ) {149 Icon(150 Icons.Filled.Extension,151 contentDescription = null,152 tint = MaterialTheme.colorScheme.onPrimaryContainer,153 modifier = Modifier.size(20.dp),154 )155 }156 },157 colors = androidx.compose.material3.ListItemDefaults.colors(158 containerColor = androidx.compose.ui.graphics.Color.Transparent,159 ),160 )161 }162 }163 // Per-manga preview cards w/ selectable checkbox & expandable chapter section.164 itemsIndexed(items, key = { _, it -> it.manga.id }) { index, item ->165 MassMigrationPreviewCard(166 item = item,167 index = index + 1,168 selected = item.manga.id in state.selectedIds,169 onToggle = { onToggle(index) },170 onLongClick = { onLongPress(index) },171 )172 }173 // Bottom CTA — picks the destination next.174 item {175 Button(176 onClick = onContinue,177 modifier = Modifier.fillMaxWidth().height(50.dp),178 enabled = selectedCount > 0,179 shape = RoundedCornerShape(50),180 ) {181 Icon(182 Icons.AutoMirrored.Filled.ArrowForward,183 contentDescription = null,184 modifier = Modifier.size(18.dp),185 )186 Spacer(Modifier.width(8.dp))187 Text(188 "Select destination source",189 style = MaterialTheme.typography.labelLarge,190 fontWeight = FontWeight.SemiBold,191 )192 }193 }194 }195}196197@OptIn(ExperimentalFoundationApi::class)198@Composable199private fun MassMigrationPreviewCard(200 item: MassMigrationPreviewItem,201 index: Int,202 selected: Boolean,203 onToggle: () -> Unit,204 onLongClick: () -> Unit,205) {206 val title = item.manga207 var expanded by remember(title.id) { mutableStateOf(false) }208 val cardColor = if (selected) {209 MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.35f)210 } else {211 MaterialTheme.colorScheme.surfaceContainer212 }213 val borderColor = if (selected) {214 MaterialTheme.colorScheme.primary.copy(alpha = 0.7f)215 } else {216 MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.4f)217 }218 androidx.compose.material3.Surface(219 modifier = Modifier220 .fillMaxWidth()221 .padding(horizontal = 8.dp, vertical = 2.dp),222 shape = MaterialTheme.shapes.large,223 color = cardColor,224 border = androidx.compose.foundation.BorderStroke(if (selected) 1.5.dp else 1.dp, borderColor),225 ) {226 Column(Modifier.fillMaxWidth().padding(12.dp), verticalArrangement = Arrangement.spacedBy(10.dp)) {227 Row(228 modifier = Modifier229 .fillMaxWidth()230 .padding(2.dp)231 .clip(MaterialTheme.shapes.medium)232 .combinedClickable(233 onClick = onToggle,234 onLongClick = onLongClick,235 )236 .semantics {237 role = Role.Checkbox238 this.selected = selected239 },240 horizontalArrangement = Arrangement.spacedBy(10.dp),241 verticalAlignment = Alignment.CenterVertically,242 ) {243 Checkbox(244 checked = selected,245 onCheckedChange = null,246 modifier = Modifier.size(20.dp),247 )248 // Index in a small tonal disc — keeps numbering legible while249 // matching the rest of the modernised badges.250 Box(251 modifier = Modifier252 .size(28.dp)253 .clip(androidx.compose.foundation.shape.CircleShape)254 .background(255 if (selected) MaterialTheme.colorScheme.primary.copy(alpha = 0.85f)256 else MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.55f),257 ),258 contentAlignment = Alignment.Center,259 ) {260 Text(261 "$index",262 style = MaterialTheme.typography.labelLarge,263 fontWeight = FontWeight.SemiBold,264 color = if (selected) MaterialTheme.colorScheme.onPrimary else MaterialTheme.colorScheme.onPrimaryContainer,265 )266 }267 Box(268 modifier = Modifier269 .size(width = 60.dp, height = 84.dp)270 .clip(MaterialTheme.shapes.medium),271 ) {272 eu.friedcutlet.yokan.ui.components.CoverImage(273 model = title.imageUrl,274 contentDescription = title.name,275 modifier = Modifier.fillMaxSize(),276 fallbackInitials = title.name.take(2).uppercase(),277 )278 }279 Column(modifier = Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(2.dp)) {280 Text(281 title.name,282 style = MaterialTheme.typography.titleSmall,283 fontWeight = FontWeight.SemiBold,284 maxLines = 2,285 overflow = TextOverflow.Ellipsis,286 )287 Text(288 listOfNotNull(title.author?.takeIf { it.isNotBlank() }, "${item.chapterCount} chapters")289 .joinToString(" • "),290 style = MaterialTheme.typography.bodySmall,291 color = MaterialTheme.colorScheme.onSurfaceVariant,292 )293 }294 }295 // Expansion toggle — rounded clickable row with chevron that296 // rotates between collapsed/expanded states.297 val expansionActionLabel = stringResource(298 if (expanded) Res.string.mass_migration_collapse_source_chapters299 else Res.string.mass_migration_expand_source_chapters,300 title.name,301 )302 val expansionStateDescription = stringResource(303 if (expanded) Res.string.accessibility_expanded else Res.string.accessibility_collapsed,304 )305 Row(306 modifier = Modifier307 .fillMaxWidth()308 .padding(horizontal = 6.dp, vertical = 6.dp)309 .clip(MaterialTheme.shapes.large)310 .hoverHighlight(onClick = { expanded = !expanded })311 .semantics {312 role = Role.Button313 contentDescription = expansionActionLabel314 stateDescription = expansionStateDescription315 },316 verticalAlignment = Alignment.CenterVertically,317 ) {318 Icon(319 if (expanded) Icons.Filled.ExpandLess else Icons.Filled.ExpandMore,320 contentDescription = null,321 tint = MaterialTheme.colorScheme.primary,322 )323 Spacer(Modifier.width(8.dp))324 Text(325 "Source chapters (${item.chapterCount})",326 style = MaterialTheme.typography.labelLarge,327 fontWeight = FontWeight.Medium,328 )329 }330 if (expanded) {331 val shown = item.previewChapterNames332 androidx.compose.material3.Surface(333 modifier = Modifier.fillMaxWidth(),334 shape = MaterialTheme.shapes.large,335 color = MaterialTheme.colorScheme.surfaceContainerHigh,336 ) {337 Column(modifier = Modifier.padding(horizontal = 12.dp, vertical = 8.dp), verticalArrangement = Arrangement.spacedBy(4.dp)) {338 shown.forEach { chapterName ->339 Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp)) {340 Icon(341 Icons.AutoMirrored.Filled.MenuBook,342 contentDescription = null,343 tint = MaterialTheme.colorScheme.onSurfaceVariant,344 modifier = Modifier.size(14.dp),345 )346 Text(347 chapterName,348 style = MaterialTheme.typography.bodySmall,349 maxLines = 1,350 overflow = TextOverflow.Ellipsis,351 )352 }353 }354 val remaining = item.chapterCount - shown.size355 if (remaining > 0) {356 Text(357 "and $remaining more chapters",358 style = MaterialTheme.typography.bodySmall,359 color = MaterialTheme.colorScheme.primary,360 fontWeight = FontWeight.Medium,361 )362 }363 }364 }365 }366 }367 }368}369