2
2
3
3
import static com .dylanvann .fastimage .FastImageRequestListener .REACT_ON_ERROR_EVENT ;
4
4
5
+ import androidx .annotation .NonNull ;
5
6
import android .annotation .SuppressLint ;
6
7
import android .content .Context ;
7
8
import android .graphics .drawable .Drawable ;
8
9
9
10
import androidx .annotation .Nullable ;
10
11
import androidx .appcompat .widget .AppCompatImageView ;
11
12
13
+ import com .bumptech .glide .Glide ;
12
14
import com .bumptech .glide .RequestBuilder ;
13
15
import com .bumptech .glide .RequestManager ;
16
+ import com .bumptech .glide .load .DataSource ;
17
+ import com .bumptech .glide .load .engine .GlideException ;
14
18
import com .bumptech .glide .load .model .GlideUrl ;
15
19
import com .bumptech .glide .request .Request ;
20
+ import com .bumptech .glide .request .RequestListener ;
21
+ import com .bumptech .glide .request .target .SimpleTarget ;
22
+ import com .bumptech .glide .request .target .Target ;
23
+ import com .bumptech .glide .request .transition .Transition ;
16
24
import com .facebook .react .bridge .ReadableMap ;
17
25
import com .facebook .react .bridge .WritableMap ;
18
26
import com .facebook .react .bridge .WritableNativeMap ;
19
27
import com .facebook .react .uimanager .ThemedReactContext ;
20
28
import com .facebook .react .uimanager .events .RCTEventEmitter ;
21
29
30
+ import java .io .File ;
22
31
import java .util .ArrayList ;
23
32
import java .util .Collections ;
24
33
import java .util .List ;
@@ -130,9 +139,34 @@ public void onAfterUpdate(
130
139
RCTEventEmitter eventEmitter = context .getJSModule (RCTEventEmitter .class );
131
140
int viewId = this .getId ();
132
141
133
- eventEmitter .receiveEvent (viewId ,
134
- FastImageViewManager .REACT_ON_LOAD_START_EVENT ,
135
- new WritableNativeMap ());
142
+ // Request the URL from cache to see if it exists there and if so pass the cache
143
+ // path as an argument in the onLoadStart event
144
+ requestManager
145
+ .asFile ()
146
+ .load (glideUrl )
147
+ .onlyRetrieveFromCache (true )
148
+ .listener (new RequestListener <File >() {
149
+ @ Override
150
+ public boolean onLoadFailed (@ Nullable GlideException e , Object model , Target <File > target , boolean isFirstResource ) {
151
+ WritableNativeMap result = new WritableNativeMap ();
152
+ result .putNull ("cachePath" );
153
+ eventEmitter .receiveEvent (viewId ,
154
+ FastImageViewManager .REACT_ON_LOAD_START_EVENT ,
155
+ result );
156
+ return false ;
157
+ }
158
+
159
+ @ Override
160
+ public boolean onResourceReady (File resource , Object model , Target <File > target , DataSource dataSource , boolean isFirstResource ) {
161
+ WritableNativeMap result = new WritableNativeMap ();
162
+ result .putString ("cachePath" , resource .getAbsolutePath ());
163
+ eventEmitter .receiveEvent (viewId ,
164
+ FastImageViewManager .REACT_ON_LOAD_START_EVENT ,
165
+ result );
166
+ return false ;
167
+ }
168
+ })
169
+ .submit ();
136
170
}
137
171
138
172
if (requestManager != null ) {
@@ -158,6 +192,25 @@ public void onAfterUpdate(
158
192
builder .listener (new FastImageRequestListener (key ));
159
193
160
194
builder .into (this );
195
+
196
+ // Used specifically to handle the `onLoad` event for the image
197
+ RCTEventEmitter eventEmitter = context .getJSModule (RCTEventEmitter .class );
198
+ int viewId = this .getId ();
199
+ requestManager
200
+ .as (Size .class )
201
+ .load (imageSource == null ? null : imageSource .getSourceForLoad ())
202
+ .into (new SimpleTarget <Size >() {
203
+ @ Override
204
+ public void onResourceReady (@ NonNull Size resource , @ Nullable Transition <? super Size > transition ) {
205
+ WritableMap resourceData = new WritableNativeMap ();
206
+ resourceData .putInt ("width" , resource .width );
207
+ resourceData .putInt ("height" , resource .height );
208
+ eventEmitter .receiveEvent (viewId ,
209
+ "onFastImageLoad" ,
210
+ resourceData
211
+ );
212
+ }
213
+ });
161
214
}
162
215
}
163
216
0 commit comments