@@ -292,6 +292,8 @@ struct ExportDatabaseItem: Identifiable {
292292 /// Set when the container's own checkbox was turned on before its unread kinds arrived, so
293293 /// they arrive selected like everything else the click reached.
294294 var selectsLoadedObjects = false
295+ // Column values set before the unread kinds arrived, by column index; they arrive with them.
296+ var arrivingOptionValues : [ Int : Bool ] = [ : ]
295297
296298 init (
297299 id : UUID = UUID ( ) ,
@@ -313,8 +315,12 @@ struct ExportDatabaseItem: Identifiable {
313315
314316 /// An object that arrives already selected (a restored row) stays so. Any other is selected
315317 /// only when the container was ticked whole and nothing in it has been unticked since.
316- func mergingLoadedObjects ( _ loaded : [ ExportObjectItem ] ) -> ExportDatabaseItem {
317- let selectsArrivals = selectsLoadedObjects && objects . allSatisfy ( \. isSelected )
318+ func mergingLoadedObjects (
319+ _ loaded : [ ExportObjectItem ] ,
320+ writes : ( ExportObjectItem ) -> Bool = { _ in true }
321+ ) -> ExportDatabaseItem {
322+ // An object that writes nothing stays out of a container tick, so it does not count as unticked.
323+ let selectsArrivals = selectsLoadedObjects && objects . filter ( writes ) . allSatisfy ( \. isSelected )
318324 let incoming = loaded . filter { unloadedKinds . contains ( $0 . kind ) }
319325 // The table listing reported these sequences as tables; one relation, so the sequence replaces it.
320326 let sequenceNames = Set ( incoming . filter { $0 . kind == . sequence } . map ( \. name ) )
@@ -331,9 +337,32 @@ struct ExportDatabaseItem: Identifiable {
331337 merged . unloadedKinds = [ ]
332338 merged . isLoadingObjects = false
333339 merged . selectsLoadedObjects = false
340+ merged . arrivingOptionValues = [ : ]
334341 return merged
335342 }
336343
344+ // Kept values go on after normalizing, which replaces an arrival's empty list with the defaults.
345+ func mergingLoadedObjects ( _ loaded : [ ExportObjectItem ] , layout : ExportOptionLayout ) -> ExportDatabaseItem ? {
346+ let merged = mergingLoadedObjects ( loaded , writes : layout . writesSomething )
347+ guard ! merged . objects . isEmpty , let normalized = [ merged ] . normalized ( for : layout ) . first else { return nil }
348+ let arrivals = Set ( normalized . objects . map ( \. id ) ) . subtracting ( objects . map ( \. id ) )
349+ return normalized . applyingOptionValues ( arrivingOptionValues , toObjects : arrivals , layout : layout )
350+ }
351+
352+ func applyingOptionValues (
353+ _ values : [ Int : Bool ] ,
354+ toObjects ids : Set < UUID > ,
355+ layout : ExportOptionLayout
356+ ) -> ExportDatabaseItem {
357+ guard ! values . isEmpty , ! ids . isEmpty else { return self }
358+ var updated = self
359+ updated . objects = objects . map { object in
360+ guard ids . contains ( object . id ) else { return object }
361+ return object . settingOptions ( values . filter { layout . supports ( columnAt : $0 . key , kind : object . kind ) } )
362+ }
363+ return updated
364+ }
365+
337366 func withID ( _ id : UUID ) -> ExportDatabaseItem {
338367 var copy = ExportDatabaseItem (
339368 id : id ,
@@ -344,6 +373,7 @@ struct ExportDatabaseItem: Identifiable {
344373 )
345374 copy. isLoadingObjects = isLoadingObjects
346375 copy. selectsLoadedObjects = selectsLoadedObjects
376+ copy. arrivingOptionValues = arrivingOptionValues
347377 return copy
348378 }
349379
@@ -377,7 +407,75 @@ struct ExportDatabaseItem: Identifiable {
377407 }
378408}
379409
410+ enum ExportKindReconciliation : Equatable {
411+ case unchanged
412+ // The kinds left after narrowing, so a wider format later reads again.
413+ case narrowed ( loadedKinds : Set < PluginExportObjectKind > )
414+ case reload
415+
416+ static func next (
417+ loadedKinds : Set < PluginExportObjectKind > ,
418+ wanted : Set < PluginExportObjectKind >
419+ ) -> ExportKindReconciliation {
420+ guard ! loadedKinds . isEmpty else { return . unchanged }
421+ guard wanted . isSubset ( of : loadedKinds ) else { return . reload }
422+ return wanted == loadedKinds ? . unchanged : . narrowed ( loadedKinds : wanted )
423+ }
424+ }
425+
426+ // Resolved by the dialog so the tree never looks a plugin up itself.
427+ struct ExportOptionLayout {
428+ let columns : [ PluginExportOptionColumn ]
429+ let defaultValues : [ Bool ]
430+ // False for a structure-only format, which ignores a row scope.
431+ let readsRows : Bool
432+ let supports : ( String , PluginExportObjectKind ) -> Bool
433+
434+ static var none : ExportOptionLayout {
435+ ExportOptionLayout ( columns : [ ] , defaultValues : [ ] , readsRows : true ) { _ , _ in true }
436+ }
437+
438+ func supports ( columnAt index : Int , kind : PluginExportObjectKind ) -> Bool {
439+ columns . indices . contains ( index ) && supports ( columns [ index ] . id , kind )
440+ }
441+
442+ func writesSomething ( _ object : ExportObjectItem ) -> Bool {
443+ columns . isEmpty || object . optionValues . contains ( true )
444+ }
445+
446+ // Unread kinds count with the value they will arrive with.
447+ func values (
448+ ofColumn index : Int ,
449+ in objects : [ ExportObjectItem ] ,
450+ unloadedKinds : Set < PluginExportObjectKind > = [ ] ,
451+ arriving : [ Int : Bool ] = [ : ]
452+ ) -> [ Bool ] {
453+ guard columns . indices . contains ( index ) else { return [ ] }
454+ let fallback = defaultValues . count == columns . count ? defaultValues [ index ] : columns [ index ] . defaultValue
455+ var values = objects
456+ . filter { supports ( columnAt : index , kind : $0 . kind ) }
457+ . map { $0 . optionValues [ safe : index ] ?? columns [ index ] . defaultValue }
458+ if unloadedKinds . contains ( where : { supports ( columnAt : index , kind : $0 ) } ) {
459+ values . append ( arriving [ index ] ?? fallback )
460+ }
461+ return values
462+ }
463+ }
464+
380465extension ExportObjectItem {
466+ // Never selects. A selected object left with nothing on is deselected: it would export nothing.
467+ func settingOptions ( _ values : [ Int : Bool ] ) -> ExportObjectItem {
468+ guard values . keys . contains ( where : optionValues . indices . contains ) else { return self }
469+ var updated = self
470+ for ( index , value ) in values where updated . optionValues . indices . contains ( index ) {
471+ updated . optionValues [ index ] = value
472+ }
473+ if ! updated . optionValues . contains ( true ) {
474+ updated . isSelected = false
475+ }
476+ return updated
477+ }
478+
381479 func normalized ( forOptionColumnCount optionColumnCount : Int , defaultOptionValues : [ Bool ] ) -> ExportObjectItem {
382480 guard optionColumnCount > 0 else { return self }
383481 let fallback = defaultOptionValues . count == optionColumnCount
@@ -451,6 +549,23 @@ extension [ExportDatabaseItem] {
451549 }
452550 }
453551
552+ // Masking keeps a routine from carrying a Data flag that would count it as exportable.
553+ func normalized ( for layout : ExportOptionLayout ) -> [ ExportDatabaseItem ] {
554+ normalizingOptionValues ( optionColumnCount : layout . columns . count , defaultOptionValues : layout . defaultValues )
555+ . maskingUnsupportedOptions ( columns : layout . columns , supports : layout . supports )
556+ }
557+
558+ // A profile's option values belong to its format, so the tree is reset for that format first.
559+ func applyingProfile (
560+ _ profile : ExportProfile ,
561+ layout : ExportOptionLayout ,
562+ databaseType : DatabaseType
563+ ) -> [ ExportDatabaseItem ] {
564+ ExportProfileStorage . apply ( profile , to : self )
565+ . clearingRowScopes ( unavailableOn : databaseType )
566+ . normalized ( for : layout )
567+ }
568+
454569 func normalizingOptionValues ( optionColumnCount : Int , defaultOptionValues : [ Bool ] ) -> [ ExportDatabaseItem ] {
455570 map { database in
456571 var normalizedDatabase = database
@@ -464,6 +579,7 @@ extension [ExportDatabaseItem] {
464579 func resettingOptionValues ( to values : [ Bool ] ) -> [ ExportDatabaseItem ] {
465580 map { database in
466581 var resetDatabase = database
582+ resetDatabase . arrivingOptionValues = [ : ]
467583 resetDatabase . objects = database . objects . map { object in
468584 var resetObject = object
469585 resetObject . optionValues = values
@@ -486,6 +602,24 @@ extension [ExportDatabaseItem] {
486602 }
487603 }
488604
605+ func settingOption (
606+ _ index : Int ,
607+ to value : Bool ,
608+ objectIDs ids : Set < UUID > ,
609+ arrivingIn databaseID : UUID ? = nil
610+ ) -> [ ExportDatabaseItem ] {
611+ map { database in
612+ var updated = database
613+ updated . objects = database . objects . map { object in
614+ ids . contains ( object . id ) ? object . settingOptions ( [ index : value ] ) : object
615+ }
616+ if database . id == databaseID {
617+ updated . arrivingOptionValues [ index ] = value
618+ }
619+ return updated
620+ }
621+ }
622+
489623 func clearingRowScopes ( unavailableOn databaseType : DatabaseType ) -> [ ExportDatabaseItem ] {
490624 map { database in
491625 var clearedDatabase = database
0 commit comments