Skip to content

Commit 6a84bdc

Browse files
authored
fix(android): update view imageSource when image is set asynchronously (#11215)
1 parent aa92b47 commit 6a84bdc

9 files changed

Lines changed: 130 additions & 95 deletions

File tree

-209 Bytes
Binary file not shown.

packages/core/ui/image/image-common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export abstract class ImageBase extends View implements ImageDefinition {
124124
);
125125
}
126126
} else if (value instanceof ImageSource) {
127-
// Support binding the imageSource trough the src property
127+
// Support binding the imageSource through the src property
128128

129129
// This will help avoid cleanup on the actual provided image source in case view gets disposed
130130
this.imageSource = new ImageSource(value.getNativeSource());

packages/core/ui/image/index.android.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,22 @@ function initializeImageLoadedListener() {
3333
return global.__native(this);
3434
}
3535

36-
onImageLoaded(success: boolean): void {
36+
public handlesImageUpdate(): boolean {
37+
return true;
38+
}
39+
40+
onImageLoaded(bitmapOrDrawable: android.graphics.Bitmap | android.graphics.drawable.Drawable): void {
3741
const owner = this.owner;
3842
if (owner) {
43+
owner.imageSource = new ImageSource(bitmapOrDrawable);
44+
owner.isLoading = false;
45+
}
46+
}
47+
48+
public onImageLoadingError(e: java.lang.Exception): void {
49+
const owner = this.owner;
50+
if (owner) {
51+
owner.imageSource = null;
3952
owner.isLoading = false;
4053
}
4154
}
@@ -45,7 +58,7 @@ function initializeImageLoadedListener() {
4558
}
4659

4760
export class Image extends ImageBase {
48-
nativeViewProtected: org.nativescript.widgets.ImageView;
61+
declare nativeViewProtected: org.nativescript.widgets.ImageView;
4962

5063
public useCache = true;
5164

@@ -86,7 +99,6 @@ export class Image extends ImageBase {
8699

87100
if (!value) {
88101
imageView.setUri(null, 0, 0, false, false, true);
89-
90102
return;
91103
}
92104

packages/core/ui/image/index.ios.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { SDK_VERSION } from '../../utils/constants';
99
export * from './image-common';
1010

1111
export class Image extends ImageBase {
12-
nativeViewProtected: UIImageView;
12+
declare nativeViewProtected: UIImageView;
1313
private _imageSourceAffectsLayout = true;
1414
private _templateImageWasCreated: boolean;
1515

packages/types-android/src/lib/android/org.nativescript.widgets.d.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,12 +557,16 @@
557557

558558
export namespace Worker {
559559
interface IOnImageLoadedListener {
560-
onImageLoaded(success: boolean): void;
560+
handlesImageUpdate(): boolean;
561+
onImageLoaded(bitmapOrDrawable: globalAndroid.graphics.Bitmap | globalAndroid.graphics.drawable.Drawable): void;
562+
onImageLoadingError(e: java.lang.Exception): void;
561563
}
562564

563565
export class OnImageLoadedListener implements IOnImageLoadedListener {
564566
constructor(implementation: IOnImageLoadedListener);
565-
public onImageLoaded(success: boolean): void;
567+
public handlesImageUpdate(): boolean;
568+
public onImageLoaded(bitmapOrDrawable: globalAndroid.graphics.Bitmap | globalAndroid.graphics.drawable.Drawable): void;
569+
public onImageLoadingError(e: java.lang.Exception): void;
566570
}
567571
}
568572

packages/ui-mobile-base/android/widgets/src/main/java/org/nativescript/widgets/BorderDrawable.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -867,8 +867,7 @@ public String toDebugString() {
867867
"backgroundBitmap: " + this.backgroundBitmap + "; " +
868868
"backgroundRepeat: " + this.backgroundRepeat + "; " +
869869
"backgroundPosition: " + this.backgroundPosition + "; " +
870-
"backgroundSize: " + this.backgroundSize + "; "
871-
;
870+
"backgroundSize: " + this.backgroundSize + "; ";
872871
}
873872

874873
@Override
@@ -879,13 +878,13 @@ public void setBitmap(Bitmap value) {
879878
}
880879

881880
@Override
882-
public void setDrawable(Drawable asyncDrawable) {
883-
drawable = asyncDrawable;
881+
public Drawable getDrawable() {
882+
return drawable;
884883
}
885884

886885
@Override
887-
public Drawable getDrawable() {
888-
return drawable;
886+
public void setDrawable(Drawable value) {
887+
drawable = value;
889888
}
890889

891890

packages/ui-mobile-base/android/widgets/src/main/java/org/nativescript/widgets/ImageView.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ public void setBitmap(Bitmap value) {
428428
}
429429

430430
@Override
431-
public void setDrawable(Drawable asyncDrawable) {
432-
this.setImageDrawable(asyncDrawable);
431+
public void setDrawable(Drawable value) {
432+
this.setImageDrawable(value);
433433
}
434434
}

packages/ui-mobile-base/android/widgets/src/main/java/org/nativescript/widgets/image/BitmapOwner.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99

1010
public interface BitmapOwner {
1111
void setBitmap(Bitmap value);
12-
13-
void setDrawable(Drawable asyncDrawable);
14-
12+
void setDrawable(Drawable value);
1513
Drawable getDrawable();
16-
1714
}

0 commit comments

Comments
 (0)