-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathFastImageViewWithUrl.java
159 lines (128 loc) · 5.85 KB
/
FastImageViewWithUrl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.dylanvann.fastimage;
import static com.dylanvann.fastimage.FastImageRequestListener.REACT_ON_ERROR_EVENT;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.request.Request;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
class FastImageViewWithUrl extends AppCompatImageView {
private boolean mNeedsReload = false;
private ReadableMap mSource = null;
private Drawable mDefaultSource = null;
public GlideUrl glideUrl;
public FastImageViewWithUrl(Context context) {
super(context);
}
public void setSource(@Nullable ReadableMap source) {
mNeedsReload = true;
mSource = source;
}
public void setDefaultSource(@Nullable Drawable source) {
mNeedsReload = true;
mDefaultSource = source;
}
private boolean isNullOrEmpty(final String url) {
return url == null || url.trim().isEmpty();
}
@SuppressLint("CheckResult")
public void onAfterUpdate(
@Nonnull FastImageViewManager manager,
@Nullable RequestManager requestManager,
@Nonnull Map<String, List<FastImageViewWithUrl>> viewsForUrlsMap) {
if (!mNeedsReload)
return;
if ((mSource == null ||
!mSource.hasKey("uri") ||
isNullOrEmpty(mSource.getString("uri"))) &&
mDefaultSource == null) {
// Cancel existing requests.
clearView(requestManager);
if (glideUrl != null) {
FastImageOkHttpProgressGlideModule.forget(glideUrl.toStringUrl());
}
// Clear the image.
setImageDrawable(null);
return;
}
//final GlideUrl glideUrl = FastImageViewConverter.getGlideUrl(view.getContext(), mSource);
final FastImageSource imageSource = FastImageViewConverter.getImageSource(getContext(), mSource);
if (imageSource != null && imageSource.getUri().toString().length() == 0) {
ThemedReactContext context = (ThemedReactContext) getContext();
RCTEventEmitter eventEmitter = context.getJSModule(RCTEventEmitter.class);
int viewId = getId();
WritableMap event = new WritableNativeMap();
event.putString("message", "Invalid source prop:" + mSource);
eventEmitter.receiveEvent(viewId, REACT_ON_ERROR_EVENT, event);
// Cancel existing requests.
clearView(requestManager);
if (glideUrl != null) {
FastImageOkHttpProgressGlideModule.forget(glideUrl.toStringUrl());
}
// Clear the image.
setImageDrawable(null);
return;
}
// `imageSource` may be null and we still continue, if `defaultSource` is not null
final GlideUrl glideUrl = imageSource == null ? null : imageSource.getGlideUrl();
// Cancel existing request.
this.glideUrl = glideUrl;
clearView(requestManager);
String key = glideUrl == null ? null : glideUrl.toStringUrl();
if (glideUrl != null) {
FastImageOkHttpProgressGlideModule.expect(key, manager);
List<FastImageViewWithUrl> viewsForKey = viewsForUrlsMap.get(key);
if (viewsForKey != null && !viewsForKey.contains(this)) {
viewsForKey.add(this);
} else if (viewsForKey == null) {
List<FastImageViewWithUrl> newViewsForKeys = new ArrayList<>(Collections.singletonList(this));
viewsForUrlsMap.put(key, newViewsForKeys);
}
}
ThemedReactContext context = (ThemedReactContext) getContext();
if (imageSource != null) {
// This is an orphan even without a load/loadend when only loading a placeholder
RCTEventEmitter eventEmitter = context.getJSModule(RCTEventEmitter.class);
int viewId = this.getId();
eventEmitter.receiveEvent(viewId,
FastImageViewManager.REACT_ON_LOAD_START_EVENT,
new WritableNativeMap());
}
if (requestManager != null) {
RequestBuilder<Drawable> builder =
requestManager
// This will make this work for remote and local images. e.g.
// - file:///
// - content://
// - res:/
// - android.resource://
// - data:image/png;base64
.load(imageSource == null ? null : imageSource.getSourceForLoad())
.apply(FastImageViewConverter
.getOptions(context, imageSource, mSource)
.placeholder(mDefaultSource) // show until loaded
.fallback(mDefaultSource)); // null will not be treated as error
if (key != null)
builder.listener(new FastImageRequestListener(key));
builder.into(this);
}
}
public void clearView(@Nullable RequestManager requestManager) {
if (requestManager != null && getTag() != null && getTag() instanceof Request) {
requestManager.clear(this);
}
}
}