Skip to content

Commit

Permalink
wire up menu bar actions to dir controller
Browse files Browse the repository at this point in the history
  • Loading branch information
NattyNarwhal committed Oct 25, 2024
1 parent c910a03 commit e320923
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 11 deletions.
18 changes: 11 additions & 7 deletions Submariner/SBServerDirectoryController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,20 @@ fileprivate let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, catego
}
}

var _selectedMusicItems: [SBStarrable] = []
override var selectedMusicItems: [any SBStarrable]! {
self._selectedMusicItems
var _selectedDirectories: [SBDirectory] = []
override var selectedDirectories: [SBDirectory]! {
self._selectedDirectories
}

var _selectedTracks: [SBTrack] = []
override var selectedTracks: [SBTrack]! {
self._selectedTracks
}

override var selectedMusicItems: [any SBStarrable]! {
self.selectedDirectories + self.selectedTracks
}

// #MARK: - Actions

// FIXME: duplicated in server user view controller
Expand Down Expand Up @@ -293,19 +297,19 @@ fileprivate let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, catego
// In the future, it would be nice to show directory info in the inspector.
if let directory = newValue.first as? SBDirectory, let id = directory.itemId {
serverDirectoryController._selectedTracks = []
serverDirectoryController._selectedMusicItems = [directory]
serverDirectoryController._selectedDirectories = [directory]
directory.server?.getServerDirectory(id: id)
children = directory.children
NotificationCenter.default.post(name: .SBTrackSelectionChanged, object: [])
} else if newValue.isEmpty {
// use ourselves for current selection, so favourites menu works
serverDirectoryController._selectedTracks = []
serverDirectoryController._selectedMusicItems = [directory]
serverDirectoryController._selectedDirectories = [directory]
NotificationCenter.default.post(name: .SBTrackSelectionChanged, object: [])
} else if let tracks = newValue as? Set<SBTrack> {
let tracksArray = Array(tracks)
serverDirectoryController._selectedTracks = tracksArray
serverDirectoryController._selectedMusicItems = tracksArray
serverDirectoryController._selectedDirectories = []
NotificationCenter.default.post(name: .SBTrackSelectionChanged, object: tracksArray)
}
}
Expand Down Expand Up @@ -446,7 +450,7 @@ fileprivate let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, catego
.onChange(of: selected) { newValue in
// We're not doing anything with this in leftmost
self.serverDirectoryController._selectedTracks = []
self.serverDirectoryController._selectedMusicItems = selected != nil ? [selected!] : []
self.serverDirectoryController._selectedDirectories = selected != nil ? [selected!] : []
NotificationCenter.default.post(name: .SBTrackSelectionChanged, object: [])
if let directory = newValue, let id = directory.itemId {
directory.server?.getServerDirectory(id: id)
Expand Down
3 changes: 2 additions & 1 deletion Submariner/SBViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

// forward declare since importing is not good here
@class SBDatabaseController;
@class SBTrack, SBAlbum, SBArtist;
@class SBTrack, SBAlbum, SBArtist, SBDirectory;
@protocol SBStarrable;

@interface SBViewController : NSViewController<NSUserInterfaceValidations> {
Expand All @@ -60,6 +60,7 @@
@property (nonatomic, strong, readonly) NSArray<SBTrack*> *selectedTracks;
@property (nonatomic, strong, readonly) NSArray<SBAlbum*> *selectedAlbums;
@property (nonatomic, strong, readonly) NSArray<SBArtist*> *selectedArtists;
@property (nonatomic, strong, readonly) NSArray<SBDirectory*> *selectedDirectories;
@property (nonatomic, strong, readonly) NSArray<id<SBStarrable>> *selectedMusicItems;
@property (strong, readwrite) NSArray<NSSortDescriptor*> *trackSortDescriptor;

Expand Down
78 changes: 75 additions & 3 deletions Submariner/SBViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ - (NSInteger)selectedTrackRow {
}


- (NSArray<SBDirectory*>*)selectedDirectories {
return @[];
}


- (NSArray<id<SBStarrable>>*)selectedMusicItems {
return [self selectedTracks];
}
Expand All @@ -107,12 +112,23 @@ - (IBAction)albumDoubleClick:(id)sender {
}
}

- (IBAction)playDirectory:(id)sender {
SBDirectory *directory = self.selectedDirectories.firstObject;

if (directory != nil) {
NSArray<SBTrack*> *tracks = [self recursiveTracksFromDirectory: directory];
[[SBPlayer sharedInstance] playTracks:[tracks sortedArrayUsingDescriptors: self.trackSortDescriptor] startingAt:0];
}
}

- (IBAction)playSelected:(id)sender {
SBSelectedItemType itemType = [self selectedItemType];
if (itemType == SBSelectedItemTypeAlbum) {
[self albumDoubleClick: sender];
} else if (itemType == SBSelectedItemTypeTrack) {
[self trackDoubleClick: sender];
} else if (itemType == SBSelectedItemTypeDirectory) {
[self playDirectory: sender];
}
}

Expand All @@ -123,6 +139,16 @@ - (IBAction)playFirstDiscFromAlbum:(id)sender {

#pragma mark Add to Tracklist

- (IBAction)addDirectoryToTracklist:(id)sender {
SBDirectory *directory = self.selectedDirectories.firstObject;

if (directory != nil) {
NSArray<SBTrack*> *tracks = [self recursiveTracksFromDirectory: directory];
[[SBPlayer sharedInstance] addTrackArray:tracks replace:NO];
}
}


- (IBAction)addArtistToTracklist:(id)sender {
NSMutableArray *tracks = [NSMutableArray array];
for (SBArtist *artist in self.selectedArtists) {
Expand Down Expand Up @@ -152,6 +178,8 @@ - (IBAction)addSelectedToTracklist:(id)sender {
[self addAlbumToTracklist: sender];
} else if (itemType == SBSelectedItemTypeTrack) {
[self addTrackToTracklist: sender];
} else if (itemType == SBSelectedItemTypeDirectory) {
[self addDirectoryToTracklist: sender];
}
}

Expand All @@ -174,6 +202,22 @@ - (IBAction)createNewLocalPlaylistWithSelectedTracks:(id)sender {

#pragma mark Downloading

- (IBAction)downloadDirectory:(id)sender {
SBDirectory *directory = self.selectedDirectories.firstObject;

if (directory != nil) {
NSArray<SBTrack*> *tracks = [self recursiveTracksFromDirectory: directory];

for (SBTrack *track in tracks) {
SBSubsonicDownloadOperation *op = [[SBSubsonicDownloadOperation alloc]
initWithManagedObjectContext: self.managedObjectContext
trackID: [track objectID]];

[[NSOperationQueue sharedDownloadQueue] addOperation:op];
}
}
}

- (IBAction)downloadTrack:(id)sender {
[self downloadTracks: self.selectedTracks databaseController: self.databaseController];
}
Expand Down Expand Up @@ -201,6 +245,8 @@ - (IBAction)downloadSelected:(id)sender {
[self downloadAlbum: sender];
} else if (itemType == SBSelectedItemTypeTrack) {
[self downloadTrack: sender];
} else if (itemType == SBSelectedItemTypeDirectory) {
[self downloadDirectory: sender];
}
}

Expand Down Expand Up @@ -404,6 +450,21 @@ - (void)createLocalPlaylistWithSelected:(NSArray<SBTrack*>*)trackList databaseCo
return [sortedTracks objectsAtIndexes: trackIndices];
}

- (NSArray<SBTrack*>*) recursiveTracksFromDirectory:(SBDirectory*) directory {
NSMutableArray<SBTrack*> *tracks = [[NSMutableArray alloc] init];
NSArray<SBMusicItem*>* children = [directory children];
for (SBMusicItem *item in children) {
if ([item isKindOfClass: SBTrack.class]) {
[tracks addObject: (SBTrack*)item];
} else if ([item isKindOfClass: SBDirectory.class]) {
SBDirectory *child = (SBDirectory*)item;
NSArray<SBTrack*> *childTracks = [self recursiveTracksFromDirectory: child];
[tracks addObjectsFromArray: childTracks];
}
}
return tracks;
}

- (SBSelectedItemType) selectedItemType {
NSObject<SBStarrable> *first = self.selectedMusicItems.firstObject;
if ([first isKindOfClass: SBArtist.class]) {
Expand All @@ -412,6 +473,8 @@ - (SBSelectedItemType) selectedItemType {
return SBSelectedItemTypeAlbum;
} else if ([first isKindOfClass: SBTrack.class]) {
return SBSelectedItemTypeTrack;
} else if ([first isKindOfClass: SBDirectory.class]) {
return SBSelectedItemTypeDirectory;
}
return SBSelectedItemTypeNone;
}
Expand All @@ -424,23 +487,30 @@ - (BOOL)validateUserInterfaceItem: (id<NSValidatedUserInterfaceItem>) item {
NSInteger artistsSelected = self.selectedArtists.count;
NSInteger albumSelected = self.selectedAlbums.count;
NSInteger tracksSelected = self.selectedTracks.count;
NSInteger directoriesSelected = self.selectedDirectories.count;

SBSelectedItemType selectedItemType = [self selectedItemType];
BOOL tracksActive = selectedItemType == SBSelectedItemTypeTrack;
BOOL albumsActive = selectedItemType == SBSelectedItemTypeAlbum;
BOOL artistsActive = selectedItemType == SBSelectedItemTypeArtist;
BOOL directoriesActive = selectedItemType == SBSelectedItemTypeDirectory;

SBSelectedRowStatus selectedTrackRowStatus = 0;
if (tracksActive) {
selectedTrackRowStatus = [self selectedRowStatus: self.selectedTracks];
}

if (action == @selector(playSelected:)) {
return (albumSelected > 0 && albumsActive) || (tracksSelected > 0 && tracksActive);
return (albumSelected > 0 && albumsActive)
|| (tracksSelected > 0 && tracksActive)
|| (directoriesSelected > 0 && directoriesActive);
}

if (action == @selector(addSelectedToTracklist:)) {
return (albumSelected > 0 && albumsActive) || (tracksSelected > 0 && tracksActive) || (artistsSelected > 0 && artistsActive);
return (albumSelected > 0 && albumsActive)
|| (tracksSelected > 0 && tracksActive)
|| (artistsSelected > 0 && artistsActive)
|| (directoriesSelected > 0 && directoriesActive);
}

if (action == @selector(trackDoubleClick:)
Expand All @@ -462,7 +532,9 @@ - (BOOL)validateUserInterfaceItem: (id<NSValidatedUserInterfaceItem>) item {
}

if (action == @selector(downloadSelected:)) {
return (selectedTrackRowStatus & SBSelectedRowDownloadable) || (albumSelected > 0 && albumsActive);
return (selectedTrackRowStatus & SBSelectedRowDownloadable)
|| (albumSelected > 0 && albumsActive)
|| (directoriesSelected > 0 && directoriesActive);
}

// for context menus
Expand Down

0 comments on commit e320923

Please sign in to comment.