From 1d38be3d3d5df30ff138d4f692c992cf60cf5a38 Mon Sep 17 00:00:00 2001 From: Elad Gil Date: Mon, 16 Jul 2018 17:29:41 +0300 Subject: [PATCH] changer all the "download" to "downloader" since npm doesn't like the name --- README.md | 42 +++++++++---------- .../com/eko/RNBackgroundDownloadModule.java | 8 ++-- .../com/eko/RNBackgroundDownloadPackage.java | 4 +- index.js | 38 ++++++++--------- ...undDownload.h => RNBackgroundDownloader.h} | 2 +- ...undDownload.m => RNBackgroundDownloader.m} | 2 +- .../project.pbxproj | 36 ++++++++-------- package.json | 4 +- 8 files changed, 68 insertions(+), 68 deletions(-) rename ios/{RNBackgroundDownload.h => RNBackgroundDownloader.h} (82%) rename ios/{RNBackgroundDownload.m => RNBackgroundDownloader.m} (99%) rename ios/{RNBackgroundDownload.xcodeproj => RNBackgroundDownloader.xcodeproj}/project.pbxproj (82%) diff --git a/README.md b/README.md index 936dfda..01155bf 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -# react-native-background-download +# react-native-background-downloader A library for React-Native to help you download large files on iOS and Android both in the foreground and most importantly in the background. @@ -12,7 +12,7 @@ On Android we are simulating this process with a separate service dedicated to j The real challenge using this method is making sure the app's UI if always up-to-date with the downloads that are happening in another process because your app might startup from scratch while the downloads are still running. -`react-native-background-download` gives you an easy API to both downloading large files and re-attaching to those downloads once your app launches again. +`react-native-background-downloader` gives you an easy API to both downloading large files and re-attaching to those downloads once your app launches again. ## ToC @@ -22,11 +22,11 @@ The real challenge using this method is making sure the app's UI if always up-to ## Getting started -`$ npm install react-native-background-download --save` +`$ npm install react-native-background-downloader --save` ### Mostly automatic installation -`$ react-native link react-native-background-download` +`$ react-native link react-native-background-downloader` ### Manual installation @@ -34,36 +34,36 @@ The real challenge using this method is making sure the app's UI if always up-to #### iOS 1. In XCode, in the project navigator, right click `Libraries` ➜ `Add Files to [your project's name]` -2. Go to `node_modules` ➜ `react-native-background-download` and add `RNBackgroundDownload.xcodeproj` -3. In XCode, in the project navigator, select your project. Add `libRNBackgroundDownload.a` to your project's `Build Phases` ➜ `Link Binary With Libraries` +2. Go to `node_modules` ➜ `react-native-background-downloader` and add `RNBackgroundDownloader.xcodeproj` +3. In XCode, in the project navigator, select your project. Add `libRNBackgroundDownloader.a` to your project's `Build Phases` ➜ `Link Binary With Libraries` 4. Run your project (`Cmd+R`) #### Android 1. Open up `android/app/src/main/java/[...]/MainActivity.java` - - Add `import com.eko.RNBackgroundDownloadPackage;` to the imports at the top of the file - - Add `new RNBackgroundDownloadPackage()` to the list returned by the `getPackages()` method + - Add `import com.eko.RNBackgroundDownloaderPackage;` to the imports at the top of the file + - Add `new RNBackgroundDownloaderPackage()` to the list returned by the `getPackages()` method 2. Append the following lines to `android/settings.gradle`: ``` - include ':react-native-background-download' - project(':react-native-background-download').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-background-download/android') + include ':react-native-background-downloader' + project(':react-native-background-downloader').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-background-downloader/android') ``` 3. Insert the following lines inside the dependencies block in `android/app/build.gradle`: ``` - compile project(':react-native-background-download') + compile project(':react-native-background-downloader') ``` ### iOS - Extra Mandatory Step In your `AppDelegate.m` add the following code: ```objc ... -#import +#import ... - (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler { - [RNBackgroundDownload setCompletionHandlerWithIdentifier:identifier completionHandler:completionHandler]; + [RNBackgroundDownloader setCompletionHandlerWithIdentifier:identifier completionHandler:completionHandler]; } ... @@ -75,12 +75,12 @@ Failing to add this code will result in canceled background downloads. ### Downloading a file ```javascript -import RNBackgroundDownload from 'react-native-background-download'; +import RNBackgroundDownloader from 'react-native-background-downloader'; -let task = RNBackgroundDownload.download({ +let task = RNBackgroundDownloader.download({ id: 'file123', url: 'https://link-to-very.large/file.zip' - destination: `${RNBackgroundDownload.directories.documents}/file.zip` + destination: `${RNBackgroundDownloader.directories.documents}/file.zip` }).begin((expectedBytes) => { console.log(`Going to download ${expectedBytes} bytes!`); }).progress((percent) => { @@ -110,9 +110,9 @@ What happens to your downloads after the OS stopped your app? Well, they are sti Add this code to app's init stage, and you'll never lose a download again! ```javascript -import RNBackgroundDownload from 'react-native-background-download'; +import RNBackgroundDownloader from 'react-native-background-downloader'; -let lostTasks = await RNBackgroundDownload.checkForExistingDownloads(); +let lostTasks = await RNBackgroundDownloader.checkForExistingDownloads(); for (let task of lostTask) { console.log(`Task ${task.id} was found!`); task.progress((percent) => { @@ -129,7 +129,7 @@ for (let task of lostTask) { ## API -### RNBackgroundDownload +### RNBackgroundDownloader ### `download(options)` @@ -161,12 +161,12 @@ Checks for downloads that ran in background while you app was terminated. Recomm ### DownloadTask -A class representing a download task created by `RNBackgroundDownload.download` +A class representing a download task created by `RNBackgroundDownloader.download` ### `Members` | Name | Type | Info | | -------------- | ------ | ---------------------------------------------------------------------------------------------------- | -| `id` | String | The id you gave the task when calling `RNBackgroundDownload.download` | +| `id` | String | The id you gave the task when calling `RNBackgroundDownloader.download` | | `percent` | Number | The current percent of completion of the task between 0 and 1 | | `bytesWritten` | Number | The number of bytes currently written by the task | | `totalBytes` | Number | The number bytes expected to be written by this task or more plainly, the file size being downloaded | diff --git a/android/src/main/java/com/eko/RNBackgroundDownloadModule.java b/android/src/main/java/com/eko/RNBackgroundDownloadModule.java index 338727d..8923887 100644 --- a/android/src/main/java/com/eko/RNBackgroundDownloadModule.java +++ b/android/src/main/java/com/eko/RNBackgroundDownloadModule.java @@ -37,7 +37,7 @@ import java.util.Map; import javax.annotation.Nullable; -public class RNBackgroundDownloadModule extends ReactContextBaseJavaModule implements FetchListener { +public class RNBackgroundDownloaderModule extends ReactContextBaseJavaModule implements FetchListener { private static final int TASK_RUNNING = 0; private static final int TASK_SUSPENDED = 1; @@ -64,11 +64,11 @@ public class RNBackgroundDownloadModule extends ReactContextBaseJavaModule imple private Date lastProgressReport = new Date(); private HashMap progressReports = new HashMap<>(); - public RNBackgroundDownloadModule(ReactApplicationContext reactContext) { + public RNBackgroundDownloaderModule(ReactApplicationContext reactContext) { super(reactContext); loadConfigMap(); - fetch = new Fetch.Builder(this.getReactApplicationContext(), "RNBackgroundDownload") + fetch = new Fetch.Builder(this.getReactApplicationContext(), "RNBackgroundDownloader") .setDownloadConcurrentLimit(4) .build(); fetch.addListener(this); @@ -81,7 +81,7 @@ public class RNBackgroundDownloadModule extends ReactContextBaseJavaModule imple @Override public String getName() { - return "RNBackgroundDownload"; + return "RNBackgroundDownloader"; } @Override diff --git a/android/src/main/java/com/eko/RNBackgroundDownloadPackage.java b/android/src/main/java/com/eko/RNBackgroundDownloadPackage.java index 4f0721e..f815dc5 100644 --- a/android/src/main/java/com/eko/RNBackgroundDownloadPackage.java +++ b/android/src/main/java/com/eko/RNBackgroundDownloadPackage.java @@ -10,10 +10,10 @@ import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.uimanager.ViewManager; import com.facebook.react.bridge.JavaScriptModule; -public class RNBackgroundDownloadPackage implements ReactPackage { +public class RNBackgroundDownloaderPackage implements ReactPackage { @Override public List createNativeModules(ReactApplicationContext reactContext) { - return Arrays.asList(new RNBackgroundDownloadModule(reactContext)); + return Arrays.asList(new RNBackgroundDownloaderModule(reactContext)); } @Override diff --git a/index.js b/index.js index 492114e..4a42944 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,11 @@ import { NativeModules, NativeEventEmitter } from 'react-native'; -const { RNBackgroundDownload } = NativeModules; -const RNBackgroundDownloadEmitter = new NativeEventEmitter(RNBackgroundDownload); +const { RNBackgroundDownloader } = NativeModules; +const RNBackgroundDownloaderEmitter = new NativeEventEmitter(RNBackgroundDownloader); import DownloadTask from './lib/downloadTask'; const tasksMap = new Map(); -RNBackgroundDownloadEmitter.addListener('downloadProgress', events => { +RNBackgroundDownloaderEmitter.addListener('downloadProgress', events => { for (let event of events) { let task = tasksMap.get(event.id); if (task) { @@ -14,7 +14,7 @@ RNBackgroundDownloadEmitter.addListener('downloadProgress', events => { } }); -RNBackgroundDownloadEmitter.addListener('downloadComplete', event => { +RNBackgroundDownloaderEmitter.addListener('downloadComplete', event => { let task = tasksMap.get(event.id); if (task) { task._onDone(event.location); @@ -22,7 +22,7 @@ RNBackgroundDownloadEmitter.addListener('downloadComplete', event => { tasksMap.delete(event.id); }); -RNBackgroundDownloadEmitter.addListener('downloadFailed', event => { +RNBackgroundDownloaderEmitter.addListener('downloadFailed', event => { let task = tasksMap.get(event.id); if (task) { task._onError(event.error); @@ -30,7 +30,7 @@ RNBackgroundDownloadEmitter.addListener('downloadFailed', event => { tasksMap.delete(event.id); }); -RNBackgroundDownloadEmitter.addListener('downloadBegin', event => { +RNBackgroundDownloaderEmitter.addListener('downloadBegin', event => { let task = tasksMap.get(event.id); if (task) { task._onBegin(event.expectedBytes); @@ -38,18 +38,18 @@ RNBackgroundDownloadEmitter.addListener('downloadBegin', event => { }); export function checkForExistingDownloads() { - return RNBackgroundDownload.checkForExistingDownloads() + return RNBackgroundDownloader.checkForExistingDownloads() .then(foundTasks => { return foundTasks.map(taskInfo => { let task = new DownloadTask(taskInfo); - if (taskInfo.state === RNBackgroundDownload.TaskRunning) { + if (taskInfo.state === RNBackgroundDownloader.TaskRunning) { task.state = 'DOWNLOADING'; - } else if (taskInfo.state === RNBackgroundDownload.TaskSuspended) { + } else if (taskInfo.state === RNBackgroundDownloader.TaskSuspended) { task.state = 'PAUSED'; - } else if (taskInfo.state === RNBackgroundDownload.TaskCanceling) { + } else if (taskInfo.state === RNBackgroundDownloader.TaskCanceling) { task.stop(); return null; - } else if (taskInfo.state === RNBackgroundDownload.TaskCompleted) { + } else if (taskInfo.state === RNBackgroundDownloader.TaskCompleted) { if (taskInfo.bytesWritten === taskInfo.totalBytes) { task.state = 'DONE'; } else { @@ -65,27 +65,27 @@ export function checkForExistingDownloads() { export function download(options) { if (!options.id || !options.url || !options.destination) { - throw new Error('[RNBackgroundDownload] id, url and destination are required'); + throw new Error('[RNBackgroundDownloader] id, url and destination are required'); } - RNBackgroundDownload.download(options); + RNBackgroundDownloader.download(options); let task = new DownloadTask(options.id); tasksMap.set(options.id, task); return task; } export const directories = { - documents: RNBackgroundDownload.documents + documents: RNBackgroundDownloader.documents }; export const Network = { - WIFI_ONLY: RNBackgroundDownload.OnlyWifi, - ALL: RNBackgroundDownload.AllNetworks + WIFI_ONLY: RNBackgroundDownloader.OnlyWifi, + ALL: RNBackgroundDownloader.AllNetworks }; export const Priority = { - HIGH: RNBackgroundDownload.PriorityHigh, - MEDIUM: RNBackgroundDownload.PriorityNormal, - LOW: RNBackgroundDownload.PriorityLow + HIGH: RNBackgroundDownloader.PriorityHigh, + MEDIUM: RNBackgroundDownloader.PriorityNormal, + LOW: RNBackgroundDownloader.PriorityLow }; export default { diff --git a/ios/RNBackgroundDownload.h b/ios/RNBackgroundDownloader.h similarity index 82% rename from ios/RNBackgroundDownload.h rename to ios/RNBackgroundDownloader.h index bb156fa..0e72fac 100644 --- a/ios/RNBackgroundDownload.h +++ b/ios/RNBackgroundDownloader.h @@ -17,7 +17,7 @@ typedef void (^CompletionHandler)(); -@interface RNBackgroundDownload : RCTEventEmitter +@interface RNBackgroundDownloader : RCTEventEmitter + (void)setCompletionHandlerWithIdentifier: (NSString *)identifier completionHandler: (CompletionHandler)completionHandler; diff --git a/ios/RNBackgroundDownload.m b/ios/RNBackgroundDownloader.m similarity index 99% rename from ios/RNBackgroundDownload.m rename to ios/RNBackgroundDownloader.m index 7bcfbd7..5fb526a 100644 --- a/ios/RNBackgroundDownload.m +++ b/ios/RNBackgroundDownloader.m @@ -13,7 +13,7 @@ static CompletionHandler storedCompletionHandler; -@implementation RNBackgroundDownload { +@implementation RNBackgroundDownloader { NSURLSession *urlSession; NSURLSessionConfiguration *sessionConfig; NSMutableDictionary *urlToConfigMap; diff --git a/ios/RNBackgroundDownload.xcodeproj/project.pbxproj b/ios/RNBackgroundDownloader.xcodeproj/project.pbxproj similarity index 82% rename from ios/RNBackgroundDownload.xcodeproj/project.pbxproj rename to ios/RNBackgroundDownloader.xcodeproj/project.pbxproj index 32e4506..c0b3c7a 100644 --- a/ios/RNBackgroundDownload.xcodeproj/project.pbxproj +++ b/ios/RNBackgroundDownloader.xcodeproj/project.pbxproj @@ -7,7 +7,7 @@ objects = { /* Begin PBXBuildFile section */ - B3E7B58A1CC2AC0600A0062D /* RNBackgroundDownload.m in Sources */ = {isa = PBXBuildFile; fileRef = B3E7B5891CC2AC0600A0062D /* RNBackgroundDownload.m */; }; + B3E7B58A1CC2AC0600A0062D /* RNBackgroundDownloader.m in Sources */ = {isa = PBXBuildFile; fileRef = B3E7B5891CC2AC0600A0062D /* RNBackgroundDownloader.m */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -23,10 +23,10 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ - 134814201AA4EA6300B7C361 /* libRNBackgroundDownload.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRNBackgroundDownload.a; sourceTree = BUILT_PRODUCTS_DIR; }; + 134814201AA4EA6300B7C361 /* libRNBackgroundDownloader.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRNBackgroundDownloader.a; sourceTree = BUILT_PRODUCTS_DIR; }; 565BF70F208F2C7C00F66231 /* RNBGDTaskConfig.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNBGDTaskConfig.h; sourceTree = ""; }; - B3E7B5881CC2AC0600A0062D /* RNBackgroundDownload.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNBackgroundDownload.h; sourceTree = ""; }; - B3E7B5891CC2AC0600A0062D /* RNBackgroundDownload.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNBackgroundDownload.m; sourceTree = ""; }; + B3E7B5881CC2AC0600A0062D /* RNBackgroundDownloader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNBackgroundDownloader.h; sourceTree = ""; }; + B3E7B5891CC2AC0600A0062D /* RNBackgroundDownloader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNBackgroundDownloader.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -43,7 +43,7 @@ 134814211AA4EA7D00B7C361 /* Products */ = { isa = PBXGroup; children = ( - 134814201AA4EA6300B7C361 /* libRNBackgroundDownload.a */, + 134814201AA4EA6300B7C361 /* libRNBackgroundDownloader.a */, ); name = Products; sourceTree = ""; @@ -52,8 +52,8 @@ isa = PBXGroup; children = ( 565BF70F208F2C7C00F66231 /* RNBGDTaskConfig.h */, - B3E7B5881CC2AC0600A0062D /* RNBackgroundDownload.h */, - B3E7B5891CC2AC0600A0062D /* RNBackgroundDownload.m */, + B3E7B5881CC2AC0600A0062D /* RNBackgroundDownloader.h */, + B3E7B5891CC2AC0600A0062D /* RNBackgroundDownloader.m */, 134814211AA4EA7D00B7C361 /* Products */, ); sourceTree = ""; @@ -61,9 +61,9 @@ /* End PBXGroup section */ /* Begin PBXNativeTarget section */ - 58B511DA1A9E6C8500147676 /* RNBackgroundDownload */ = { + 58B511DA1A9E6C8500147676 /* RNBackgroundDownloader */ = { isa = PBXNativeTarget; - buildConfigurationList = 58B511EF1A9E6C8500147676 /* Build configuration list for PBXNativeTarget "RNBackgroundDownload" */; + buildConfigurationList = 58B511EF1A9E6C8500147676 /* Build configuration list for PBXNativeTarget "RNBackgroundDownloader" */; buildPhases = ( 58B511D71A9E6C8500147676 /* Sources */, 58B511D81A9E6C8500147676 /* Frameworks */, @@ -73,9 +73,9 @@ ); dependencies = ( ); - name = RNBackgroundDownload; + name = RNBackgroundDownloader; productName = RCTDataManager; - productReference = 134814201AA4EA6300B7C361 /* libRNBackgroundDownload.a */; + productReference = 134814201AA4EA6300B7C361 /* libRNBackgroundDownloader.a */; productType = "com.apple.product-type.library.static"; }; /* End PBXNativeTarget section */ @@ -92,7 +92,7 @@ }; }; }; - buildConfigurationList = 58B511D61A9E6C8500147676 /* Build configuration list for PBXProject "RNBackgroundDownload" */; + buildConfigurationList = 58B511D61A9E6C8500147676 /* Build configuration list for PBXProject "RNBackgroundDownloader" */; compatibilityVersion = "Xcode 3.2"; developmentRegion = English; hasScannedForEncodings = 0; @@ -104,7 +104,7 @@ projectDirPath = ""; projectRoot = ""; targets = ( - 58B511DA1A9E6C8500147676 /* RNBackgroundDownload */, + 58B511DA1A9E6C8500147676 /* RNBackgroundDownloader */, ); }; /* End PBXProject section */ @@ -114,7 +114,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - B3E7B58A1CC2AC0600A0062D /* RNBackgroundDownload.m in Sources */, + B3E7B58A1CC2AC0600A0062D /* RNBackgroundDownloader.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -206,7 +206,7 @@ ); LIBRARY_SEARCH_PATHS = "$(inherited)"; OTHER_LDFLAGS = "-ObjC"; - PRODUCT_NAME = RNBackgroundDownload; + PRODUCT_NAME = RNBackgroundDownloader; SKIP_INSTALL = YES; }; name = Debug; @@ -222,7 +222,7 @@ ); LIBRARY_SEARCH_PATHS = "$(inherited)"; OTHER_LDFLAGS = "-ObjC"; - PRODUCT_NAME = RNBackgroundDownload; + PRODUCT_NAME = RNBackgroundDownloader; SKIP_INSTALL = YES; }; name = Release; @@ -230,7 +230,7 @@ /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ - 58B511D61A9E6C8500147676 /* Build configuration list for PBXProject "RNBackgroundDownload" */ = { + 58B511D61A9E6C8500147676 /* Build configuration list for PBXProject "RNBackgroundDownloader" */ = { isa = XCConfigurationList; buildConfigurations = ( 58B511ED1A9E6C8500147676 /* Debug */, @@ -239,7 +239,7 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; - 58B511EF1A9E6C8500147676 /* Build configuration list for PBXNativeTarget "RNBackgroundDownload" */ = { + 58B511EF1A9E6C8500147676 /* Build configuration list for PBXNativeTarget "RNBackgroundDownloader" */ = { isa = XCConfigurationList; buildConfigurations = ( 58B511F01A9E6C8500147676 /* Debug */, diff --git a/package.json b/package.json index 237bbdb..3a48546 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "react-native-background-download", + "name": "react-native-background-downloader", "version": "1.0.0", "description": "A library for React-Native to help you download large files on iOS and Android both in the foreground and most importantly in the background.", "main": "index.js", @@ -10,7 +10,7 @@ }, "repository": { "type": "git", - "url": "https://github.com/EkoLabs/react-native-background-download.git" + "url": "https://github.com/EkoLabs/react-native-background-downloader.git" }, "keywords": [ "react-native",