Skip to content

Commit dbf352b

Browse files
author
farfromrefug
committed
fix: prevent duplicate calls to initImage
1 parent 0db61df commit dbf352b

File tree

3 files changed

+32
-127
lines changed

3 files changed

+32
-127
lines changed

src/image/index-common.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export const localThumbnailPreviewsEnabledProperty = new Property<ImageBase, boo
150150
export const roundAsCircleProperty = new Property<ImageBase, boolean>({ name: 'roundAsCircle', valueConverter: booleanConverter, affectsLayout: isAndroid });
151151
export const blurRadiusProperty = new Property<ImageBase, number>({ name: 'blurRadius', valueConverter: (v) => parseFloat(v) });
152152
export const blurDownSamplingProperty = new Property<ImageBase, number>({ name: 'blurDownSampling', valueConverter: (v) => parseFloat(v) });
153-
export const imageRotationProperty = new Property<ImageBase, number>({ name: 'imageRotation', valueConverter: (v) => parseFloat(v), defaultValue: 0 , affectsLayout: true});
153+
export const imageRotationProperty = new Property<ImageBase, number>({ name: 'imageRotation', valueConverter: (v) => parseFloat(v), defaultValue: 0, affectsLayout: true });
154154
export const autoPlayAnimationsProperty = new Property<ImageBase, boolean>({ name: 'autoPlayAnimations', valueConverter: booleanConverter });
155155
export const tapToRetryEnabledProperty = new Property<ImageBase, boolean>({ name: 'tapToRetryEnabled', valueConverter: booleanConverter });
156156
export const aspectRatioProperty = new Property<ImageBase, number>({ name: 'aspectRatio', affectsLayout: true, valueConverter: (v) => parseFloat(v) });
@@ -233,6 +233,7 @@ export const needRequestImage = function (target: any, propertyKey: string | sym
233233
this.mNeedRequestImage = true;
234234
return;
235235
}
236+
this.initImage();
236237
return originalMethod.apply(this, args);
237238
};
238239
};

src/image/index.android.ts

Lines changed: 25 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -372,37 +372,6 @@ export class ImageInfo implements ImageInfoBase {
372372
}
373373
}
374374

