Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an option to only be notified when new tracks are played #7

Merged
merged 2 commits into from
Nov 22, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions Spotify Notifications.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@
ORGANIZATIONNAME = "Mihir Singh";
TargetAttributes = {
04A77B9E170FC75200A50173 = {
DevelopmentTeam = SH52BQ2ZXA;
DevelopmentTeam = ZB7877QUZP;
SystemCapabilities = {
com.apple.Sandbox = {
enabled = 0;
Expand Down Expand Up @@ -364,8 +364,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "Developer ID Application";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Mac Developer";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "";
COMBINE_HIDPI_IMAGES = YES;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Spotify Notifications/Spotify Notifications-Prefix.pch";
Expand All @@ -380,8 +379,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "Developer ID Application";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Mac Developer";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "";
COMBINE_HIDPI_IMAGES = YES;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Spotify Notifications/Spotify Notifications-Prefix.pch";
Expand Down
2 changes: 2 additions & 0 deletions Spotify Notifications/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
@property (strong, nonatomic) IBOutlet NSPopUpButton *soundToggle;
@property (strong, nonatomic) IBOutlet NSPopUpButton *iconToggle;
@property (strong, nonatomic) IBOutlet NSPopUpButton *startupToggle;
@property (strong, nonatomic) IBOutlet NSPopUpButton *showTracksToggle;
@property (nonatomic, weak) IBOutlet MASShortcutView *shortcutView;

- (IBAction)showHome:(id)sender;
Expand All @@ -25,5 +26,6 @@
- (IBAction)toggleIcons:(id)sender;
- (IBAction)toggleSound:(id)sender;
- (IBAction)toggleStartup:(id)sender;
- (IBAction)toggleShowTracks:(id)sender;

@end
68 changes: 41 additions & 27 deletions Spotify Notifications/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@ @implementation AppDelegate
@synthesize window;
@synthesize iconToggle;
@synthesize startupToggle;
@synthesize showTracksToggle;
@synthesize shortcutView;

NSString *artist;
NSString *track;
NSString *album;
NSImage *art;
NSString *lastTrackId;

- (void)applicationDidFinishLaunching:(NSNotification *)notification{


lastTrackId = @"";

[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];

[[NSDistributedNotificationCenter defaultCenter] addObserver:self
Expand Down Expand Up @@ -59,6 +63,7 @@ - (void)applicationDidFinishLaunching:(NSNotification *)notification{
[soundToggle selectItemAtIndex:[self getProperty:@"notificationSound"]];
[iconToggle selectItemAtIndex:[self getProperty:@"iconSelection"]];
[startupToggle selectItemAtIndex:[self getProperty:@"startupSelection"]];
[showTracksToggle selectItemAtIndex:[self getProperty:@"showTracks"]];

if ([self getProperty:@"startupSelection"] == 0){
[GBLaunchAtLogin addAppAsLoginItem];
Expand Down Expand Up @@ -109,36 +114,39 @@ - (void)eventOccured:(NSNotification *)notification{
track = [information objectForKey: @"Name"];

NSString *trackId = [information objectForKey:@"Track ID"];
if (trackId){
NSString *metaLoc = [NSString stringWithFormat:@"https://embed.spotify.com/oembed/?url=%@",trackId];
NSURL *metaReq = [NSURL URLWithString:metaLoc];
NSData *metaD = [NSData dataWithContentsOfURL:metaReq];

if (metaD){
NSError *error;
NSDictionary *meta = [NSJSONSerialization JSONObjectWithData:metaD options:NSJSONReadingAllowFragments error:&error];
NSURL *artReq = [NSURL URLWithString:[meta objectForKey:@"thumbnail_url"]];
NSData *artD = [NSData dataWithContentsOfURL:artReq];
if (artD)
art = [[NSImage alloc] initWithData:artD];
}
}


NSUserNotification *notification = [[NSUserNotification alloc] init];
notification.title = track;
notification.subtitle = album;
notification.informativeText = artist;
if (![lastTrackId isEqualToString:trackId] || [self getProperty:@"showTracks"] == 0) {
lastTrackId = trackId;
if (trackId){
NSString *metaLoc = [NSString stringWithFormat:@"https://embed.spotify.com/oembed/?url=%@",trackId];
NSURL *metaReq = [NSURL URLWithString:metaLoc];
NSData *metaD = [NSData dataWithContentsOfURL:metaReq];

if (metaD){
NSError *error;
NSDictionary *meta = [NSJSONSerialization JSONObjectWithData:metaD options:NSJSONReadingAllowFragments error:&error];
NSURL *artReq = [NSURL URLWithString:[meta objectForKey:@"thumbnail_url"]];
NSData *artD = [NSData dataWithContentsOfURL:artReq];
if (artD)
art = [[NSImage alloc] initWithData:artD];
}
}

if (art)
notification.contentImage = art;

if ([self getProperty:@"notificationSound"] == 0){
notification.soundName = NSUserNotificationDefaultSoundName;
}
NSUserNotification *notification = [[NSUserNotification alloc] init];
notification.title = track;
notification.subtitle = album;
notification.informativeText = artist;

if (art)
notification.contentImage = art;

[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
if ([self getProperty:@"notificationSound"] == 0){
notification.soundName = NSUserNotificationDefaultSoundName;
}

[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];

}
}
}

Expand All @@ -148,6 +156,12 @@ - (IBAction)toggleSound:(id)sender{

}

- (IBAction)toggleShowTracks:(id)sender{

[self saveProperty:@"showTracks" :(int)[showTracksToggle indexOfSelectedItem]];

}

- (IBAction)toggleStartup:(id)sender{

[self saveProperty:@"startupSelection" :(int)[startupToggle indexOfSelectedItem]];
Expand Down