-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat:preload callbacks #995
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nicomontanari
wants to merge
27
commits into
DylanVann:main
Choose a base branch
from
nicomontanari:master
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 25 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
ead60db
Add onProgress and onComplete callbacks for `preload` both on Android…
doomsower 5c11dec
Merge branch 'master' into preload_callbacks
doomsower 5f9b91c
Merge remote-tracking branch 'banana/preload_callbacks' into feature/…
Mickagd 179af4c
fix flow
Mickagd 29b01a9
fix: an empty sources array never called onComplete
Elindorath 39d4cd5
Merge pull request #1 from DylanVann/master
Mickagd 4c09121
Merge branch 'master' into feature/preload-callbacks
Mickagd 40cb5b8
fix lint
Mickagd c86abda
update swebimage
Mickagd 1d93c0d
Merge pull request #2 from DylanVann/master
joan-saum 0f67ddb
Merge branch 'master' into feature/preload-callbacks
3812d45
Merge remote-tracking branch 'upstream/master' into feature/preload-c…
6dd7261
fix: revert FastImagePreloaderModule class to FastImageViewModule + a…
70fe646
fix lint
7dffb00
fix NativeEventEmitter mock in test
7dcb4ae
fix NativeEventEmitter mock in test
d7ea691
turn preloadermanager.js to typescript file
440ee83
fix lint + add listener to FastImageViewModule.java
2af79e5
bind context to listeners
f8d13ec
add SDWebImageDownloader.h import in FFFastImagePreloaderManager.m file
bbe6c9d
Merge pull request #4 from Sparted/feature/preload-callbacks-fetch-up…
Mickagd 0ee6e1f
Merge branch 'main' into feature/preload-callbacks
Flictuum 4f88f36
Import FastImageViewNativeModule into index file
nicomontanari a5a45e9
Update FastImageViewNativeModule variable
nicomontanari f5847b3
Merge remote-tracking branch 'upstream/main'
nicomontanari fbb6199
Update android/src/main/java/com/dylanvann/fastimage/FastImagePreload…
nicomontanari 242add2
Update preload manager references
nicomontanari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
74 changes: 74 additions & 0 deletions
74
android/src/main/java/com/dylanvann/fastimage/FastImagePreloaderListener.java
This file contains hidden or 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,74 @@ | ||
package com.dylanvann.fastimage; | ||
|
||
import android.support.annotation.Nullable; | ||
import android.util.Log; | ||
|
||
import com.bumptech.glide.load.DataSource; | ||
import com.bumptech.glide.load.engine.GlideException; | ||
import com.bumptech.glide.request.RequestListener; | ||
import com.bumptech.glide.request.target.Target; | ||
import com.facebook.react.bridge.Arguments; | ||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.bridge.WritableMap; | ||
import com.facebook.react.modules.core.DeviceEventManagerModule; | ||
|
||
import java.io.File; | ||
|
||
class FastImagePreloaderListener implements RequestListener<File> { | ||
private static final String LOG = "[FFFastImage]"; | ||
private static final String EVENT_PROGRESS = "fffastimage-progress"; | ||
private static final String EVENT_COMPLETE = "fffastimage-complete"; | ||
|
||
private final ReactApplicationContext reactContext; | ||
private final int id; | ||
private final int total; | ||
private int succeeded = 0; | ||
private int failed = 0; | ||
|
||
public FastImagePreloaderListener(ReactApplicationContext reactContext, int id, int totalImages) { | ||
this.id = id; | ||
this.reactContext = reactContext; | ||
this.total = totalImages; | ||
} | ||
|
||
@Override | ||
public boolean onLoadFailed(@Nullable GlideException e, Object o, Target<File> target, boolean b) { | ||
// o is whatever was passed to .load() = GlideURL, String, etc. | ||
Log.d(LOG, "Preload failed: " + o.toString()); | ||
this.failed++; | ||
this.dispatchProgress(); | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean onResourceReady(File file, Object o, Target<File> target, DataSource dataSource, boolean b) { | ||
// o is whatever was passed to .load() = GlideURL, String, etc. | ||
Log.d(LOG, "Preload succeeded: " + o.toString()); | ||
this.succeeded++; | ||
this.dispatchProgress(); | ||
return false; | ||
} | ||
|
||
private void maybeDispatchComplete() { | ||
if (this.failed + this.succeeded >= this.total) { | ||
WritableMap params = Arguments.createMap(); | ||
params.putInt("id", this.id); | ||
params.putInt("finished", this.succeeded + this.failed); | ||
params.putInt("skipped", this.failed); | ||
reactContext | ||
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) | ||
.emit(EVENT_COMPLETE, params); | ||
} | ||
} | ||
|
||
private void dispatchProgress() { | ||
WritableMap params = Arguments.createMap(); | ||
params.putInt("id", this.id); | ||
params.putInt("finished", this.succeeded + this.failed); | ||
params.putInt("total", this.total); | ||
reactContext | ||
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) | ||
.emit(EVENT_PROGRESS, params); | ||
this.maybeDispatchComplete(); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,9 @@ | ||
#import "FFFastImageSource.h" | ||
#import <Foundation/Foundation.h> | ||
#import <SDWebImage/SDWebImagePrefetcher.h> | ||
|
||
@interface FFFastImagePreloader : SDWebImagePrefetcher | ||
|
||
@property (nonatomic, readonly) NSNumber* id; | ||
|
||
@end |
This file contains hidden or 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,16 @@ | ||
#import "FFFastImagePreloader.h" | ||
#import "FFFastImageSource.h" | ||
|
||
static int instanceCounter = 0; | ||
|
||
@implementation FFFastImagePreloader | ||
|
||
-(instancetype) init { | ||
if (self = [super init]) { | ||
instanceCounter ++; | ||
_id = [NSNumber numberWithInt:instanceCounter]; | ||
} | ||
return self; | ||
} | ||
|
||
@end |
This file contains hidden or 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,7 @@ | ||
#import <React/RCTBridgeModule.h> | ||
#import <React/RCTEventEmitter.h> | ||
#import <SDWebImage/SDWebImagePrefetcher.h> | ||
|
||
@interface FFFastImagePreloaderManager : RCTEventEmitter <RCTBridgeModule, SDWebImagePrefetcherDelegate> | ||
|
||
@end |
This file contains hidden or 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,79 @@ | ||
#import "FFFastImagePreloaderManager.h" | ||
#import "FFFastImagePreloader.h" | ||
#import "FFFastImageSource.h" | ||
#import "SDWebImageDownloader.h" | ||
|
||
@implementation FFFastImagePreloaderManager | ||
{ | ||
bool _hasListeners; | ||
NSMutableDictionary* _preloaders; | ||
} | ||
|
||
RCT_EXPORT_MODULE(FastImagePreloaderManager); | ||
|
||
- (dispatch_queue_t)methodQueue | ||
{ | ||
return dispatch_queue_create("com.dylanvann.fastimage.FastImagePreloaderManager", DISPATCH_QUEUE_SERIAL); | ||
} | ||
|
||
+ (BOOL)requiresMainQueueSetup | ||
{ | ||
return YES; | ||
} | ||
|
||
-(instancetype) init { | ||
if (self = [super init]) { | ||
_preloaders = [[NSMutableDictionary alloc] init]; | ||
} | ||
return self; | ||
} | ||
|
||
- (NSArray<NSString *> *)supportedEvents | ||
{ | ||
return @[@"fffastimage-progress", @"fffastimage-complete"]; | ||
} | ||
|
||
- (void) imagePrefetcher:(nonnull SDWebImagePrefetcher *)imagePrefetcher | ||
didFinishWithTotalCount:(NSUInteger)totalCount | ||
skippedCount:(NSUInteger)skippedCount | ||
{ | ||
NSNumber* id = ((FFFastImagePreloader*) imagePrefetcher).id; | ||
[_preloaders removeObjectForKey:id]; | ||
[self sendEventWithName:@"fffastimage-complete" | ||
body:@{ @"id": id, @"finished": [NSNumber numberWithLong:totalCount], @"skipped": [NSNumber numberWithLong:skippedCount]} | ||
]; | ||
} | ||
|
||
- (void) imagePrefetcher:(nonnull SDWebImagePrefetcher *)imagePrefetcher | ||
didPrefetchURL:(nullable NSURL *)imageURL | ||
finishedCount:(NSUInteger)finishedCount | ||
totalCount:(NSUInteger)totalCount | ||
{ | ||
NSNumber* id = ((FFFastImagePreloader*) imagePrefetcher).id; | ||
[self sendEventWithName:@"fffastimage-progress" | ||
body:@{ @"id": id, @"finished": [NSNumber numberWithLong:finishedCount], @"total": [NSNumber numberWithLong:totalCount] } | ||
]; | ||
} | ||
|
||
RCT_EXPORT_METHOD(createPreloader:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { | ||
FFFastImagePreloader* preloader = [[FFFastImagePreloader alloc] init]; | ||
preloader.delegate = self; | ||
_preloaders[preloader.id] = preloader; | ||
resolve(preloader.id); | ||
} | ||
|
||
RCT_EXPORT_METHOD(preload:(nonnull NSNumber*)preloaderId sources:(nonnull NSArray<FFFastImageSource *> *)sources) { | ||
NSMutableArray *urls = [NSMutableArray arrayWithCapacity:sources.count]; | ||
|
||
[sources enumerateObjectsUsingBlock:^(FFFastImageSource * _Nonnull source, NSUInteger idx, BOOL * _Nonnull stop) { | ||
[source.headers enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString* header, BOOL *stop) { | ||
[[SDWebImageDownloader sharedDownloader] setValue:header forHTTPHeaderField:key]; | ||
}]; | ||
[urls setObject:source.url atIndexedSubscript:idx]; | ||
}]; | ||
|
||
FFFastImagePreloader* preloader = _preloaders[preloaderId]; | ||
[preloader prefetchURLs:urls]; | ||
} | ||
|
||
@end |
This file contains hidden or 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,87 @@ | ||
import { NativeEventEmitter, NativeModules } from 'react-native' | ||
|
||
import type { EmitterSubscription } from 'react-native' | ||
|
||
import type { | ||
Source, | ||
PreloadProgressHandler, | ||
PreloadCompletionHandler, | ||
} from './index' | ||
|
||
const nativeManager = NativeModules.FastImagePreloaderManager | ||
const nativeEmitter = new NativeEventEmitter(nativeManager) | ||
|
||
type PreloadCallbacks = { | ||
onProgress?: PreloadProgressHandler | ||
onComplete?: PreloadCompletionHandler | ||
} | ||
|
||
type OnProgressParams = { | ||
id: number | ||
finished: number | ||
total: number | ||
} | ||
|
||
type OnCompleteParams = { | ||
id: number | ||
finished: number | ||
skipped: number | ||
} | ||
|
||
class PreloaderManager { | ||
_instances: Map<number, PreloadCallbacks> = new Map() | ||
_subProgress!: EmitterSubscription | ||
_subComplete!: EmitterSubscription | ||
|
||
preload( | ||
sources: Source[], | ||
onProgress?: PreloadProgressHandler, | ||
onComplete?: PreloadCompletionHandler, | ||
) { | ||
nativeManager.createPreloader().then((id: number) => { | ||
if (this._instances.size === 0) { | ||
this._subProgress = nativeEmitter.addListener( | ||
'fffastimage-progress', | ||
this.onProgress.bind(this), | ||
) | ||
this._subComplete = nativeEmitter.addListener( | ||
'fffastimage-complete', | ||
this.onComplete.bind(this), | ||
) | ||
} | ||
|
||
this._instances.set(id, { onProgress, onComplete }) | ||
|
||
nativeManager.preload(id, sources) | ||
}) | ||
} | ||
|
||
onProgress({ id, finished, total }: OnProgressParams) { | ||
const instance = this._instances.get(id) | ||
|
||
if (instance && instance.onProgress) | ||
instance.onProgress(finished, total) | ||
} | ||
|
||
onComplete({ id, finished, skipped }: OnCompleteParams) { | ||
const instance = this._instances.get(id) | ||
|
||
if (instance && instance.onComplete) | ||
instance.onComplete(finished, skipped) | ||
|
||
this._instances.delete(id) | ||
|
||
if ( | ||
this._instances.size === 0 && | ||
this._subProgress && | ||
this._subComplete | ||
) { | ||
this._subProgress.remove() | ||
this._subComplete.remove() | ||
} | ||
} | ||
} | ||
|
||
const preloaderManager = new PreloaderManager() | ||
|
||
export default preloaderManager |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.