Skip to content

Commit 116bc26

Browse files
committed
Add iOS-specific known folders to fs.knownFolders
1 parent e4cf173 commit 116bc26

File tree

7 files changed

+209
-9
lines changed

7 files changed

+209
-9
lines changed

apps/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"noImplicitAny": false,
99
"noImplicitUseStrict": true,
1010
"experimentalDecorators": true,
11-
"diagnostics": true
11+
"diagnostics": true,
12+
"sourceMap": true
1213
},
1314
"exclude": [
1415
"node_modules",

tests/app/file-system-tests.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,40 @@ export var testGetKnownFolders = function () {
218218
// << file-system-known-folders
219219
};
220220

221+
function _testIOSSpecificKnownFolder(knownFolderName: string){
222+
let knownFolder: fs.Folder;
223+
let createdFile: fs.File;
224+
let testFunc = function testFunc(){
225+
knownFolder = fs.knownFolders.ios[knownFolderName]();
226+
createdFile = knownFolder.getFile("createdFile");
227+
createdFile.writeTextSync("some text");
228+
};
229+
if (platform.isIOS){
230+
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.`);
236+
}
237+
else {
238+
TKUnit.assertThrows(testFunc,
239+
`Trying to retrieve the ${knownFolderName} known folder on a platform different from iOS should throw!`,
240+
`The "${knownFolderName}" known folder is available on iOS only!`);
241+
}
242+
}
243+
244+
export var testIOSSpecificKnownFolders = function () {
245+
_testIOSSpecificKnownFolder("library");
246+
_testIOSSpecificKnownFolder("developer");
247+
_testIOSSpecificKnownFolder("desktop");
248+
_testIOSSpecificKnownFolder("downloads");
249+
_testIOSSpecificKnownFolder("movies");
250+
_testIOSSpecificKnownFolder("music");
251+
_testIOSSpecificKnownFolder("pictures");
252+
_testIOSSpecificKnownFolder("sharedPublic");
253+
}
254+
221255
export var testGetEntities = function () {
222256
// >> file-system-folders-content
223257
var documents = fs.knownFolders.documents();

tests/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"noImplicitAny": false,
99
"noImplicitUseStrict": true,
1010
"experimentalDecorators": true,
11-
"diagnostics": true
11+
"diagnostics": true,
12+
"sourceMap": true
1213
},
1314
"exclude": [
1415
"node_modules",

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@ import * as utilsModule from "utils/utils";
55
// TODO: Check whether we need try/catch blocks for the iOS implementation
66

77
export class FileSystemAccess {
8-
//private keyFileType = "NSFileType";
9-
//private keyReadonly = "NSFileImmutable";
10-
//private NSUTF8StringEncoding = 4;
118
private keyModificationTime = "NSFileModificationDate";
12-
private documentDir = 9;
13-
private cachesDir = 13;
14-
private userDomain = 1;
9+
private userDomain = NSSearchPathDirectory.NSApplicationDirectory; //1
10+
private documentDir = NSSearchPathDirectory.NSDocumentDirectory; //9
11+
private cachesDir = NSSearchPathDirectory.NSCachesDirectory; //13
1512

1613
public getLastModified(path: string): Date {
1714
var fileManager = NSFileManager.defaultManager();

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,51 @@ declare module "file-system" {
208208
* iOS - this folder is read-only and contains the app and all its resources.
209209
*/
210210
export function currentApp(): Folder;
211+
212+
/**
213+
* Contains iOS-specific known folders.
214+
*/
215+
module ios {
216+
/**
217+
* Gets the NSLibraryDirectory.
218+
*/
219+
export function library(): Folder;
220+
221+
/**
222+
* Gets the NSDeveloperDirectory.
223+
*/
224+
export function developer(): Folder;
225+
226+
/**
227+
* Gets the NSDesktopDirectory.
228+
*/
229+
export function desktop(): Folder;
230+
231+
/**
232+
* Gets the NSDownloadsDirectory.
233+
*/
234+
export function downloads(): Folder;
235+
236+
/**
237+
* Gets the NSMoviesDirectory.
238+
*/
239+
export function movies(): Folder;
240+
241+
/**
242+
* Gets the NSMusicDirectory.
243+
*/
244+
export function music(): Folder;
245+
246+
/**
247+
* Gets the NSPicturesDirectory.
248+
*/
249+
export function pictures(): Folder;
250+
251+
/**
252+
* Gets the NSSharedPublicDirectory.
253+
*/
254+
export function sharedPublic(): Folder;
255+
}
211256
}
212257

213258
/**

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

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import file_access_module = require("file-system/file-system-access");
2+
import * as platformModule from "platform";
23

34
// The FileSystemAccess implementation, used through all the APIs.
45
var fileAccess;
@@ -10,6 +11,13 @@ var getFileAccess = function (): file_access_module.FileSystemAccess {
1011
return fileAccess;
1112
};
1213

14+
let platform: typeof platformModule;
15+
function ensurePlatform() {
16+
if (!platform) {
17+
platform = require("platform");
18+
}
19+
}
20+
1321
// we are defining these as private variables within the IO scope and will use them to access the corresponding properties for each FSEntity instance.
1422
// this allows us to encapsulate (hide) the explicit property setters and force the users go through the exposed APIs to receive FSEntity instances.
1523
var nameProperty = "_name";
@@ -491,6 +499,119 @@ export module knownFolders {
491499

492500
return _app;
493501
};
502+
503+
export module ios {
504+
function _checkPlatform(knownFolderName: string){
505+
ensurePlatform();
506+
if (!platform.isIOS){
507+
throw new Error(`The "${knownFolderName}" known folder is available on iOS only!`);
508+
}
509+
}
510+
511+
let _library: Folder;
512+
export var library = function(): Folder {
513+
_checkPlatform("library");
514+
if (!_library) {
515+
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.NSLibraryDirectory);
516+
_library = Folder.fromPath(path);
517+
_library[pathProperty] = path;
518+
_library[isKnownProperty] = true;
519+
}
520+
521+
return _library;
522+
};
523+
524+
let _developer: Folder;
525+
export var developer = function(): Folder {
526+
_checkPlatform("developer");
527+
if (!_developer) {
528+
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.NSDeveloperDirectory);
529+
_developer = Folder.fromPath(path);
530+
_developer[pathProperty] = path;
531+
_developer[isKnownProperty] = true;
532+
}
533+
534+
return _developer;
535+
};
536+
537+
let _desktop: Folder;
538+
export var desktop = function(): Folder {
539+
_checkPlatform("desktop");
540+
if (!_desktop) {
541+
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.NSDesktopDirectory);
542+
_desktop = Folder.fromPath(path);
543+
_desktop[pathProperty] = path;
544+
_desktop[isKnownProperty] = true;
545+
}
546+
547+
return _desktop;
548+
};
549+
550+
let _downloads: Folder;
551+
export var downloads = function(): Folder {
552+
_checkPlatform("downloads");
553+
if (!_downloads) {
554+
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.NSDownloadsDirectory);
555+
_downloads = Folder.fromPath(path);
556+
_downloads[pathProperty] = path;
557+
_downloads[isKnownProperty] = true;
558+
}
559+
560+
return _downloads;
561+
};
562+
563+
let _movies: Folder;
564+
export var movies = function(): Folder {
565+
_checkPlatform("movies");
566+
if (!_movies) {
567+
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.NSMoviesDirectory);
568+
_movies = Folder.fromPath(path);
569+
_movies[pathProperty] = path;
570+
_movies[isKnownProperty] = true;
571+
}
572+
573+
return _movies;
574+
};
575+
576+
let _music: Folder;
577+
export var music = function(): Folder {
578+
_checkPlatform("music");
579+
if (!_music) {
580+
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.NSMusicDirectory);
581+
_music = Folder.fromPath(path);
582+
_music[pathProperty] = path;
583+
_music[isKnownProperty] = true;
584+
}
585+
586+
return _music;
587+
};
588+
589+
let _pictures: Folder;
590+
export var pictures = function(): Folder {
591+
_checkPlatform("pictures");
592+
if (!_pictures) {
593+
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.NSPicturesDirectory);
594+
_pictures = Folder.fromPath(path);
595+
_pictures[pathProperty] = path;
596+
_pictures[isKnownProperty] = true;
597+
}
598+
599+
return _pictures;
600+
};
601+
602+
let _sharedPublic: Folder;
603+
export var sharedPublic = function(): Folder {
604+
_checkPlatform("sharedPublic");
605+
if (!_sharedPublic) {
606+
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.NSSharedPublicDirectory);
607+
_sharedPublic = Folder.fromPath(path);
608+
_sharedPublic[pathProperty] = path;
609+
_sharedPublic[isKnownProperty] = true;
610+
}
611+
612+
return _sharedPublic;
613+
};
614+
}
494615
}
495616

496617
export module path {

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"noImplicitAny": false,
1010
"noImplicitUseStrict": true,
1111
"experimentalDecorators": true,
12-
"diagnostics": true
12+
"diagnostics": true,
13+
"sourceMap": true
1314
},
1415
"exclude": [
1516
"node_modules",

0 commit comments

Comments
 (0)