mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-02-06 17:27:54 +08:00
Summary: This PR adds 4 native events to NetworkImage.  Using these events I could wrap `Image` component into something like: ```javascript class NetworkImage extends React.Component { getInitialState() { return { downloading: false, progress: 0 } } render() { var loader = this.state.downloading ? <View style={this.props.loaderStyles}> <ActivityIndicatorIOS animating={true} size={'large'} /> <Text style={{color: '#bbb'}}>{this.state.progress}%</Text> </View> : null; return <Image source={this.props.source} onLoadStart={() => this.setState({downloading: true}) } onLoaded={() => this.setState({downloading: false}) } onLoadProgress={(e)=> this.setState({progress: Math.round(100 * e.nativeEvent.written / e.nativeEvent.total)}); onLoadError={(e)=> { alert('the image cannot be downloaded because: ', JSON.stringify(e)); this.setState({downloading: false}); }}> {loader} </Image> } } ``` Useful on slow connections and server errors. There are dozen lines of Objective C, which I don't have experience with. There are neither specific tests nor documentation yet. And I do realize that you're already working right now on better `<Image/>` (pipeline, new asset management, etc.). So this is basically a proof concept of events for images, and if this idea is not completely wrong I could improve it or help somehow. Closes https://github.com/facebook/react-native/pull/1318 Github Author: Dmitriy Loktev <unknownliveid@hotmail.com>
70 lines
2.1 KiB
Objective-C
70 lines
2.1 KiB
Objective-C
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*/
|
|
|
|
|
|
#import "RCTDownloadTaskWrapper.h"
|
|
|
|
@implementation RCTDownloadTaskWrapper
|
|
{
|
|
NSURLSession *_URLSession;
|
|
}
|
|
|
|
- (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)configuration delegateQueue:(NSOperationQueue *)delegateQueue
|
|
{
|
|
if ((self = [super init])) {
|
|
_URLSession = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
|
|
}
|
|
|
|
return self;
|
|
}
|
|
|
|
- (NSURLSessionDownloadTask *)downloadData:(NSURL *)url progressBlock:(RCTDataProgressBlock)progressBlock completionBlock:(RCTDataCompletionBlock)completionBlock
|
|
{
|
|
self.completionBlock = completionBlock;
|
|
self.progressBlock = progressBlock;
|
|
|
|
NSURLSessionDownloadTask *task = [_URLSession downloadTaskWithURL:url completionHandler:nil];
|
|
[task resume];
|
|
return task;
|
|
}
|
|
|
|
#pragma mark - NSURLSessionTaskDelegate methods
|
|
|
|
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
|
|
{
|
|
if (self.completionBlock) {
|
|
NSData *data = [NSData dataWithContentsOfURL:location];
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
self.completionBlock(downloadTask.response, data, nil);
|
|
});
|
|
}
|
|
}
|
|
|
|
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)didWriteData totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite;
|
|
{
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
if (self.progressBlock != nil) {
|
|
self.progressBlock(totalBytesWritten, totalBytesExpectedToWrite);
|
|
}
|
|
});
|
|
}
|
|
|
|
- (void)URLSession:(NSURLSession *)session
|
|
task:(NSURLSessionTask *)task
|
|
didCompleteWithError:(NSError *)error
|
|
{
|
|
if (error && self.completionBlock) {
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
self.completionBlock(NULL, NULL, error);
|
|
});
|
|
}
|
|
}
|
|
|
|
@end
|