Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support item expanding and collapsing #146

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

bmarkowitz
Copy link
Contributor

@bmarkowitz bmarkowitz commented Nov 16, 2024

Issue #33

Describe your changes

This is a WIP of an implementation to support nested items (i.e. nested CellViewModels), enabled by NSDiffableDataSourceSectionSnapshots.

What's been done so far

  1. As discussed in the issue, CellViewModel now includes a children property, which allows us to provide an array of AnyCellViewModels. Each of those can have children, and so on.
  2. Created DiffableSectionSnapshot, which is a typealias for NSDiffableDataSourceSectionSnapshot. The ability to append items to other items is only supported through section snapshots. The logic to do so is recursive - appending the items for each top level cell, then for each of those going into all of their children, and so on.
  3. Inside DiffableDataSource, I added a check to see if we have at least one section with at least one cell that has non-empty children. If that's the case, we split off into creating section snapshots. Otherwise, we proceed with the existing standard snapshot approach.
    • I believe we'll want the same completion logic, so I tried to separate that into a method so as to not repeat all of the comments in there.
  4. The final commit is purely for demo purposes, which implements a rough version of some things described below that are still needed + in order to be able to show a quick demo in the example app.
Simulator.Screen.Recording.-.iPhone.16.Pro.-.2024-11-16.at.12.06.50.mp4

What still needs to be done

  1. We need to figure out an approach for reconfiguring items for the section snapshots. Section snapshots don't have the same reconfigure APIs as standard snapshots, so my thinking is we'll need to apply all of the section snapshots, then at the end grab the current full snapshot via self.snapshot(), then reconfigure from there.
  2. We'll need additional recursive logic in CollectionViewModel to ensure that we're referencing the snapshot's visibleItems when we're grabbing a CellViewModel for a given index path. The index paths all change when you expand/collapse an item, so we have to account for that.
  3. Example app work - not sure if want that in this PR or a separate one.
  4. Perhaps some additional/updated comments
  5. Tests

@bmarkowitz bmarkowitz force-pushed the feature/expandable-items branch from 1787986 to 238a869 Compare November 16, 2024 17:19
self.apply(destinationSectionSnapshot, to: $0.id, animatingDifferences: animated) { [weak self] in
self?._applySnapshotCompletion(source: source,
destination: destination,
completion)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Execute completion multiple times?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, yup 🤦 Great catch.

Any ideas? Thinking a dispatch group might work here?

@bmarkowitz
Copy link
Contributor Author

One other note - this warning appears whenever collapsing or expanding an item, and then any time the collection view updates after that. Only way I've been able to "fix" it is by disabling diffing on a background queue, but not sure why.

Screenshot 2024-11-16 at 4 39 27 PM

@bmarkowitz
Copy link
Contributor Author

Related to the above, found this super interesting response from Apple:

Currently, you do need to apply snapshots on the main queue when using certain features (such as outline disclosure expand/collapse support, or interactive item reordering) where the user is directly manipulating the collection view and an immediate UI update is expected in response.

Also goes on to say that "applying snapshots from a background queue is almost always unnecessary in general."

https://forums.developer.apple.com/forums/thread/757270

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants