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

Code before:
        closeButton.addTarget(self, action: #selector(closeTapped(_:)), for: .touchUpInside)

        let backButtonItem = UIBarButtonItem(customView: closeButton)
        navigationItem.leftBarButtonItem = backButtonItem
    }

    @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> = []

        manualPlaylists.forEach { playlist in
            if added.contains(playlist.uuid) {
                dataManager.add(episodes: [episode], to: playlist)
                changedPlaylists.insert(playlist)
            }
            if removed.contains(playlist.uuid) {
                dataManager.deleteEpisodes([episode.uuid], from: playlist)
                changedPlaylists.insert(playlist)
            }
        }

        changedPlaylists.forEach { playlist in
            playlist.syncStatus = SyncStatus.notSynced.rawValue
            DataManager.sharedManager.save(playlist: playlist)
        }

        dismiss(animated: true, completion: nil)
    }
}

extension ManualPlaylistsChooserViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return TableSection.allCases.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case TableSection.addNewPlaylist.rawValue:
            return 1
        default:
            return manualPlaylists.count
        }
    }

Review comment (suggestion):
I don't think it's necessary here as the data manager here calls the `PlaylistDataManager` and the method `func deleteEpisodes(_ episodeUuids: [String], from playlist: EpisodeFilter, dbQueue: PCDBQueue)` already set the playlist as not synced:
```
playlist.syncStatus = SyncStatus.notSynced.rawValue
                try db.executeUpdate("UPDATE \(DataManager.playlistsTableName) SET syncStatus = ? WHERE uuid = ?", values: [playlist.syncStatus, playlist.uuid])
```

Code after:
        closeButton.addTarget(self, action: #selector(closeTapped(_:)), for: .touchUpInside)

        let backButtonItem = UIBarButtonItem(customView: closeButton)
        navigationItem.leftBarButtonItem = backButtonItem
    }

    @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> = []

        manualPlaylists.forEach { playlist in
            if added.contains(playlist.uuid) {
                dataManager.add(episodes: [episode], to: playlist)
                changedPlaylists.insert(playlist)
            }
            if removed.contains(playlist.uuid) {
                dataManager.deleteEpisodes([episode.uuid], from: playlist)
            }
        }

        changedPlaylists.forEach { playlist in
            playlist.syncStatus = SyncStatus.notSynced.rawValue
            dataManager.save(playlist: playlist)
        }

        dismiss(animated: true, completion: nil)
    }
}

extension ManualPlaylistsChooserViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return TableSection.allCases.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case TableSection.addNewPlaylist.rawValue:
            return 1
        default:
            return manualPlaylists.count
        }
    }