Skip to content

Commit

Permalink
Allows deleting multiple downloads at once in downloads window, fixes…
Browse files Browse the repository at this point in the history
… crash with delete pressed while no item selected (Closes #157)
  • Loading branch information
insidegui committed Jun 20, 2016
1 parent a195f6e commit a725971
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
4 changes: 2 additions & 2 deletions WWDC/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15G12a" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15F34" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="10117"/>
</dependencies>
Expand Down Expand Up @@ -130,7 +130,7 @@
<action selector="paste:" target="Ady-hI-5gd" id="UvS-8e-Qdg"/>
</connections>
</menuItem>
<menuItem title="Delete" id="pa3-QI-u2k">
<menuItem title="Delete" tag="1077" id="pa3-QI-u2k">
<string key="keyEquivalent" base64-UTF8="YES">
CA
</string>
Expand Down
41 changes: 29 additions & 12 deletions WWDC/DownloadListWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,6 @@ class DownloadListWindowController: NSWindowController, NSTableViewDelegate, NST
}
}
}
self.downloadCancelledHndl = nc.addObserverForName(VideoStoreNotificationDownloadCancelled, object: nil, queue: NSOperationQueue.mainQueue()) { note in
if let object = note.object as? String {
let url = object as String
let (item, _) = self.listItemForURL(url)
if item != nil {
self.items.remove(item!)
self.tableView.removeRowsAtIndexes(NSIndexSet(index: self.tableView.selectedRow), withAnimation: .EffectGap)
}
}
}
self.downloadPausedHndl = nc.addObserverForName(VideoStoreNotificationDownloadPaused, object: nil, queue: NSOperationQueue.mainQueue()) { note in
if let object = note.object as? String {
let url = object as String
Expand All @@ -137,6 +127,9 @@ class DownloadListWindowController: NSWindowController, NSTableViewDelegate, NST
}
}
}
self.downloadCancelledHndl = nc.addObserverForName(VideoStoreNotificationDownloadCancelled, object: nil, queue: NSOperationQueue.mainQueue()) { note in
self.populateDownloadItems()
}

populateDownloadItems()
}
Expand Down Expand Up @@ -250,8 +243,32 @@ class DownloadListWindowController: NSWindowController, NSTableViewDelegate, NST
}

func delete(sender: AnyObject?) {
let item = self.items[tableView.selectedRow]
self.videoStore.cancelDownload(item.url)
guard tableView.selectedRowIndexes.count > 0 else { return }

var downloadsToCancel = [String]()

tableView.selectedRowIndexes.enumerateIndexesUsingBlock { index, _ in
downloadsToCancel.append(self.items[index].url)
}

downloadsToCancel.forEach { self.videoStore.cancelDownload($0) }
}

// MARK: Menu Validation

private enum MenuItemTags: Int {
case Delete = 1077
}

override func validateMenuItem(menuItem: NSMenuItem) -> Bool {
guard let item = MenuItemTags(rawValue: menuItem.tag) else {
return super.validateMenuItem(menuItem)
}

switch item {
case .Delete:
return tableView.selectedRowIndexes.count > 0
}
}

}
2 changes: 1 addition & 1 deletion WWDC/DownloadListWindowController.xib
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<rect key="frame" x="1" y="17" width="238" height="117"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView verticalHuggingPriority="750" allowsExpansionToolTips="YES" columnAutoresizingStyle="firstColumnOnly" alternatingRowBackgroundColors="YES" columnReordering="NO" columnSelection="YES" multipleSelection="NO" autosaveColumns="NO" rowHeight="51" rowSizeStyle="automatic" viewBased="YES" id="9xt-Zc-hak">
<tableView verticalHuggingPriority="750" allowsExpansionToolTips="YES" columnAutoresizingStyle="firstColumnOnly" alternatingRowBackgroundColors="YES" columnReordering="NO" columnSelection="YES" autosaveColumns="NO" rowHeight="51" rowSizeStyle="automatic" viewBased="YES" id="9xt-Zc-hak">
<rect key="frame" x="0.0" y="0.0" width="482" height="0.0"/>
<autoresizingMask key="autoresizingMask"/>
<size key="intercellSpacing" width="10" height="3"/>
Expand Down

0 comments on commit a725971

Please sign in to comment.