Skip to content

Commit

Permalink
feat: noCache property
Browse files Browse the repository at this point in the history
  • Loading branch information
farfromrefug committed Apr 27, 2020
1 parent 49fe919 commit 11fcd32
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 25 deletions.
3 changes: 3 additions & 0 deletions src/image-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export class ImageBase extends View {
public decodeWidth: number;
public decodeHeight: number;
alwaysFade: boolean;
noCache: boolean
// fade: boolean;
tintColor: Color;
// transition: Transition;
Expand Down Expand Up @@ -155,6 +156,7 @@ export class ImageBase extends View {
public static alwaysFadeProperty = new Property<ImageBase, boolean>({ name: 'alwaysFade', valueConverter: booleanConverter, defaultValue: false });
// public static fadeProperty = new Property<ImageBase, boolean>({ name: 'fade', valueConverter: booleanConverter });
public static fadeDurationProperty = new Property<ImageBase, number>({ name: 'fadeDuration', valueConverter: v => parseFloat(v) });
public static noCacheProperty = new Property<ImageBase, boolean>({ name: 'noCache', defaultValue:false, valueConverter: booleanConverter });

protected handleImageProgress(value: number, totalSize?: number) {
// console.log('handleImageProgress ', value, totalSize);
Expand Down Expand Up @@ -185,3 +187,4 @@ ImageBase.aspectRatioProperty.register(ImageBase);
ImageBase.decodeWidthProperty.register(ImageBase);
ImageBase.decodeHeightProperty.register(ImageBase);
ImageBase.alwaysFadeProperty.register(ImageBase);
ImageBase.noCacheProperty.register(ImageBase);
60 changes: 35 additions & 25 deletions src/image.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import { ImageAsset } from '@nativescript/core/image-asset/image-asset';

let BaseDataSubscriber: new (onNewResult: () => void, onFailure: () => void) => com.facebook.datasource.BaseDataSubscriber<any>;


let initialized = false;
let initializeConfig:ImagePipelineConfigSetting
let initializeConfig: ImagePipelineConfigSetting;
export function initialize(config?: ImagePipelineConfigSetting): void {
if (!initialized) {
const context = application.android.context;
Expand All @@ -21,9 +20,7 @@ export function initialize(config?: ImagePipelineConfigSetting): void {
return;
}
if (config && config.isDownsampleEnabled) {
const imagePipelineConfig = com.facebook.imagepipeline.core.ImagePipelineConfig.newBuilder(context)
.setDownsampleEnabled(true)
.build();
const imagePipelineConfig = com.facebook.imagepipeline.core.ImagePipelineConfig.newBuilder(context).setDownsampleEnabled(true).build();
com.facebook.drawee.backends.pipeline.Fresco.initialize(context, imagePipelineConfig);
} else {
com.facebook.drawee.backends.pipeline.Fresco.initialize(context);
Expand All @@ -47,7 +44,7 @@ export function getImagePipeline(): ImagePipeline {
export function shutDown(): void {
if (!initialized) {
return;
}
}
initialized = false;
com.facebook.drawee.view.SimpleDraweeView.shutDown();
com.facebook.drawee.backends.pipeline.Fresco.shutDown();
Expand Down Expand Up @@ -101,10 +98,7 @@ function getUri(src: string | ImageAsset) {
const resName = imagePath.substr(utils.RESOURCE_PREFIX.length);
const identifier = res.getIdentifier(resName, 'drawable', utils.ad.getApplication().getPackageName());
if (0 < identifier) {
uri = new android.net.Uri.Builder()
.scheme(com.facebook.common.util.UriUtil.LOCAL_RESOURCE_SCHEME)
.path(java.lang.String.valueOf(identifier))
.build();
uri = new android.net.Uri.Builder().scheme(com.facebook.common.util.UriUtil.LOCAL_RESOURCE_SCHEME).path(java.lang.String.valueOf(identifier)).build();
}
} else if (imagePath.indexOf('~/') === 0) {
uri = android.net.Uri.parse(`file:${fs.path.join(fs.knownFolders.currentApp().path, imagePath.replace('~/', ''))}`);
Expand Down Expand Up @@ -334,19 +328,19 @@ export class FailureEventData extends EventData {
}
}

export const needUpdateHierarchy = function(target: any, propertyKey: string | Symbol, descriptor: PropertyDescriptor) {
export const needUpdateHierarchy = function (target: any, propertyKey: string | Symbol, descriptor: PropertyDescriptor) {
let originalMethod = descriptor.value;
descriptor.value = function(...args: any[]) {
descriptor.value = function (...args: any[]) {
if (!this._canUpdateHierarchy) {
this._needUpdateHierarchy = true;
return;
}
return originalMethod.apply(this, args);
};
};
export const needRequestImage = function(target: any, propertyKey: string | Symbol, descriptor: PropertyDescriptor) {
export const needRequestImage = function (target: any, propertyKey: string | Symbol, descriptor: PropertyDescriptor) {
let originalMethod = descriptor.value;
descriptor.value = function(...args: any[]) {
descriptor.value = function (...args: any[]) {
if (!this._canRequestImage) {
this._needRequestImage = true;
return;
Expand Down Expand Up @@ -418,7 +412,6 @@ export class Img extends ImageBase {
this.src = src;
}


@needUpdateHierarchy
[ImageBase.placeholderImageUriProperty.setNative]() {
this.updateHierarchy();
Expand Down Expand Up @@ -497,12 +490,12 @@ export class Img extends ImageBase {
[ImageBase.srcProperty.setNative]() {
this.initImage();
}

@needRequestImage
[ImageBase.lowerResSrcProperty.setNative]() {
this.initImage();
}

@needRequestImage
[ImageBase.blurDownSamplingProperty.setNative]() {
this.initImage();
Expand All @@ -525,6 +518,23 @@ export class Img extends ImageBase {
if (src instanceof ImageSource) {
this.nativeViewProtected.setImageBitmap(src.android);
return;
} else if (utils.isFontIconURI(src as string)) {
const fontIconCode = (src as string).split('//')[1];
if (fontIconCode !== undefined) {
// support sync mode only
const font = this.style.fontInternal;
const color = this.style.color;
this.nativeViewProtected.setImageBitmap(ImageSource.fromFontIconCodeSync(fontIconCode, font, color).android);
}
return;
}
if (this.noCache) {
const uri = getUri(src);
const imagePipeLine = getImagePipeline();
const isInCache = imagePipeLine.isInBitmapMemoryCache(uri);
if (isInCache) {
imagePipeLine.evictFromCache(uri);
}
}
this.isLoading = true;
const uri = getUri(src);
Expand Down Expand Up @@ -568,7 +578,7 @@ export class Img extends ImageBase {
eventName: ImageBase.finalImageSetEvent,
object: that.get(),
imageInfo: info,
animatable: animatable as AnimatedImage
animatable: animatable as AnimatedImage,
} as FinalEventData;

nativeView.notify(args);
Expand All @@ -585,7 +595,7 @@ export class Img extends ImageBase {
const args: FailureEventData = {
eventName: ImageBase.failureEvent,
object: nativeView,
error: imageError
error: imageError,
} as FailureEventData;

that.get().notify(args);
Expand All @@ -601,7 +611,7 @@ export class Img extends ImageBase {
const args: FailureEventData = {
eventName: ImageBase.intermediateImageFailedEvent,
object: nativeView,
error: imageError
error: imageError,
} as FailureEventData;

that.get().notify(args);
Expand All @@ -617,7 +627,7 @@ export class Img extends ImageBase {
const args: IntermediateEventData = {
eventName: ImageBase.intermediateImageSetEvent,
object: nativeView,
imageInfo: info
imageInfo: info,
} as IntermediateEventData;

that.get().notify(args);
Expand All @@ -631,7 +641,7 @@ export class Img extends ImageBase {
if (nativeView) {
const args: EventData = {
eventName: ImageBase.releaseEvent,
object: nativeView
object: nativeView,
} as EventData;

that.get().notify(args);
Expand All @@ -645,14 +655,14 @@ export class Img extends ImageBase {
if (nativeView) {
const args: EventData = {
eventName: ImageBase.submitEvent,
object: nativeView
object: nativeView,
} as EventData;

that.get().notify(args);
} else {
console.log("Warning: WeakRef<Image> was GC and no 'submitEvent' callback will be raised.");
}
}
},
});
const builder = com.facebook.drawee.backends.pipeline.Fresco.newDraweeControllerBuilder();
builder.setImageRequest(request);
Expand All @@ -666,7 +676,7 @@ export class Img extends ImageBase {
},
onImageVisibilityUpdated(param0: com.facebook.drawee.backends.pipeline.info.ImagePerfData, param1: number) {
CLog(CLogTypes.info, 'onImageVisibilityUpdated', param0, param1);
}
},
})
);
}
Expand Down
18 changes: 18 additions & 0 deletions src/image.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,24 @@ export class Img extends ImageBase {
if (src instanceof ImageSource) {
this._setNativeImage(src.ios);
return;
} else if (utils.isFontIconURI(src as string)) {
const fontIconCode = (src as string).split("//")[1];
if (fontIconCode !== undefined) {
// support sync mode only
const font = this.style.fontInternal;
const color = this.style.color;
this._setNativeImage(ImageSource.fromFontIconCodeSync(fontIconCode, font, color).ios);
}
return;
}

if (this.noCache) {
const uri = getUri(src);
const imagePipeLine = getImagePipeline();
const isInCache = imagePipeLine.isInBitmapMemoryCache(uri);
if (isInCache) {
imagePipeLine.evictFromCache(uri);
}
}
this.isLoading = true;
let options = SDWebImageOptions.ScaleDownLargeImages | SDWebImageOptions.AvoidAutoSetImage;
Expand Down

0 comments on commit 11fcd32

Please sign in to comment.