375-
export const needUpdateHierarchy = function (targetOrNeedsLayout: any, propertyKey?: string | symbol, descriptor?: PropertyDescriptor): any {
376-
if (typeof targetOrNeedsLayout === 'boolean') {
377-
return function (target2: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) {
378-
const originalMethod = descriptor.value;
379-
descriptor.value = function (...args: any[]) {
380-
if (!this.mCanUpdateHierarchy) {
381-
this.mNeedUpdateHierarchy = true;
382-
if (this.isLoaded && targetOrNeedsLayout) {
383-
const layoutParams = (this.nativeViewProtected as android.widget.ImageView)?.getLayoutParams();
384-
if (layoutParams) {
385-
if (layout.getMeasureSpecMode(layoutParams.height) !== layout.EXACTLY || layout.getMeasureSpecMode(layoutParams.width) !== layout.EXACTLY) {
386-
this.mNeedUpdateLayout = true;
387-
}
388-
}
389-
}
390-
return;
391-
}
392-
return originalMethod.apply(this, args);
393-
};
394-
};
395-
}
396-
const originalMethod = descriptor.value;
397-
descriptor.value = function (...args: any[]) {
398-
if (!this.mCanUpdateHierarchy) {
399-
this.mNeedUpdateHierarchy = true;
400-
return;
401-
}
402-
return originalMethod.apply(this, args);
403-
};
404-
};
405-
406375
export class Img extends ImageBase {
407376
//TODO: remove as it needs to be added after TS 5.7 change https://github.com/microsoft/TypeScript/pull/59860
408377
[key: symbol]: (...args: any[]) => any | void;
@@ -413,29 +382,13 @@ export class Img extends ImageBase {
413382
//@ts-expect-error just for declaration
414383
nativeImageViewProtected: com.nativescript.image.MatrixImageView;
415384

416-
mCanUpdateHierarchy = true;
417-
mNeedUpdateHierarchy = false;
418385
mNeedUpdateLayout = false;
419386

420387
private currentTarget: any = null;
421388
private isNetworkRequest = false;
422389
private progressCallback: any = null;
423390
private loadSourceCallback: any = null;
424391

425-
public onResumeNativeUpdates(): void {
426-
this.mCanUpdateHierarchy = false;
427-
super.onResumeNativeUpdates();
428-
this.mCanUpdateHierarchy = true;
429-
if (this.mNeedUpdateHierarchy) {
430-
this.mNeedUpdateHierarchy = false;
431-
this.updateHierarchy();
432-
}
433-
if (this.mNeedUpdateLayout) {
434-
this.mNeedUpdateLayout = false;
435-
this.nativeViewProtected.requestLayout();
436-
}
437-
}
438-
439392
public createNativeView() {
440393
if (!initialized) {
441394
initialize();
@@ -469,15 +422,11 @@ export class Img extends ImageBase {
469422
this.initImage();
470423
}
471424

472-
@needUpdateHierarchy
473-
[placeholderImageUriProperty.setNative]() {
474-
this.updateHierarchy();
475-
}
425+
@needRequestImage
426+
[placeholderImageUriProperty.setNative]() {}
476427

477-
@needUpdateHierarchy
478-
[failureImageUriProperty.setNative]() {
479-
this.updateHierarchy();
480-
}
428+
@needRequestImage
429+
[failureImageUriProperty.setNative]() {}
481430

482431
[stretchProperty.setNative]() {
483432
// Scale type
@@ -489,44 +438,30 @@ export class Img extends ImageBase {
489438
}
490439
}
491440

492-
@needUpdateHierarchy
493-
[fadeDurationProperty.setNative]() {
494-
this.updateHierarchy();
495-
}
441+
@needRequestImage
442+
[fadeDurationProperty.setNative]() {}
496443

497-
@needUpdateHierarchy
498-
[backgroundUriProperty.setNative]() {
499-
this.updateHierarchy();
500-
}
444+
@needRequestImage
445+
[backgroundUriProperty.setNative]() {}
501446

502-
@needUpdateHierarchy
503-
[roundAsCircleProperty.setNative](value) {
504-
this.updateHierarchy();
505-
}
447+
@needRequestImage
448+
[roundAsCircleProperty.setNative](value) {}
506449

507-
@needUpdateHierarchy
508-
[roundTopLeftRadiusProperty.setNative]() {
509-
this.updateHierarchy();
510-
}
450+
@needRequestImage
451+
[roundTopLeftRadiusProperty.setNative]() {}
511452

512453
[imageRotationProperty.setNative](value) {
513454
this.nativeImageViewProtected?.setImageRotation(value);
514455
}
515456

516-
@needUpdateHierarchy
517-
[roundTopRightRadiusProperty.setNative]() {
518-
this.updateHierarchy();
519-
}
457+
@needRequestImage
458+
[roundTopRightRadiusProperty.setNative]() {}
520459

521-
@needUpdateHierarchy
522-
[roundBottomLeftRadiusProperty.setNative]() {
523-
this.updateHierarchy();
524-
}
460+
@needRequestImage
461+
[roundBottomLeftRadiusProperty.setNative]() {}
525462

526-
@needUpdateHierarchy
527-
[roundBottomRightRadiusProperty.setNative]() {
528-
this.updateHierarchy();
529-
}
463+
@needRequestImage
464+
[roundBottomRightRadiusProperty.setNative]() {}
530465

531466
[tintColorProperty.setNative](value: Color) {
532467
if (this.nativeImageViewProtected) {
@@ -535,44 +470,29 @@ export class Img extends ImageBase {
535470
}
536471

537472
@needRequestImage
538-
[blurRadiusProperty.setNative](value) {
539-
this.initImage();
540-
}
473+
[blurRadiusProperty.setNative](value) {}
541474

542475
@needRequestImage
543-
[srcProperty.setNative]() {
544-
this.initImage();
545-
}
476+
[srcProperty.setNative]() {}
546477
@needRequestImage
547-
[decodeWidthProperty.setNative]() {
548-
this.initImage();
549-
}
478+
[decodeWidthProperty.setNative]() {}
550479
@needRequestImage
551-
[decodeHeightProperty.setNative]() {
552-
this.initImage();
553-
}
480+
[decodeHeightProperty.setNative]() {}
554481

555482
@needRequestImage
556-
[lowerResSrcProperty.setNative]() {
557-
this.initImage();
558-
}
483+
[lowerResSrcProperty.setNative]() {}
559484

560485
@needRequestImage
561-
[blurDownSamplingProperty.setNative]() {
562-
this.initImage();
563-
}
486+
[blurDownSamplingProperty.setNative]() {}
564487

565-
@needRequestImage
566488
[aspectRatioProperty.setNative](value) {
567489
if (this.nativeViewProtected) {
568490
this.nativeViewProtected.setAspectRatio(value || 0);
569491
}
570492
}
571493

572494
@needRequestImage
573-
[headersProperty.setNative](value) {
574-
this.initImage();
575-
}
495+
[headersProperty.setNative](value) {}
576496

577497
[backgroundInternalProperty.setNative](value: Background) {
578498
super[backgroundInternalProperty.setNative](value);
@@ -1007,18 +927,6 @@ export class Img extends ImageBase {
1007927
}
1008928
}
1009929

1010-
private updateHierarchy() {
1011-
if (!this.mCanUpdateHierarchy) {
1012-
this.mNeedUpdateHierarchy = true;
1013-
return;
1014-
}
1015-
1016-
// Force reload with new settings
1017-
if (this.nativeImageViewProtected && this.src) {
1018-
this.initImage();
1019-
}
1020-
}
1021-
1022930
private getDrawable(path: string | ImageSource): android.graphics.drawable.Drawable | number {
1023931
if (typeof path === 'string') {
1024932
if (Utils.isFontIconURI(path)) {

src/image/index.ios.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ export class Img extends ImageBase {
299299
//@ts-ignore
300300
nativeImageViewProtected: SDAnimatedImageView | UIImageView;
301301
mCacheKey: string;
302+
placeholderImage: UIImage;
302303

303304
// network detection + notification guard
304305
private _isRemote: boolean = false;
@@ -483,7 +484,7 @@ export class Img extends ImageBase {
483484
private onLoadProgress = (currentSize: number, totalSize: number) => {
484485
const fraction = totalSize > 0 ? currentSize / totalSize : -1;
485486
this.handleImageProgress(fraction, totalSize);
486-
Utils.executeOnMainThread(()=>{
487+
Utils.executeOnMainThread(() => {
487488
const eventName = ImageBase.progressEvent;
488489
if (this.hasListeners(eventName)) {
489490
// Notify progress event
@@ -505,7 +506,7 @@ export class Img extends ImageBase {
505506
} as LoadSourceEventData);
506507
}
507508
}
508-
})
509+
});
509510
};
510511

511512
private getUIImage(imagePath: string | ImageSource) {
@@ -644,14 +645,9 @@ export class Img extends ImageBase {
644645
}
645646
}
646647
@needRequestImage
647-
[srcProperty.setNative](value) {
648-
this.initImage();
649-
}
648+
[srcProperty.setNative](value) {}
650649
@needRequestImage
651-
[imageRotationProperty.setNative](value) {
652-
this.initImage();
653-
}
654-
placeholderImage: UIImage;
650+
[imageRotationProperty.setNative](value) {}
655651
@needRequestImage
656652
[placeholderImageUriProperty.setNative]() {}
657653

0 commit comments

Comments
 (0)