@@ -48,26 +48,44 @@ - (void)viewDidLoad
48
48
49
49
// Show placeholder image
50
50
self.placeholderImageView .hidden = NO ;
51
-
52
- // Initialize file view. We have to initialize with a valid file. Otherwise the drawing context is messed up (for some reason).
53
- NSError *error = nil ;
54
-
55
- _eagleFile = [EAGLEBoard boardFromBoardFile: @" " error: &error]; // Empty board
56
- // _eagleFile = [EAGLESchematic schematicFromSchematicFile:@"" error:&error]; // Empty schematic
57
- // _eagleFile = [EAGLESchematic schematicFromSchematicFile:@"#2014-003_Powerpack" error:&error];
58
- // _eagleFile = [EAGLEBoard boardFromBoardFile:@"Gift card" error:nil];
59
-
60
- _eagleFile.fileName = @" " ;
61
- _eagleFile.fileDate = [NSDate date ];
62
- NSAssert ( error == nil , @" Error loading file: %@ " , [error localizedDescription ] );
63
51
[self .fileView setRelativeZoomFactor: 0.1 ];
64
- self.fileView .file = _eagleFile;
65
52
66
- dispatch_after ( dispatch_time ( DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC ), dispatch_get_main_queue (), ^{
67
- [self zoomToFitAction: nil ];
68
- });
69
-
70
- [self updateBackgroundAndStatusBar ];
53
+ // If we have a local file path saved in user defaults, attempt to open that
54
+ NSError *error = nil ;
55
+ BOOL hasLoadedLastFile = NO ;
56
+ NSString *lastUsedFilePath = [[NSUserDefaults standardUserDefaults ] objectForKey: kUserDefaults_lastFilePath ];
57
+ if ( [lastUsedFilePath length ] > 0 )
58
+ {
59
+ // Construct full path. Value in user defaults is relative to the app's dropbox folder in the documents folder.
60
+ NSArray *paths = NSSearchPathForDirectoriesInDomains ( NSDocumentDirectory, NSUserDomainMask, YES );
61
+ NSString *documentsFolder = paths[0 ];
62
+ NSString *fullPath = [[documentsFolder stringByAppendingPathComponent: kDropboxFolderName ] stringByAppendingPathComponent: lastUsedFilePath];
63
+
64
+ DEBUG_LOG ( @" Opening last used file: %@ " , fullPath );
65
+ hasLoadedLastFile = [self openFileAtPath: fullPath error: &error];
66
+ if ( error )
67
+ NSLog ( @" Error loading last used file: %@ " , error );
68
+ }
69
+ if ( !hasLoadedLastFile )
70
+ {
71
+ // Initialize file view. We have to initialize with a valid file. Otherwise the drawing context is messed up (for some reason).
72
+ error = nil ;
73
+ _eagleFile = [EAGLEBoard boardFromBoardFile: @" " error: &error]; // Empty board
74
+ // _eagleFile = [EAGLESchematic schematicFromSchematicFile:@"" error:&error]; // Empty schematic
75
+ // _eagleFile = [EAGLESchematic schematicFromSchematicFile:@"#2014-003_Powerpack" error:&error];
76
+ // _eagleFile = [EAGLEBoard boardFromBoardFile:@"Gift card" error:nil];
77
+
78
+ _eagleFile.fileName = @" " ;
79
+ _eagleFile.fileDate = [NSDate date ];
80
+ NSAssert ( error == nil , @" Error loading file: %@ " , [error localizedDescription ] );
81
+ self.fileView .file = _eagleFile;
82
+
83
+ dispatch_after ( dispatch_time ( DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC ), dispatch_get_main_queue (), ^{
84
+ [self zoomToFitAction: nil ];
85
+ });
86
+
87
+ [self updateBackgroundAndStatusBar ];
88
+ }
71
89
72
90
// Add double tap recognizer (NB: behaves differently on iPad/iPhone – see the handler method
73
91
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc ] initWithTarget: self action: @selector (handleDoubleTapGesture: )];
@@ -431,6 +449,8 @@ - (IBAction)zoomToFitAction:(id)sender
431
449
- (void )openFileFromURL : (NSURL *)fileURL
432
450
{
433
451
// Make sure it's a file URL
452
+ [NSException raise :@" Possibly wrong method." format: @" This method seems to load only schematics. Are you sure that's right?" ];
453
+
434
454
if ( ![fileURL isFileURL ] )
435
455
[NSException raise :@" Invalid URL" format: @" Expected file URL: %@ " , [fileURL absoluteString ]];
436
456
@@ -461,6 +481,42 @@ - (void)openFile:(EAGLEFile*)file
461
481
});
462
482
}
463
483
484
+ - (BOOL )openFileAtPath : (NSString *)filePath error : (NSError **)error
485
+ {
486
+ NSError *err;
487
+
488
+ // Schematic or board?
489
+ NSString *fileName = [filePath lastPathComponent ];
490
+
491
+ if ( [[[fileName pathExtension ] lowercaseString ] isEqualToString: @" sch" ] )
492
+ _eagleFile = [EAGLESchematic schematicFromSchematicAtPath: filePath error: &err];
493
+ else if ( [[[fileName pathExtension ] lowercaseString ] isEqualToString: @" brd" ] )
494
+ _eagleFile = [EAGLEBoard boardFromBoardFileAtPath: filePath error: &err];
495
+
496
+ // Pass back error and return NO if we have an error
497
+ if ( err != nil )
498
+ {
499
+ if ( error )
500
+ *error = err;
501
+ return NO ;
502
+ }
503
+
504
+ _eagleFile.fileName = fileName;
505
+ // _eagleFile.fileDate = fileDate; /// TODO: date?
506
+
507
+ [self updateBackgroundAndStatusBar ];
508
+
509
+ self.fileView .file = _eagleFile;
510
+ self.placeholderImageView .hidden = YES ; // Hide initial placeholder
511
+
512
+ dispatch_async (dispatch_get_main_queue (), ^{
513
+ [self .fileView zoomToFitSize: self .scrollView.bounds.size animated: YES ];
514
+ [MBProgressHUD hideHUDForView: self .view animated: YES ];
515
+ });
516
+
517
+ return YES ;
518
+ }
519
+
464
520
#pragma mark - Document Chooser Delegate methods
465
521
466
522
- (void )documentChooserPickedDropboxFile : (DBMetadata *)metadata lastPath : (NSString *)lastPath
@@ -482,7 +538,7 @@ - (void)documentChooserPickedDropboxFile:(DBMetadata *)metadata lastPath:(NSStri
482
538
// Remember file data
483
539
NSDate *fileDate = metadata.lastModifiedDate ;
484
540
NSString *fileName = [metadata.path lastPathComponent ];
485
- [[Dropbox sharedInstance ] loadFileAtPath: metadata.path completion: ^(BOOL success, NSString *filePath) {
541
+ [[Dropbox sharedInstance ] loadFileAtPath: metadata.path completion: ^(BOOL success, NSString *filePath, DBMetadata *metadata ) {
486
542
487
543
if ( success )
488
544
{
@@ -504,6 +560,10 @@ - (void)documentChooserPickedDropboxFile:(DBMetadata *)metadata lastPath:(NSStri
504
560
self.fileView .file = _eagleFile;
505
561
self.placeholderImageView .hidden = YES ; // Hide initial placeholder
506
562
563
+ // Save path in user defaults. This path is relative to the app's documents directory.
564
+ [[NSUserDefaults standardUserDefaults ] setObject: metadata.path forKey: kUserDefaults_lastFilePath ];
565
+ [[NSUserDefaults standardUserDefaults ] synchronize ];
566
+
507
567
dispatch_async (dispatch_get_main_queue (), ^{
508
568
[self .fileView zoomToFitSize: self .scrollView.bounds.size animated: YES ];
509
569
[MBProgressHUD hideHUDForView: self .view animated: YES ];
0 commit comments