ListPublisher section name transformer not working #355

Closed
opened 2025-12-29 15:29:55 +01:00 by adam · 1 comment
Owner

Originally created by @joeljfischer on GitHub (Jan 1, 2021).

Hi, I have a .sectionBy clause set up, but it appears that the name transformer isn't working.

listPublisher = CoreStoreDefaults.dataStack.publishList(
    From<Stopwatch>()
        .sectionBy(\.$isStarted, { (sectionName) -> String? in
            if sectionName == "1" {
                return "Running".localized()
            } else {
                return "Stopped".localized()
            }
        })
        .orderBy(.descending(\.$name))
)

Here, $.isStarted a boolean virtual property, but that all appears to be working correctly. I can breakpoint within the .sectionBy closure and it's running and returning the correct string, but 0 or 1 is still appearing as the section header of my list. Do I need to implement titleForHeader / viewForHeaderInSection? It says to do so under the ListMonitor documentation but not the list publisher documentation. If so, what would I access to see my transformed string?

Originally created by @joeljfischer on GitHub (Jan 1, 2021). Hi, I have a `.sectionBy` clause set up, but it appears that the name transformer isn't working. ```swift listPublisher = CoreStoreDefaults.dataStack.publishList( From<Stopwatch>() .sectionBy(\.$isStarted, { (sectionName) -> String? in if sectionName == "1" { return "Running".localized() } else { return "Stopped".localized() } }) .orderBy(.descending(\.$name)) ) ``` Here, `$.isStarted` a boolean virtual property, but that all appears to be working correctly. I can breakpoint within the `.sectionBy` closure and it's running and returning the correct string, but `0` or `1` is still appearing as the section header of my list. Do I need to implement `titleForHeader` / `viewForHeaderInSection`? It says to do so under the `ListMonitor` documentation but not the list publisher documentation. If so, what would I access to see my transformed string?
adam added the enhancement label 2025-12-29 15:29:55 +01:00
adam closed this issue 2025-12-29 15:29:55 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Jan 3, 2021):

@joeljfischer The titleForHeader / viewForHeaderInSection for DiffableDataSource adapters currently doesn't support the custom sectionIndex transformers, sorry about that. I'll try to rework the adapters in an update, but for now you have the following options to work around it:

  • Subclassing DiffableDataSource.TableViewAdapter or DiffableDataSource.CollectionViewAdapter and override the tableView(_:titleForHeaderInSection:) or collectionView(_:viewForSupplementaryElementOfKind:at:)
  • Adding a Field.Virtual property that implements the logic. You can then use that property as the sectionBy key. See the Demo app's example:
Modern.ColorsDemo.dataStack.publishList(
            From<Modern.ColorsDemo.Palette>()
                .sectionBy(\.$colorGroup)
                / /...
        )
        @Field.Virtual(
            "colorGroup",
            customGetter: { object, field in
                
                if let colorGroup = field.primitiveValue {
                    
                    return colorGroup
                }
                let colorGroup: String
                switch object.$hue.value * 359 {
                    
                case 0 ..< 20: colorGroup = "Lower Reds"
                case 20 ..< 57: colorGroup = "Oranges and Browns"
                case 57 ..< 90: colorGroup = "Yellow-Greens"
                case 90 ..< 159: colorGroup = "Greens"
                case 159 ..< 197: colorGroup = "Blue-Greens"
                case 197 ..< 241: colorGroup = "Blues"
                case 241 ..< 297: colorGroup = "Violets"
                case 297 ..< 331: colorGroup = "Magentas"
                default: colorGroup = "Upper Reds"
                }
                field.primitiveValue = colorGroup
                return colorGroup
            }
        )
        var colorGroup: String
@JohnEstropia commented on GitHub (Jan 3, 2021): @joeljfischer The titleForHeader / viewForHeaderInSection for DiffableDataSource adapters currently doesn't support the custom sectionIndex transformers, sorry about that. I'll try to rework the adapters in an update, but for now you have the following options to work around it: - Subclassing `DiffableDataSource.TableViewAdapter` or `DiffableDataSource.CollectionViewAdapter` and override the `tableView(_:titleForHeaderInSection:)` or `collectionView(_:viewForSupplementaryElementOfKind:at:)` - Adding a `Field.Virtual` property that implements the logic. You can then use that property as the `sectionBy` key. See the Demo app's example: ```swift Modern.ColorsDemo.dataStack.publishList( From<Modern.ColorsDemo.Palette>() .sectionBy(\.$colorGroup) / /... ) ``` ```swift @Field.Virtual( "colorGroup", customGetter: { object, field in if let colorGroup = field.primitiveValue { return colorGroup } let colorGroup: String switch object.$hue.value * 359 { case 0 ..< 20: colorGroup = "Lower Reds" case 20 ..< 57: colorGroup = "Oranges and Browns" case 57 ..< 90: colorGroup = "Yellow-Greens" case 90 ..< 159: colorGroup = "Greens" case 159 ..< 197: colorGroup = "Blue-Greens" case 197 ..< 241: colorGroup = "Blues" case 241 ..< 297: colorGroup = "Violets" case 297 ..< 331: colorGroup = "Magentas" default: colorGroup = "Upper Reds" } field.primitiveValue = colorGroup return colorGroup } ) var colorGroup: String ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#355