11package com .dylanvann .fastimage ;
22
3+ import static com .dylanvann .fastimage .FastImageRequestListener .REACT_ON_ERROR_EVENT ;
4+ import static com .dylanvann .fastimage .FastImageRequestListener .REACT_ON_LOAD_END_EVENT ;
5+ import static com .dylanvann .fastimage .FastImageRequestListener .REACT_ON_LOAD_EVENT ;
6+
37import android .app .Activity ;
48import android .content .Context ;
59import android .content .ContextWrapper ;
610import android .graphics .PorterDuff ;
711import android .os .Build ;
812
13+ import androidx .annotation .NonNull ;
14+
915import com .bumptech .glide .Glide ;
1016import com .bumptech .glide .RequestManager ;
11- import com .bumptech .glide .load .model .GlideUrl ;
12- import com .bumptech .glide .request .Request ;
1317import com .facebook .react .bridge .ReadableMap ;
1418import com .facebook .react .bridge .WritableMap ;
1519import com .facebook .react .bridge .WritableNativeMap ;
1822import com .facebook .react .uimanager .ThemedReactContext ;
1923import com .facebook .react .uimanager .annotations .ReactProp ;
2024import com .facebook .react .uimanager .events .RCTEventEmitter ;
25+ import com .facebook .react .views .imagehelper .ResourceDrawableIdHelper ;
2126
22- import java .util .ArrayList ;
23- import java .util .Collections ;
2427import java .util .List ;
2528import java .util .Map ;
2629import java .util .WeakHashMap ;
2730
2831import javax .annotation .Nullable ;
2932
30- import static com .dylanvann .fastimage .FastImageRequestListener .REACT_ON_ERROR_EVENT ;
31- import static com .dylanvann .fastimage .FastImageRequestListener .REACT_ON_LOAD_END_EVENT ;
32- import static com .dylanvann .fastimage .FastImageRequestListener .REACT_ON_LOAD_EVENT ;
33-
3433class FastImageViewManager extends SimpleViewManager <FastImageViewWithUrl > implements FastImageProgressListener {
3534
36- private static final String REACT_CLASS = "FastImageView" ;
37- private static final String REACT_ON_LOAD_START_EVENT = "onFastImageLoadStart" ;
38- private static final String REACT_ON_PROGRESS_EVENT = "onFastImageProgress" ;
35+ static final String REACT_CLASS = "FastImageView" ;
36+ static final String REACT_ON_LOAD_START_EVENT = "onFastImageLoadStart" ;
37+ static final String REACT_ON_PROGRESS_EVENT = "onFastImageProgress" ;
3938 private static final Map <String , List <FastImageViewWithUrl >> VIEWS_FOR_URLS = new WeakHashMap <>();
4039
4140 @ Nullable
4241 private RequestManager requestManager = null ;
4342
43+ @ NonNull
4444 @ Override
4545 public String getName () {
4646 return REACT_CLASS ;
4747 }
4848
49+ @ NonNull
4950 @ Override
50- protected FastImageViewWithUrl createViewInstance (ThemedReactContext reactContext ) {
51+ protected FastImageViewWithUrl createViewInstance (@ NonNull ThemedReactContext reactContext ) {
5152 if (isValidContextForGlide (reactContext )) {
5253 requestManager = Glide .with (reactContext );
5354 }
@@ -56,76 +57,15 @@ protected FastImageViewWithUrl createViewInstance(ThemedReactContext reactContex
5657 }
5758
5859 @ ReactProp (name = "source" )
59- public void setSrc (FastImageViewWithUrl view , @ Nullable ReadableMap source ) {
60- if (source == null || !source .hasKey ("uri" ) || isNullOrEmpty (source .getString ("uri" ))) {
61- // Cancel existing requests.
62- clearView (view );
63-
64- if (view .glideUrl != null ) {
65- FastImageOkHttpProgressGlideModule .forget (view .glideUrl .toStringUrl ());
66- }
67- // Clear the image.
68- view .setImageDrawable (null );
69- return ;
70- }
71-
72- //final GlideUrl glideUrl = FastImageViewConverter.getGlideUrl(view.getContext(), source);
73- final FastImageSource imageSource = FastImageViewConverter .getImageSource (view .getContext (), source );
74- if (imageSource .getUri ().toString ().length () == 0 ) {
75- ThemedReactContext context = (ThemedReactContext ) view .getContext ();
76- RCTEventEmitter eventEmitter = context .getJSModule (RCTEventEmitter .class );
77- int viewId = view .getId ();
78- WritableMap event = new WritableNativeMap ();
79- event .putString ("message" , "Invalid source prop:" + source );
80- eventEmitter .receiveEvent (viewId , REACT_ON_ERROR_EVENT , event );
81-
82- // Cancel existing requests.
83- if (requestManager != null ) {
84- requestManager .clear (view );
85- }
86-
87- if (view .glideUrl != null ) {
88- FastImageOkHttpProgressGlideModule .forget (view .glideUrl .toStringUrl ());
89- }
90- // Clear the image.
91- view .setImageDrawable (null );
92- return ;
93- }
94-
95- final GlideUrl glideUrl = imageSource .getGlideUrl ();
96-
97- // Cancel existing request.
98- view .glideUrl = glideUrl ;
99- clearView (view );
100-
101- String key = glideUrl .toStringUrl ();
102- FastImageOkHttpProgressGlideModule .expect (key , this );
103- List <FastImageViewWithUrl > viewsForKey = VIEWS_FOR_URLS .get (key );
104- if (viewsForKey != null && !viewsForKey .contains (view )) {
105- viewsForKey .add (view );
106- } else if (viewsForKey == null ) {
107- List <FastImageViewWithUrl > newViewsForKeys = new ArrayList <>(Collections .singletonList (view ));
108- VIEWS_FOR_URLS .put (key , newViewsForKeys );
109- }
60+ public void setSource (FastImageViewWithUrl view , @ Nullable ReadableMap source ) {
61+ view .setSource (source );
62+ }
11063
111- ThemedReactContext context = (ThemedReactContext ) view .getContext ();
112- RCTEventEmitter eventEmitter = context .getJSModule (RCTEventEmitter .class );
113- int viewId = view .getId ();
114- eventEmitter .receiveEvent (viewId , REACT_ON_LOAD_START_EVENT , new WritableNativeMap ());
115-
116- if (requestManager != null ) {
117- requestManager
118- // This will make this work for remote and local images. e.g.
119- // - file:///
120- // - content://
121- // - res:/
122- // - android.resource://
123- // - data:image/png;base64
124- .load (imageSource .getSourceForLoad ())
125- .apply (FastImageViewConverter .getOptions (context , imageSource , source ))
126- .listener (new FastImageRequestListener (key ))
127- .into (view );
128- }
64+ @ ReactProp (name = "defaultSource" )
65+ public void setDefaultSource (FastImageViewWithUrl view , @ Nullable String source ) {
66+ view .setDefaultSource (
67+ ResourceDrawableIdHelper .getInstance ()
68+ .getResourceDrawable (view .getContext (), source ));
12969 }
13070
13171 @ ReactProp (name = "tintColor" , customType = "Color" )
@@ -144,9 +84,9 @@ public void setResizeMode(FastImageViewWithUrl view, String resizeMode) {
14484 }
14585
14686 @ Override
147- public void onDropViewInstance (FastImageViewWithUrl view ) {
87+ public void onDropViewInstance (@ NonNull FastImageViewWithUrl view ) {
14888 // This will cancel existing requests.
149- clearView (view );
89+ view . clearView (requestManager );
15090
15191 if (view .glideUrl != null ) {
15292 final String key = view .glideUrl .toString ();
@@ -193,11 +133,6 @@ public float getGranularityPercentage() {
193133 return 0.5f ;
194134 }
195135
196- private boolean isNullOrEmpty (final String url ) {
197- return url == null || url .trim ().isEmpty ();
198- }
199-
200-
201136 private static boolean isValidContextForGlide (final Context context ) {
202137 Activity activity = getActivityFromContext (context );
203138
@@ -235,14 +170,14 @@ private static boolean isActivityDestroyed(Activity activity) {
235170 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .JELLY_BEAN_MR1 ) {
236171 return activity .isDestroyed () || activity .isFinishing ();
237172 } else {
238- return activity .isDestroyed () || activity . isFinishing () || activity .isChangingConfigurations ();
173+ return activity .isFinishing () || activity .isChangingConfigurations ();
239174 }
240175
241176 }
242177
243- private void clearView ( FastImageViewWithUrl view ) {
244- if ( requestManager != null && view != null && view . getTag () != null && view . getTag () instanceof Request ) {
245- requestManager . clear (view );
246- }
178+ @ Override
179+ protected void onAfterUpdateTransaction ( @ NonNull FastImageViewWithUrl view ) {
180+ super . onAfterUpdateTransaction (view );
181+ view . onAfterUpdate ( this , requestManager , VIEWS_FOR_URLS );
247182 }
248183}
0 commit comments