FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Merge pull request #2699 from NativeScript/ios-folders · NativeScript/NativeScript@e4e5324 · GitHub

Commit e4e5324

Browse files
authored
Merge pull request #2699 from NativeScript/ios-folders
IOS Known folders will not be created if do not exist
2 parents 576ad14 + e55ab34 commit e4e5324

3 files changed

Lines changed: 104 additions & 39 deletions

File tree

‎tests/app/file-system-tests.ts‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,16 +223,19 @@ function _testIOSSpecificKnownFolder(knownFolderName: string){
223223
let createdFile: fs.File;
224224
let testFunc = function testFunc(){
225225
knownFolder = fs.knownFolders.ios[knownFolderName]();
226-
createdFile = knownFolder.getFile("createdFile");
227-
createdFile.writeTextSync("some text");
226+
if (knownFolder) {
227+
createdFile = knownFolder.getFile("createdFile");
228+
createdFile.writeTextSync("some text");
229+
}
228230
};
229231
if (platform.isIOS){
230232
testFunc();
231-
TKUnit.assertNotNull(knownFolder, `Could not retrieve the ${knownFolderName} known folder.`);
232-
TKUnit.assertTrue(knownFolder.isKnown, `The ${knownFolderName} folder should have its "isKnown" property set to true.`);
233-
TKUnit.assertNotNull(createdFile, `Could not create a new file in the ${knownFolderName} known folder.`);
234-
TKUnit.assertTrue(fs.File.exists(createdFile.path), `Could not create a new file in the ${knownFolderName} known folder.`);
235-
TKUnit.assertEqual(createdFile.readTextSync(), "some text", `The contents of the new file created in the ${knownFolderName} known folder are not as expected.`);
233+
if (knownFolder) {
234+
TKUnit.assertTrue(knownFolder.isKnown, `The ${knownFolderName} folder should have its "isKnown" property set to true.`);
235+
TKUnit.assertNotNull(createdFile, `Could not create a new file in the ${knownFolderName} known folder.`);
236+
TKUnit.assertTrue(fs.File.exists(createdFile.path), `Could not create a new file in the ${knownFolderName} known folder.`);
237+
TKUnit.assertEqual(createdFile.readTextSync(), "some text", `The contents of the new file created in the ${knownFolderName} known folder are not as expected.`);
238+
}
236239
}
237240
else {
238241
TKUnit.assertThrows(testFunc,

‎tns-core-modules/file-system/file-system-access.ios.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,30 @@ export class FileSystemAccess {
107107
}
108108
}
109109

110+
public getExistingFolder(path: string, onError?: (error: any) => any): { path: string; name: string } {
111+
try {
112+
var fileManager = utils.ios.getter(NSFileManager, NSFileManager.defaultManager);
113+
var exists = this.folderExists(path);
114+
115+
if (exists) {
116+
var dirName = fileManager.displayNameAtPath(path);
117+
118+
return {
119+
path: path,
120+
name: dirName
121+
};
122+
}
123+
return undefined;
124+
}
125+
catch (ex) {
126+
if (onError) {
127+
onError(new Error("Failed to get folder at path '" + path + "'"));
128+
}
129+
130+
return undefined;
131+
}
132+
}
133+
110134
public eachEntity(path: string, onEntity: (file: { path: string; name: string; extension: string }) => any, onError?: (error: any) => any) {
111135
if (!onEntity) {
112136
return;

‎tns-core-modules/file-system/file-system.ts‎

Lines changed: 70 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -512,10 +512,13 @@ export module knownFolders {
512512
export var library = function(): Folder {
513513
_checkPlatform("library");
514514
if (!_library) {
515-
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.LibraryDirectory);
516-
_library = Folder.fromPath(path);
517-
_library[pathProperty] = path;
518-
_library[isKnownProperty] = true;
515+
let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.LibraryDirectory);
516+
517+
if (existingFolderInfo) {
518+
_library = existingFolderInfo.folder;
519+
_library[pathProperty] = existingFolderInfo.path;
520+
_library[isKnownProperty] = true;
521+
}
519522
}
520523

521524
return _library;
@@ -525,10 +528,13 @@ export module knownFolders {
525528
export var developer = function(): Folder {
526529
_checkPlatform("developer");
527530
if (!_developer) {
528-
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.DeveloperDirectory);
529-
_developer = Folder.fromPath(path);
530-
_developer[pathProperty] = path;
531-
_developer[isKnownProperty] = true;
531+
let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.DeveloperDirectory);
532+
533+
if (existingFolderInfo) {
534+
_developer = existingFolderInfo.folder;
535+
_developer[pathProperty] = existingFolderInfo.path;
536+
_developer[isKnownProperty] = true;
537+
}
532538
}
533539

534540
return _developer;
@@ -538,10 +544,13 @@ export module knownFolders {
538544
export var desktop = function(): Folder {
539545
_checkPlatform("desktop");
540546
if (!_desktop) {
541-
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.DesktopDirectory);
542-
_desktop = Folder.fromPath(path);
543-
_desktop[pathProperty] = path;
544-
_desktop[isKnownProperty] = true;
547+
let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.DesktopDirectory);
548+
549+
if (existingFolderInfo) {
550+
_desktop = existingFolderInfo.folder;
551+
_desktop[pathProperty] = existingFolderInfo.path;
552+
_desktop[isKnownProperty] = true;
553+
}
545554
}
546555

547556
return _desktop;
@@ -551,10 +560,13 @@ export module knownFolders {
551560
export var downloads = function(): Folder {
552561
_checkPlatform("downloads");
553562
if (!_downloads) {
554-
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.DownloadsDirectory);
555-
_downloads = Folder.fromPath(path);
556-
_downloads[pathProperty] = path;
557-
_downloads[isKnownProperty] = true;
563+
let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.DownloadsDirectory);
564+
565+
if (existingFolderInfo) {
566+
_downloads = existingFolderInfo.folder;
567+
_downloads[pathProperty] = existingFolderInfo.path;
568+
_downloads[isKnownProperty] = true;
569+
}
558570
}
559571

560572
return _downloads;
@@ -564,10 +576,13 @@ export module knownFolders {
564576
export var movies = function(): Folder {
565577
_checkPlatform("movies");
566578
if (!_movies) {
567-
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.MoviesDirectory);
568-
_movies = Folder.fromPath(path);
569-
_movies[pathProperty] = path;
570-
_movies[isKnownProperty] = true;
579+
let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.MoviesDirectory);
580+
581+
if (existingFolderInfo) {
582+
_movies = existingFolderInfo.folder;
583+
_movies[pathProperty] = existingFolderInfo.path;
584+
_movies[isKnownProperty] = true;
585+
}
571586
}
572587

573588
return _movies;
@@ -577,10 +592,13 @@ export module knownFolders {
577592
export var music = function(): Folder {
578593
_checkPlatform("music");
579594
if (!_music) {
580-
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.MusicDirectory);
581-
_music = Folder.fromPath(path);
582-
_music[pathProperty] = path;
583-
_music[isKnownProperty] = true;
595+
let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.MusicDirectory);
596+
597+
if (existingFolderInfo) {
598+
_music = existingFolderInfo.folder;
599+
_music[pathProperty] = existingFolderInfo.path;
600+
_music[isKnownProperty] = true;
601+
}
584602
}
585603

586604
return _music;
@@ -590,10 +608,13 @@ export module knownFolders {
590608
export var pictures = function(): Folder {
591609
_checkPlatform("pictures");
592610
if (!_pictures) {
593-
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.PicturesDirectory);
594-
_pictures = Folder.fromPath(path);
595-
_pictures[pathProperty] = path;
596-
_pictures[isKnownProperty] = true;
611+
let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.PicturesDirectory);
612+
613+
if (existingFolderInfo) {
614+
_pictures = existingFolderInfo.folder;
615+
_pictures[pathProperty] = existingFolderInfo.path;
616+
_pictures[isKnownProperty] = true;
617+
}
597618
}
598619

599620
return _pictures;
@@ -603,14 +624,31 @@ export module knownFolders {
603624
export var sharedPublic = function(): Folder {
604625
_checkPlatform("sharedPublic");
605626
if (!_sharedPublic) {
606-
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.SharedPublicDirectory);
607-
_sharedPublic = Folder.fromPath(path);
608-
_sharedPublic[pathProperty] = path;
609-
_sharedPublic[isKnownProperty] = true;
627+
let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.SharedPublicDirectory);
628+
629+
if (existingFolderInfo) {
630+
_sharedPublic = existingFolderInfo.folder;
631+
_sharedPublic[pathProperty] = existingFolderInfo.path;
632+
_sharedPublic[isKnownProperty] = true;
633+
}
610634
}
611635

612636
return _sharedPublic;
613637
};
638+
639+
function getExistingFolderInfo(pathDirectory: NSSearchPathDirectory): { folder: Folder; path: string } {
640+
var fileAccess = (<any>getFileAccess());
641+
var folderPath = fileAccess.getKnownPath(pathDirectory);
642+
var folderInfo = fileAccess.getExistingFolder(folderPath);
643+
644+
if (folderInfo) {
645+
return {
646+
folder: createFolder(folderInfo),
647+
path: folderPath
648+
};
649+
}
650+
return undefined;
651+
}
614652
}
615653
}
616654

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL