Polished the BCL; started FileSystem.ios

This commit is contained in:
atanasovg
2014-03-21 16:15:04 +02:00
parent ca201cf9d7
commit 001d828eaa
6 changed files with 118 additions and 99 deletions

View File

@ -130,6 +130,7 @@ export module tk {
public init() { public init() {
this._eventsToken = initEvents(); this._eventsToken = initEvents();
this.nativeApp.registerActivityLifecycleCallbacks(this._eventsToken); this.nativeApp.registerActivityLifecycleCallbacks(this._eventsToken);
this.context = this.nativeApp.getApplicationContext();
} }
} }
} }

View File

@ -12,7 +12,7 @@
/** /**
* Gets the Folder object representing the parent of this entity. Will be null for a root folder like Documents or Temporary. * Gets the Folder object representing the parent of this entity. Will be null for a root folder like Documents or Temporary.
*/ */
public getParent(onSuccess: (folder: Folder) => any, onError?: (error: any) => any); public getParent(onError?: (error: any) => any): Folder;
/** /**
* Deletes the current Entity from the file system. * Deletes the current Entity from the file system.
*/ */

View File

@ -1,5 +1,6 @@
import file_access_module = require("FileSystem/file_system_access"); import file_access_module = require("FileSystem/file_system_access");
// The FileSystemAccess implementation, used through all the APIs. // The FileSystemAccess implementation, used through all the APIs.
var fileAccess; var fileAccess;
var getFileAccess = function (): file_access_module.FileSystemAccess { var getFileAccess = function (): file_access_module.FileSystemAccess {
@ -10,9 +11,9 @@ var getFileAccess = function (): file_access_module.FileSystemAccess {
return fileAccess; return fileAccess;
} }
// we are defining these as private variables within the IO scope and will use them to access the corresponding properties for each FSEntity instance. // we are defining these as private variables within the IO scope and will use them to access the corresponding properties for each FSEntity instance.
// this allows us to encapsulate (hide) the explicit property setters and force the users go through the exposed APIs to receive FSEntity instances. // this allows us to encapsulate (hide) the explicit property setters and force the users go through the exposed APIs to receive FSEntity instances.
var nameProperty = "_name"; var nameProperty = "_name";
var pathProperty = "_path"; var pathProperty = "_path";
var isKnownProperty = "_isKnown"; var isKnownProperty = "_isKnown";
var fileLockedProperty = "_locked"; var fileLockedProperty = "_locked";
@ -21,26 +22,23 @@ var readonlyProperty = "_readonly";
var lastModifiedProperty = "_lastModified"; var lastModifiedProperty = "_lastModified";
/** /**
* Represents the basic file system entity - a File or a Folder. * Represents the basic file system entity - a File or a Folder.
*/ */
export class FileSystemEntity { export class FileSystemEntity {
/** /**
* Gets the Folder object representing the parent of this entity. Will be null for a root folder like Documents or Temporary. * Gets the Folder object representing the parent of this entity. Will be null for a root folder like Documents or Temporary.
*/ */
public getParent(onSuccess: (parent: Folder) => any, onError?: (error: any) => any) { public getParent(onError?: (error: any) => any): Folder {
var localSuccess = function (path: string) { var path = getFileAccess().getParent(this.path, onError);
var folder = new Folder();
folder[pathProperty] = path;
if (onSuccess) { var folder = new Folder();
onSuccess(folder); folder[pathProperty] = path;
}
} return folder;
getFileAccess().getParent(this.path, localSuccess, onError);
} }
/** /**
* Deletes the current entity from the file system. * Deletes the current entity from the file system.
*/ */
public delete(onSuccess?: () => any, onError?: (error: any) => any) { public delete(onSuccess?: () => any, onError?: (error: any) => any) {
if (this instanceof File) { if (this instanceof File) {
getFileAccess().deleteFile(this.path, onSuccess, onError); getFileAccess().deleteFile(this.path, onSuccess, onError);
@ -49,26 +47,26 @@ export class FileSystemEntity {
} }
} }
/** /**
* Renames the current entity using the specified name. * Renames the current entity using the specified name.
*/ */
public rename(newName: string, onSuccess?: () => any, onError?: (error: any) => any) { public rename(newName: string, onSuccess?: () => any, onError?: (error: any) => any) {
// TODO: No implementation // TODO: No implementation
} }
/** /**
* Gets the name of the entity. * Gets the name of the entity.
*/ */
get name(): string { get name(): string {
return this[nameProperty]; return this[nameProperty];
} }
/** /**
* Gets the fully-qualified path (including the extension for a File) of the entity. * Gets the fully-qualified path (including the extension for a File) of the entity.
*/ */
get path(): string { get path(): string {
return this[pathProperty]; return this[pathProperty];
} }
/** /**
* Gets a value indicating whether this entity is read-only (no write persmissions). * Gets a value indicating whether this entity is read-only (no write persmissions).
*/ */
get readonly(): boolean { get readonly(): boolean {
var value = this[readonlyProperty]; var value = this[readonlyProperty];
if (this[readonlyProperty] === undefined) { if (this[readonlyProperty] === undefined) {
@ -78,8 +76,8 @@ export class FileSystemEntity {
return value; return value;
} }
/** /**
* Gets the fully-qualified path (including the extension for a File) of the entity. * Gets the fully-qualified path (including the extension for a File) of the entity.
*/ */
get lastModified(): Date { get lastModified(): Date {
var value = this[lastModifiedProperty]; var value = this[lastModifiedProperty];
if (this[lastModifiedProperty] === undefined) { if (this[lastModifiedProperty] === undefined) {
@ -91,12 +89,12 @@ export class FileSystemEntity {
} }
/** /**
* Represents a File entity. * Represents a File entity.
*/ */
export class File extends FileSystemEntity { export class File extends FileSystemEntity {
/** /**
* Gets the File instance associated with the specified path. * Gets the File instance associated with the specified path.
*/ */
public static fromPath(path: string, onSuccess: (file: File) => any, onError?: (error: any) => any) { public static fromPath(path: string, onSuccess: (file: File) => any, onError?: (error: any) => any) {
var localSuccess = function (path: string) { var localSuccess = function (path: string) {
var file = new File(); var file = new File();
@ -106,43 +104,43 @@ export class File extends FileSystemEntity {
onSuccess(file); onSuccess(file);
} }
} }
getFileAccess().getFile(path, localSuccess, onError); getFileAccess().getFile(path, localSuccess, onError);
} }
/** /**
* Checks whether a File with the specified path already exists. * Checks whether a File with the specified path already exists.
*/ */
public static exists(path: string): boolean { public static exists(path: string): boolean {
return getFileAccess().fileExists(path); return getFileAccess().fileExists(path);
} }
/** /**
* Deletes the current File from the file system. * Deletes the current File from the file system.
*/ */
public delete(onSuccess?: () => any, onError?: (error: any) => any) { public delete(onSuccess?: () => any, onError?: (error: any) => any) {
getFileAccess().deleteFile(this.path, onSuccess, onError); getFileAccess().deleteFile(this.path, onSuccess, onError);
} }
/** /**
* Creates a FileReader object over this file and locks the file until the reader is released. * Creates a FileReader object over this file and locks the file until the reader is released.
*/ */
public openRead(): FileReader { public openRead(): FileReader {
this.checkAccess(); this.checkAccess();
return new FileReader(this); return new FileReader(this);
} }
/** /**
* Creates a FileWriter object over this file and locks the file until the writer is released. * Creates a FileWriter object over this file and locks the file until the writer is released.
*/ */
public openWrite(): FileWriter { public openWrite(): FileWriter {
this.checkAccess(); this.checkAccess();
return new FileWriter(this); return new FileWriter(this);
} }
/** /**
* Gets the extension of the entity. * Gets the extension of the entity.
*/ */
get extension(): string { get extension(): string {
return this[extensionProperty]; return this[extensionProperty];
} }
/** /**
* Gets a value indicating whether the file is currently locked, meaning a background operation associated with this file is running. * Gets a value indicating whether the file is currently locked, meaning a background operation associated with this file is running.
*/ */
get isLocked(): boolean { get isLocked(): boolean {
return this[fileLockedProperty]; return this[fileLockedProperty];
} }
@ -157,12 +155,12 @@ export class File extends FileSystemEntity {
} }
/** /**
* Represents a Folder entity. * Represents a Folder entity.
*/ */
export class Folder extends FileSystemEntity { export class Folder extends FileSystemEntity {
/** /**
* Attempts to access a Folder at the specified path and creates a new Folder if there is no existing one. * Attempts to access a Folder at the specified path and creates a new Folder if there is no existing one.
*/ */
public static fromPath(path: string, onSuccess: (folder: Folder) => any, onError?: (error: any) => any) { public static fromPath(path: string, onSuccess: (folder: Folder) => any, onError?: (error: any) => any) {
var localSuccess = function (path: string) { var localSuccess = function (path: string) {
var folder = new Folder(); var folder = new Folder();
@ -172,19 +170,19 @@ export class Folder extends FileSystemEntity {
onSuccess(folder); onSuccess(folder);
} }
} }
getFileAccess().getFolder(path, localSuccess, onError); getFileAccess().getFolder(path, localSuccess, onError);
} }
/** /**
* Checks whether a Folder with the specified path already exists. * Checks whether a Folder with the specified path already exists.
*/ */
public static exists(path: string): boolean { public static exists(path: string): boolean {
return getFileAccess().folderExists(path); return getFileAccess().folderExists(path);
} }
/** /**
* Checks whether this Folder contains a file with the specified name. * Checks whether this Folder contains a file with the specified name.
*/ */
public containsFile(name: string): boolean { public containsFile(name: string): boolean {
var fileAccess = getFileAccess(); var fileAccess = getFileAccess();
var path = fileAccess.concatPath(this.path, name); var path = fileAccess.concatPath(this.path, name);
@ -192,29 +190,38 @@ export class Folder extends FileSystemEntity {
} }
/** /**
* Deletes the current Folder (recursively) from the file system. * Checks whether this Folder contains a Folder with the specified name.
*/ */
public containsFolder(name: string): boolean {
var fileAccess = getFileAccess();
var path = fileAccess.concatPath(this.path, name);
return fileAccess.folderExists(path);
}
/**
* Deletes the current Folder (recursively) from the file system.
*/
public delete(onSuccess?: () => any, onError?: (error: any) => any) { public delete(onSuccess?: () => any, onError?: (error: any) => any) {
getFileAccess().deleteFolder(this.path, this.isKnown, onSuccess, onError); getFileAccess().deleteFolder(this.path, this.isKnown, onSuccess, onError);
} }
/** /**
* Deletes all the files and folders (recursively), contained within this Folder. * Deletes all the files and folders (recursively), contained within this Folder.
*/ */
public empty(onSuccess?: () => any, onError?: (error: any) => any) { public empty(onSuccess?: () => any, onError?: (error: any) => any) {
getFileAccess().emptyFolder(this.path, onSuccess, onError); getFileAccess().emptyFolder(this.path, onSuccess, onError);
} }
/** /**
* Determines whether this instance is a KnownFolder (accessed through the KnownFolders object). * Determines whether this instance is a KnownFolder (accessed through the KnownFolders object).
*/ */
get isKnown(): boolean { get isKnown(): boolean {
return this[isKnownProperty]; return this[isKnownProperty];
} }
/** /**
* Attempts to open a File with the specified name within this Folder and optionally creates a new File if there is no existing one. * Attempts to open a File with the specified name within this Folder and optionally creates a new File if there is no existing one.
*/ */
public getFile(name: string, onSuccess: (file: File) => any, onError?: (error: any) => any, createIfNonExisting?: boolean) { public getFile(name: string, onSuccess: (file: File) => any, onError?: (error: any) => any, createIfNonExisting?: boolean) {
var localSuccess = function (filePath: string) { var localSuccess = function (filePath: string) {
var newFile = new File(); var newFile = new File();
@ -226,14 +233,14 @@ export class Folder extends FileSystemEntity {
onSuccess(newFile); onSuccess(newFile);
} }
} }
var fileAccess = getFileAccess(); var fileAccess = getFileAccess();
var path = fileAccess.concatPath(this.path, name); var path = fileAccess.concatPath(this.path, name);
fileAccess.getFile(path, localSuccess, onError); fileAccess.getFile(path, localSuccess, onError);
} }
/** /**
* Attempts to open a Folder with the specified name within this Folder and optionally creates a new Folder if there is no existing one. * Attempts to open a Folder with the specified name within this Folder and optionally creates a new Folder if there is no existing one.
*/ */
public getFolder(name: string, onSuccess: (folder: Folder) => any, onError?: (error: any) => any) { public getFolder(name: string, onSuccess: (folder: Folder) => any, onError?: (error: any) => any) {
var localSuccess = function (filePath: string) { var localSuccess = function (filePath: string) {
var newFolder = new Folder(); var newFolder = new Folder();
@ -246,14 +253,14 @@ export class Folder extends FileSystemEntity {
} }
} }
var fileAccess = getFileAccess(); var fileAccess = getFileAccess();
var path = fileAccess.concatPath(this.path, name); var path = fileAccess.concatPath(this.path, name);
fileAccess.getFolder(path, localSuccess, onError); fileAccess.getFolder(path, localSuccess, onError);
} }
/** /**
* Gets all the top-level files residing within this Folder. * Gets all the top-level files residing within this Folder.
*/ */
public enumFiles(onSuccess: (files: Array<File>) => any, onError?: (error: any) => any) { public enumFiles(onSuccess: (files: Array<File>) => any, onError?: (error: any) => any) {
var localSuccess = function (paths: Array<string>) { var localSuccess = function (paths: Array<string>) {
if (onSuccess) { if (onSuccess) {
@ -271,21 +278,21 @@ export class Folder extends FileSystemEntity {
onSuccess(files); onSuccess(files);
} }
} }
getFileAccess().enumFiles(this.path, localSuccess, onError); getFileAccess().enumFiles(this.path, localSuccess, onError);
} }
} }
/** /**
* Provides access to the top-level Folders instances that are accessible from the application. Use these as entry points to access the FileSystem. * Provides access to the top-level Folders instances that are accessible from the application. Use these as entry points to access the FileSystem.
*/ */
export class KnownFolders { export class KnownFolders {
private static _documents: Folder; private static _documents: Folder;
private static _temp: Folder; private static _temp: Folder;
/** /**
* Gets the Documents folder available for the current application. This Folder is private for the application and not accessible from Users/External apps. * Gets the Documents folder available for the current application. This Folder is private for the application and not accessible from Users/External apps.
*/ */
public static Documents(): Folder { public static documents(): Folder {
if (!KnownFolders._documents) { if (!KnownFolders._documents) {
var path = getFileAccess().getDocumentsFolderPath(); var path = getFileAccess().getDocumentsFolderPath();
KnownFolders._documents = new Folder(); KnownFolders._documents = new Folder();
@ -297,9 +304,9 @@ export class KnownFolders {
} }
/** /**
* Gets the Temporary (Caches) folder available for the current application. This Folder is private for the application and not accessible from Users/External apps. * Gets the Temporary (Caches) folder available for the current application. This Folder is private for the application and not accessible from Users/External apps.
*/ */
public static Temporary(): Folder { public static temporary(): Folder {
if (!KnownFolders._temp) { if (!KnownFolders._temp) {
var path = getFileAccess().getTempFolderPath(); var path = getFileAccess().getTempFolderPath();
KnownFolders._temp = new Folder(); KnownFolders._temp = new Folder();
@ -312,8 +319,8 @@ export class KnownFolders {
} }
/** /**
* Base class for FileReader and FileWriter APIs. * Base class for FileReader and FileWriter APIs.
*/ */
export class FileAccess { export class FileAccess {
private _file; private _file;
@ -323,36 +330,36 @@ export class FileAccess {
} }
/** /**
* Unlocks the file and allows other operations over it. * Unlocks the file and allows other operations over it.
*/ */
public release() { public release() {
this._file[fileLockedProperty] = false; this._file[fileLockedProperty] = false;
this._file = undefined; this._file = undefined;
} }
/** /**
* Gets the underlying File instance. * Gets the underlying File instance.
*/ */
get file(): File { get file(): File {
return this._file; return this._file;
} }
} }
/** /**
* Enables reading the content of a File entity. * Enables reading the content of a File entity.
*/ */
export class FileReader extends FileAccess { export class FileReader extends FileAccess {
/** /**
* Reads the content of the underlying File as a UTF8 encoded string. * Reads the content of the underlying File as a UTF8 encoded string.
*/ */
public readText(onSuccess: (content: string) => any, onError?: (error: any) => any) { public readText(onSuccess: (content: string) => any, onError?: (error: any) => any) {
getFileAccess().readText(this.file.path, onSuccess, onError); getFileAccess().readText(this.file.path, onSuccess, onError);
} }
} }
/** /**
* Enables saving data to a File entity. * Enables saving data to a File entity.
*/ */
export class FileWriter extends FileAccess { export class FileWriter extends FileAccess {
public writeText(content: string, onSuccess?: () => any, onError?: (error: any) => any) { public writeText(content: string, onSuccess?: () => any, onError?: (error: any) => any) {
getFileAccess().writeText(this.file.path, content, onSuccess, onError); getFileAccess().writeText(this.file.path, content, onSuccess, onError);

View File

@ -1,5 +1,6 @@
import app_module = require("Application/application"); import app_module = require("Application/application");
export class FileSystemAccess { export class FileSystemAccess {
private _encoding = "UTF-8"; private _encoding = "UTF-8";
private _pathSeparator = "/"; private _pathSeparator = "/";
@ -14,18 +15,18 @@ export class FileSystemAccess {
return new Date(javaFile.lastModified()); return new Date(javaFile.lastModified());
} }
public getParent(path: string, onSuccess: (path: string) => any, onError?: (error: any) => any) { public getParent(path: string, onError?: (error: any) => any): string {
try { try {
var javaFile = new java.io.File(path); var javaFile = new java.io.File(path);
if (onSuccess) { var parent = javaFile.getParentFile();
var parent = javaFile.getParentFile(); return parent.getPath();
onSuccess(parent.getPath());
}
} catch (exception) { } catch (exception) {
// TODO: unified approach for error messages // TODO: unified approach for error messages
if (onError) { if (onError) {
onError(exception); onError(exception);
} }
return undefined;
} }
} }

View File

@ -1,10 +1,11 @@
// TODO: Implement "hidden" notation so that such declarations are not included in the d.ts file we will provide for the users. // TODO: Implement "hidden" notation so that such declarations are not included in the d.ts file we will provide for the users.
//@hidden //@hidden
export declare class FileSystemAccess { export declare class FileSystemAccess {
getReadonly(path: string): boolean; getReadonly(path: string): boolean;
getLastModified(path: string): Date; getLastModified(path: string): Date;
getParent(path: string, onSuccess: (path: string) => any, onError?: (error: any) => any); getParent(path: string, onError?: (error: any) => any): string;
getFile(path: string, onSuccess: (path: string) => any, onError?: (error: any) => any); getFile(path: string, onSuccess: (path: string) => any, onError?: (error: any) => any);
getFolder(path: string, onSuccess: (path: string) => any, onError?: (error: any) => any); getFolder(path: string, onSuccess: (path: string) => any, onError?: (error: any) => any);
enumFiles(path: string, onSuccess: (files: Array<string>) => any, onError?: (error: any) => any); enumFiles(path: string, onSuccess: (files: Array<string>) => any, onError?: (error: any) => any);
@ -20,3 +21,4 @@ export declare class FileSystemAccess {
readText(path: string, onSuccess: (content: string) => any, onError?: (error: any) => any); readText(path: string, onSuccess: (content: string) => any, onError?: (error: any) => any);
writeText(path: string, content: string, onSuccess?: () => any, onError?: (error: any) => any); writeText(path: string, content: string, onSuccess?: () => any, onError?: (error: any) => any);
} }

12
declarations.ios.d.ts vendored
View File

@ -60,8 +60,9 @@ declare module Foundation {
} }
export class NSString { export class NSString {
static initWithString(s: string): NSString;
static initWithDataEncoding(data: any, encoding: any): any; static initWithDataEncoding(data: any, encoding: any): any;
static initWithString(source: string): any; stringByDeletingLastPathComponent(): string;
} }
export class NSURLSessionConfiguration { export class NSURLSessionConfiguration {
@ -77,7 +78,14 @@ declare module Foundation {
} }
export class NSURL { export class NSURL {
static URLWithString(url : string): any; static URLWithString(url: string): any;
path(): string;
relativePath(): string;
relativeString(): string;
}
export class NSDate {
timeIntervalSince1970(): number;
} }
export class NSMutableURLRequest { export class NSMutableURLRequest {