Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow user to chose whether or not to watch files

  • Loading branch information...
commit bff2b4a54cbc87bf80d6c445ab553ef7b27900a3 1 parent a6fa21f
@kylehickinson authored
View
3  Mac/Reflect/RFAppDelegate.m
@@ -31,7 +31,8 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
@"bonjour-name" : computerName,
@"bonjour-use-type" : @NO,
@"bonjour-type" : @"_reflect",
- @"recent-files" : @[ ]
+ @"recent-files" : @[ ],
+ @"watch-files" : @YES
}];
// (Maybe) Start Reflect server.
View
3  Mac/Reflect/RFFileMonitor.h
@@ -88,6 +88,9 @@ typedef enum : NSInteger {
/** The delegate */
@property (nonatomic, weak) id<RFFileMonitorDelegate> delegate;
+/** `YES` if the file monitor is watching a file, `NO` otherwise. */
+@property (nonatomic, readonly, getter=isMonitoring) BOOL monitoring;
+
/**
Begin monitoring a file with a given path.
View
3  Mac/Reflect/RFFileMonitor.m
@@ -74,6 +74,7 @@ - (void)beginMonitoringForPath:(NSString *)path
self.monitoringThread = [[NSThread alloc] initWithTarget:self selector:@selector(_monitorInBackground) object:nil];
[self.monitoringThread start];
+ _monitoring = YES;
if (_flags.respondsToDidBeginMonitoring) {
[self.delegate fileMonitor:self didBeginMonitoringPath:self.path];
}
@@ -84,6 +85,8 @@ - (void)stopMonitoring
close(self.fileDescriptor);
close(self.queue);
[self.monitoringThread cancel];
+
+ _monitoring = NO;
}
- (void)setDelegate:(id<RFFileMonitorDelegate>)delegate
View
31 Mac/Reflect/RFReflect.m
@@ -19,6 +19,7 @@ @interface RFReflect ()
@property (nonatomic, strong) NSImage *highlightedIcon;
@property (nonatomic, strong) RFFileMonitor *fileMonitor;
+@property (nonatomic, copy) NSString *chosenPath;
- (void)_setup;
- (void)_updateRecentList;
@@ -119,13 +120,19 @@ - (BOOL)performDragOperation:(id<NSDraggingInfo>)sender
- (void)selectFileWithPath:(NSString *)path
{
+ // Should probably add some error handling here...
+ self.chosenPath = path;
+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
- self.server.data = [NSData dataWithContentsOfFile:path];
+ self.server.data = [NSData dataWithContentsOfFile:self.chosenPath];
});
- [self.fileMonitor beginMonitoringForPath:path];
-
NSUserDefaults *userDefaults = [[NSUserDefaultsController sharedUserDefaultsController] defaults];
+
+ if ([userDefaults boolForKey:@"watch-files"]) {
+ [self.fileMonitor beginMonitoringForPath:self.chosenPath];
+ }
+
NSMutableArray *recents = [[userDefaults objectForKey:@"recent-files"] mutableCopy];
NSInteger idx = [recents indexOfObjectIdenticalTo:path];
@@ -245,6 +252,18 @@ - (void)_clearRecentList
[self _updateRecentList];
}
+- (void)_toggleWatchFiles:(NSMenuItem *)sender
+{
+ sender.state = !sender.state;
+ [[[NSUserDefaultsController sharedUserDefaultsController] defaults] setBool:sender.state forKey:@"watch-files"];
+
+ if (sender.state == NSOnState && [self.chosenPath length] > 0) {
+ [self.fileMonitor beginMonitoringForPath:self.chosenPath];
+ } else {
+ [self.fileMonitor stopMonitoring];
+ }
+}
+
- (void)_setup
{
NSDictionary *userDefaults = [[[NSUserDefaultsController sharedUserDefaultsController] defaults] dictionaryRepresentation];
@@ -274,6 +293,12 @@ - (void)_setup
[self.menu addItem:startStop];
[self.menu addItem:[NSMenuItem separatorItem]];
[self.menu addItem:[self _recentFilesMenuItem]];
+
+ NSMenuItem *watchFiles = [[NSMenuItem alloc] initWithTitle:@"Watch for Changes" action:@selector(_toggleWatchFiles:) keyEquivalent:@""];
+ watchFiles.target = self;
+ watchFiles.state = [userDefaults[@"watch-files"] boolValue];
+ [self.menu addItem:watchFiles];
+
[self.menu addItem:[NSMenuItem separatorItem]];
[self.menu addItemWithTitle:@"Preferences…" action:@selector(_showPreferences:) keyEquivalent:@""];
[self.menu addItemWithTitle:@"Quit" action:@selector(_quit) keyEquivalent:@""];
View
2  iOS/Reflect/RFClient.m
@@ -206,8 +206,6 @@ - (void)_appendBytesAndCheck:(uint8_t *)bytes length:(NSInteger)length
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
- static NSMutableArray *packetLog = nil;
-
if (self.data == nil) {
self.data = [[NSMutableData alloc] init];
}
Please sign in to comment.
Something went wrong with that request. Please try again.