Skip to content

Instantly share code, notes, and snippets.

@KrisYu
Created February 15, 2018 04:27
Show Gist options
  • Save KrisYu/08156bc6b238e1556505990b3ce5dce9 to your computer and use it in GitHub Desktop.
Save KrisYu/08156bc6b238e1556505990b3ce5dce9 to your computer and use it in GitHub Desktop.
func addAlbumAtIndex(album: Album, index: Int) {
LibraryAPI.shared.addAlbum(album, at: index)
currentAlbumIndex = index
// everytime we change the albums, we need to reload the data and table.
allAlbums = LibraryAPI.shared.getAlbums()
horizontalScrollerView.reload()
showDataForAlbum(at: currentAlbumIndex)
}
@IBAction func deleteAlbum(_ sender: UIBarButtonItem) {
let deletedAlbum = allAlbums[currentAlbumIndex]
let undoAction = (deletedAlbum, currentAlbumIndex)
undoStack.insert(undoAction, at: 0)
LibraryAPI.shared.deleteAlbum(at: currentAlbumIndex)
allAlbums = LibraryAPI.shared.getAlbums()
horizontalScrollerView.reload()
showDataForAlbum(at: currentAlbumIndex)
undoBarButtonItem.isEnabled = true
if allAlbums.isEmpty {
trashBarButtonItem.isEnabled = false
}
}
@IBAction func undoAction(_ sender: UIBarButtonItem) {
if undoStack.count > 0 {
let (deletedAlbum, index) = undoStack.remove(at: 0)
addAlbumAtIndex(album: deletedAlbum, index: index)
}
if undoStack.count == 0 {
undoBarButtonItem.isEnabled = false
}
trashBarButtonItem.isEnabled = true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment