1+ package com .dylanvann .fastimage ;
2+
3+ import android .support .annotation .Nullable ;
4+ import android .util .Log ;
5+
6+ import com .bumptech .glide .load .DataSource ;
7+ import com .bumptech .glide .load .engine .GlideException ;
8+ import com .bumptech .glide .request .RequestListener ;
9+ import com .bumptech .glide .request .target .Target ;
10+ import com .facebook .react .bridge .Arguments ;
11+ import com .facebook .react .bridge .ReactApplicationContext ;
12+ import com .facebook .react .bridge .WritableMap ;
13+ import com .facebook .react .modules .core .DeviceEventManagerModule ;
14+
15+ import java .io .File ;
16+
17+ class FastImagePreloaderListener implements RequestListener <File > {
18+ private static final String LOG = "[FFFastImage]" ;
19+ private static final String EVENT_PROGRESS = "fffastimage-progress" ;
20+ private static final String EVENT_COMPLETE = "fffastimage-complete" ;
21+
22+ private final ReactApplicationContext reactContext ;
23+ private final int id ;
24+ private final int total ;
25+ private int succeeded = 0 ;
26+ private int failed = 0 ;
27+
28+ public FastImagePreloaderListener (ReactApplicationContext reactContext , int id , int totalImages ) {
29+ this .id = id ;
30+ this .reactContext = reactContext ;
31+ this .total = totalImages ;
32+ }
33+
34+ @ Override
35+ public boolean onLoadFailed (@ Nullable GlideException e , Object o , Target <File > target , boolean b ) {
36+ // o is whatever was passed to .load() = GlideURL, String, etc.
37+ Log .d (LOG , "Preload failed: " + o .toString ());
38+ this .failed ++;
39+ this .dispatchProgress ();
40+ return false ;
41+ }
42+
43+ @ Override
44+ public boolean onResourceReady (File file , Object o , Target <File > target , DataSource dataSource , boolean b ) {
45+ // o is whatever was passed to .load() = GlideURL, String, etc.
46+ Log .d (LOG , "Preload succeeded: " + o .toString ());
47+ this .succeeded ++;
48+ this .dispatchProgress ();
49+ return false ;
50+ }
51+
52+ private void maybeDispatchComplete () {
53+ if (this .failed + this .succeeded >= this .total ) {
54+ WritableMap params = Arguments .createMap ();
55+ params .putInt ("id" , this .id );
56+ params .putInt ("finished" , this .succeeded + this .failed );
57+ params .putInt ("skipped" , this .failed );
58+ reactContext
59+ .getJSModule (DeviceEventManagerModule .RCTDeviceEventEmitter .class )
60+ .emit (EVENT_COMPLETE , params );
61+ }
62+ }
63+
64+ private void dispatchProgress () {
65+ WritableMap params = Arguments .createMap ();
66+ params .putInt ("id" , this .id );
67+ params .putInt ("finished" , this .succeeded + this .failed );
68+ params .putInt ("total" , this .total );
69+ reactContext
70+ .getJSModule (DeviceEventManagerModule .RCTDeviceEventEmitter .class )
71+ .emit (EVENT_PROGRESS , params );
72+ this .maybeDispatchComplete ();
73+ }
74+ }
0 commit comments