Skip to content

Commit

Permalink
Features and cleanup
Browse files Browse the repository at this point in the history
- Volume works
- Artwork updates when playback state changes
- Views are tied to properties now, instead of being manually filtered
  • Loading branch information
Ellis Kenyo committed Aug 21, 2016
1 parent f931a65 commit 1e37ada
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 52 deletions.
98 changes: 48 additions & 50 deletions SpotifyTicker/PopoverController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,77 +14,70 @@ class PopoverController: NSViewController {
var repeatBox: NSButton!
var spotify: SpotifyController!

var currentArtwork: NSString!
var currentArtwork: NSString! = "";

var imageView: NSImageView!
@IBOutlet weak var artistLabel: NSTextField!
@IBOutlet weak var songLabel: NSTextField!
@IBOutlet weak var albumLabel: NSTextField!

@IBOutlet weak var imageView: NSImageView!

@IBOutlet weak var playPauseButton: NSButton!
@IBOutlet weak var repeatButton: NSButton!
@IBOutlet weak var shuffleButton: NSButton!

@IBOutlet weak var volumeSlider: NSSlider!
@IBOutlet weak var volumeLabel: NSTextField!

var playPauseButton: NSButton!
var shuffleButton: NSButton!
var repeatButton: NSButton!

override func viewDidLoad() {
super.viewDidLoad();
NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: #selector(timerDidFire), userInfo: nil, repeats: true);
imageView = view.subviews.filter{$0 is NSImageView}[0] as! NSImageView;
spotify = SpotifyController();
let id = spotify.currentTrack().id!().characters.split{ $0 == ":" }.map(String.init)[2];
// downloadArtwork(currentArtwork);
if let url = NSURL(string: "https://api.spotify.com/v1/tracks/\(id)"){
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "GET"

let session = NSURLSession.sharedSession()
session.dataTaskWithRequest(request, completionHandler: { (returnData, response, error) -> Void in
do {
let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(returnData!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
print(jsonResult["album"]!["images"]!!);
self.downloadArtwork(jsonResult["album"]!["images"]!![0]["url"]!! as! NSString);
} catch {
print("Error parsing.");
}

}).resume()
}

updateView();
}

/**
Update the relevant view items. Could be lazier.
*/
func updateView() {
let buttons: [NSButton] = view.subviews.filter{$0 is NSButton} as! [NSButton];
let labels: [NSTextField] = view.subviews.filter{$0 is NSTextField} as! [NSTextField];
for button in buttons {
if button.tag == 2 {
playPauseButton = button;
button.image = NSImage(named: spotify.isPlaying() ? "pauseTemplate" : "playTemplate");
} else if button.tag == 4 {
repeatButton = button;
updateRepeatStatus();
} else if button.tag == 5 {
shuffleButton = button;
updateShuffleStatus();
}
}
artistLabel.stringValue = spotify.currentTrack().artist!;
albumLabel.stringValue = spotify.currentTrack().album!;
songLabel.stringValue = spotify.currentTrack().name!;

for label in labels {
if label.tag == 1 {
label.stringValue = spotify.currentTrack().artist!;
} else if label.tag == 2 {
label.stringValue = spotify.currentTrack().name!;
} else if label.tag == 3 {
label.stringValue = spotify.currentTrack().album!;
}
}
playPauseButton.image = NSImage(named: spotify.isPlaying() ? "pauseTemplate" : "playTemplate");

volumeLabel.stringValue = "Volume: \(spotify.volume()) %";
volumeSlider.integerValue = spotify.volume();
updateShuffleStatus();
updateRepeatStatus();
updateArtwork();
}

func timerDidFire() {
updateView();
}

func updateArtwork() {
if !currentArtwork.isEqualToString(spotify.artworkURL()) {
downloadArtwork(spotify.artworkURL());
let id = spotify.currentTrack().id!().characters.split{ $0 == ":" }.map(String.init)[2];
if let url = NSURL(string: "https://api.spotify.com/v1/tracks/\(id)"){
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "GET"

let session = NSURLSession.sharedSession()
session.dataTaskWithRequest(request, completionHandler: { (returnData, response, error) -> Void in
do {
let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(returnData!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
let result = jsonResult["album"]!["images"]!![0]["url"]!! as! NSString;
if !self.currentArtwork.isEqualToString(result as String) {
self.downloadArtwork(result);
self.currentArtwork = result;
}
} catch {
print("Error parsing.");
}

}).resume()
}
}

Expand Down Expand Up @@ -149,4 +142,9 @@ class PopoverController: NSViewController {
playPauseButton.image = NSImage(named: "pauseTemplate");
}
}

@IBAction func sliderChange(sender: NSSliderCell) {
volumeLabel.stringValue = "\(sender.intValue) %";
spotify.setVolume(sender.integerValue);
}
}
32 changes: 30 additions & 2 deletions SpotifyTicker/PopoverController.xib
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="PopoverController" customModule="SpotifyTicker" customModuleProvider="target">
<connections>
<outlet property="albumLabel" destination="Qiy-lv-KbP" id="kvB-my-uGA"/>
<outlet property="artistLabel" destination="SPA-hU-1QL" id="D3t-v9-MM6"/>
<outlet property="imageView" destination="0BS-Cd-Y84" id="ueM-We-Q6X"/>
<outlet property="playPauseButton" destination="7B1-Fu-jrz" id="h1i-gJ-S2g"/>
<outlet property="repeatButton" destination="RD2-IW-9Ft" id="1P0-ve-nFq"/>
<outlet property="shuffleButton" destination="d9c-OM-d5j" id="1tY-u9-COS"/>
<outlet property="songLabel" destination="Zji-Ab-QY9" id="WdM-zT-AUv"/>
<outlet property="view" destination="Hz6-mo-xeY" id="0bl-1N-x8E"/>
<outlet property="volumeLabel" destination="Mun-iR-RQ3" id="uld-HU-x27"/>
<outlet property="volumeSlider" destination="ggg-iG-LeF" id="SBn-qU-ihe"/>
</connections>
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
Expand Down Expand Up @@ -60,6 +69,7 @@
</buttonCell>
<connections>
<action selector="repeatChecked:" target="-2" id="jdb-KZ-GPZ"/>
<binding destination="-2" name="image" keyPath="self.repeatButton" id="EOE-PV-CfO"/>
</connections>
</button>
<button fixedFrame="YES" tag="5" translatesAutoresizingMaskIntoConstraints="NO" id="d9c-OM-d5j">
Expand All @@ -70,6 +80,7 @@
</buttonCell>
<connections>
<action selector="shuffleChecked:" target="-2" id="yF0-D9-Nbo"/>
<binding destination="-2" name="image" keyPath="self.shuffleButton" id="5SR-rf-RvV"/>
</connections>
</button>
<imageView horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="0BS-Cd-Y84">
Expand All @@ -93,8 +104,8 @@
</textFieldCell>
</textField>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="PlJ-Sk-X3P">
<rect key="frame" x="224" y="214" width="39" height="17"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="Song:" id="Ceo-ga-WFX">
<rect key="frame" x="224" y="214" width="36" height="17"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="Song" id="Ceo-ga-WFX">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
Expand Down Expand Up @@ -124,9 +135,26 @@
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<slider verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="ggg-iG-LeF">
<rect key="frame" x="224" y="101" width="187" height="21"/>
<sliderCell key="cell" state="on" alignment="left" maxValue="100" doubleValue="15.963855421686745" tickMarkPosition="above" sliderType="linear" id="iYj-Kq-i65">
<connections>
<action selector="sliderChange:" target="-2" id="KU3-iA-SFo"/>
</connections>
</sliderCell>
</slider>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Mun-iR-RQ3">
<rect key="frame" x="273" y="83" width="89" height="17"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="Volume: 100%" id="VXm-CR-Yue">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
</subviews>
<point key="canvasLocation" x="-25.5" y="31"/>
</customView>
<userDefaultsController representsSharedInstance="YES" id="ZsR-el-xjP"/>
</objects>
<resources>
<image name="forwardTemplate" width="50" height="50"/>
Expand Down
8 changes: 8 additions & 0 deletions SpotifyTicker/Spotify.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,12 @@ class SpotifyController {
func play() {
spotify.play!();
}

func volume() -> Int {
return spotify.soundVolume!;
}

func setVolume(volume: Int) {
spotify.setSoundVolume!(volume);
}
}

0 comments on commit 1e37ada

Please sign in to comment.