refactor(image-source): throw if source is not a correct native instance (#5273)

* refactor(image-source): throw if source is not a correct native instance

* refactor(image-source): fromNativeSource and setNativeSource methods

* refactor: update image-source.d.ts file

BREAKING CHANGE:

Change the return type of `setNativeSource` method from `boolean` to `void`.
This commit is contained in:
Vasil Chimev
2018-01-23 16:57:29 +02:00
committed by GitHub
parent 1f77301453
commit 58d61caf59
3 changed files with 16 additions and 12 deletions

View File

@@ -139,9 +139,11 @@ export class ImageSource implements ImageSourceDefinition {
});
}
public setNativeSource(source: any): boolean {
public setNativeSource(source: any): void {
if (source && !(source instanceof android.graphics.Bitmap)) {
throw new Error("The method setNativeSource() expects android.graphics.Bitmap instance.");
}
this.android = source;
return source != null;
}
public saveToFile(path: string, format: "png" | "jpeg" | "jpg", quality = 100): boolean {
@@ -239,8 +241,9 @@ export function fromBase64(source: string): ImageSource {
}
export function fromNativeSource(source: any): ImageSource {
const image = new ImageSource();
return image.setNativeSource(source) ? image : null;
const imageSource = new ImageSource();
imageSource.setNativeSource(source);
return imageSource;
}
export function fromUrl(url: string): Promise<ImageSourceDefinition> {

View File

@@ -88,11 +88,11 @@ export class ImageSource {
fromBase64(source: string): Promise<boolean>;
/**
* Sets the provided native source object (typically a Bitmap).
* Sets the provided native source object (typically a Bitmap or a UIImage).
* This will update either the android or ios properties, depending on the target os.
* @param source The native image object. Will be either a Bitmap for Android or a UIImage for iOS.
*/
setNativeSource(source: any): boolean;
setNativeSource(source: any): void;
/**
* Saves this instance to the specified file, using the provided image format and quality.

View File

@@ -118,11 +118,11 @@ export class ImageSource implements ImageSourceDefinition {
});
}
public setNativeSource(source: any): boolean {
if (source instanceof UIImage) {
this.ios = source;
public setNativeSource(source: any): void {
if (source && !(source instanceof UIImage)) {
throw new Error("The method setNativeSource() expects UIImage instance.");
}
return source != null;
this.ios = source;
}
public saveToFile(path: string, format: "png" | "jpeg" | "jpg", quality?: number): boolean {
@@ -221,8 +221,9 @@ export function fromBase64(source: string): ImageSource {
}
export function fromNativeSource(source: any): ImageSource {
const image = new ImageSource();
return image.setNativeSource(source) ? image : null;
const imageSource = new ImageSource();
imageSource.setNativeSource(source);
return imageSource;
}
export function fromUrl(url: string): Promise<ImageSource> {