Repository: Automattic/pocket-casts-ios
File: podcasts/DiscoverViewController.swift (Swift)

Code before:
            case ("podcast_list", "single_podcast", _):
                addSummaryController(SinglePodcastViewController(), discoverItem: discoverItem)
            case ("podcast_list", "collection", _):
                addSummaryController(CollectionSummaryViewController(), discoverItem: discoverItem)
            case ("network_list", _, _):
                addSummaryController(NetworkSummaryViewController(), discoverItem: discoverItem)
            case ("categories", "category", _):
                addSummaryController(CategorySummaryViewController(regionCode: currentRegion), discoverItem: discoverItem)
            case ("episode_list", "single_episode", _):
                addSummaryController(SingleEpisodeViewController(), discoverItem: discoverItem)
            case ("episode_list", "collection", "plain_list"):
                addSummaryController(CollectionSummaryViewController(), discoverItem: discoverItem)
            default:
                print("Unknown Discover Item: \(type) \(summaryStyle)")
                continue
            }
        }
        currentSnapshot = snapshot
    }

    private func populateFrom(discoverLayout: DiscoverLayout?) {
        loadingContent = false

        guard let layout = discoverLayout, let items = layout.layout, let _ = layout.regions, items.count > 0 else {
            handleLoadFailed()
            return
        }

        self.discoverLayout = layout
        loadingIndicator.stopAnimating()

        let currentRegion = Settings.discoverRegion(discoverLayout: layout)

        func makeDataSourceSnapshot(from items: [DiscoverItem]) -> NSDiffableDataSourceSnapshot<Int, DiscoverItem> {
            var snapshot = NSDiffableDataSourceSnapshot<Int, DiscoverItem>()

            let section = 0
            snapshot.appendSections([section])
            snapshot.appendItems(items)

            return snapshot
        }

        let snapshot = makeDataSourceSnapshot(from: items)
        apply(snapshot: snapshot, currentRegion: currentRegion)

        let countrySummary = CountrySummaryViewController()
        countrySummary.discoverLayout = layout
        countrySummary.registerDiscoverDelegate(self)
        let regions = layout.regions?.keys as? [String]
        let item = DiscoverItem(id: "country-summary", regions: regions ?? [])

Review comment (suggestion):
I don't love the implementation of this but I think it needs to change to a `UICollectionView` or something sooner rather than later. I started looking at that and have made some good progress so will try to submit that some time next week, maybe.

Code after:
            case ("podcast_list", "single_podcast", _):
                addSummaryController(SinglePodcastViewController(), discoverItem: discoverItem)
            case ("podcast_list", "collection", _):
                addSummaryController(CollectionSummaryViewController(), discoverItem: discoverItem)
            case ("network_list", _, _):
                addSummaryController(NetworkSummaryViewController(), discoverItem: discoverItem)
            case ("categories", "category", _):
                addSummaryController(CategorySummaryViewController(regionCode: currentRegion), discoverItem: discoverItem)
            case ("episode_list", "single_episode", _):
                addSummaryController(SingleEpisodeViewController(), discoverItem: discoverItem)
            case ("episode_list", "collection", "plain_list"):
                addSummaryController(CollectionSummaryViewController(), discoverItem: discoverItem)
            default:
                print("Unknown Discover Item: \(type) \(summaryStyle)")
                continue
            }
        }
        currentSnapshot = snapshot
    }

    /// Populate the scroll view using a DiscoverLayout with optional shouldInclude and shouldReset filters.
    /// - Parameters:
    ///   - discoverLayout: A `DiscoverLayout`
    ///   - shouldInclude: Whether a `DiscoverItem` from the layout should be included in the scroll view. This is used to filter items meant only for certain categories, for instance.
    ///   - shouldReset: Whether a view controller from `summaryViewControllers` should be reset during this operation. This is used by the Categories pills to avoid triggering a view reload, allowing animations to continue.
    private func populateFrom(discoverLayout: DiscoverLayout?, shouldInclude: ((DiscoverItem) -> Bool)? = nil, shouldReset: ((DiscoverItem) -> Bool)? = nil) {
        loadingContent = false

        guard let layout = discoverLayout, let items = layout.layout, let _ = layout.regions, items.count > 0 else {
            handleLoadFailed()
            return
        }

        self.discoverLayout = layout
        loadingIndicator.stopAnimating()

        let currentRegion = Settings.discoverRegion(discoverLayout: layout)

        func makeDataSourceSnapshot(from items: [DiscoverItem]) -> NSDiffableDataSourceSnapshot<Int, DiscoverItem> {
            var snapshot = NSDiffableDataSourceSnapshot<Int, DiscoverItem>()

            let section = 0
            snapshot.appendSections([section])
            snapshot.appendItems(items.filter({ shouldInclude?($0) ?? true }))

            return snapshot
        }

        let snapshot = makeDataSourceSnapshot(from: items)
        resetSummaryViewControllers {
            shouldReset?($0) ?? true