How can I find the playlist index and do the same as in func prepare but in addButtonTapped(_ sender: Any)
I mean, there is no indexPath of the created playlist.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let cell = sender as? UICollectionViewCell, let indexPath = self.collectionView.indexPath(for: cell) {
let playlist = PlaylistController.shared.playlists[indexPath.item]
let ChannelVC = segue.destination as? ChannelsViewController
ChannelVC?.playlist = playlist
}
}
// MARK: Actions
@IBAction func addButtonTapped(_ sender: Any) {
let createPlaylist = UIAlertController(title: "", message: "", preferredStyle: .alert)
createPlaylist.addTextField()
createPlaylist.addTextField()
createPlaylist.addAction( UIAlertAction(title: "Save", style: .default) {
action in
guard let playlistName = createPlaylist.textFields![0].text,
let playlistURL = createPlaylist.textFields![1].text, !playlistName.isEmpty && !playlistURL.isEmpty else { return }
self.networkService.playlist(from: URL (string: playlistURL)!) { result in
switch result {
case .value(let Playlist):
DispatchQueue.main.async {
PlaylistController.shared.create(playlistWithName: playlistName, url: URL (string: playlistURL)!, content: Playlist.content)
let playlist = PlaylistController.shared.playlists[IndexPath]
extractChannelsFromString( Playlist.content, #PLAYLIST ID WHERE TO BE SAVED[indepath] )
// self.collectionView.reloadData()
}
case .error(let error):
print(error) //TODO:
}
}
self.collectionView.reloadData()
})
createPlaylist.addAction(UIAlertAction(title: "Cancel", style: .cancel))
present(createPlaylist, animated: true, completion: nil)
}