Improve FSVolumesDataSource to work better with new API

This commit is contained in:
Allan Odgaard
2015-07-15 22:09:17 +02:00
parent a9c22cf803
commit c151465d98

View File

@@ -5,7 +5,39 @@
#import <oak/oak.h>
#import <oak/debug.h>
@implementation FSVolumesDataSource { OBJC_WATCH_LEAKS(FSVolumesDataSource); }
@interface FSVolumeListItem : FSItem
@property (nonatomic, weak) FSDataSource* dataSource;
@end
@implementation FSVolumeListItem
- (id)initWithURL:(NSURL*)anURL dataSource:(FSDataSource*)aDataSource
{
if((self = [super initWithURL:anURL]))
{
_dataSource = aDataSource;
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(workspaceDidChangeVolumeList:) name:NSWorkspaceDidMountNotification object:[NSWorkspace sharedWorkspace]];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(workspaceDidChangeVolumeList:) name:NSWorkspaceDidUnmountNotification object:[NSWorkspace sharedWorkspace]];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(workspaceDidChangeVolumeList:) name:NSWorkspaceDidRenameVolumeNotification object:[NSWorkspace sharedWorkspace]];
}
return self;
}
- (void)dealloc
{
[[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
}
- (void)workspaceDidChangeVolumeList:(NSNotification*)aNotification
{
[[NSNotificationCenter defaultCenter] postNotificationName:FSItemDidReloadNotification object:_dataSource userInfo:@{ @"item" : self }];
}
- (void)loadChildren:(FSDataSource*)dataSource completionHandler:(void(^)(NSArray*))block
{
block([self volumeList]);
}
- (NSArray*)volumeList
{
NSMutableArray* volumes = [NSMutableArray new];
@@ -19,30 +51,22 @@
}
return [FSDataSource sortArray:volumes usingOptions:0];
}
@end
- (void)workspaceDidChangeVolumeList:(NSNotification*)aNotification
{
[[NSNotificationCenter defaultCenter] postNotificationName:FSItemDidReloadNotification object:self userInfo:@{ @"item" : self.rootItem, @"children" : [self volumeList], @"recursive" : @YES }];
}
@implementation FSVolumesDataSource { OBJC_WATCH_LEAKS(FSVolumesDataSource); }
- (id)initWithURL:(NSURL*)anURL options:(NSUInteger)someOptions
{
if((self = [super init]))
{
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(workspaceDidChangeVolumeList:) name:NSWorkspaceDidMountNotification object:[NSWorkspace sharedWorkspace]];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(workspaceDidChangeVolumeList:) name:NSWorkspaceDidUnmountNotification object:[NSWorkspace sharedWorkspace]];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(workspaceDidChangeVolumeList:) name:NSWorkspaceDidRenameVolumeNotification object:[NSWorkspace sharedWorkspace]];
self.rootItem = [FSItem itemWithURL:anURL];
self.rootItem = [[FSVolumeListItem alloc] initWithURL:anURL dataSource:self];
self.rootItem.icon = [NSImage imageNamed:NSImageNameComputer]; // FIXME Assigning to property of type OakFileIconImage
self.rootItem.displayName = [[NSHost currentHost] localizedName];
self.rootItem.children = [self volumeList];
}
return self;
}
- (void)dealloc
- (void)reloadItem:(FSItem*)anItem completionHandler:(void(^)(NSArray*))block
{
[[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
[(FSVolumeListItem*)anItem loadChildren:self completionHandler:block];
}
@end