Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'fjcaetano-feature/prefetch'
- Loading branch information
Showing
10 changed files
with
244 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
android/src/main/java/com/dylanvann/fastimage/FastImageViewConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.dylanvann.fastimage; | ||
|
||
import android.widget.ImageView; | ||
import android.widget.ImageView.ScaleType; | ||
|
||
import com.bumptech.glide.Priority; | ||
import com.bumptech.glide.load.model.GlideUrl; | ||
import com.bumptech.glide.load.model.LazyHeaders; | ||
import com.facebook.react.bridge.NoSuchKeyException; | ||
import com.facebook.react.bridge.ReadableMap; | ||
import com.facebook.react.bridge.ReadableMapKeySetIterator; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
class FastImageViewConverter { | ||
static GlideUrl glideUrl(ReadableMap source) { | ||
final String uriProp = source.getString("uri"); | ||
// Get the headers prop and add to glideUrl. | ||
GlideUrl glideUrl; | ||
try { | ||
final ReadableMap headersMap = source.getMap("headers"); | ||
ReadableMapKeySetIterator headersIterator = headersMap.keySetIterator(); | ||
LazyHeaders.Builder headersBuilder = new LazyHeaders.Builder(); | ||
while (headersIterator.hasNextKey()) { | ||
String key = headersIterator.nextKey(); | ||
String value = headersMap.getString(key); | ||
headersBuilder.addHeader(key, value); | ||
} | ||
LazyHeaders headers = headersBuilder.build(); | ||
glideUrl = new GlideUrl(uriProp, headers); | ||
} catch (NoSuchKeyException e) { | ||
// If there is no headers object. | ||
glideUrl = new GlideUrl(uriProp); | ||
} | ||
return glideUrl; | ||
} | ||
|
||
private static Map<String, Priority> REACT_PRIORITY_MAP = | ||
new HashMap<String, Priority>() {{ | ||
put("low", Priority.LOW); | ||
put("normal", Priority.NORMAL); | ||
put("high", Priority.HIGH); | ||
}}; | ||
|
||
static Priority priority(ReadableMap source) { | ||
// Get the priority prop. | ||
String priorityProp = "normal"; | ||
try { | ||
priorityProp = source.getString("priority"); | ||
} catch (Exception e) { | ||
// Noop. | ||
} | ||
final Priority priority = REACT_PRIORITY_MAP.get(priorityProp); | ||
return priority; | ||
} | ||
|
||
private static Map<String, ImageView.ScaleType> REACT_RESIZE_MODE_MAP = | ||
new HashMap<String, ImageView.ScaleType>() {{ | ||
put("contain", ScaleType.FIT_CENTER); | ||
put("cover", ScaleType.CENTER_CROP); | ||
put("stretch", ScaleType.FIT_XY); | ||
put("center", ScaleType.CENTER); | ||
}}; | ||
|
||
public static ScaleType scaleType(String resizeMode) { | ||
if (resizeMode == null) resizeMode = "contain"; | ||
final ImageView.ScaleType scaleType = REACT_RESIZE_MODE_MAP.get(resizeMode); | ||
return scaleType; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
android/src/main/java/com/dylanvann/fastimage/FastImageViewModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.dylanvann.fastimage; | ||
|
||
import android.app.Activity; | ||
import android.graphics.Color; | ||
import android.graphics.drawable.ColorDrawable; | ||
import android.graphics.drawable.Drawable; | ||
|
||
import com.bumptech.glide.Glide; | ||
import com.bumptech.glide.Priority; | ||
import com.bumptech.glide.load.engine.DiskCacheStrategy; | ||
import com.bumptech.glide.load.model.GlideUrl; | ||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.bridge.ReactContextBaseJavaModule; | ||
import com.facebook.react.bridge.ReactMethod; | ||
import com.facebook.react.bridge.ReadableArray; | ||
import com.facebook.react.bridge.ReadableMap; | ||
|
||
class FastImageViewModule extends ReactContextBaseJavaModule { | ||
|
||
private static final String REACT_CLASS = "FastImageView"; | ||
|
||
FastImageViewModule(ReactApplicationContext reactContext) { | ||
super(reactContext); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return REACT_CLASS; | ||
} | ||
|
||
private static Drawable TRANSPARENT_DRAWABLE = new ColorDrawable(Color.TRANSPARENT); | ||
|
||
@ReactMethod | ||
public void preload(final ReadableArray sources) { | ||
final Activity activity = getCurrentActivity(); | ||
activity.runOnUiThread(new Runnable() { | ||
@Override | ||
public void run() { | ||
for (int i = 0; i < sources.size(); i++) { | ||
final ReadableMap source = sources.getMap(i); | ||
final GlideUrl glideUrl = FastImageViewConverter.glideUrl(source); | ||
final Priority priority = FastImageViewConverter.priority(source); | ||
Glide | ||
.with(activity.getApplicationContext()) | ||
.load(glideUrl) | ||
.priority(priority) | ||
.placeholder(TRANSPARENT_DRAWABLE) | ||
.diskCacheStrategy(DiskCacheStrategy.SOURCE) | ||
.preload(); | ||
} | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.