@@ -407,6 +407,8 @@ export class Img extends ImageBase {
407407 //TODO: remove as it needs to be added after TS 5.7 change https://github.com/microsoft/TypeScript/pull/59860
408408 [ key : symbol ] : ( ...args : any [ ] ) => any | void ;
409409
410+ public preventPreClearDrawable = false ;
411+
410412 nativeViewProtected : com . nativescript . image . MatrixImageView ;
411413 //@ts -expect-error just for declaration
412414 nativeImageViewProtected : com . nativescript . image . MatrixImageView ;
@@ -442,6 +444,8 @@ export class Img extends ImageBase {
442444 }
443445
444446 public disposeNativeView ( ) {
447+ // Cancel any running Glide requests before dispose
448+ this . cancelCurrentRequest ( ) ;
445449 this . progressCallback = null ;
446450 this . loadSourceCallback = null ;
447451 }
@@ -583,9 +587,35 @@ export class Img extends ImageBase {
583587 }
584588 }
585589
590+ // Clear any active Glide target/request attached to this view.
591+ private cancelCurrentRequest ( ) : void {
592+ const ctx = this . _context ;
593+ if ( ! ctx ) {
594+ return ;
595+ }
596+ if ( this . currentTarget ) {
597+ // cancel and drop reference; Any callbacks from the old target will be swallowed.
598+ try {
599+ com . bumptech . glide . Glide . with ( ctx ) . clear ( this . currentTarget ) ;
600+ } catch ( err ) {
601+ // ignore
602+ }
603+ this . currentTarget = null ;
604+ }
605+ // if (this.nativeViewProtected) {
606+ // try {
607+ // com.bumptech.glide.Glide.with(ctx).clear(this.nativeViewProtected);
608+ // } catch (err) {
609+ // // ignore
610+ // }
611+ // }
612+ }
613+
586614 private loadImageWithGlide ( uri : string ) {
587615 const view = this . nativeViewProtected ;
588616 const context = this . _context ;
617+ // Cancel any prior Glide request/target for this view before starting a new one.
618+ this . cancelCurrentRequest ( ) ;
589619 // Determine if this is a network request
590620 this . isNetworkRequest = typeof uri === 'string' && ( uri . startsWith ( 'http://' ) || uri . startsWith ( 'https://' ) ) ;
591621
@@ -747,6 +777,15 @@ export class Img extends ImageBase {
747777 onLoadFailed ( e : any , model : any , target : any , isFirstResource : boolean ) : boolean {
748778 const instance = owner . get ( ) ;
749779 if ( instance ) {
780+ // If this callback is for a previously canceled request, swallow it to avoid
781+ // emitting/logging errors for obsolete requests.
782+ if ( instance . currentTarget && target !== instance . currentTarget ) {
783+ instance . progressCallback = null ;
784+ instance . loadSourceCallback = null ;
785+ // Swallow: we handled it (don't let Glide default log/clear)
786+ return true ;
787+ }
788+ instance . currentTarget = null ;
750789 instance . progressCallback = null ; // Clean up
751790 instance . loadSourceCallback = null ;
752791 instance . notifyFailure ( e ) ;
@@ -756,6 +795,13 @@ export class Img extends ImageBase {
756795 onResourceReady ( resource : android . graphics . drawable . Drawable , model : any , target : any , dataSource : any , isFirstResource : boolean ) : boolean {
757796 const instance = owner . get ( ) ;
758797 if ( instance ) {
798+ // Ignore if the callback is from a previous request (stale).
799+ if ( instance . currentTarget && target !== instance . currentTarget ) {
800+ instance . progressCallback = null ;
801+ instance . loadSourceCallback = null ;
802+ return true ;
803+ }
804+ instance . currentTarget = null ;
759805 instance . progressCallback = null ; // Clean up
760806 instance . loadSourceCallback = null ;
761807
@@ -802,8 +848,14 @@ export class Img extends ImageBase {
802848 }
803849 } ) ;
804850
805- this . currentTarget = new com . nativescript . image . MatrixDrawableImageViewTarget ( view )
806- requestBuilder . signature ( signature ) . listener ( new com . nativescript . image . CompositeRequestListener ( objectArr ) ) . into ( this . currentTarget ) ;
851+ // Always create and track our custom target so we can detect stale callbacks later.
852+ const target = new com . nativescript . image . MatrixDrawableImageViewTarget ( view ) ;
853+ this . currentTarget = target ;
854+ if ( this . preventPreClearDrawable ) {
855+ target . setClearFirst ( false ) ;
856+ }
857+
858+ requestBuilder . signature ( signature ) . listener ( new com . nativescript . image . CompositeRequestListener ( objectArr ) ) . into ( target ) ;
807859 }
808860
809861 private notifyLoadSource ( source : string ) {
@@ -942,12 +994,7 @@ export class Img extends ImageBase {
942994 this . loadImageWithGlide ( uri ) ;
943995 } else {
944996 // Clear existing request before removing the drawable
945- if ( this . currentTarget ) {
946- com . bumptech . glide . Glide . with ( this . _context ) . clear ( this . currentTarget ) ;
947- this . currentTarget = null ;
948- } else {
949- com . bumptech . glide . Glide . with ( this . _context ) . clear ( view ) ;
950- }
997+ this . cancelCurrentRequest ( ) ;
951998 view . setImageDrawable ( null ) ;
952999 }
9531000 }
@@ -1010,14 +1057,14 @@ export class Img extends ImageBase {
10101057 }
10111058
10121059 startAnimating ( ) {
1013- const drawable = this . nativeViewProtected . getDrawable ( )
1060+ const drawable = this . nativeViewProtected . getDrawable ( ) ;
10141061 if ( drawable && drawable instanceof android . graphics . drawable . Animatable ) {
10151062 drawable . start ( ) ;
10161063 }
10171064 }
10181065
10191066 stopAnimating ( ) {
1020- const drawable = this . nativeViewProtected . getDrawable ( )
1067+ const drawable = this . nativeViewProtected . getDrawable ( ) ;
10211068 if ( drawable && drawable instanceof android . graphics . drawable . Animatable ) {
10221069 drawable . stop ( ) ;
10231070 }
0 commit comments