Skip to content

Instantly share code, notes, and snippets.

@dennislysenko
Created April 23, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dennislysenko/6a7ff957f1ed2f7ca6f5 to your computer and use it in GitHub Desktop.
Save dennislysenko/6a7ff957f1ed2f7ca6f5 to your computer and use it in GitHub Desktop.
//
// SpotifyTabAdapter.m
// BeardedSpice
//
// Created by Dennis Lysenko on 4/4/15.
// Copyright (c) 2015 Tyler Rhodes / Jose Falcon. All rights reserved.
//
#import "SpotifyTabAdapter.h"
#import "runningSBApplication.h"
#import "NSString+Utils.h"
#import "MediaStrategy.h"
@implementation SpotifyTabAdapter
+ (instancetype)spotifyTabAdapterWithApplication:(runningSBApplication *)application
{
SpotifyTabAdapter *tab = [SpotifyTabAdapter new];
tab.application = application;
return tab;
}
- (NSString *)title{
@autoreleasepool {
SpotifyApplication *spotify = (SpotifyApplication *)[self.application sbApplication];
SpotifyTrack *currentTrack = [spotify currentTrack];
NSMutableString *title = [NSMutableString string];
if (currentTrack) {
if (![NSString isNullOrEmpty:currentTrack.name]) {
[title appendString:currentTrack.name];
}
if (![NSString isNullOrEmpty:currentTrack.artist]) {
[title appendFormat:@" - %@ ", currentTrack.artist];
}
}
return [NSString stringWithFormat:@"%@(%@)", title, @"Spotify"];
}
}
- (NSString *)URL
{
return @"Spotify";
}
// We have only one window.
- (NSString *)key
{
return [(SpotifyApplication *)self.application.sbApplication name];
}
// We have only one window.
-(BOOL) isEqual:(__autoreleasing id)otherTab{
if (otherTab == nil || ![otherTab isKindOfClass:[SpotifyTabAdapter class]]) return NO;
return YES;
}
- (void)activateTab{
@autoreleasepool {
if (![(SpotifyApplication *)self.application.sbApplication frontmost]) {
[self.application activate];
_wasActivated = YES;
}
else
_wasActivated = NO;
}
}
- (void)toggleTab{
if ([(SpotifyApplication *)self.application.sbApplication frontmost]){
if (_wasActivated) {
[self.application hide];
_wasActivated = NO;
}
}
else
[self activateTab];
}
- (BOOL)frontmost{
return self.application.frontmost;
}
- (id)executeJavascript:(NSString *)javascript{
return nil;
}
//////////////////////////////////////////////////////////////
#pragma mark Player control methods
//////////////////////////////////////////////////////////////
- (void)toggle{
SpotifyApplication *spotify = (SpotifyApplication *)[self.application sbApplication];
if (spotify) {
[spotify playpause];
}
}
- (void)pause{
SpotifyApplication *spotify = (SpotifyApplication *)[self.application sbApplication];
if (spotify) {
[spotify pause];
}
}
- (void)next{
SpotifyApplication *spotify = (SpotifyApplication *)[self.application sbApplication];
if (spotify) {
[spotify nextTrack];
}
}
- (void)previous{
SpotifyApplication *spotify = (SpotifyApplication *)[self.application sbApplication];
if (spotify) {
[spotify previousTrack];
}
}
- (void)favorite{
// SpotifyApplication *spotify = (SpotifyApplication *)[self.application sbApplication];
// if (spotify) {
// SpotifyTrack *track = [[spotify currentTrack] get];
// if (track.starred)
// track.rating = 0;
// else
// track.rating = 100;
// }
NSLog(@"Favoriting not supported for Spotify");
}
- (Track *)trackInfo{
SpotifyApplication *spotify = (SpotifyApplication *)[self.application sbApplication];
if (spotify) {
SpotifyTrack *spotifyTrack = [[spotify currentTrack] get];
Track *track = [Track new];
track.track = spotifyTrack.name;
track.album = spotifyTrack.album;
track.artist = spotifyTrack.artist;
track.image = [spotifyTrack artwork];
track.favorited = @(spotifyTrack.starred);
return track;
}
return nil;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment