1
1
package com .dylanvann .fastimage ;
2
2
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
+
3
7
import android .app .Activity ;
4
8
import android .content .Context ;
5
9
import android .content .ContextWrapper ;
6
10
import android .graphics .PorterDuff ;
7
11
import android .os .Build ;
8
12
13
+ import androidx .annotation .NonNull ;
14
+
9
15
import com .bumptech .glide .Glide ;
10
16
import com .bumptech .glide .RequestManager ;
11
- import com .bumptech .glide .load .model .GlideUrl ;
12
- import com .bumptech .glide .request .Request ;
13
17
import com .facebook .react .bridge .ReadableMap ;
14
18
import com .facebook .react .bridge .WritableMap ;
15
19
import com .facebook .react .bridge .WritableNativeMap ;
18
22
import com .facebook .react .uimanager .ThemedReactContext ;
19
23
import com .facebook .react .uimanager .annotations .ReactProp ;
20
24
import com .facebook .react .uimanager .events .RCTEventEmitter ;
25
+ import com .facebook .react .views .imagehelper .ResourceDrawableIdHelper ;
21
26
22
- import java .util .ArrayList ;
23
- import java .util .Collections ;
24
27
import java .util .List ;
25
28
import java .util .Map ;
26
29
import java .util .WeakHashMap ;
27
30
28
31
import javax .annotation .Nullable ;
29
32
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
-
34
33
class FastImageViewManager extends SimpleViewManager <FastImageViewWithUrl > implements FastImageProgressListener {
35
34
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" ;
39
38
private static final Map <String , List <FastImageViewWithUrl >> VIEWS_FOR_URLS = new WeakHashMap <>();
40
39
41
40
@ Nullable
42
41
private RequestManager requestManager = null ;
43
42
43
+ @ NonNull
44
44
@ Override
45
45
public String getName () {
46
46
return REACT_CLASS ;
47
47
}
48
48
49
+ @ NonNull
49
50
@ Override
50
- protected FastImageViewWithUrl createViewInstance (ThemedReactContext reactContext ) {
51
+ protected FastImageViewWithUrl createViewInstance (@ NonNull ThemedReactContext reactContext ) {
51
52
if (isValidContextForGlide (reactContext )) {
52
53
requestManager = Glide .with (reactContext );
53
54
}
@@ -56,76 +57,15 @@ protected FastImageViewWithUrl createViewInstance(ThemedReactContext reactContex
56
57
}
57
58
58
59
@ 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
+ }
110
63
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 ));
129
69
}
130
70
131
71
@ ReactProp (name = "tintColor" , customType = "Color" )
@@ -144,9 +84,9 @@ public void setResizeMode(FastImageViewWithUrl view, String resizeMode) {
144
84
}
145
85
146
86
@ Override
147
- public void onDropViewInstance (FastImageViewWithUrl view ) {
87
+ public void onDropViewInstance (@ NonNull FastImageViewWithUrl view ) {
148
88
// This will cancel existing requests.
149
- clearView (view );
89
+ view . clearView (requestManager );
150
90
151
91
if (view .glideUrl != null ) {
152
92
final String key = view .glideUrl .toString ();
@@ -193,11 +133,6 @@ public float getGranularityPercentage() {
193
133
return 0.5f ;
194
134
}
195
135
196
- private boolean isNullOrEmpty (final String url ) {
197
- return url == null || url .trim ().isEmpty ();
198
- }
199
-
200
-
201
136
private static boolean isValidContextForGlide (final Context context ) {
202
137
Activity activity = getActivityFromContext (context );
203
138
@@ -235,14 +170,14 @@ private static boolean isActivityDestroyed(Activity activity) {
235
170
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .JELLY_BEAN_MR1 ) {
236
171
return activity .isDestroyed () || activity .isFinishing ();
237
172
} else {
238
- return activity .isDestroyed () || activity . isFinishing () || activity .isChangingConfigurations ();
173
+ return activity .isFinishing () || activity .isChangingConfigurations ();
239
174
}
240
175
241
176
}
242
177
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 );
247
182
}
248
183
}
0 commit comments