Skip to content

Commit

Permalink
Unify play album code
Browse files Browse the repository at this point in the history
weird place in SBPlayer for now. also means double-clicking the album
will respect your pref on replacing
  • Loading branch information
NattyNarwhal committed Sep 2, 2023
1 parent 962c1b7 commit c283384
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 132 deletions.
27 changes: 2 additions & 25 deletions Submariner/SBMusicController.m
Original file line number Diff line number Diff line change
Expand Up @@ -227,22 +227,7 @@ - (IBAction)filterArtist:(id)sender {
- (IBAction)trackDoubleClick:(id)sender {
NSInteger selectedRow = [tracksTableView selectedRow];
if(selectedRow != -1) {
SBTrack *clickedTrack = [[tracksController arrangedObjects] objectAtIndex:selectedRow];
if(clickedTrack) {

// stop current playing tracks
[[SBPlayer sharedInstance] stop];

// add track to player
if([[NSUserDefaults standardUserDefaults] integerForKey:@"playerBehavior"] == 1) {
[[SBPlayer sharedInstance] addTrackArray:[tracksController arrangedObjects] replace:YES];
// play track
[[SBPlayer sharedInstance] playTrack:clickedTrack];
} else {
[[SBPlayer sharedInstance] addTrackArray:[tracksController arrangedObjects] replace:NO];
[[SBPlayer sharedInstance] playTrack:clickedTrack];
}
}
[[SBPlayer sharedInstance] playTracks: [tracksController arrangedObjects] startingAt: selectedRow];
}
}

Expand All @@ -255,15 +240,7 @@ - (IBAction)albumDoubleClick:(id)sender {
if(doubleClickedAlbum) {

NSArray *tracks = [doubleClickedAlbum.tracks sortedArrayUsingDescriptors:trackSortDescriptor];

// stop current playing tracks
[[SBPlayer sharedInstance] stop];

// add track to player
[[SBPlayer sharedInstance] addTrackArray:tracks replace:YES];

// play track
[[SBPlayer sharedInstance] playTracklistAtBeginning];
[[SBPlayer sharedInstance] playTracks: tracks startingAt: 0];
}
}
}
Expand Down
19 changes: 2 additions & 17 deletions Submariner/SBMusicSearchController.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,23 +101,8 @@ - (void)searchString:(NSString *)query {

- (IBAction)trackDoubleClick:(id)sender {
NSInteger selectedRow = [tracksTableView selectedRow];
if(selectedRow != -1) {
SBTrack *clickedTrack = [[tracksController arrangedObjects] objectAtIndex:selectedRow];
if(clickedTrack) {

// stop current playing tracks
[[SBPlayer sharedInstance] stop];

// add track to player
if([[NSUserDefaults standardUserDefaults] integerForKey:@"playerBehavior"] == 1) {
[[SBPlayer sharedInstance] addTrackArray:[tracksController arrangedObjects] replace:YES];
// play track
[[SBPlayer sharedInstance] playTrack:clickedTrack];
} else {
[[SBPlayer sharedInstance] addTrackArray:[tracksController arrangedObjects] replace:NO];
[[SBPlayer sharedInstance] playTrack:clickedTrack];
}
}
if (selectedRow != -1) {
[[SBPlayer sharedInstance] playTracks: [tracksController arrangedObjects] startingAt: selectedRow];
}
}

Expand Down
20 changes: 20 additions & 0 deletions Submariner/SBPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,26 @@ extension NSNotification.Name {
NotificationCenter.default.post(name: .SBPlayerPlaylistUpdated, object: self)
}

// #MARK: - Playlist+Playback Frontend Helpers

/// This function is mostly used by the frontend to replace a common pattern in the UI for playing albums.
///
/// That is,
/// 1. it replaces or appends a bunch of tracks to the tracklist, depending on preferences
/// 2. it starts playing at a certain track based on user feedback
@objc(playTracks:startingAt:) func play(tracks: [SBTrack], startingAt: Int) {
self.stop()

if UserDefaults.standard.playerBehavior == 1 /* replace */ {
self.add(tracks: tracks, replace: true)
self.play(index: startingAt)
} else {
let beforeCount = playlist.count
self.add(tracks: tracks, replace: false)
self.play(index: beforeCount + startingAt)
}
}

// #MARK: - Player Control

@objc dynamic var currentTrack: SBTrack? {
Expand Down
17 changes: 1 addition & 16 deletions Submariner/SBPlaylistController.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,7 @@ - (void)clearPlaylist {
- (IBAction)trackDoubleClick:(id)sender {
NSInteger selectedRow = [tracksTableView selectedRow];
if(selectedRow != -1) {
SBTrack *clickedTrack = [[tracksController arrangedObjects] objectAtIndex:selectedRow];
if(clickedTrack) {

// stop current playing tracks
[[SBPlayer sharedInstance] stop];

// add track to player
if([[NSUserDefaults standardUserDefaults] integerForKey:@"playerBehavior"] == 1) {
[[SBPlayer sharedInstance] addTrackArray:[tracksController arrangedObjects] replace:YES];
// play track
[[SBPlayer sharedInstance] playTrack:clickedTrack];
} else {
[[SBPlayer sharedInstance] addTrackArray:[tracksController arrangedObjects] replace:NO];
[[SBPlayer sharedInstance] playTrack:clickedTrack];
}
}
[[SBPlayer sharedInstance] playTracks: [tracksController arrangedObjects] startingAt: selectedRow];
}
}

Expand Down
31 changes: 2 additions & 29 deletions Submariner/SBServerHomeController.m
Original file line number Diff line number Diff line change
Expand Up @@ -186,22 +186,7 @@ - (void) reloadServersWithIdentifier: (NSString*)identifier {
- (IBAction)trackDoubleClick:(id)sender {
NSInteger selectedRow = [tracksTableView selectedRow];
if(selectedRow != -1) {
SBTrack *clickedTrack = [[tracksController arrangedObjects] objectAtIndex:selectedRow];
if(clickedTrack) {

// stop current playing tracks
//[[SBPlayer sharedInstance] stop];

// add track to player
if([[NSUserDefaults standardUserDefaults] integerForKey:@"playerBehavior"] == 1) {
[[SBPlayer sharedInstance] addTrackArray:[tracksController arrangedObjects] replace:YES];
// play track
[[SBPlayer sharedInstance] playTrack:clickedTrack];
} else {
[[SBPlayer sharedInstance] addTrackArray:[tracksController arrangedObjects] replace:NO];
[[SBPlayer sharedInstance] playTrack:clickedTrack];
}
}
[[SBPlayer sharedInstance] playTracks: [tracksController arrangedObjects] startingAt: selectedRow];
}
}

Expand All @@ -213,19 +198,7 @@ - (IBAction)albumDoubleClick:(id)sender {
if(doubleClickedAlbum) {

NSArray *tracks = [doubleClickedAlbum.tracks sortedArrayUsingDescriptors:trackSortDescriptor];

// stop current playing tracks
//[[SBPlayer sharedInstance] stop];

// add track to player
if([[NSUserDefaults standardUserDefaults] integerForKey:@"playerBehavior"] == 1) {
[[SBPlayer sharedInstance] addTrackArray:tracks replace:YES];
// play track
[[SBPlayer sharedInstance] playTracklistAtBeginning];
} else {
[[SBPlayer sharedInstance] addTrackArray:tracks replace:NO];
[[SBPlayer sharedInstance] playTracklistAtBeginning];
}
[[SBPlayer sharedInstance] playTracks: tracks startingAt: 0];
}
}
}
Expand Down
31 changes: 2 additions & 29 deletions Submariner/SBServerLibraryController.m
Original file line number Diff line number Diff line change
Expand Up @@ -346,22 +346,7 @@ - (IBAction)filterArtist:(id)sender {
- (IBAction)trackDoubleClick:(id)sender {
NSInteger selectedRow = [tracksTableView selectedRow];
if(selectedRow != -1) {
SBTrack *clickedTrack = [[tracksController arrangedObjects] objectAtIndex:selectedRow];
if(clickedTrack) {

// stop current playing tracks
//[[SBPlayer sharedInstance] stop];

// add track to player
if([[NSUserDefaults standardUserDefaults] integerForKey:@"playerBehavior"] == 1) {
[[SBPlayer sharedInstance] addTrackArray:[tracksController arrangedObjects] replace:YES];
// play track
[[SBPlayer sharedInstance] playTrack:clickedTrack];
} else {
[[SBPlayer sharedInstance] addTrackArray:[tracksController arrangedObjects] replace:NO];
[[SBPlayer sharedInstance] playTrack:clickedTrack];
}
}
[[SBPlayer sharedInstance] playTracks: [tracksController arrangedObjects] startingAt: selectedRow];
}
}

Expand All @@ -373,19 +358,7 @@ - (IBAction)albumDoubleClick:(id)sender {
if(doubleClickedAlbum) {

NSArray *tracks = [doubleClickedAlbum.tracks sortedArrayUsingDescriptors:trackSortDescriptor];

// stop current playing tracks
//[[SBPlayer sharedInstance] stop];

// add track to player
if([[NSUserDefaults standardUserDefaults] integerForKey:@"playerBehavior"] == 1) {
[[SBPlayer sharedInstance] addTrackArray:tracks replace:YES];
// play track
[[SBPlayer sharedInstance] playTracklistAtBeginning];
} else {
[[SBPlayer sharedInstance] addTrackArray:tracks replace:NO];
[[SBPlayer sharedInstance] playTracklistAtBeginning];
}
[[SBPlayer sharedInstance] playTracks: tracks startingAt: 0];
}
}
}
Expand Down
17 changes: 1 addition & 16 deletions Submariner/SBServerSearchController.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,7 @@ - (void)subsonicSearchResultUpdatedNotification:(NSNotification *)notification {
- (IBAction)trackDoubleClick:(id)sender {
NSInteger selectedRow = [tracksTableView selectedRow];
if(selectedRow != -1) {
SBTrack *clickedTrack = [[tracksController arrangedObjects] objectAtIndex:selectedRow];
if(clickedTrack) {

// stop current playing tracks
[[SBPlayer sharedInstance] stop];

// add track to player
if([[NSUserDefaults standardUserDefaults] integerForKey:@"playerBehavior"] == 1) {
[[SBPlayer sharedInstance] addTrackArray:[tracksController arrangedObjects] replace:YES];
// play track
[[SBPlayer sharedInstance] playTrack:clickedTrack];
} else {
[[SBPlayer sharedInstance] addTrackArray:[tracksController arrangedObjects] replace:NO];
[[SBPlayer sharedInstance] playTrack:clickedTrack];
}
}
[[SBPlayer sharedInstance] playTracks: [tracksController arrangedObjects] startingAt: selectedRow];
}
}

Expand Down
4 changes: 4 additions & 0 deletions Submariner/UserDefaults+Submariner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ extension UserDefaults {
@objc dynamic var scrobbleToServer: Bool {
return bool(forKey: "scrobbleToServer")
}

@objc dynamic var playerBehavior: Int {
return integer(forKey: "playerBehavior")
}
}

0 comments on commit c283384

Please sign in to comment.