Repository: Automattic/pocket-casts-ios
File: podcasts/New Detail/Episode Add/ManualPlaylistsChooserViewController.swift (Swift)

Code before:

            tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0),
            tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0),
            tableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0),
            tableView.bottomAnchor.constraint(equalTo: footerView.topAnchor, constant: 0)
        ])

        view.layoutSubviews()

        manualPlaylists = dataManager.allManualPlaylists(includeDeleted: false)

        if episodes.count == 1, let episode = episodes.first {
            let uuids = dataManager.manualPlaylistUUIDs(for: episode.uuid)
            initialSelectedPlaylists = Set(uuids)
        } else {
            initialSelectedPlaylists = []
        }
        newSelectedPlaylists = initialSelectedPlaylists
    }

    private func addCloseButton() {
        let closeButton = createStandardCloseButton(imageName: "cancel")
        closeButton.target = self
        closeButton.action = #selector(closeTapped)
        navigationItem.leftBarButtonItem = closeButton
    }

    @objc private func closeTapped(_ sender: Any) {
        dismiss(animated: true, completion: nil)
    }

    @objc private func doneTapped() {
        let added = newSelectedPlaylists.subtracting(initialSelectedPlaylists)
        let removed = initialSelectedPlaylists.subtracting(newSelectedPlaylists)

        FileLog.shared.console("Added \(added), removed \(removed)")

        var changedPlaylists: Set<EpisodeFilter> = []

        let maxPlaylistItems = Constants.Limits.maxFilterItems

        manualPlaylists.forEach { playlist in
            if added.contains(playlist.uuid) {
                if episodes.count > maxPlaylistItems {
                    Toast.show(L10n.playlistManualAddTooManyEpisodesToast(maxPlaylistItems.localized(.decimal)))
                    return
                }
                let currentCount = dataManager.allPlaylistEpisodeCount(for: playlist, episodeUuidToAdd: nil, includingArchivedEpisodes: true)
                if currentCount + episodes.count > maxPlaylistItems {
                    Toast.show(L10n.playlistManualAddEpisodesAlmostFullToast)
                    return

Review comment (suggestion):
This could be sped up by iterating over and storing the playlist UUIDs in a Set and intersecting the sets. The more playlists you have the more this matters and in my testing it took quite a few (many dozens + hundreds of episodes) to notice any difference. We could fix this up later or now

```suggestion
            // For bulk episodes, find playlists that contain ALL selected episodes
            let playlistSets = episodes.map { episode in
                Set(dataManager.manualPlaylistUUIDs(for: episode.uuid))
            }

            initialSelectedPlaylists = playlistSets.dropFirst().reduce(playlistSets.first ?? []) { accumulated, uuids in
                accumulated.intersection(uuids)
            }
```

Code after:

            tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0),
            tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0),
            tableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0),
            tableView.bottomAnchor.constraint(equalTo: footerView.topAnchor, constant: 0)
        ])

        view.layoutSubviews()

        manualPlaylists = dataManager.allManualPlaylists(includeDeleted: false)

        if episodes.count == 1, let episode = episodes.first {
            let uuids = dataManager.manualPlaylistUUIDs(for: episode.uuid)
            initialSelectedPlaylists = Set(uuids)
        } else {
            // For bulk episodes, find playlists that contain ALL selected episodes
            var playlistsContainingAllEpisodes: Set<String> = []
            for playlist in manualPlaylists {
                let allEpisodesInPlaylist = episodes.allSatisfy { episode in
                    dataManager.manualPlaylistUUIDs(for: episode.uuid).contains(playlist.uuid)
                }
                if allEpisodesInPlaylist {
                    playlistsContainingAllEpisodes.insert(playlist.uuid)
                }
            }
            initialSelectedPlaylists = playlistsContainingAllEpisodes
        }
        newSelectedPlaylists = initialSelectedPlaylists
    }

    private func addCloseButton() {
        let closeButton = createStandardCloseButton(imageName: "cancel")
        closeButton.target = self
        closeButton.action = #selector(closeTapped)
        navigationItem.leftBarButtonItem = closeButton
    }

    @objc private func closeTapped(_ sender: Any) {
        dismiss(animated: true, completion: nil)
    }

    @objc private func doneTapped() {
        let added = newSelectedPlaylists.subtracting(initialSelectedPlaylists)
        let removed = initialSelectedPlaylists.subtracting(newSelectedPlaylists)

        FileLog.shared.console("Added \(added), removed \(removed)")

        var changedPlaylists: Set<EpisodeFilter> = []

        let maxPlaylistItems = Constants.Limits.maxFilterItems